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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 HT
= ht
(Capacity
,_
,Table
),
150 NewCapacity is Capacity
* 2,
151 functor
(NewTable
,t
,NewCapacity
),
152 setarg
(1,HT
,NewCapacity
),
153 setarg
(3,HT
,NewTable
),
154 expand_copy
(Table
,1,Capacity
,NewTable
,NewCapacity
).
156 expand_copy
(Table
,I
,N
,NewTable
,NewCapacity
) :-
163 ; Bucket
= Key
- Value
->
164 expand_insert
(NewTable
,NewCapacity
,Key
,Value
)
166 expand_inserts
(Bucket
,NewTable
,NewCapacity
)
169 expand_copy
(Table
,J
,N
,NewTable
,NewCapacity
)
172 expand_inserts
([],_
,_
).
173 expand_inserts
([K
-V
|R
],Table
,Capacity
) :-
174 expand_insert
(Table
,Capacity
,K
,V
),
175 expand_inserts
(R
,Table
,Capacity
).
177 expand_insert
(Table
,Capacity
,K
,V
) :-
179 Index is
(Hash mod Capacity
) + 1,
180 arg
(Index
,Table
,Bucket
),
184 setarg
(Index
,Table
,[K
-V
,Bucket
])
186 setarg
(Index
,Table
,[K
-V
|Bucket
])
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 term_hash
(Term
,Hash
) :-
190 hash_term
(Term
,Hash
).
193 chr_delete
([], _
, []).
194 chr_delete
([H
|T
], X
, L
) :-