1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
26
<title>orddict</title>
27
<prepared>Robert Virding</prepared>
28
<responsible>nobody</responsible>
30
<approved>nobody</approved>
32
<date>2007-04-16</date>
34
<file>orddict.sgml</file>
36
<module>orddict</module>
37
<modulesummary>Key-Value Dictionary as Ordered List</modulesummary>
39
<p><c>Orddict</c> implements a <c>Key</c> - <c>Value</c> dictionary.
40
An <c>orddict</c> is a representation of a dictionary, where a
41
list of pairs is used to store the keys and values. The list is
42
ordered after the keys.</p>
43
<p>This module provides exactly the same interface as the module
44
<c>dict</c> but with a defined representation. One difference is
45
that while <c>dict</c> considers two keys as different if they
46
do not match (<c>=:=</c>), this module considers two keys as
47
different if and only if they do not compare equal
52
<title>DATA TYPES</title>
55
as returned by new/0</code>
59
<name>append(Key, Value, Orddict1) -> Orddict2</name>
60
<fsummary>Append a value to keys in a dictionary</fsummary>
62
<v>Key = Value = term()</v>
63
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
66
<p>This function appends a new <c>Value</c> to the current list
67
of values associated with <c>Key</c>. An exception is
68
generated if the initial value associated with <c>Key</c> is
69
not a list of values.</p>
73
<name>append_list(Key, ValList, Orddict1) -> Orddict2</name>
74
<fsummary>Append new values to keys in a dictionary</fsummary>
76
<v>ValList = [Value]</v>
77
<v>Key = Value = term()</v>
78
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
81
<p>This function appends a list of values <c>ValList</c> to
82
the current list of values associated with <c>Key</c>. An
83
exception is generated if the initial value associated with
84
<c>Key</c> is not a list of values.</p>
88
<name>erase(Key, Orddict1) -> Orddict2</name>
89
<fsummary>Erase a key from a dictionary</fsummary>
92
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
95
<p>This function erases all items with a given key from a
100
<name>fetch(Key, Orddict) -> Value</name>
101
<fsummary>Look-up values in a dictionary</fsummary>
103
<v>Key = Value = term()</v>
104
<v>Orddict = ordered_dictionary()</v>
107
<p>This function returns the value associated with <c>Key</c>
108
in the dictionary <c>Orddict</c>. <c>fetch</c> assumes that
109
the <c>Key</c> is present in the dictionary and an exception
110
is generated if <c>Key</c> is not in the dictionary.</p>
114
<name>fetch_keys(Orddict) -> Keys</name>
115
<fsummary>Return all keys in a dictionary</fsummary>
117
<v>Orddict = ordered_dictionary()</v>
118
<v>Keys = [term()]</v>
121
<p>This function returns a list of all keys in the dictionary.</p>
125
<name>filter(Pred, Orddict1) -> Orddict2</name>
126
<fsummary>Choose elements which satisfy a predicate</fsummary>
128
<v>Pred = fun(Key, Value) -> bool()</v>
129
<v> Key = Value = term()</v>
130
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
133
<p><c>Orddict2</c> is a dictionary of all keys and values in
134
<c>Orddict1</c> for which <c>Pred(Key, Value)</c> is <c>true</c>.</p>
138
<name>find(Key, Orddict) -> {ok, Value} | error</name>
139
<fsummary>Search for a key in a dictionary</fsummary>
141
<v>Key = Value = term()</v>
142
<v>Orddict = ordered_dictionary()</v>
145
<p>This function searches for a key in a dictionary. Returns
146
<c>{ok, Value}</c> where <c>Value</c> is the value associated
147
with <c>Key</c>, or <c>error</c> if the key is not present in
152
<name>fold(Fun, Acc0, Orddict) -> Acc1</name>
153
<fsummary>Fold a function over a dictionary</fsummary>
155
<v>Fun = fun(Key, Value, AccIn) -> AccOut</v>
156
<v>Key = Value = term()</v>
157
<v>Acc0 = Acc1 = AccIn = AccOut = term()</v>
158
<v>Orddict = ordered_dictionary()</v>
161
<p>Calls <c>Fun</c> on successive keys and values of
162
<c>Orddict</c> together with an extra argument <c>Acc</c>
163
(short for accumulator). <c>Fun</c> must return a new
164
accumulator which is passed to the next call. <c>Acc0</c> is
165
returned if the list is empty. The evaluation order is
170
<name>from_list(List) -> Orddict</name>
171
<fsummary>Convert a list of pairs to a dictionary</fsummary>
173
<v>List = [{Key, Value}]</v>
174
<v>Orddict = ordered_dictionary()</v>
177
<p>This function converts the key/value list <c>List</c> to a
182
<name>is_key(Key, Orddict) -> bool()</name>
183
<fsummary>Test if a key is in a dictionary</fsummary>
186
<v>Orddict = ordered_dictionary()</v>
189
<p>This function tests if <c>Key</c> is contained in
190
the dictionary <c>Orddict</c>.</p>
194
<name>map(Fun, Orddict1) -> Orddict2</name>
195
<fsummary>Map a function over a dictionary</fsummary>
197
<v>Fun = fun(Key, Value1) -> Value2</v>
198
<v> Key = Value1 = Value2 = term()</v>
199
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
202
<p><c>map</c> calls <c>Func</c> on successive keys and values
203
of <c>Orddict</c> to return a new value for each key.
204
The evaluation order is undefined.</p>
208
<name>merge(Fun, Orddict1, Orddict2) -> Orddict3</name>
209
<fsummary>Merge two dictionaries</fsummary>
211
<v>Fun = fun(Key, Value1, Value2) -> Value</v>
212
<v> Key = Value1 = Value2 = Value3 = term()</v>
213
<v>Orddict1 = Orddict2 = Orddict3 = ordered_dictionary()</v>
216
<p><c>merge</c> merges two dictionaries, <c>Orddict1</c> and
217
<c>Orddict2</c>, to create a new dictionary. All the <c>Key</c>
218
- <c>Value</c> pairs from both dictionaries are included in
219
the new dictionary. If a key occurs in both dictionaries then
220
<c>Fun</c> is called with the key and both values to return a
221
new value. <c>merge</c> could be defined as:</p>
223
merge(Fun, D1, D2) ->
224
fold(fun (K, V1, D) ->
225
update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D)
227
<p>but is faster.</p>
231
<name>new() -> ordered_dictionary()</name>
232
<fsummary>Create a dictionary</fsummary>
234
<p>This function creates a new dictionary.</p>
238
<name>size(Orddict) -> int()</name>
239
<fsummary>Return the number of elements in an ordered dictionary</fsummary>
241
<v>Orddict = ordered_dictionary()</v>
244
<p>Returns the number of elements in an <c>Orddict</c>.</p>
248
<name>store(Key, Value, Orddict1) -> Orddict2</name>
249
<fsummary>Store a value in a dictionary</fsummary>
251
<v>Key = Value = term()</v>
252
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
255
<p>This function stores a <c>Key</c> - <c>Value</c> pair in a
256
dictionary. If the <c>Key</c> already exists in <c>Orddict1</c>,
257
the associated value is replaced by <c>Value</c>.</p>
261
<name>to_list(Orddict) -> List</name>
262
<fsummary>Convert a dictionary to a list of pairs</fsummary>
264
<v>Orddict = ordered_dictionary()</v>
265
<v>List = [{Key, Value}]</v>
268
<p>This function converts the dictionary to a list
273
<name>update(Key, Fun, Orddict1) -> Orddict2</name>
274
<fsummary>Update a value in a dictionary</fsummary>
277
<v>Fun = fun(Value1) -> Value2</v>
278
<v> Value1 = Value2 = term()</v>
279
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
282
<p>Update the a value in a dictionary by calling <c>Fun</c> on
283
the value to get a new value. An exception is generated if
284
<c>Key</c> is not present in the dictionary.</p>
288
<name>update(Key, Fun, Initial, Orddict1) -> Orddict2</name>
289
<fsummary>Update a value in a dictionary</fsummary>
291
<v>Key = Initial = term()</v>
292
<v>Fun = fun(Value1) -> Value2</v>
293
<v> Value1 = Value2 = term()</v>
294
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
297
<p>Update the a value in a dictionary by calling <c>Fun</c> on
298
the value to get a new value. If <c>Key</c> is not present
299
in the dictionary then <c>Initial</c> will be stored as
300
the first value. For example <c>append/3</c> could be defined
303
append(Key, Val, D) ->
304
update(Key, fun (Old) -> Old ++ [Val] end, [Val], D).</code>
308
<name>update_counter(Key, Increment, Orddict1) -> Orddict2</name>
309
<fsummary>Increment a value in a dictionary</fsummary>
312
<v>Increment = number()</v>
313
<v>Orddict1 = Orddict2 = ordered_dictionary()</v>
316
<p>Add <c>Increment</c> to the value associated with <c>Key</c>
317
and store this value. If <c>Key</c> is not present in
318
the dictionary then <c>Increment</c> will be stored as
320
<p>This could be defined as:</p>
322
update_counter(Key, Incr, D) ->
323
update(Key, fun (Old) -> Old + Incr end, Incr, D).</code>
324
<p>but is faster.</p>
331
<p>The functions <c>append</c> and <c>append_list</c> are included
332
so we can store keyed values in a list <em>accumulator</em>. For
335
> D0 = orddict:new(),
336
D1 = orddict:store(files, [], D0),
337
D2 = orddict:append(files, f1, D1),
338
D3 = orddict:append(files, f2, D2),
339
D4 = orddict:append(files, f3, D3),
340
orddict:fetch(files, D4).
342
<p>This saves the trouble of first fetching a keyed value,
343
appending a new value to the list of stored values, and storing
346
<p>The function <c>fetch</c> should be used if the key is known to
347
be in the dictionary, otherwise <c>find</c>.</p>
351
<title>See Also</title>
352
<p><seealso marker="dict">dict(3)</seealso>,
353
<seealso marker="gb_trees">gb_trees(3)</seealso></p>