3 Part of CHR
(Constraint Handling Rules
)
6 E
-mail
: Tom
.Schrijvers
@cs.kuleuven
.be
7 WWW
: http
://www
.swi
-prolog
.org
8 Copyright
(C
): 2003-2004, K
.U
. Leuven
10 This program is free software
; you can redistribute it
and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation
; either version
2
13 of the License
, or (at your option
) any later version
.
15 This program is distributed
in the hope that it will be useful
,
16 but WITHOUT ANY WARRANTY
; without even the implied warranty of
17 MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE
. See the
18 GNU General Public License
for more details
.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library
; if not, write to the Free Software
22 Foundation
, Inc
., 59 Temple Place
, Suite
330, Boston
, MA
02111-1307 USA
24 As a special exception
, if you
link this library with other files
,
25 compiled with a Free Software compiler
, to produce an executable
, this
26 library does
not by itself cause the resulting executable to be covered
27 by the GNU General Public License
. This exception does
not however
28 invalidate any other reasons why the executable file might be covered by
29 the GNU General Public License
.
31 % author
: Tom Schrijvers
32 % email
: Tom
.Schrijvers
@cs.kuleuven
.be
33 % copyright
: K
.U
.Leuven
, 2004
35 :- module
(chr_hashtable_store
,
43 :- use_module
(pairlist
).
44 :- use_module
(hprolog
).
45 :- use_module
(library
(lists
)).
50 initial_capacity
(Capacity
),
53 new_ht
(Capacity
,HT
) :-
54 functor
(T1
,t
,Capacity
),
55 HT
= ht
(Capacity
,0,Table
),
58 lookup_ht
(HT
,Key
,Values
) :-
60 HT
= ht
(Capacity
,_
,Table
),
61 Index is
(Hash mod Capacity
) + 1,
62 arg
(Index
,Table
,Bucket
),
68 lookup_eq
(Bucket
,Key
,Values
)
71 lookup_pair_eq
([P
| KVs
],Key
,Pair
) :-
76 lookup_pair_eq
(KVs
,Key
,Pair
)
79 insert_ht
(HT
,Key
,Value
) :-
81 HT
= ht
(Capacity0
,Load
,Table0
),
82 LookupIndex is
(Hash mod Capacity0
) + 1,
83 arg
(LookupIndex
,Table0
,LookupBucket
),
84 ( var
(LookupBucket
) ->
86 LookupBucket
= Key
- [Value
]
87 ; LookupBucket
= K
-Values
->
89 ( hprolog
:memberchk_eq
(Value
,Values
) ->
93 setarg
(2,LookupBucket
,[Value
|Values
])
97 setarg
(LookupIndex
,Table0
,[Key
-[Value
],LookupBucket
])
100 ( lookup_pair_eq
(LookupBucket
,Key
,Pair
) ->
102 ( hprolog
:memberchk_eq
(Value
,Values
) ->
106 setarg
(2,Pair
,[Value
|Values
])
110 setarg
(LookupIndex
,Table0
,[Key
-[Value
]|LookupBucket
])
116 ( Load
== Capacity0
->
117 expand_ht
(HT
,_Capacity
)
125 delete_ht
(HT
,Key
,Value
) :-
126 HT
= ht
(Capacity
,Load
,Table
),
129 Index is
(Hash mod Capacity
) + 1,
130 arg
(Index
,Table
,Bucket
),
136 delete_first_fail
(Vs
,Value
,NVs
) ->
139 setarg
(Index
,Table
,_
)
147 ( lookup_pair_eq
(Bucket
,Key
,Pair
),
149 delete_first_fail
(Vs
,Value
,NVs
) ->
152 pairlist_delete_eq
(Bucket
,Key
,NBucket
),
153 setarg
(Index
,Table
,NBucket
)
163 delete_first_fail
([X
| Xs
], Y
, Zs
) :-
168 delete_first_fail
(Xs
, Y
, Zs1
)
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 value_ht
(HT
,Value
) :-
172 HT
= ht
(Capacity
,_
,Table
),
173 value_ht
(1,Capacity
,Table
,Value
).
175 value_ht
(I
,N
,Table
,Value
) :-
188 value_ht
(J
,N
,Table
,Value
)
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 expand_ht
(HT
,NewCapacity
) :-
194 HT
= ht
(Capacity
,_
,Table
),
195 NewCapacity is Capacity
* 2,
196 functor
(NewTable
,t
,NewCapacity
),
197 setarg
(1,HT
,NewCapacity
),
198 setarg
(3,HT
,NewTable
),
199 expand_copy
(Table
,1,Capacity
,NewTable
,NewCapacity
).
201 expand_copy
(Table
,I
,N
,NewTable
,NewCapacity
) :-
208 ; Bucket
= Key
- Value
->
209 expand_insert
(NewTable
,NewCapacity
,Key
,Value
)
211 expand_inserts
(Bucket
,NewTable
,NewCapacity
)
214 expand_copy
(Table
,J
,N
,NewTable
,NewCapacity
)
217 expand_inserts
([],_
,_
).
218 expand_inserts
([K
-V
|R
],Table
,Capacity
) :-
219 expand_insert
(Table
,Capacity
,K
,V
),
220 expand_inserts
(R
,Table
,Capacity
).
222 expand_insert
(Table
,Capacity
,K
,V
) :-
224 Index is
(Hash mod Capacity
) + 1,
225 arg
(Index
,Table
,Bucket
),
229 setarg
(Index
,Table
,[K
-V
,Bucket
])
231 setarg
(Index
,Table
,[K
-V
|Bucket
])
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 term_hash
(Term
,Hash
) :-
235 hash_term
(Term
,Hash
).