@@ -54,39 +54,47 @@ index_to_key(::Type{MyKey}, i::Int64) = MyKey(i)
5454key_to_index(key::MyKey) = key.x
5555```
5656"""
57- mutable struct CleverDict{K, V, F<: Function , I<: Function } <: AbstractDict{K, V}
57+ mutable struct CleverDict{K,V, F<: Function ,I<: Function } <: AbstractDict{K,V}
5858 last_index:: Int64
5959 hash:: F
6060 inverse_hash:: I
6161 is_dense:: Bool
6262 set:: BitSet
6363 vector:: Vector{V}
64- dict:: OrderedCollections.OrderedDict{K, V}
65- function CleverDict {K, V} (
66- n:: Integer = 0
67- ) where {K, V}
64+ dict:: OrderedCollections.OrderedDict{K,V}
65+ function CleverDict {K,V} (n:: Integer = 0 ) where {K,V}
6866 set = BitSet ()
6967 sizehint! (set, n)
7068 vec = Vector {K} (undef, n)
7169 inverse_hash = x -> index_to_key (K, x)
7270 hash = key_to_index
73- new {K, V, typeof(hash), typeof(inverse_hash)} (
74- 0 , hash, inverse_hash, true ,
71+ return new {K,V,typeof(hash),typeof(inverse_hash)} (
72+ 0 ,
73+ hash,
74+ inverse_hash,
75+ true ,
7576 set,
7677 vec,
77- OrderedCollections. OrderedDict {K, V} ())
78+ OrderedCollections. OrderedDict {K,V} (),
79+ )
7880 end
79- function CleverDict {K, V} (hash:: F , inverse_hash:: I ,
80- n:: Integer = 0
81- ) where {K, V, F, I}
81+ function CleverDict {K,V} (
82+ hash:: F ,
83+ inverse_hash:: I ,
84+ n:: Integer = 0 ,
85+ ) where {K,V,F,I}
8286 set = BitSet ()
8387 sizehint! (set, n)
8488 vec = Vector {K} (undef, n)
85- new {K, V, F, I} (
86- 0 , hash, inverse_hash, true ,
89+ return new {K,V,F,I} (
90+ 0 ,
91+ hash,
92+ inverse_hash,
93+ true ,
8794 set,
8895 vec,
89- OrderedCollections. OrderedDict {K, V} ())
96+ OrderedCollections. OrderedDict {K,V} (),
97+ )
9098 end
9199end
92100
@@ -112,7 +120,7 @@ _is_dense(c::CleverDict) = c.is_dense
112120
113121Set `val` in the next available key, and return that key.
114122"""
115- function add_item (c:: CleverDict{K, V} , val:: V ):: K where {K, V}
123+ function add_item (c:: CleverDict{K,V} , val:: V ):: K where {K,V}
116124 if c. last_index == - 1
117125 error (" Keys were added out of order. `add_item` requires that keys are always added in order." )
118126 end
@@ -139,7 +147,7 @@ function Base.length(c::CleverDict)
139147 return _is_dense (c) ? length (c. set) : length (c. dict)
140148end
141149
142- function Base. haskey (c:: CleverDict{K} , key:: K ) where K
150+ function Base. haskey (c:: CleverDict{K} , key:: K ) where {K}
143151 return _is_dense (c) ? c. hash (key):: Int64 in c. set : haskey (c. dict, key)
144152end
145153
@@ -165,7 +173,7 @@ function Base.getindex(c::CleverDict, key)
165173 end
166174end
167175
168- function Base. setindex! (c:: CleverDict{K, V} , value:: V , key:: K ):: V where {K, V}
176+ function Base. setindex! (c:: CleverDict{K,V} , value:: V , key:: K ):: V where {K,V}
169177 h = c. hash (key):: Int64
170178 if c. last_index != - 1
171179 if h == c. last_index + 1
@@ -194,7 +202,7 @@ function Base.setindex!(c::CleverDict{K, V}, value::V, key::K)::V where {K, V}
194202 return value
195203end
196204
197- function _rehash (c:: CleverDict{K} ) where K
205+ function _rehash (c:: CleverDict{K} ) where {K}
198206 sizehint! (c. dict, length (c. vector))
199207 @assert _is_dense (c)
200208 # Since c is currently dense, iterator protocol from CleverDict is used.
@@ -207,7 +215,7 @@ function _rehash(c::CleverDict{K}) where K
207215 return
208216end
209217
210- function Base. delete! (c:: CleverDict{K} , k:: K ) where K
218+ function Base. delete! (c:: CleverDict{K} , k:: K ) where {K}
211219 if _is_dense (c)
212220 _rehash (c)
213221 end
@@ -222,7 +230,7 @@ struct LinearIndex
222230 i:: Int64
223231end
224232
225- function Base. getindex (c:: CleverDict{K, V} , index:: LinearIndex ):: V where {K, V}
233+ function Base. getindex (c:: CleverDict{K,V} , index:: LinearIndex ):: V where {K,V}
226234 if ! (1 <= index. i <= length (c))
227235 throw (KeyError (index))
228236 end
@@ -265,12 +273,12 @@ struct State
265273 uint:: UInt64
266274 # ::Integer is needed for 32-bit machines.
267275 State (i:: Integer ) = new (Int64 (i), UInt64 (0 ))
268- State (i:: Integer , j:: UInt64 ) = new (Int64 (i) , j)
276+ State (i:: Integer , j:: UInt64 ) = new (Int64 (i), j)
269277end
270278
271279function Base. iterate (
272- c:: CleverDict{K, V}
273- ):: Union{Nothing, Tuple{Pair{K, V}, State}} where {K, V}
280+ c:: CleverDict{K,V} ,
281+ ):: Union{Nothing,Tuple{Pair{K,V},State}} where {K,V}
274282 if _is_dense (c)
275283 itr = iterate (c. set)
276284 if itr === nothing
@@ -290,14 +298,15 @@ function Base.iterate(
290298 return nothing
291299 else
292300 el, i = itr
293- return convert (Pair{K, V}, el), State (i)
301+ return convert (Pair{K,V}, el), State (i)
294302 end
295303 end
296304end
297305
298306function Base. iterate (
299- c:: CleverDict{K, V} , s:: State
300- ):: Union{Nothing, Tuple{Pair{K, V}, State}} where {K, V}
307+ c:: CleverDict{K,V} ,
308+ s:: State ,
309+ ):: Union{Nothing,Tuple{Pair{K,V},State}} where {K,V}
301310 # Note that BitSet is defined by machine Int, so we need to cast any `.int`
302311 # fields to `Int` for 32-bit machines.
303312 if _is_dense (c)
@@ -323,15 +332,15 @@ function Base.iterate(
323332 return nothing
324333 else
325334 el, i = itr
326- return convert (Pair{K, V}, el), State (i)
335+ return convert (Pair{K,V}, el), State (i)
327336 end
328337 end
329338end
330339
331340# we do not have to implement values and keys functions because they can rely
332341# on `Base`s fallback that uses the iterator protocol
333342
334- function Base. sizehint! (c:: CleverDict{K, V} , n) where {K, V}
343+ function Base. sizehint! (c:: CleverDict{K,V} , n) where {K,V}
335344 if _is_dense (c)
336345 sizehint! (c. vector, n)
337346 sizehint! (c. set, n)
@@ -341,7 +350,7 @@ function Base.sizehint!(c::CleverDict{K, V}, n) where {K, V}
341350 return
342351end
343352
344- function Base. resize! (c:: CleverDict{K, V} , n) where {K, V}
353+ function Base. resize! (c:: CleverDict{K,V} , n) where {K,V}
345354 if _is_dense (c)
346355 if n < length (c. vector)
347356 error (" CleverDict cannot be resized to a size smaller than the current" )
0 commit comments