-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
dict.ex
443 lines (374 loc) · 10.6 KB
/
dict.ex
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
defmodule Dict do
@moduledoc ~S"""
Generic API for dictionaries.
If you need a general dictionary, use the `Map` module.
If you need to manipulate keyword lists, use `Keyword`.
To convert maps into keywords and vice-versa, use the
`new` function in the respective modules.
"""
@moduledoc deprecated: "Use Map or Keyword modules instead"
@type key :: any
@type value :: any
@type t :: list | map
message =
"Use the Map module for working with maps or the Keyword module for working with keyword lists"
defmacro __using__(_) do
# Use this import to guarantee proper code expansion
import Kernel, except: [size: 1]
if __CALLER__.module != HashDict do
IO.warn("use Dict is deprecated. " <> unquote(message), __CALLER__)
end
quote do
message = "Use maps and the Map module instead"
@deprecated message
def get(dict, key, default \\ nil) do
case fetch(dict, key) do
{:ok, value} -> value
:error -> default
end
end
@deprecated message
def get_lazy(dict, key, fun) when is_function(fun, 0) do
case fetch(dict, key) do
{:ok, value} -> value
:error -> fun.()
end
end
@deprecated message
def get_and_update(dict, key, fun) do
current_value = get(dict, key)
{get, new_value} = fun.(current_value)
{get, put(dict, key, new_value)}
end
@deprecated message
def fetch!(dict, key) do
case fetch(dict, key) do
{:ok, value} -> value
:error -> raise KeyError, key: key, term: dict
end
end
@deprecated message
def has_key?(dict, key) do
match?({:ok, _}, fetch(dict, key))
end
@deprecated message
def put_new(dict, key, value) do
case has_key?(dict, key) do
true -> dict
false -> put(dict, key, value)
end
end
@deprecated message
def put_new_lazy(dict, key, fun) when is_function(fun, 0) do
case has_key?(dict, key) do
true -> dict
false -> put(dict, key, fun.())
end
end
@deprecated message
def drop(dict, keys) do
Enum.reduce(keys, dict, &delete(&2, &1))
end
@deprecated message
def take(dict, keys) do
Enum.reduce(keys, new(), fn key, acc ->
case fetch(dict, key) do
{:ok, value} -> put(acc, key, value)
:error -> acc
end
end)
end
@deprecated message
def to_list(dict) do
reduce(dict, {:cont, []}, fn kv, acc -> {:cont, [kv | acc]} end)
|> elem(1)
|> :lists.reverse()
end
@deprecated message
def keys(dict) do
reduce(dict, {:cont, []}, fn {k, _}, acc -> {:cont, [k | acc]} end)
|> elem(1)
|> :lists.reverse()
end
@deprecated message
def values(dict) do
reduce(dict, {:cont, []}, fn {_, v}, acc -> {:cont, [v | acc]} end)
|> elem(1)
|> :lists.reverse()
end
@deprecated message
def equal?(dict1, dict2) do
# Use this import to avoid conflicts in the user code
import Kernel, except: [size: 1]
case size(dict1) == size(dict2) do
false ->
false
true ->
reduce(dict1, {:cont, true}, fn {k, v}, _acc ->
case fetch(dict2, k) do
{:ok, ^v} -> {:cont, true}
_ -> {:halt, false}
end
end)
|> elem(1)
end
end
@deprecated message
def merge(dict1, dict2, fun \\ fn _k, _v1, v2 -> v2 end) do
# Use this import to avoid conflicts in the user code
import Kernel, except: [size: 1]
if size(dict1) < size(dict2) do
reduce(dict1, {:cont, dict2}, fn {k, v1}, acc ->
{:cont, update(acc, k, v1, &fun.(k, v1, &1))}
end)
else
reduce(dict2, {:cont, dict1}, fn {k, v2}, acc ->
{:cont, update(acc, k, v2, &fun.(k, &1, v2))}
end)
end
|> elem(1)
end
@deprecated message
def update(dict, key, default, fun) do
case fetch(dict, key) do
{:ok, value} ->
put(dict, key, fun.(value))
:error ->
put(dict, key, default)
end
end
@deprecated message
def update!(dict, key, fun) do
case fetch(dict, key) do
{:ok, value} ->
put(dict, key, fun.(value))
:error ->
raise KeyError, key: key, term: dict
end
end
@deprecated message
def pop(dict, key, default \\ nil) do
case fetch(dict, key) do
{:ok, value} ->
{value, delete(dict, key)}
:error ->
{default, dict}
end
end
@deprecated message
def pop_lazy(dict, key, fun) when is_function(fun, 0) do
case fetch(dict, key) do
{:ok, value} ->
{value, delete(dict, key)}
:error ->
{fun.(), dict}
end
end
@deprecated message
def split(dict, keys) do
Enum.reduce(keys, {new(), dict}, fn key, {inc, exc} = acc ->
case fetch(exc, key) do
{:ok, value} ->
{put(inc, key, value), delete(exc, key)}
:error ->
acc
end
end)
end
defoverridable merge: 2,
merge: 3,
equal?: 2,
to_list: 1,
keys: 1,
values: 1,
take: 2,
drop: 2,
get: 2,
get: 3,
fetch!: 2,
has_key?: 2,
put_new: 3,
pop: 2,
pop: 3,
split: 2,
update: 4,
update!: 3,
get_and_update: 3,
get_lazy: 3,
pop_lazy: 3,
put_new_lazy: 3
end
end
defmacrop target(dict) do
quote do
case unquote(dict) do
%module{} -> module
%{} -> Map
dict when is_list(dict) -> Keyword
dict -> unsupported_dict(dict)
end
end
end
@deprecated message
@spec keys(t) :: [key]
def keys(dict) do
target(dict).keys(dict)
end
@deprecated message
@spec values(t) :: [value]
def values(dict) do
target(dict).values(dict)
end
@deprecated message
@spec size(t) :: non_neg_integer
def size(dict) do
target(dict).size(dict)
end
@deprecated message
@spec has_key?(t, key) :: boolean
def has_key?(dict, key) do
target(dict).has_key?(dict, key)
end
@deprecated message
@spec get(t, key, value) :: value
def get(dict, key, default \\ nil) do
target(dict).get(dict, key, default)
end
@deprecated message
@spec get_lazy(t, key, (-> value)) :: value
def get_lazy(dict, key, fun) do
target(dict).get_lazy(dict, key, fun)
end
@deprecated message
@spec get_and_update(t, key, (value -> {value, value})) :: {value, t}
def get_and_update(dict, key, fun) do
target(dict).get_and_update(dict, key, fun)
end
@deprecated message
@spec fetch(t, key) :: value
def fetch(dict, key) do
target(dict).fetch(dict, key)
end
@deprecated message
@spec fetch!(t, key) :: value
def fetch!(dict, key) do
target(dict).fetch!(dict, key)
end
@deprecated message
@spec put(t, key, value) :: t
def put(dict, key, val) do
target(dict).put(dict, key, val)
end
@deprecated message
@spec put_new(t, key, value) :: t
def put_new(dict, key, val) do
target(dict).put_new(dict, key, val)
end
@deprecated message
@spec put_new_lazy(t, key, (-> value)) :: t
def put_new_lazy(dict, key, fun) do
target(dict).put_new_lazy(dict, key, fun)
end
@deprecated message
@spec delete(t, key) :: t
def delete(dict, key) do
target(dict).delete(dict, key)
end
@deprecated message
@spec merge(t, t) :: t
def merge(dict1, dict2) do
target1 = target(dict1)
target2 = target(dict2)
if target1 == target2 do
target1.merge(dict1, dict2)
else
do_merge(target1, dict1, dict2, fn _k, _v1, v2 -> v2 end)
end
end
@deprecated message
@spec merge(t, t, (key, value, value -> value)) :: t
def merge(dict1, dict2, fun) do
target1 = target(dict1)
target2 = target(dict2)
if target1 == target2 do
target1.merge(dict1, dict2, fun)
else
do_merge(target1, dict1, dict2, fun)
end
end
defp do_merge(target1, dict1, dict2, fun) do
Enumerable.reduce(dict2, {:cont, dict1}, fn {k, v}, acc ->
{:cont, target1.update(acc, k, v, fn other -> fun.(k, other, v) end)}
end)
|> elem(1)
end
@deprecated message
@spec pop(t, key, value) :: {value, t}
def pop(dict, key, default \\ nil) do
target(dict).pop(dict, key, default)
end
@deprecated message
@spec pop_lazy(t, key, (-> value)) :: {value, t}
def pop_lazy(dict, key, fun) do
target(dict).pop_lazy(dict, key, fun)
end
@deprecated message
@spec update!(t, key, (value -> value)) :: t
def update!(dict, key, fun) do
target(dict).update!(dict, key, fun)
end
@deprecated message
@spec update(t, key, value, (value -> value)) :: t
def update(dict, key, default, fun) do
target(dict).update(dict, key, default, fun)
end
@deprecated message
@spec split(t, [key]) :: {t, t}
def split(dict, keys) do
target(dict).split(dict, keys)
end
@deprecated message
@spec drop(t, [key]) :: t
def drop(dict, keys) do
target(dict).drop(dict, keys)
end
@deprecated message
@spec take(t, [key]) :: t
def take(dict, keys) do
target(dict).take(dict, keys)
end
@deprecated message
@spec empty(t) :: t
def empty(dict) do
target(dict).empty(dict)
end
@deprecated message
@spec equal?(t, t) :: boolean
def equal?(dict1, dict2) do
target1 = target(dict1)
target2 = target(dict2)
cond do
target1 == target2 ->
target1.equal?(dict1, dict2)
target1.size(dict1) == target2.size(dict2) ->
Enumerable.reduce(dict2, {:cont, true}, fn {k, v}, _acc ->
case target1.fetch(dict1, k) do
{:ok, ^v} -> {:cont, true}
_ -> {:halt, false}
end
end)
|> elem(1)
true ->
false
end
end
@deprecated message
@spec to_list(t) :: list
def to_list(dict) do
target(dict).to_list(dict)
end
@spec unsupported_dict(t) :: no_return
defp unsupported_dict(dict) do
raise ArgumentError, "unsupported dict: #{inspect(dict)}"
end
end