@@ -55,12 +55,6 @@ defmodule HashDict do
5555 root: @ node_template
5656
5757
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-
6458 import Bitwise
6559
6660 # Let's inline common instructions
@@ -69,7 +63,7 @@ defmodule HashDict do
6963 @ doc """
7064 Creates a new empty dict.
7165 """
72- @ spec new :: t
66+ @ spec new :: Dict . t
7367 def new do
7468 ordered ( )
7569 end
@@ -83,7 +77,7 @@ defmodule HashDict do
8377 #=> HashDict[a: 1, b: 2]
8478
8579 """
86- @ spec new ( [ { term :: term , value :: term } ] ) :: t
80+ @ spec new ( list ( { key :: term , value :: term } ) ) :: Dict . t
8781 def new ( pairs ) do
8882 Enum . reduce pairs , ordered ( ) , fn { k , v } , dict ->
8983 put ( dict , k , v )
@@ -100,7 +94,7 @@ defmodule HashDict do
10094 #=> HashDict[{ "a", "a" }, { "b", "b" }]
10195
10296 """
103- @ spec new ( list , ( term -> { key :: term , key :: term } ) ) :: t
97+ @ spec new ( list , ( term -> { key :: term , value :: term } ) ) :: Dict . t
10498 def new ( list , transform ) when is_function ( transform ) do
10599 Enum . reduce list , new ( ) , fn i , dict ->
106100 { k , v } = transform . ( i )
@@ -111,7 +105,6 @@ defmodule HashDict do
111105 @ doc """
112106 Puts the given key and value in the dict.
113107 """
114- @ spec put ( t , key :: term , value :: term ) :: t
115108 def put ( dict , key , value ) do
116109 { dict , _ } = dict_put ( dict , key , { :put , value } )
117110 dict
@@ -121,7 +114,6 @@ defmodule HashDict do
121114 Puts the given value under key in the dictionary
122115 only if one does not exist yet.
123116 """
124- @ spec put_new ( t , key :: term , value :: term ) :: t
125117 def put_new ( dict , key , value ) do
126118 update ( dict , key , value , fn ( v ) -> v end )
127119 end
@@ -131,7 +123,6 @@ defmodule HashDict do
131123 to the given function. Raises if the key does
132124 not exist in the dictionary.
133125 """
134- @ spec update ( t , key :: term , ( term -> term ) ) :: t
135126 def update ( dict , key , fun ) when is_function ( fun , 1 ) do
136127 case dict_put ( dict , key , { :update , nil , fun } ) do
137128 { dict , 0 } ->
@@ -146,7 +137,6 @@ defmodule HashDict do
146137 to the given function. Adds initial value if
147138 the key does not exist in the dicionary.
148139 """
149- @ spec update ( t , key :: term , initial :: term , ( term -> term ) ) :: t
150140 def update ( dict , key , initial , fun ) when is_function ( fun , 1 ) do
151141 { dict , _ } = dict_put ( dict , key , { :update , initial , fun } )
152142 dict
@@ -155,8 +145,6 @@ defmodule HashDict do
155145 @ doc """
156146 Gets the value under key from the dict.
157147 """
158- @ spec get ( t , key :: term ) :: term
159- @ spec get ( t , key :: term , default :: term ) :: term
160148 def get ( dict , key , default // nil ) do
161149 case dict_get ( dict , key ) do
162150 { ^ key , value } -> value
@@ -168,7 +156,6 @@ defmodule HashDict do
168156 Gets the value under key from the dict,
169157 raises KeyError if such key does not exist.
170158 """
171- @ spec get! ( t , key :: term ) :: term | no_return
172159 def get! ( dict , key ) when is_tuple ( dict ) do
173160 case dict_get ( dict , key ) do
174161 { ^ key , value } -> value
@@ -179,15 +166,13 @@ defmodule HashDict do
179166 @ doc """
180167 Checks if the dict has the given key.
181168 """
182- @ spec has_key? ( t , key :: term ) :: boolean
183169 def has_key? ( dict , key ) do
184170 match? { ^ key , _ } , dict_get ( dict , key )
185171 end
186172
187173 @ doc """
188174 Deletes a value from the dict.
189175 """
190- @ spec delete ( t , key :: term ) :: t
191176 def delete ( ordered ( bucket: bucket , size: size ) = dict , key ) do
192177 case bucket_delete ( bucket , key ) do
193178 { _ , 0 } ->
@@ -220,23 +205,20 @@ defmodule HashDict do
220205 @ doc """
221206 Returns the dict size.
222207 """
223- @ spec size ( t ) :: non_neg_integer
224208 def size ( dict ) do
225209 elem ( dict , 1 )
226210 end
227211
228212 @ doc """
229213 Returns an empty dict.
230214 """
231- @ spec empty ( t ) :: t
232215 def empty ( _ ) do
233216 ordered ( )
234217 end
235218
236219 @ doc """
237220 Converts the dict to a list.
238221 """
239- @ spec to_list ( t ) :: list ( { key :: term , value :: term } )
240222 def to_list ( ordered ( bucket: bucket ) ) do
241223 bucket
242224 end
@@ -248,24 +230,20 @@ defmodule HashDict do
248230 @ doc """
249231 Get all keys in the dict.
250232 """
251- @ spec keys ( t ) :: list ( key :: term )
252233 def keys ( dict ) do
253234 dict_fold ( dict , [ ] , fn { k , _ } , acc -> [ k | acc ] end )
254235 end
255236
256237 @ doc """
257238 Get all values in the dict.
258239 """
259- @ spec values ( t ) :: list ( values :: term )
260240 def values ( dict ) do
261241 dict_fold ( dict , [ ] , fn { _ , v } , acc -> [ v | acc ] end )
262242 end
263243
264244 @ doc """
265245 Merges two dictionaries.
266246 """
267- @ spec merge ( t , t | Dict . t ) :: t
268- @ spec merge ( t , t | Dict . t , ( ( key :: term , value1 :: term , value2 :: term ) -> value :: term ) ) :: t
269247 def merge ( dict , enum , callback // fn ( _k , _v1 , v2 ) -> v2 end )
270248
271249 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