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
,Term
,Values
) :-
30 int_lookup_ht
(HT
,Hash
,Term
,Values
).
32 int_lookup_ht
(HT
,Hash
,Key
,Values
) :-
33 HT
= ht
(Capacity
,_
,Table
),
34 Index is
(Hash mod Capacity
) + 1,
35 arg
(Index
,Table
,Bucket
),
41 lookup_eq
(Bucket
,Key
,Values
)
44 insert_ht
(HT
,Term
,Value
) :-
46 ( int_lookup_ht
(HT
,Hash
,Term
,Values
),
47 hprolog
:memberchk_eq
(Value
,Values
) ->
50 int_insert_ht
(HT
,Hash
,Term
,Value
)
53 int_insert_ht
(HT
,Hash
,Key
,Value
) :-
54 HT
= ht
(Capacity0
,Load
,Table0
),
55 ( Load
== Capacity0
->
58 Capacity is Capacity0
* 2
65 Index is
(Hash mod Capacity
) + 1,
66 arg
(Index
,Table
,Bucket
),
71 setarg
(2,Bucket
,[Value
|Vs
])
73 setarg
(Index
,Table
,[Key
-[Value
],Bucket
])
75 ; lookup_pair_eq
(Bucket
,Key
,Pair
) ->
77 setarg
(2,Pair
,[Value
|Vs
])
79 setarg
(Index
,Table
,[Key
-[Value
]|Bucket
])
83 lookup_pair_eq
([P
| KVs
],Key
,Pair
) :-
88 lookup_pair_eq
(KVs
,Key
,Pair
)
91 delete_ht
(HT
,Term
,Value
) :-
93 ( int_lookup_ht
(HT
,Hash
,Term
,Values
),
94 hprolog
:memberchk_eq
(Value
,Values
) ->
95 int_delete_ht
(HT
,Hash
,Term
,Value
)
100 int_delete_ht
(HT
,Hash
,Key
,Value
) :-
101 HT
= ht
(Capacity
,Load
,Table
),
104 Index is
(Hash mod Capacity
) + 1,
105 arg
(Index
,Table
,Bucket
),
107 delete(Vs
,Value
,NVs
),
109 setarg
(Index
,Table
,_
)
114 lookup_pair_eq
(Bucket
,Key
,Pair
),
116 delete(Vs
,Value
,NVs
),
118 pairlist
:delete_eq
(Bucket
,Key
,NBucket
),
119 setarg
(Index
,Table
,NBucket
)
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 value_ht
(HT
,Value
) :-
127 HT
= ht
(Capacity
,_
,Table
),
128 value_ht
(1,Capacity
,Table
,Value
).
130 value_ht
(I
,N
,Table
,Value
) :-
143 value_ht
(J
,N
,Table
,Value
)
146 values_ht
(HT
,Values
) :-
147 HT
= ht
(Capacity
,_
,Table
),
148 values_ht
(1,Capacity
,Table
,Values
).
149 values_ht
(I
,N
,Table
,Values
) :-
154 append
(Vs
,Tail
,Values
)
156 append_snd
(Bucket
,Tail
,Values
)
162 values_ht
(J
,N
,Table
,Tail
)
168 append_snd
([_
-H
|Ps
],L
,NL
) :-
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 HT
= ht
(Capacity
,_
,Table
),
175 NewCapacity is Capacity
* 2,
176 functor
(NewTable
,t
,NewCapacity
),
177 setarg
(1,HT
,NewCapacity
),
178 setarg
(3,HT
,NewTable
),
179 expand_copy
(Table
,1,Capacity
,NewTable
,NewCapacity
).
181 expand_copy
(Table
,I
,N
,NewTable
,NewCapacity
) :-
188 ; Bucket
= Key
- Value
->
189 expand_insert
(NewTable
,NewCapacity
,Key
,Value
)
191 expand_inserts
(Bucket
,NewTable
,NewCapacity
)
194 expand_copy
(Table
,J
,N
,NewTable
,NewCapacity
)
197 expand_inserts
([],_
,_
).
198 expand_inserts
([K
-V
|R
],Table
,Capacity
) :-
199 expand_insert
(Table
,Capacity
,K
,V
),
200 expand_inserts
(R
,Table
,Capacity
).
202 expand_insert
(Table
,Capacity
,K
,V
) :-
204 Index is
(Hash mod Capacity
) + 1,
205 arg
(Index
,Table
,Bucket
),
209 setarg
(Index
,Table
,[K
-V
,Bucket
])
211 setarg
(Index
,Table
,[K
-V
|Bucket
])
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 term_hash
(Term
,Hash
) :-
215 hash_term
(Term
,Hash
).