1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
.TH orddict 3 "stdlib 1.15.3" "Ericsson AB" "ERLANG MODULE DEFINITION"
.SH MODULE
orddict \- Key-Value Dictionary as Ordered List
.SH DESCRIPTION
.LP
\fIOrddict\fR implements a \fIKey\fR - \fIValue\fR dictionary\&. An \fIorddict\fR is a representation of a dictionary, where a list of pairs is used to store the keys and values\&. The list is ordered after the keys\&.
.LP
This module provides exactly the same interface as the module \fIdict\fR but with a defined representation\&. One difference is that while \fIdict\fR considers two keys as different if they do not match (\fI=:=\fR), this module considers two keys as different if and only if they do not compare equal (\fI==\fR)\&.
.SH DATA TYPES
.nf
ordered_dictionary()
as returned by new/0
.fi
.SH EXPORTS
.LP
.B
append(Key, Value, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Key = Value = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
This function appends a new \fIValue\fR to the current list of values associated with \fIKey\fR\&. An exception is generated if the initial value associated with \fIKey\fR is not a list of values\&.
.RE
.LP
.B
append_list(Key, ValList, Orddict1) -> Orddict2
.br
.RS
.TP
Types
ValList = [Value]
.br
Key = Value = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
This function appends a list of values \fIValList\fR to the current list of values associated with \fIKey\fR\&. An exception is generated if the initial value associated with \fIKey\fR is not a list of values\&.
.RE
.LP
.B
erase(Key, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Key = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
This function erases all items with a given key from a dictionary\&.
.RE
.LP
.B
fetch(Key, Orddict) -> Value
.br
.RS
.TP
Types
Key = Value = term()
.br
Orddict = ordered_dictionary()
.br
.RE
.RS
.LP
This function returns the value associated with \fIKey\fR in the dictionary \fIOrddict\fR\&. \fIfetch\fR assumes that the \fIKey\fR is present in the dictionary and an exception is generated if \fIKey\fR is not in the dictionary\&.
.RE
.LP
.B
fetch_keys(Orddict) -> Keys
.br
.RS
.TP
Types
Orddict = ordered_dictionary()
.br
Keys = [term()]
.br
.RE
.RS
.LP
This function returns a list of all keys in the dictionary\&.
.RE
.LP
.B
filter(Pred, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Pred = fun(Key, Value) -> bool()
.br
Key = Value = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
\fIOrddict2\fR is a dictionary of all keys and values in \fIOrddict1\fR for which \fIPred(Key, Value)\fR is \fItrue\fR\&.
.RE
.LP
.B
find(Key, Orddict) -> {ok, Value} | error
.br
.RS
.TP
Types
Key = Value = term()
.br
Orddict = ordered_dictionary()
.br
.RE
.RS
.LP
This function searches for a key in a dictionary\&. Returns \fI{ok, Value}\fR where \fIValue\fR is the value associated with \fIKey\fR, or \fIerror\fR if the key is not present in the dictionary\&.
.RE
.LP
.B
fold(Fun, Acc0, Orddict) -> Acc1
.br
.RS
.TP
Types
Fun = fun(Key, Value, AccIn) -> AccOut
.br
Key = Value = term()
.br
Acc0 = Acc1 = AccIn = AccOut = term()
.br
Orddict = ordered_dictionary()
.br
.RE
.RS
.LP
Calls \fIFun\fR on successive keys and values of \fIOrddict\fR together with an extra argument \fIAcc\fR (short for accumulator)\&. \fIFun\fR must return a new accumulator which is passed to the next call\&. \fIAcc0\fR is returned if the list is empty\&. The evaluation order is undefined\&.
.RE
.LP
.B
from_list(List) -> Orddict
.br
.RS
.TP
Types
List = [{Key, Value}]
.br
Orddict = ordered_dictionary()
.br
.RE
.RS
.LP
This function converts the key/value list \fIList\fR to a dictionary\&.
.RE
.LP
.B
is_key(Key, Orddict) -> bool()
.br
.RS
.TP
Types
Key = term()
.br
Orddict = ordered_dictionary()
.br
.RE
.RS
.LP
This function tests if \fIKey\fR is contained in the dictionary \fIOrddict\fR\&.
.RE
.LP
.B
map(Fun, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Fun = fun(Key, Value1) -> Value2
.br
Key = Value1 = Value2 = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
\fImap\fR calls \fIFunc\fR on successive keys and values of \fIOrddict\fR to return a new value for each key\&. The evaluation order is undefined\&.
.RE
.LP
.B
merge(Fun, Orddict1, Orddict2) -> Orddict3
.br
.RS
.TP
Types
Fun = fun(Key, Value1, Value2) -> Value
.br
Key = Value1 = Value2 = Value3 = term()
.br
Orddict1 = Orddict2 = Orddict3 = ordered_dictionary()
.br
.RE
.RS
.LP
\fImerge\fR merges two dictionaries, \fIOrddict1\fR and \fIOrddict2\fR, to create a new dictionary\&. All the \fIKey\fR - \fIValue\fR pairs from both dictionaries are included in the new dictionary\&. If a key occurs in both dictionaries then \fIFun\fR is called with the key and both values to return a new value\&. \fImerge\fR could be defined as:
.nf
merge(Fun, D1, D2) ->
fold(fun (K, V1, D) ->
update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D)
end, D2, D1)\&.
.fi
.LP
but is faster\&.
.RE
.LP
.B
new() -> ordered_dictionary()
.br
.RS
.LP
This function creates a new dictionary\&.
.RE
.LP
.B
size(Orddict) -> int()
.br
.RS
.TP
Types
Orddict = ordered_dictionary()
.br
.RE
.RS
.LP
Returns the number of elements in an \fIOrddict\fR\&.
.RE
.LP
.B
store(Key, Value, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Key = Value = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
This function stores a \fIKey\fR - \fIValue\fR pair in a dictionary\&. If the \fIKey\fR already exists in \fIOrddict1\fR, the associated value is replaced by \fIValue\fR\&.
.RE
.LP
.B
to_list(Orddict) -> List
.br
.RS
.TP
Types
Orddict = ordered_dictionary()
.br
List = [{Key, Value}]
.br
.RE
.RS
.LP
This function converts the dictionary to a list representation\&.
.RE
.LP
.B
update(Key, Fun, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Key = term()
.br
Fun = fun(Value1) -> Value2
.br
Value1 = Value2 = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
Update the a value in a dictionary by calling \fIFun\fR on the value to get a new value\&. An exception is generated if \fIKey\fR is not present in the dictionary\&.
.RE
.LP
.B
update(Key, Fun, Initial, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Key = Initial = term()
.br
Fun = fun(Value1) -> Value2
.br
Value1 = Value2 = term()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
Update the a value in a dictionary by calling \fIFun\fR on the value to get a new value\&. If \fIKey\fR is not present in the dictionary then \fIInitial\fR will be stored as the first value\&. For example \fIappend/3\fR could be defined as:
.nf
append(Key, Val, D) ->
update(Key, fun (Old) -> Old ++ [Val] end, [Val], D)\&.
.fi
.RE
.LP
.B
update_counter(Key, Increment, Orddict1) -> Orddict2
.br
.RS
.TP
Types
Key = term()
.br
Increment = number()
.br
Orddict1 = Orddict2 = ordered_dictionary()
.br
.RE
.RS
.LP
Add \fIIncrement\fR to the value associated with \fIKey\fR and store this value\&. If \fIKey\fR is not present in the dictionary then \fIIncrement\fR will be stored as the first value\&.
.LP
This could be defined as:
.nf
update_counter(Key, Incr, D) ->
update(Key, fun (Old) -> Old + Incr end, Incr, D)\&.
.fi
.LP
but is faster\&.
.RE
.SH NOTES
.LP
The functions \fIappend\fR and \fIappend_list\fR are included so we can store keyed values in a list \fIaccumulator\fR\&. For example:
.nf
> D0 = orddict:new(),
D1 = orddict:store(files, [], D0),
D2 = orddict:append(files, f1, D1),
D3 = orddict:append(files, f2, D2),
D4 = orddict:append(files, f3, D3),
orddict:fetch(files, D4)\&.
[f1,f2,f3]
.fi
.LP
This saves the trouble of first fetching a keyed value, appending a new value to the list of stored values, and storing the result\&.
.LP
The function \fIfetch\fR should be used if the key is known to be in the dictionary, otherwise \fIfind\fR\&.
.SH SEE ALSO
.LP
dict(3), gb_trees(3)
|