@@ -55,12 +55,6 @@ defmodule HashDict do
55
55
root: @ node_template
56
56
57
57
58
- @ typep ordered :: { HashDict , size :: non_neg_integer , [ { key :: term , value :: term } ] }
59
- @ typep trie :: { HashDict , size :: non_neg_integer , depth :: non_neg_integer ,
60
- expand_on :: non_neg_integer , contract_on :: non_neg_integer ,
61
- root :: tuple }
62
- @ opaque t :: ordered | trie
63
-
64
58
import Bitwise
65
59
66
60
# Let's inline common instructions
@@ -69,7 +63,7 @@ defmodule HashDict do
69
63
@ doc """
70
64
Creates a new empty dict.
71
65
"""
72
- @ spec new :: t
66
+ @ spec new :: Dict . t
73
67
def new do
74
68
ordered ( )
75
69
end
@@ -83,7 +77,7 @@ defmodule HashDict do
83
77
#=> HashDict[a: 1, b: 2]
84
78
85
79
"""
86
- @ spec new ( [ { term :: term , value :: term } ] ) :: t
80
+ @ spec new ( list ( { key :: term , value :: term } ) ) :: Dict . t
87
81
def new ( pairs ) do
88
82
Enum . reduce pairs , ordered ( ) , fn { k , v } , dict ->
89
83
put ( dict , k , v )
@@ -100,7 +94,7 @@ defmodule HashDict do
100
94
#=> HashDict[{ "a", "a" }, { "b", "b" }]
101
95
102
96
"""
103
- @ spec new ( list , ( term -> { key :: term , key :: term } ) ) :: t
97
+ @ spec new ( list , ( term -> { key :: term , value :: term } ) ) :: Dict . t
104
98
def new ( list , transform ) when is_function ( transform ) do
105
99
Enum . reduce list , new ( ) , fn i , dict ->
106
100
{ k , v } = transform . ( i )
@@ -111,7 +105,6 @@ defmodule HashDict do
111
105
@ doc """
112
106
Puts the given key and value in the dict.
113
107
"""
114
- @ spec put ( t , key :: term , value :: term ) :: t
115
108
def put ( dict , key , value ) do
116
109
{ dict , _ } = dict_put ( dict , key , { :put , value } )
117
110
dict
@@ -121,7 +114,6 @@ defmodule HashDict do
121
114
Puts the given value under key in the dictionary
122
115
only if one does not exist yet.
123
116
"""
124
- @ spec put_new ( t , key :: term , value :: term ) :: t
125
117
def put_new ( dict , key , value ) do
126
118
update ( dict , key , value , fn ( v ) -> v end )
127
119
end
@@ -131,7 +123,6 @@ defmodule HashDict do
131
123
to the given function. Raises if the key does
132
124
not exist in the dictionary.
133
125
"""
134
- @ spec update ( t , key :: term , ( term -> term ) ) :: t
135
126
def update ( dict , key , fun ) when is_function ( fun , 1 ) do
136
127
case dict_put ( dict , key , { :update , nil , fun } ) do
137
128
{ dict , 0 } ->
@@ -146,7 +137,6 @@ defmodule HashDict do
146
137
to the given function. Adds initial value if
147
138
the key does not exist in the dicionary.
148
139
"""
149
- @ spec update ( t , key :: term , initial :: term , ( term -> term ) ) :: t
150
140
def update ( dict , key , initial , fun ) when is_function ( fun , 1 ) do
151
141
{ dict , _ } = dict_put ( dict , key , { :update , initial , fun } )
152
142
dict
@@ -155,8 +145,6 @@ defmodule HashDict do
155
145
@ doc """
156
146
Gets the value under key from the dict.
157
147
"""
158
- @ spec get ( t , key :: term ) :: term
159
- @ spec get ( t , key :: term , default :: term ) :: term
160
148
def get ( dict , key , default // nil ) do
161
149
case dict_get ( dict , key ) do
162
150
{ ^ key , value } -> value
@@ -168,7 +156,6 @@ defmodule HashDict do
168
156
Gets the value under key from the dict,
169
157
raises KeyError if such key does not exist.
170
158
"""
171
- @ spec get! ( t , key :: term ) :: term | no_return
172
159
def get! ( dict , key ) when is_tuple ( dict ) do
173
160
case dict_get ( dict , key ) do
174
161
{ ^ key , value } -> value
@@ -179,15 +166,13 @@ defmodule HashDict do
179
166
@ doc """
180
167
Checks if the dict has the given key.
181
168
"""
182
- @ spec has_key? ( t , key :: term ) :: boolean
183
169
def has_key? ( dict , key ) do
184
170
match? { ^ key , _ } , dict_get ( dict , key )
185
171
end
186
172
187
173
@ doc """
188
174
Deletes a value from the dict.
189
175
"""
190
- @ spec delete ( t , key :: term ) :: t
191
176
def delete ( ordered ( bucket: bucket , size: size ) = dict , key ) do
192
177
case bucket_delete ( bucket , key ) do
193
178
{ _ , 0 } ->
@@ -220,23 +205,20 @@ defmodule HashDict do
220
205
@ doc """
221
206
Returns the dict size.
222
207
"""
223
- @ spec size ( t ) :: non_neg_integer
224
208
def size ( dict ) do
225
209
elem ( dict , 1 )
226
210
end
227
211
228
212
@ doc """
229
213
Returns an empty dict.
230
214
"""
231
- @ spec empty ( t ) :: t
232
215
def empty ( _ ) do
233
216
ordered ( )
234
217
end
235
218
236
219
@ doc """
237
220
Converts the dict to a list.
238
221
"""
239
- @ spec to_list ( t ) :: list ( { key :: term , value :: term } )
240
222
def to_list ( ordered ( bucket: bucket ) ) do
241
223
bucket
242
224
end
@@ -248,24 +230,20 @@ defmodule HashDict do
248
230
@ doc """
249
231
Get all keys in the dict.
250
232
"""
251
- @ spec keys ( t ) :: list ( key :: term )
252
233
def keys ( dict ) do
253
234
dict_fold ( dict , [ ] , fn { k , _ } , acc -> [ k | acc ] end )
254
235
end
255
236
256
237
@ doc """
257
238
Get all values in the dict.
258
239
"""
259
- @ spec values ( t ) :: list ( values :: term )
260
240
def values ( dict ) do
261
241
dict_fold ( dict , [ ] , fn { _ , v } , acc -> [ v | acc ] end )
262
242
end
263
243
264
244
@ doc """
265
245
Merges two dictionaries.
266
246
"""
267
- @ spec merge ( t , t | Dict . t ) :: t
268
- @ spec merge ( t , t | Dict . t , ( ( key :: term , value1 :: term , value2 :: term ) -> value :: term ) ) :: t
269
247
def merge ( dict , enum , callback // fn ( _k , _v1 , v2 ) -> v2 end )
270
248
271
249
def merge ( dict1 , dict2 , callback ) when is_record ( dict1 , HashDict ) and is_record ( dict2 , HashDict ) and elem ( dict1 , 1 ) < elem ( dict2 , 1 ) do
0 commit comments