forked from erlang/otp
/
dict.erl
1352 lines (1115 loc) · 43.8 KB
/
dict.erl
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2000-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%% @author Robert Virding
%% @doc Key-Value Dictionary. The `dict' module implements a Key-Value
%% dictionary. The representation of a dictionary is not defined. This
%% module provides exactly the same interface as the module {@link orddict}.
%% One difference is that while this module considers two keys as different
%% if they are not exactly equal (`=/='), `orddict' considers two keys as
%% different if and only if they are not arithmetically equal (`/=').
%%
%% @see orddict
%% @see gb_trees
-module(dict).
%% Standard interface.
-export([new/0,new/1,is_key/2,size/1,is_empty/1,info/1,info/2]).
-export([to_list/1,to_orddict/1]).
-export([from_list/1,from_list/2,from_orddict/1,from_orddict/2]).
-export([get/2,get/3,find/2,keys/1,values/1,values/2,erase/2]).
-export([store/3,replace/3,increment/3]).
-export([foreach/2,map/2,map/3,map/4,filter/2,merge/3]).
-export([foldl/3,foldr/3,foldl1/2,foldr1/2,foldln/3,foldrn/3]).
-export([first_key/1,last_key/1,next_key/2,prev_key/2]).
-export([take_first/1,take_last/1,take/2]).
%% Nonstandard interface (not recommended)
-export([append/3,append_list/3]).
%% Deprecated interface
-export([fetch/2,fetch_keys/1,update/3,update/4,update_counter/3,fold/3]).
%% Low-level interface.
%%-export([get_slot/2,get_bucket/2,on_bucket/3,foldl/3,
%% maybe_expand/2,maybe_contract/2]).
-compile({no_auto_import,[size/1]}).
%% This module also defines the standard dict behaviour:
-export([behaviour_info/1]).
-spec behaviour_info(atom()) -> 'undefined' | [{atom(), arity()}].
%% @private
behaviour_info(callbacks) ->
[{new,0},{new,1},{from_orddict,1},{from_orddict,2},
{from_list,1},{from_list,2},{to_orddict,1},{to_list,1},
{is_empty,1},{size,1},{info,1},{info,2},
{keys,1},
{values,1},{values,2},% always return lists, possibly empty
{get,2},{get,3},% for multimaps, return a list (empty if key not present)
{find,2},% for multimaps, return {ok,List}|error, List nonempty
{store,3},
{replace,3},% (no effect if key not present)
{erase,2},
{map,2},{map,3},{map,4},% (map all/single entry/default)
{increment,3},
{filter,2},
{foldl,3},{foldr,3},{foldl1,2},{foldr1,2},{foldln,3},{foldrn,3},
{foreach,2},
{merge,3},
{first_key,1},{last_key,1},{next_key,2},{prev_key,2},
{take_first,1},{take_last,1},{take,2}
];
behaviour_info(_Other) ->
undefined.
%% We use the dynamic hashing techniques by Per-Åke Larsson as
%% described in "The Design and Implementation of Dynamic Hashing for
%% Sets and Tables in Icon" by Griswold and Townsend. Much of the
%% terminology comes from that paper as well.
%%
%% The segments are all of the same fixed size and we just keep
%% increasing the size of the top tuple as the table grows. At the
%% end of the segments tuple we keep an empty segment which we use
%% when we expand the segments. The segments are expanded by doubling
%% every time n reaches maxn instead of increasing the tuple one
%% element at a time. It is easier and does not seem detrimental to
%% speed. The same applies when contracting the segments.
%%
%% Note that as the order of the keys is undefined we may freely
%% reorder keys within a bucket.
%% Note: mk_seg/1 must be changed too if seg_size is changed.
-define(seg_size, 16).
-define(max_seg, 32).
-define(expand_load, 5).
-define(contract_load, 3).
-define(exp_size, (?seg_size * ?expand_load)).
-define(con_size, (?seg_size * ?contract_load)).
%% Define a hashtable. The default values are the standard ones.
-record(dict,
{size=0 :: non_neg_integer(), % Number of elements
n=?seg_size :: non_neg_integer(), % Number of active slots
maxn=?seg_size :: non_neg_integer(), % Maximum slots
bso=?seg_size div 2 :: non_neg_integer(), % Buddy slot offset
exp_size=?exp_size :: non_neg_integer(), % Size to expand at
con_size=?con_size :: non_neg_integer(), % Size to contract at
empty :: tuple(), % Empty segment
segs :: tuple() % Segments
}).
%% A declaration equivalent to the following one is hard-coded in erl_types.
%% That declaration contains hard-coded information about the #dict{}
%% structure and the types of its fields. So, please make sure that any
%% changes to its structure are also propagated to erl_types.erl.
%%
%% -opaque dict() :: #dict{}.
-define(kv(K,V), [K|V]). % Key-Value pair format
%%-define(kv(K,V), {K,V}). % Key-Value pair format
%% use this pattern to match for gb_trees
-define(gb(N,T), {N,T}).
%% Note: options are processed in the order they occur in the list, i.e.,
%% later options have higher precedence.
-record(opts, {type=set}).
opts(Options) when is_list(Options) -> opts_0(Options);
opts(Option) -> opts_0([Option]).
opts_0(Options) ->
opts_1(Options, #opts{}).
opts_1([set | Options], _Type) ->
opts_1(Options, #opts{type=set});
opts_1([ordered_set | Options], _Type) ->
opts_1(Options, #opts{type=ordered_set});
opts_1([], Type) ->
Type.
-spec new() -> dict().
%% @doc Create a dictionary. This function creates a new dictionary of the
%% default type, i.e., unordered (hash-based).
new() -> new([]).
-spec new(Opts :: [Option]) -> Dict when
Option :: 'set' | 'ordered_set',
Dict :: dict().
%% @doc Create a dictionary. This function creates a new dictionary of the
%% type specified by the options. The default type is `set', which means
%% that the entries are not kept in a predictable order (using hashing). The
%% `ordered_set' type uses a tree structure (see {@link gb_trees}) and keeps
%% the keys in increasing term order, just like the {@link orddict} module
%% (although a tree is much more efficient, for larger dictionaries).
%%
%% Note that options toward the end of the options list take precedence over
%% earlier ones.
new(Opts) ->
try opts(Opts) of
R -> new_1(R)
catch
_:_ -> erlang:error(badarg, [Opts])
end.
new_1(#opts{type=set}) ->
new_dict();
new_1(#opts{type=ordered_set}) ->
gb_trees:empty().
new_dict() ->
Empty = mk_seg(?seg_size),
#dict{empty=Empty,segs={Empty}}.
-spec is_key(Key, Dict) -> boolean() when
Key :: term(),
Dict :: dict().
%% @doc Test if a key is in a dictionary. This function tests if `Key' is
%% contained in the dictionary `Dict'.
is_key(Key, #dict{}=D) ->
Slot = get_slot(D, Key),
Bkt = get_bucket(D, Slot),
find_key(Key, Bkt);
is_key(Key, ?gb(_,_)=D) ->
gb_trees:is_defined(Key, D).
find_key(K, [?kv(K,_Val)|_]) -> true;
find_key(K, [_|Bkt]) -> find_key(K, Bkt);
find_key(_, []) -> false.
-spec to_list(Dict) -> List when
Dict :: dict(),
List :: [{Key :: term(), Value :: term()}].
%% @doc Convert a dictionary to a list of pairs. This function converts the
%% dictionary to a list representation. The result is an ordered list (an
%% `orddict') if `Dict' is of type `ordered_set' (see {@link new/1}), and
%% otherwise in the order defined by {@link foldl/3}.
to_list(#dict{}=D) ->
%% list in default traversal order, hence foldr, not foldl
foldr(fun (Key, Val, List) -> [{Key,Val}|List] end, [], D);
to_list(?gb(_,_)=D) ->
gb_trees:to_list(D). % always ordered
-spec to_orddict(Dict) -> List when
Dict :: dict(),
List :: [{Key :: term(), Value :: term()}].
%% @doc Convert a dictionary to an {@link orddict}. This differs from
%% `to_list' in that the result is always an ordered list, even if `Dict' is
%% of type `set'.
to_orddict(#dict{}=D) ->
orddict:from_list(to_list(D));
to_orddict(?gb(_,_)=D) ->
gb_trees:to_list(D). % always ordered
-spec from_list(List) -> Dict when
List :: [{Key :: term(), Value :: term()}],
Dict :: dict().
%% @doc Convert a list of pairs to a dictionary. This function converts the
%% `Key'-`Value' list `List' to a dictionary. In case of duplicate keys in
%% the list, later entries take precedence.
from_list(L) -> from_list(L, []).
-spec from_list(List, Opts :: [Option]) -> Dict when
List :: [{Key :: term(), Value :: term()}],
Option :: 'set' | 'ordered_set',
Dict :: dict().
%% @doc Convert a list of pairs to a dictionary. Like {@link from_list/1},
%% but takes an option list just like {@link new/1} to specify the type of
%% the new dictionary. The default type is `set'.
from_list(L, Opts) ->
try opts(Opts) of
R -> from_list_1(L, R)
catch
_:_ -> erlang:error(badarg, [L, Opts])
end.
from_list_1(L, #opts{type=set}) ->
lists:foldl(fun ({K,V}, D) -> store_dict(K, V, D) end, new_dict(), L);
from_list_1(L, #opts{type=ordered_set}) ->
%% NOTE: using from_orddict(lists:ukeysort(1, L)) would be nice, but
%% causes earlier entries to take precedence in case of duplicate keys
lists:foldl(fun ({K,V}, D) -> gb_trees:enter(K, V, D) end,
gb_trees:empty(), L).
-spec from_orddict(List) -> Dict when
List :: [{Key :: term(), Value :: term()}],
Dict :: dict().
%% @doc Convert an ordered list of pairs to a dictionary. Like {@link
%% from_list/1}, but relies on the fact that the `List' is already ordered
%% by its keys for better efficiency. Note that if `List' is not ordered or
%% contains duplicates, this function might throw an exception.
from_orddict(L) -> from_orddict(L, []).
-spec from_orddict(List, Opts :: [Option]) -> Dict when
List :: [{Key :: term(), Value :: term()}],
Option :: 'set' | 'ordered_set',
Dict :: dict().
%% @doc Convert an ordered list of pairs to a dictionary. Like {@link
%% from_orddict/1}, but takes an option list just like {@link new/1} to
%% specify the type of the new dictionary. The default type is `set'.
from_orddict(L, Opts) ->
try opts(Opts) of
R -> from_orddict_1(L, R)
catch
_:_ -> erlang:error(badarg, [L, Opts])
end.
from_orddict_1(L, #opts{type=ordered_set}) ->
gb_trees:from_orddict(L); % the list *must* be ordered for this!
from_orddict_1(L, Opts) ->
from_list_1(L, Opts).
-spec size(Dict) -> non_neg_integer() when
Dict :: dict().
%% @doc Return the number of elements in a dictionary. Returns the number of
%% elements in `Dict'. This is a constant time operation.
size(#dict{size=N}) when is_integer(N), N >= 0 -> N;
size(?gb(N,_)) when is_integer(N), N >= 0 -> N.
-spec is_empty(Dict) -> boolean() when
Dict :: dict().
%% @doc Test for empty dictionary. Returns `true' if `Dict' is empty,
%% and `false' otherwise. This is a constant time operation.
is_empty(Dict) -> size(Dict) =< 0.
-spec info(Dict) -> [InfoTuple] when
Dict :: dict(),
InfoTuple :: {InfoTag, Value},
InfoTag :: 'size' | 'type',
Value :: term().
%% @doc Get information about a dictionary. Returns a list of tagged tuples
%% corresponding to individual calls to {@link info/2} for all allowed tags.
%% This is a constant time operation.
info(Dict) ->
Items = [size,type],
[info(Item, Dict) || Item <- Items].
-spec info(InfoTag, Dict) -> Value when
Dict :: dict(),
InfoTag :: 'size' | 'type' | atom(),
Value :: term().
%% @doc Get information about a dictionary. This is a constant time
%% operation.
info(size, Dict) -> size(Dict);
info(type, #dict{}) -> set;
info(type, ?gb(_,_)) -> ordered_set;
info(_, _) -> undefined.
-spec values(Dict) -> [Val] when
Dict :: dict(),
Val :: term().
%% @doc Get all values in a dictionary. Returns the values in `Dict' as a
%% list, in the order defined by the {@link foldl/3} traversal order.
%% Duplicates are not removed.
values(Dict) ->
%% list in default traversal order, hence foldr, not foldl
foldr(fun (_Key, Val, Acc) -> [Val|Acc] end, [], Dict).
-spec values(Key, Dict) -> [Value] | [] when
Key :: term(),
Dict :: dict(),
Value :: term().
%% @doc List the values (if any) stored for a key. Returns either a list
%% `[Value]' of length 1, where `Value' is the value stored for `Key' in
%% `Dict', or `[]' if the key is not present in the dictionary.
%% @see find/2
values(Key, #dict{}=Dict) ->
Slot = get_slot(Dict, Key),
Bkt = get_bucket(Dict, Slot),
values_1(Key, Bkt);
values(Key, ?gb(_,_)=Dict) ->
gb_trees:values(Key,Dict).
values_1(K, [?kv(K,Val)|_]) -> [Val];
values_1(K, [_|Bkt]) -> find_val(K, Bkt);
values_1(_, []) -> [].
-spec fetch(Key, Dict) -> Value when
Key :: term(),
Dict :: dict(),
Value :: term().
%% @doc Get a value from a dictionary.
%% @deprecated This is an old name for {@link get/2}
%% @see get/2
fetch(Key, D) ->
get(Key, D).
-spec get(Key, Dict) -> Value when
Key :: term(),
Dict :: dict(),
Value :: term().
%% @doc Get a value from a dictionary. This function returns the value
%% associated with `Key' in the dictionary `Dict'. `get' assumes that `Key'
%% is present in the dictionary; if `Key' is not present, an exception is
%% generated instead.
get(Key, #dict{}=D) ->
Slot = get_slot(D, Key),
Bkt = get_bucket(D, Slot),
try get_val(Key, Bkt)
catch
badarg -> erlang:error(badarg, [Key, D])
end;
get(Key, ?gb(_,_)=D) ->
gb_trees:get(Key, D).
get_val(K, [?kv(K,Val)|_]) -> Val;
get_val(K, [_|Bkt]) -> get_val(K, Bkt);
get_val(_, []) -> throw(badarg).
-spec get(Key, Default, Dict) -> Value when
Key :: term(),
Default :: term(),
Dict :: dict(),
Value :: term().
%% @doc Get a value from a dictionary, or use a default. Returns the value
%% associated with `Key' in the dictionary `Dict', or returns `Default' if
%% the key is not present in `Dict'.
get(Key, Def, #dict{}=D) ->
Slot = get_slot(D, Key),
Bkt = get_bucket(D, Slot),
get_val(Key, Bkt, Def);
get(Key, Def, ?gb(_,_)=D) ->
gb_trees:get(Key, Def, D).
get_val(K, [?kv(K,Val)|_], _Def) -> Val;
get_val(K, [_|Bkt], Def) -> get_val(K, Bkt, Def);
get_val(_, [], Def) -> Def.
-spec find(Key, Dict) -> {'ok', Value} | 'error' when
Key :: term(),
Dict :: dict(),
Value :: term().
%% @doc Search for a key in a dictionary. This function searches for a key
%% in a dictionary. Returns `{ok, Value}' where `Value' is the value
%% associated with `Key', or `error' if the key is not present in the
%% dictionary.
find(Key, #dict{}=D) ->
Slot = get_slot(D, Key),
Bkt = get_bucket(D, Slot),
find_val(Key, Bkt);
find(Key, ?gb(_,_)=D) ->
gb_trees:find(Key, D).
find_val(K, [?kv(K,Val)|_]) -> {ok,Val};
find_val(K, [_|Bkt]) -> find_val(K, Bkt);
find_val(_, []) -> error.
-spec take(Key, Dict0) -> {Value, Dict1} when
Key :: term(),
Dict0 :: dict(),
Dict1 :: dict(),
Value :: term().
%% @doc Extract an entry from a dictionary. Returns a tuple with the value
%% for `Key' in `Dict0' and a new dictionary with the entry for the key
%% deleted. Throws an exception if the key is not present in `Dict0'.
take(Key, Dict) ->
{get(Key, Dict), erase(Key,Dict)}.
-spec fetch_keys(Dict) -> Keys when
Dict :: dict(),
Keys :: [term()].
%% @doc Return all keys in a dictionary.
%% @deprecated This is an old name for {@link keys/1}.
%% @see keys/1
fetch_keys(Dict) -> keys(Dict).
-spec keys(Dict) -> Keys when
Dict :: dict(),
Keys :: [term()].
%% @doc Return all keys in a dictionary. This function returns a list of all
%% keys in `Dict'. The result is an ordered list only if `Dict' is of type
%% `ordered_set' (see {@link new/1}).
keys(#dict{}=D) ->
%% list in default traversal order, hence foldr, not foldl
foldr(fun (Key, _Val, Keys) -> [Key|Keys] end, [], D);
keys(?gb(_,_)=D) ->
gb_trees:keys(D).
-spec erase(Key, Dict1) -> Dict2 when
Key :: term(),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Erase a key from a dictionary. This function erases all items with a
%% given key from a dictionary.
%% (note that this builds a new data structure even if Key is not present)
erase(Key, #dict{}=D0) ->
Slot = get_slot(D0, Key),
{D1,Dc} = on_bucket(fun (B0) -> erase_key(Key, B0) end,
D0, Slot),
maybe_contract(D1, Dc);
erase(Key, ?gb(_,_)=D0) ->
gb_trees:delete_any(Key, D0).
erase_key(Key, [?kv(Key,_Val)|Bkt]) -> {Bkt,1};
erase_key(Key, [E|Bkt0]) ->
{Bkt1,Dc} = erase_key(Key, Bkt0),
{[E|Bkt1],Dc};
erase_key(_, []) -> {[],0}.
-spec store(Key, Value, Dict1) -> Dict2 when
Key :: term(),
Value :: term(),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Store a value in a dictionary. This function stores a `Key'-`Value'
%% pair in a dictionary. If `Key' already exists in `Dict1', the associated
%% value is replaced by `Value'.
store(Key, Val, #dict{}=D0) ->
store_dict(Key, Val, D0);
store(Key, Val, ?gb(_,_)=D0) ->
gb_trees:enter(Key, Val, D0).
store_dict(Key, Val, D0) ->
Slot = get_slot(D0, Key),
{D1,Ic} = on_bucket(fun (B0) -> store_bkt_val(Key, Val, B0) end,
D0, Slot),
maybe_expand(D1, Ic).
%% store_bkt_val(Key, Val, Bucket) -> {NewBucket,PutCount}.
store_bkt_val(Key, New, [?kv(Key,_Old)|Bkt]) -> {[?kv(Key,New)|Bkt],0};
store_bkt_val(Key, New, [Other|Bkt0]) ->
{Bkt1,Ic} = store_bkt_val(Key, New, Bkt0),
{[Other|Bkt1],Ic};
store_bkt_val(Key, New, []) -> {[?kv(Key,New)],1}.
-spec replace(Key, Value, Dict1) -> Dict2 when
Key :: term(),
Value :: term(),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Replace the value for a key in a dictionary. This function replaces
%% the current value for `Key' in `Dict1' with `Value'. If `Key' does not
%% exist in `Dict1', the dictionary is returned unchanged.
replace(Key, Val, Dict) ->
case is_key(Key, Dict) of
true -> store(Key, Val, Dict);
false -> Dict
end.
-spec append(Key, Value, Dict1) -> Dict2 when
Key :: term(),
Value :: term(),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Append a value to keys in a dictionary. This function appends a new
%% `Value' to the current list of values associated with `Key'. Note that it
%% is generally a bad idea to accumulate a set of values by appending to a
%% list, if there may be more than just a few elements per key.
append(Key, Val, #dict{}=D0) ->
Slot = get_slot(D0, Key),
{D1,Ic} = on_bucket(fun (B0) -> append_bkt(Key, Val, B0) end,
D0, Slot),
maybe_expand(D1, Ic);
append(Key, Val, ?gb(_,_)=D0) ->
case gb_trees:lookup(Key, D0) of
{value, Bag} -> gb_trees:update(Key, Bag ++ [Val], D0);
none -> gb_trees:insert(Key, [Val], D0)
end.
%% append_bkt(Key, Val, Bucket) -> {NewBucket,PutCount}.
append_bkt(Key, Val, [?kv(Key,Bag)|Bkt]) -> {[?kv(Key,Bag ++ [Val])|Bkt],0};
append_bkt(Key, Val, [Other|Bkt0]) ->
{Bkt1,Ic} = append_bkt(Key, Val, Bkt0),
{[Other|Bkt1],Ic};
append_bkt(Key, Val, []) -> {[?kv(Key,[Val])],1}.
-spec append_list(Key, ValList, Dict1) -> Dict2 when
Key :: term(),
ValList :: [Value :: term()],
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Append new values to keys in a dictionary. This function appends a
%% list of values `ValList' to the current list of values associated with
%% `Key'. An exception is generated if the initial value associated with
%% `Key' is not a list of values. Note that it is generally a bad idea to
%% accumulate a set of values by appending to a list, if there may be more
%% than just a few elements per key.
append_list(Key, L, #dict{}=D0) ->
Slot = get_slot(D0, Key),
{D1,Ic} = on_bucket(fun (B0) -> app_list_bkt(Key, L, B0) end,
D0, Slot),
maybe_expand(D1, Ic);
append_list(Key, L, ?gb(_,_)=D0) ->
case gb_trees:lookup(Key, D0) of
{value, Bag} -> gb_trees:update(Key, Bag ++ L, D0);
none -> gb_trees:insert(Key, L, D0)
end.
%% app_list_bkt(Key, L, Bucket) -> {NewBucket,PutCount}.
app_list_bkt(Key, L, [?kv(Key,Bag)|Bkt]) -> {[?kv(Key,Bag ++ L)|Bkt],0};
app_list_bkt(Key, L, [Other|Bkt0]) ->
{Bkt1,Ic} = app_list_bkt(Key, L, Bkt0),
{[Other|Bkt1],Ic};
app_list_bkt(Key, L, []) -> {[?kv(Key,L)],1}.
-spec first_key(Dict) -> {ok, Key} | error when
Dict :: dict(),
Key :: term().
%% @doc Get the first key in the dictionary. Returns `{ok, Key}' where `Key'
%% is the smallest key in `Dict', or returns 'error' if `Dict' is empty.
first_key(#dict{}=T) ->
case next_bucket(T, T#dict.n) of
[?kv(K,_Val)|_Bkt] -> {ok,K};
[] -> error %No elements
end;
first_key(?gb(_,_)=T) ->
gb_trees:first_key(T).
%% see foldl for details on why this goes from higher bucket numbers to lower
next_bucket(_T, Slot) when Slot < 1 -> [];
next_bucket(T, Slot) ->
case get_bucket(T, Slot) of
[] -> next_bucket(T, Slot-1); %Empty bucket
B -> B
end.
-spec take_first(Dict1) -> {{Key, Val}, Dict2} | error when
Dict1 :: dict(),
Dict2 :: dict(),
Key :: term(),
Val :: term().
%% @doc Extract the first entry in the dictionary. Returns the key/value
%% pair for the smallest key in `Dict1' and a new dictionary with the entry
%% for the key deleted, or returns `error' if `Dict1' is empty.
take_first(Dict) ->
case first_key(Dict) of
{ok, Key} ->
{Val, Dict1} = take(Key, Dict),
{{Key, Val}, Dict1};
error ->
error
end.
-spec next_key(Key, Dict) -> {ok, Key1} | error when
Dict :: dict(),
Key :: term(),
Key1 :: term().
%% @doc Get the next larger key in the dictionary. Returns `{ok, Larger}'
%% where `Larger' is the smallest key in `Dict' larger than the given `Key',
%% or returns 'error' if `Key' is the last key in `Dict'. Throws an
%% exception if `Key' does not exist in `Dict'.
next_key(Key, #dict{}=T) ->
Slot = get_slot(T, Key),
B = get_bucket(T, Slot),
%% Find a bucket with something in it.
Bkt = case bucket_after_key(Key, B) of
error -> erlang:error(badarg,[Key, T]);
[] -> next_bucket(T, Slot-1);
Found -> Found
end,
case Bkt of
[?kv(Next,_Val)|_] -> {ok,Next};
[] -> error %We have reached the end!
end;
next_key(Key, ?gb(_,_)=T) ->
gb_trees:next_key(Key, T).
bucket_after_key(Key, [?kv(Key,_Val)|Bkt]) -> Bkt;
bucket_after_key(Key, [_Other|Bkt]) ->
bucket_after_key(Key, Bkt);
bucket_after_key(_Key, []) -> error. %Key not found!
-spec last_key(Dict) -> {ok, Key} | error when
Dict :: dict(),
Key :: term().
%% @doc Get the last key in the dictionary. Returns `{ok, Key}' where `Key'
%% is the largest key in `Dict', or returns 'error' if `Dict' is empty.
last_key(#dict{}=T) ->
case prev_bucket(T, 1) of
[] -> error; %No elements
Bkt ->
?kv(K,_Val) = lists:last(Bkt),
{ok,K}
end;
last_key(?gb(_,_)=T) ->
gb_trees:last_key(T).
%% see foldl for details on why this goes from lower bucket numbers to higher
prev_bucket(T, Slot) when Slot > T#dict.n -> [];
prev_bucket(T, Slot) ->
case get_bucket(T, Slot) of
[] -> prev_bucket(T, Slot+1); %Empty bucket
B -> B
end.
-spec take_last(Dict1) -> {{Key, Val}, Dict2} | error when
Dict1 :: dict(),
Dict2 :: dict(),
Key :: term(),
Val :: term().
%% @doc Extract the last entry in the dictionary. Returns the key/value pair
%% for the largest key in `Dict1' and a new dictionary with the entry for
%% the key deleted, or returns `error' if `Dict1' is empty.
take_last(Dict) ->
case last_key(Dict) of
{ok, Key} ->
{Val, Dict1} = take(Key, Dict),
{{Key, Val}, Dict1};
error ->
error
end.
-spec prev_key(Key, Dict) -> {ok, Key1} | error when
Dict :: dict(),
Key :: term(),
Key1 :: term().
%% @doc Get the next smaller key in the dictionary. Returns `{ok, Smaller}'
%% where `Smaller' is the largest key in `Dict' smaller than the given
%% `Key', or returns 'error' if `Key' is the smallest key in `Dict'. Throws
%% an exception if `Key' does not exist in `Dict'.
prev_key(Key, #dict{}=T) ->
Slot = get_slot(T, Key),
B = get_bucket(T, Slot),
%% Find a bucket with something in it.
Bkt = case bucket_before_key(Key, B) of
error -> erlang:error(badarg,[Key, T]);
[] -> prev_bucket(T, Slot+1);
Found -> Found
end,
case Bkt of
[] -> error; %We have reached the end!
Bkt ->
?kv(Prev,_Val) = lists:last(Bkt),
{ok,Prev}
end;
prev_key(Key, ?gb(_,_)=T) ->
gb_trees:prev_key(Key, T).
bucket_before_key(Key, [?kv(Key,_Val)|_]) -> []; % key was first in bucket
bucket_before_key(Key, [KV|Bkt]) ->
bucket_before_key(Key, Bkt, KV);
bucket_before_key(_Key, []) -> error. %Key not found!
bucket_before_key(Key, [?kv(Key,_Val)|_],KV) -> [KV]; % return as a bucket
bucket_before_key(Key, [KV|Bkt],_PrevKV) ->
bucket_before_key(Key, Bkt, KV);
bucket_before_key(_Key, [], _PrevKV) -> error. %Key not found!
-spec update(Fun, Key, Dict1) -> Dict2 when
Key :: term(),
Fun :: fun((Value1 :: term()) -> Value2 :: term()),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Update a value in a dictionary.
%% @deprecated This is an old variant of {@link map/3}. Note that the
%% argument order differs.
%% @see map/3
update(Key, F, D0) ->
map(F, Key, D0).
-spec map(Fun, Key, Dict1) -> Dict2 when
Key :: term(),
Fun :: fun((Value1 :: term()) -> Value2 :: term()),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Update a value in a dictionary. Update a value in a dictionary by
%% calling `Fun' on the value to get a new value. An exception is generated
%% if `Key' is not present in the dictionary.
map(F, Key, #dict{}=D0) ->
Slot = get_slot(D0, Key),
try on_bucket(fun (B0) -> update_bkt(Key, F, B0) end, D0, Slot) of
{D1,_Uv} -> D1
catch
badarg -> erlang:error(badarg, [F, Key, D0])
end;
map(F, Key, ?gb(_,_)=D0) ->
gb_trees:map(F, Key, D0).
update_bkt(Key, F, [?kv(Key,Val)|Bkt]) ->
Upd = F(Val),
{[?kv(Key,Upd)|Bkt],Upd};
update_bkt(Key, F, [Other|Bkt0]) ->
{Bkt1,Upd} = update_bkt(Key, F, Bkt0),
{[Other|Bkt1],Upd};
update_bkt(_Key, _F, []) ->
throw(badarg).
-spec update(Fun, Key, Initial, Dict1) -> Dict2 when
Key :: term(),
Initial :: term(),
Fun :: fun((Value1 :: term()) -> Value2 :: term()),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Update a value in a dictionary or insert a default.
%% @deprecated This is an old variant of {@link map/4}. Note that the
%% argument order differs.
%% @see map/4
update(Key, F, Init, D0) ->
map(F, Key, Init, D0).
-spec map(Fun, Key, Initial, Dict1) -> Dict2 when
Key :: term(),
Initial :: term(),
Fun :: fun((Value1 :: term()) -> Value2 :: term()),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Update a value in a dictionary or insert a default. Update a value
%% in a dictionary by calling `Fun' on the value to get a new value. If
%% `Key' is not present in the dictionary then `Initial' will be stored as
%% the first value.
%%
%% For example, {@link append/3} can be defined as:
%% ```append(Key, Val, D) ->
%% map(fun (Old) -> Old ++ [Val] end, [Val], Key, D).'''
map(F, Key, Init, #dict{}=D0) ->
Slot = get_slot(D0, Key),
{D1,Ic} = on_bucket(fun (B0) -> update_bkt(Key, F, Init, B0) end,
D0, Slot),
maybe_expand(D1, Ic);
map(F, Key, Init, ?gb(_,_)=D0) ->
gb_trees:map(F, Key, Init, D0).
update_bkt(Key, F, _, [?kv(Key,Val)|Bkt]) ->
{[?kv(Key,F(Val))|Bkt],0};
update_bkt(Key, F, I, [Other|Bkt0]) ->
{Bkt1,Ic} = update_bkt(Key, F, I, Bkt0),
{[Other|Bkt1],Ic};
update_bkt(Key, F, I, []) when is_function(F, 1) -> {[?kv(Key,I)],1}.
-spec update_counter(Key, Increment, Dict1) -> Dict2 when
Key :: term(),
Increment :: number(),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Map a function over a dictionary.
%% @deprecated This is an old name for {@link increment/3}.
%% @see increment/3
update_counter(Key, Incr, D0) ->
increment(Key, Incr, D0).
-spec increment(Key, Increment, Dict1) -> Dict2 when
Key :: term(),
Increment :: number(),
Dict1 :: dict(),
Dict2 :: dict().
%% @doc Increment a value in a dictionary. Add `Increment' to the value
%% associated with `Key' and store this value. If `Key' is not present in
%% the dictionary then `Increment' will be stored as the first value.
%%
%% This could be defined as:
%% ```increment(Key, Incr, D) ->
%% map(fun (Old) -> Old + Incr end, Key, Incr, D).'''
increment(Key, Incr, #dict{}=D0) when is_number(Incr) ->
Slot = get_slot(D0, Key),
{D1,Ic} = on_bucket(fun (B0) -> counter_bkt(Key, Incr, B0) end,
D0, Slot),
maybe_expand(D1, Ic);
increment(Key, Incr, ?gb(_,_)=D0) when is_number(Incr) ->
gb_trees:increment(Key, Incr, D0).
counter_bkt(Key, I, [?kv(Key,Val)|Bkt]) ->
{[?kv(Key,Val+I)|Bkt],0};
counter_bkt(Key, I, [Other|Bkt0]) ->
{Bkt1,Ic} = counter_bkt(Key, I, Bkt0),
{[Other|Bkt1],Ic};
counter_bkt(Key, I, []) -> {[?kv(Key,I)],1}.
%% get_slot(Hashdb, Key) -> Slot.
%% Get the slot. First hash on the new range, if we hit a bucket
%% which has not been split use the unsplit buddy bucket.
get_slot(T, Key) ->
H = erlang:phash(Key, T#dict.maxn),
if
H > T#dict.n -> H - T#dict.bso;
true -> H
end.
%% get_bucket(Hashdb, Slot) -> Bucket.
get_bucket(T, Slot) -> get_bucket_s(T#dict.segs, Slot).
%% on_bucket(Fun, Hashdb, Slot) -> {NewHashDb,Result}.
%% Apply Fun to the bucket in Slot and replace the returned bucket.
on_bucket(F, T, Slot) ->
SegI = ((Slot-1) div ?seg_size) + 1,
BktI = ((Slot-1) rem ?seg_size) + 1,
Segs = T#dict.segs,
Seg = element(SegI, Segs),
B0 = element(BktI, Seg),
{B1,Res} = F(B0), %Op on the bucket.
{T#dict{segs=setelement(SegI, Segs, setelement(BktI, Seg, B1))},Res}.
%% Work functions for fold, map and filter operations. These
%% traverse the hash structure rebuilding as necessary. Note we
%% could have implemented map and filter using fold but these are
%% faster. We hope!
%% Fold function Fun over all "bags" in Table and return Accumulator.
-spec fold(Fun, Acc0, Dict) -> Acc1 when
Fun :: fun((Key, Value, AccIn) -> AccOut),
Key :: term(),
Value :: term(),
Acc0 :: term(),
Acc1 :: term(),
AccIn :: term(),
AccOut :: term(),
Dict :: dict().
%% @doc Fold a function over a dictionary.
%% @deprecated This is an old name for {@link foldl/3}.
%% @see foldl/3
fold(Fun, Acc0, Dict) -> foldl(Fun, Acc0, Dict).
-spec foldl(Fun, Acc0, Dict) -> Acc1 when
Fun :: fun((Key, Value, AccIn) -> AccOut),
Key :: term(),
Value :: term(),
Acc0 :: term(),
Acc1 :: term(),
AccIn :: term(),
AccOut :: term(),
Dict :: dict().
%% @doc Fold a function over a dictionary. Calls `Fun' on successive keys
%% and values of `Dict' together with an extra argument `Acc' (short for
%% accumulator). `Fun' must return a new accumulator which is passed to the
%% next call. `Acc0' is returned if the list is empty. For an ordered
%% dictionary, the evaluation order is from lower keys towards higher.
foldl(Fun, Acc, #dict{}=Dict) ->
Segs = Dict#dict.segs,
foldl_segs(Fun, Acc, Segs, tuple_size(Segs));
foldl(Fun, Acc, ?gb(_,_)=Dict) ->
gb_trees:foldl(Fun, Acc, Dict).
%% Note that this traversal *defines* what the default order is - even if it
%% traverses the individual tuples from right to left, it's still "foldl"!
%% This order must match the functions first_key/last_key/next_key/prev_key!
%% (Rather than change the legacy traversal order of the default fold, we
%% have chosen to implement first/last/next/prev to go from higher bucket
%% indices towards lower, just like foldl.)
foldl_segs(F, Acc, Segs, I) when I >= 1 ->
Seg = element(I, Segs),
foldl_segs(F, foldl_seg(F, Acc, Seg, tuple_size(Seg)), Segs, I-1);
foldl_segs(F, Acc, _, 0) when is_function(F, 3) -> Acc.
foldl_seg(F, Acc, Seg, I) when I >= 1 ->
foldl_seg(F, foldl_bucket(F, Acc, element(I, Seg)), Seg, I-1);
foldl_seg(F, Acc, _, 0) when is_function(F, 3) -> Acc.
foldl_bucket(F, Acc, [?kv(Key,Val)|Bkt]) ->
foldl_bucket(F, F(Key, Val, Acc), Bkt);