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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 expand_ht
(HT
,NewCapacity
) :-
164 HT
= ht
(Capacity
,_
,Table
),
165 NewCapacity is Capacity
* 2,
166 functor
(NewTable
,t
,NewCapacity
),
167 setarg
(1,HT
,NewCapacity
),
168 setarg
(3,HT
,NewTable
),
169 expand_copy
(Table
,1,Capacity
,NewTable
,NewCapacity
).
171 expand_copy
(Table
,I
,N
,NewTable
,NewCapacity
) :-
178 ; Bucket
= Key
- Value
->
179 expand_insert
(NewTable
,NewCapacity
,Key
,Value
)
181 expand_inserts
(Bucket
,NewTable
,NewCapacity
)
184 expand_copy
(Table
,J
,N
,NewTable
,NewCapacity
)
187 expand_inserts
([],_
,_
).
188 expand_inserts
([K
-V
|R
],Table
,Capacity
) :-
189 expand_insert
(Table
,Capacity
,K
,V
),
190 expand_inserts
(R
,Table
,Capacity
).
192 expand_insert
(Table
,Capacity
,K
,V
) :-
194 Index is
(Hash mod Capacity
) + 1,
195 arg
(Index
,Table
,Bucket
),
199 setarg
(Index
,Table
,[K
-V
,Bucket
])
201 setarg
(Index
,Table
,[K
-V
|Bucket
])
203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 term_hash
(Term
,Hash
) :-
205 hash_term
(Term
,Hash
).