11# Hash table acceleration index
2- struct HashIndex{D <: AbstractDict } <: AbstractIndex
2+ struct HashIndex{D <: HashDictionary } <: AbstractIndex
33 dict:: D
44end
55
66function HashIndex (a:: AbstractArray )
7- dict = Dict {eltype(a), Vector{eltype(keys(a))}} ()
7+ dict = HashDictionary {eltype(a), Vector{eltype(keys(a))}} ()
88
99 @inbounds for i in keys (a)
1010 value = a[i]
@@ -24,85 +24,85 @@ Base.summary(i::HashIndex) = "HashIndex ($(length(i.dict)) unique element$(lengt
2424Base. in (x, a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} ) = haskey (a. index. dict, x)
2525
2626function Base. count (f:: Fix2{typeof(isequal)} , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
27- index = Base . ht_keyindex (a. index. dict, f. x)
28- if index < 0
29- return 0
27+ (hasindex, token) = gettoken (a. index. dict, f. x)
28+ if hasindex
29+ return length ( @inbounds gettokenvalue (a . index . dict, token))
3030 else
31- return length (a . index . dict . vals[index])
31+ return 0
3232 end
3333end
3434
3535function Base. findall (f:: Fix2{typeof(isequal)} , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
36- index = Base . ht_keyindex (a. index. dict, f. x)
37- if index < 0
38- return Vector {eltype(keys(a))} ( )
39- else
40- return @inbounds a . index . dict . vals[index]
41- end
36+ (hasindex, token) = gettoken (a. index. dict, f. x)
37+ if hasindex
38+ return @inbounds gettokenvalue (a . index . dict, token )
39+ else
40+ return Vector {eltype(keys(a))} ()
41+ end
4242end
4343
4444# TODO : findall for arbitrary predicates by just checking each unique key? (Sometimes faster, sometimes slower?)
4545
4646function Base. findfirst (f:: Fix2{typeof(isequal)} , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
47- index = Base . ht_keyindex (a. index. dict, f. x)
48- if index < 0
49- return nothing
50- else
51- return @inbounds first (a . index . dict . vals[index])
52- end
47+ (hasindex, token) = gettoken (a. index. dict, f. x)
48+ if hasindex
49+ return @inbounds first ( gettokenvalue (a . index . dict, token))
50+ else
51+ return nothing
52+ end
5353end
5454
5555function Base. findlast (f:: Fix2{typeof(isequal)} , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
56- index = Base . ht_keyindex (a. index. dict, f. x)
57- if index < 0
58- return nothing
59- else
60- return @inbounds last (a . index . dict . vals[index])
61- end
56+ (hasindex, token) = gettoken (a. index. dict, f. x)
57+ if hasindex
58+ return @inbounds last ( gettokenvalue (a . index . dict, token))
59+ else
60+ return nothing
61+ end
6262end
6363
6464function Base. filter (f:: Fix2{typeof(isequal)} , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
65- index = Base . ht_keyindex (a. index. dict, f. x)
66- if index < 0
67- return empty (a)
68- else
69- return @inbounds parent (a)[a . index . dict . vals[index]]
70- end
65+ (hasindex, token) = gettoken (a. index. dict, f. x)
66+ if hasindex
67+ return @inbounds parent (a)[( gettokenvalue (a . index . dict, token))]
68+ else
69+ return empty (a)
70+ end
7171end
7272
7373# TODO : filter for arbitrary predicates by just checking each unique key? (Sometimes faster, sometimes slower?)
7474
7575function Base. unique (a:: AcceleratedArray{T, <:Any, <:Any, <:HashIndex} ) where {T}
76- out = Vector {T} ()
77- @inbounds for value in keys (a. index. dict)
78- push! (out, value)
79- end
80- return out
76+ out = Vector {T} ()
77+ @inbounds for value in keys (a. index. dict)
78+ push! (out, value)
79+ end
80+ return out
8181end
8282
83- function SplitApplyCombine. group2 (a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} , b:: AbstractArray )
84- return Dict ((key, @inbounds b[inds]) for (key, inds) in a. index. dict)
83+ function SplitApplyCombine. group (a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} , b:: AbstractArray )
84+ return map (inds -> @inbounds ( b[inds]), a. index. dict)
8585end
8686
87- function SplitApplyCombine. groupreduce (:: typeof (identity), f, op , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} ; kw... )
88- return Dict ((k, mapreduce (i -> f ( @inbounds a[i]), op, v ; kw... )) for (k,v) in a. index. dict)
87+ function SplitApplyCombine. groupreduce (op :: Callable , a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} ; kw... )
88+ return map (inds -> @inbounds ( reduce (op, view (a, inds) ; kw... )), a. index. dict)
8989end
9090
91- function SplitApplyCombine. _groupinds (a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
92- return a. index. dict
91+ function SplitApplyCombine. groupfind (a:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} )
92+ return a. index. dict
9393end
9494
9595function SplitApplyCombine. _innerjoin! (out, left:: AbstractArray , right:: AcceleratedArray{<:Any, <:Any, <:Any, <:HashIndex} , v:: AbstractArray , :: typeof (isequal))
96- @boundscheck if (axes (l )... , axes (r )... ) != axes (v)
96+ @boundscheck if (axes (left )... , axes (right )... ) != axes (v)
9797 throw (DimensionMismatch (" innerjoin arrays do not have matching dimensions" ))
9898 end
9999
100100 dict = right. index. dict
101101
102- @inbounds for i ∈ keys (left)
103- dict_index = Base . ht_keyindex ( dict, left ( i_l) )
104- if dict_index > 0 # -1 if key not found
105- for i_r ∈ dict. vals[dict_index]
102+ @inbounds for i_l ∈ keys (left)
103+ (hasindex, token) = gettoken (right . index . dict, @inbounds left[ i_l] )
104+ if hasindex
105+ for i_r ∈ gettokenvalue ( dict, token)
106106 push! (out, v[Tuple (i_l)... , Tuple (i_r)... ])
107107 end
108108 end
@@ -116,13 +116,13 @@ function SplitApplyCombine.leftgroupjoin(lkey, ::typeof(identity), f, ::typeof(i
116116 K = promote_op (lkey, eltype (left))
117117
118118 dict = right. index. dict
119- out = Dict {K, Vector{T}} ()
119+ out = HashDictionary {K, Vector{T}} ()
120120 for a ∈ left
121121 key = lkey (a)
122122 group = get! (() -> T[], out, key)
123- dict_index = Base . ht_keyindex (dict, key)
124- if dict_index > 0 # -1 if key not found
125- for b ∈ dict. vals[dict_index]
123+ (hasindex, token) = gettoken (dict, key)
124+ if hasindex
125+ for b ∈ @inbounds gettokenvalue ( dict, token)
126126 push! (group, f (a, b))
127127 end
128128 end
0 commit comments