1 % author
: Tom Schrijvers
2 % email
: Tom
.Schrijvers
@cs.kuleuven
.ac
.be
3 % copyright
: K
.U
.Leuven
, 2004
5 :- module
(chr_hashtable_store
,
13 :- use_module
(pairlist
).
14 :- use_module
(hprolog
).
15 :- use_module
(library
(lists
)).
20 initial_capacity
(Capacity
),
23 new_ht
(Capacity
,HT
) :-
24 functor
(T1
,t
,Capacity
),
25 HT
= ht
(Capacity
,0,Table
),
28 lookup_ht
(HT
,Key
,Values
) :-
30 HT
= ht
(Capacity
,_
,Table
),
31 Index is
(Hash mod Capacity
) + 1,
32 arg
(Index
,Table
,Bucket
),
38 lookup_eq
(Bucket
,Key
,Values
)
41 lookup_pair_eq
([P
| KVs
],Key
,Pair
) :-
46 lookup_pair_eq
(KVs
,Key
,Pair
)
49 insert_ht
(HT
,Key
,Value
) :-
51 HT
= ht
(Capacity0
,Load
,Table0
),
52 LookupIndex is
(Hash mod Capacity0
) + 1,
53 arg
(LookupIndex
,Table0
,LookupBucket
),
54 ( var
(LookupBucket
) ->
56 LookupBucket
= Key
- [Value
]
57 ; LookupBucket
= K
-Values
->
59 ( hprolog
:memberchk_eq
(Value
,Values
) ->
63 setarg
(2,LookupBucket
,[Value
|Values
])
67 setarg
(LookupIndex
,Table0
,[Key
-[Value
],LookupBucket
])
70 ( lookup_pair_eq
(LookupBucket
,Key
,Pair
) ->
72 ( hprolog
:memberchk_eq
(Value
,Values
) ->
76 setarg
(2,Pair
,[Value
|Values
])
80 setarg
(LookupIndex
,Table0
,[Key
-[Value
]|LookupBucket
])
86 ( Load
== Capacity0
->
87 expand_ht
(HT
,_Capacity
)
95 delete_ht
(HT
,Key
,Value
) :-
96 HT
= ht
(Capacity
,Load
,Table
),
99 Index is
(Hash mod Capacity
) + 1,
100 arg
(Index
,Table
,Bucket
),
106 delete_first_fail
(Vs
,Value
,NVs
) ->
109 setarg
(Index
,Table
,_
)
117 ( lookup_pair_eq
(Bucket
,Key
,Pair
),
119 delete_first_fail
(Vs
,Value
,NVs
) ->
122 pairlist
:delete_eq
(Bucket
,Key
,NBucket
),
123 setarg
(Index
,Table
,NBucket
)
133 delete_first_fail
([X
| Xs
], Y
, Zs
) :-
138 delete_first_fail
(Xs
, Y
, Zs1
)
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 value_ht
(HT
,Value
) :-
142 HT
= ht
(Capacity
,_
,Table
),
143 value_ht
(1,Capacity
,Table
,Value
).
145 value_ht
(I
,N
,Table
,Value
) :-
158 value_ht
(J
,N
,Table
,Value
)
161 values_ht
(HT
,Values
) :-
162 HT
= ht
(Capacity
,_
,Table
),
163 values_ht
(1,Capacity
,Table
,Values
).
164 values_ht
(I
,N
,Table
,Values
) :-
169 append
(Vs
,Tail
,Values
)
171 append_snd
(Bucket
,Tail
,Values
)
177 values_ht
(J
,N
,Table
,Tail
)
183 append_snd
([_
-H
|Ps
],L
,NL
) :-
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 expand_ht
(HT
,NewCapacity
) :-
189 HT
= ht
(Capacity
,_
,Table
),
190 NewCapacity is Capacity
* 2,
191 functor
(NewTable
,t
,NewCapacity
),
192 setarg
(1,HT
,NewCapacity
),
193 setarg
(3,HT
,NewTable
),
194 expand_copy
(Table
,1,Capacity
,NewTable
,NewCapacity
).
196 expand_copy
(Table
,I
,N
,NewTable
,NewCapacity
) :-
203 ; Bucket
= Key
- Value
->
204 expand_insert
(NewTable
,NewCapacity
,Key
,Value
)
206 expand_inserts
(Bucket
,NewTable
,NewCapacity
)
209 expand_copy
(Table
,J
,N
,NewTable
,NewCapacity
)
212 expand_inserts
([],_
,_
).
213 expand_inserts
([K
-V
|R
],Table
,Capacity
) :-
214 expand_insert
(Table
,Capacity
,K
,V
),
215 expand_inserts
(R
,Table
,Capacity
).
217 expand_insert
(Table
,Capacity
,K
,V
) :-
219 Index is
(Hash mod Capacity
) + 1,
220 arg
(Index
,Table
,Bucket
),
224 setarg
(Index
,Table
,[K
-V
,Bucket
])
226 setarg
(Index
,Table
,[K
-V
|Bucket
])
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 term_hash
(Term
,Hash
) :-
230 hash_term
(Term
,Hash
).