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.
27
<prepared>Robert Virding</prepared>
29
<date>1997-01-15</date>
33
<modulesummary>Key-Value Dictionary</modulesummary>
35
<p><c>Dict</c> implements a <c>Key</c> - <c>Value</c> dictionary.
36
The representation of a dictionary is not defined.</p>
37
<p>This module provides exactly the same interface as the module
38
<c>orddict</c>. One difference is that while this module
39
considers two keys as different if they do not match (<c>=:=</c>),
40
<c>orddict</c> considers two keys as different if and only if
41
they do not compare equal (<c>==</c>).</p>
45
<title>DATA TYPES</title>
48
as returned by new/0</code>
52
<name>append(Key, Value, Dict1) -> Dict2</name>
53
<fsummary>Append a value to keys in a dictionary</fsummary>
55
<v>Key = Value = term()</v>
56
<v>Dict1 = Dict2 = dictionary()</v>
59
<p>This function appends a new <c>Value</c> to the current list
60
of values associated with <c>Key</c>. An exception is
61
generated if the initial value associated with <c>Key</c> is
62
not a list of values.</p>
66
<name>append_list(Key, ValList, Dict1) -> Dict2</name>
67
<fsummary>Append new values to keys in a dictionary</fsummary>
69
<v>ValList = [Value]</v>
70
<v>Key = Value = term()</v>
71
<v>Dict1 = Dict2 = dictionary()</v>
74
<p>This function appends a list of values <c>ValList</c> to
75
the current list of values associated with <c>Key</c>. An
76
exception is generated if the initial value associated with
77
<c>Key</c> is not a list of values.</p>
81
<name>erase(Key, Dict1) -> Dict2</name>
82
<fsummary>Erase a key from a dictionary</fsummary>
85
<v>Dict1 = Dict2 = dictionary()</v>
88
<p>This function erases all items with a given key from a
93
<name>fetch(Key, Dict) -> Value</name>
94
<fsummary>Look-up values in a dictionary</fsummary>
96
<v>Key = Value = term()</v>
97
<v>Dict = dictionary()</v>
100
<p>This function returns the value associated with <c>Key</c>
101
in the dictionary <c>Dict</c>. <c>fetch</c> assumes that
102
the <c>Key</c> is present in the dictionary and an exception
103
is generated if <c>Key</c> is not in the dictionary.</p>
107
<name>fetch_keys(Dict) -> Keys</name>
108
<fsummary>Return all keys in a dictionary</fsummary>
110
<v>Dict = dictionary()</v>
111
<v>Keys = [term()]</v>
114
<p>This function returns a list of all keys in the dictionary.</p>
118
<name>filter(Pred, Dict1) -> Dict2</name>
119
<fsummary>Choose elements which satisfy a predicate</fsummary>
121
<v>Pred = fun(Key, Value) -> bool()</v>
122
<v> Key = Value = term()</v>
123
<v>Dict1 = Dict2 = dictionary()</v>
126
<p><c>Dict2</c> is a dictionary of all keys and values in
127
<c>Dict1</c> for which <c>Pred(Key, Value)</c> is <c>true</c>.</p>
131
<name>find(Key, Dict) -> {ok, Value} | error</name>
132
<fsummary>Search for a key in a dictionary</fsummary>
134
<v>Key = Value = term()</v>
135
<v>Dict = dictionary()</v>
138
<p>This function searches for a key in a dictionary. Returns
139
<c>{ok, Value}</c> where <c>Value</c> is the value associated
140
with <c>Key</c>, or <c>error</c> if the key is not present in
145
<name>fold(Fun, Acc0, Dict) -> Acc1</name>
146
<fsummary>Fold a function over a dictionary</fsummary>
148
<v>Fun = fun(Key, Value, AccIn) -> AccOut</v>
149
<v>Key = Value = term()</v>
150
<v>Acc0 = Acc1 = AccIn = AccOut = term()</v>
151
<v>Dict = dictionary()</v>
154
<p>Calls <c>Fun</c> on successive keys and values of
155
<c>Dict</c> together with an extra argument <c>Acc</c>
156
(short for accumulator). <c>Fun</c> must return a new
157
accumulator which is passed to the next call. <c>Acc0</c> is
158
returned if the list is empty. The evaluation order is
163
<name>from_list(List) -> Dict</name>
164
<fsummary>Convert a list of pairs to a dictionary</fsummary>
166
<v>List = [{Key, Value}]</v>
167
<v>Dict = dictionary()</v>
170
<p>This function converts the key/value list <c>List</c> to a
175
<name>is_key(Key, Dict) -> bool()</name>
176
<fsummary>Test if a key is in a dictionary</fsummary>
179
<v>Dict = dictionary()</v>
182
<p>This function tests if <c>Key</c> is contained in
183
the dictionary <c>Dict</c>.</p>
187
<name>map(Fun, Dict1) -> Dict2</name>
188
<fsummary>Map a function over a dictionary</fsummary>
190
<v>Fun = fun(Key, Value1) -> Value2</v>
191
<v> Key = Value1 = Value2 = term()</v>
192
<v>Dict1 = Dict2 = dictionary()</v>
195
<p><c>map</c> calls <c>Func</c> on successive keys and values
196
of <c>Dict</c> to return a new value for each key.
197
The evaluation order is undefined.</p>
201
<name>merge(Fun, Dict1, Dict2) -> Dict3</name>
202
<fsummary>Merge two dictionaries</fsummary>
204
<v>Fun = fun(Key, Value1, Value2) -> Value</v>
205
<v> Key = Value1 = Value2 = Value3 = term()</v>
206
<v>Dict1 = Dict2 = Dict3 = dictionary()</v>
209
<p><c>merge</c> merges two dictionaries, <c>Dict1</c> and
210
<c>Dict2</c>, to create a new dictionary. All the <c>Key</c>
211
- <c>Value</c> pairs from both dictionaries are included in
212
the new dictionary. If a key occurs in both dictionaries then
213
<c>Fun</c> is called with the key and both values to return a
214
new value. <c>merge</c> could be defined as:</p>
216
merge(Fun, D1, D2) ->
217
fold(fun (K, V1, D) ->
218
update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D)
220
<p>but is faster.</p>
224
<name>new() -> dictionary()</name>
225
<fsummary>Create a dictionary</fsummary>
227
<p>This function creates a new dictionary.</p>
231
<name>size(Dict) -> int()</name>
232
<fsummary>Return the number of elements in a dictionary</fsummary>
234
<v>Dict = dictionary()</v>
237
<p>Returns the number of elements in a <c>Dict</c>.</p>
241
<name>store(Key, Value, Dict1) -> Dict2</name>
242
<fsummary>Store a value in a dictionary</fsummary>
244
<v>Key = Value = term()</v>
245
<v>Dict1 = Dict2 = dictionary()</v>
248
<p>This function stores a <c>Key</c> - <c>Value</c> pair in a
249
dictionary. If the <c>Key</c> already exists in <c>Dict1</c>,
250
the associated value is replaced by <c>Value</c>.</p>
254
<name>to_list(Dict) -> List</name>
255
<fsummary>Convert a dictionary to a list of pairs</fsummary>
257
<v>Dict = dictionary()</v>
258
<v>List = [{Key, Value}]</v>
261
<p>This function converts the dictionary to a list
266
<name>update(Key, Fun, Dict1) -> Dict2</name>
267
<fsummary>Update a value in a dictionary</fsummary>
270
<v>Fun = fun(Value1) -> Value2</v>
271
<v> Value1 = Value2 = term()</v>
272
<v>Dict1 = Dict2 = dictionary()</v>
275
<p>Update the a value in a dictionary by calling <c>Fun</c> on
276
the value to get a new value. An exception is generated if
277
<c>Key</c> is not present in the dictionary.</p>
281
<name>update(Key, Fun, Initial, Dict1) -> Dict2</name>
282
<fsummary>Update a value in a dictionary</fsummary>
284
<v>Key = Initial = term()</v>
285
<v>Fun = fun(Value1) -> Value2</v>
286
<v> Value1 = Value2 = term()</v>
287
<v>Dict1 = Dict2 = dictionary()</v>
290
<p>Update the a value in a dictionary by calling <c>Fun</c> on
291
the value to get a new value. If <c>Key</c> is not present
292
in the dictionary then <c>Initial</c> will be stored as
293
the first value. For example <c>append/3</c> could be defined
296
append(Key, Val, D) ->
297
update(Key, fun (Old) -> Old ++ [Val] end, [Val], D).</code>
301
<name>update_counter(Key, Increment, Dict1) -> Dict2</name>
302
<fsummary>Increment a value in a dictionary</fsummary>
305
<v>Increment = number()</v>
306
<v>Dict1 = Dict2 = dictionary()</v>
309
<p>Add <c>Increment</c> to the value associated with <c>Key</c>
310
and store this value. If <c>Key</c> is not present in
311
the dictionary then <c>Increment</c> will be stored as
313
<p>This could be defined as:</p>
315
update_counter(Key, Incr, D) ->
316
update(Key, fun (Old) -> Old + Incr end, Incr, D).</code>
317
<p>but is faster.</p>
324
<p>The functions <c>append</c> and <c>append_list</c> are included
325
so we can store keyed values in a list <em>accumulator</em>. For
329
D1 = dict:store(files, [], D0),
330
D2 = dict:append(files, f1, D1),
331
D3 = dict:append(files, f2, D2),
332
D4 = dict:append(files, f3, D3),
333
dict:fetch(files, D4).
335
<p>This saves the trouble of first fetching a keyed value,
336
appending a new value to the list of stored values, and storing
339
<p>The function <c>fetch</c> should be used if the key is known to
340
be in the dictionary, otherwise <c>find</c>.</p>
344
<title>See Also</title>
345
<p><seealso marker="gb_trees">gb_trees(3)</seealso>,
346
<seealso marker="orddict">orddict(3)</seealso></p>