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
,
45 :- use_module
(pairlist
).
46 :- use_module
(hprolog
).
47 :- use_module
(library
(lists
)).
49 :- multifile user
:goal_expansion
/2.
50 :- dynamic user
:goal_expansion
/2.
52 user
:goal_expansion
(term_hash
(Term
,Hash
),hash_term
(Term
,Hash
)).
54 % term_hash
(Term
,Hash
) :-
55 % hash_term
(Term
,Hash
).
59 initial_capacity
(Capacity
),
62 new_ht
(Capacity
,HT
) :-
63 functor
(T1
,t
,Capacity
),
64 HT
= ht
(Capacity
,0,Table
),
67 lookup_ht
(HT
,Key
,Values
) :-
69 HT
= ht
(Capacity
,_
,Table
),
70 Index is
(Hash mod Capacity
) + 1,
71 arg
(Index
,Table
,Bucket
),
77 lookup
(Bucket
,Key
,Values
)
80 lookup_pair_eq
([P
| KVs
],Key
,Pair
) :-
85 lookup_pair_eq
(KVs
,Key
,Pair
)
88 insert_ht
(HT
,Key
,Value
) :-
90 HT
= ht
(Capacity0
,Load
,Table0
),
91 LookupIndex is
(Hash mod Capacity0
) + 1,
92 arg
(LookupIndex
,Table0
,LookupBucket
),
93 ( var
(LookupBucket
) ->
94 LookupBucket
= Key
- [Value
]
95 ; LookupBucket
= K
-Values
->
97 setarg
(2,LookupBucket
,[Value
|Values
])
99 setarg
(LookupIndex
,Table0
,[Key
-[Value
],LookupBucket
])
102 ( lookup_pair_eq
(LookupBucket
,Key
,Pair
) ->
104 setarg
(2,Pair
,[Value
|Values
])
106 setarg
(LookupIndex
,Table0
,[Key
-[Value
]|LookupBucket
])
111 ( Load
== Capacity0
->
112 expand_ht
(HT
,_Capacity
)
117 % LDK
: insert version with extra argument denoting result
119 insert_ht
(HT
,Key
,Value
,Result
) :-
120 HT
= ht
(Capacity
,Load
,Table
),
122 LookupIndex is
(Hash mod Capacity
) + 1,
123 arg
(LookupIndex
,Table
,LookupBucket
),
126 LookupBucket
= Key
- Result
,
128 ; LookupBucket
= K
- V
130 -> Result
= [Value
|V
],
131 setarg
(2,LookupBucket
,Result
),
134 setarg
(LookupIndex
,Table
,[Key
- Result
,LookupBucket
]),
137 ; ( lookup_pair_eq
(LookupBucket
,Key
,Pair
)
139 Result
= [Value
|Values
],
140 setarg
(2,Pair
,Result
),
143 setarg
(LookupIndex
,Table
,[Key
- Result
|LookupBucket
]),
147 setarg
(2,HT
,NewLoad
),
153 % LDK
: deletion of the first element of a bucket
154 delete_first_ht
(HT
,Key
,Values
) :-
155 HT
= ht
(Capacity
,Load
,Table
),
157 Index is
(Hash mod Capacity
) + 1,
158 arg
(Index
,Table
,Bucket
),
159 ( Bucket
= _
-[_
|Values
]
161 -> setarg
(Index
,Table
,_
),
163 ; setarg
(2,Bucket
,Values
),
166 ; lookup_pair_eq
(Bucket
,Key
,Pair
)
167 -> Pair
= _
-[_
|Values
],
169 -> pairlist_delete_eq
(Bucket
,Key
,NewBucket
),
171 -> setarg
(Index
,Table
,_
)
172 ; NewBucket
= [OtherPair
]
173 -> setarg
(Index
,Table
,OtherPair
)
174 ; setarg
(Index
,Table
,NewBucket
)
177 ; setarg
(2,Pair
,Values
),
182 delete_ht
(HT
,Key
,Value
) :-
183 HT
= ht
(Capacity
,Load
,Table
),
186 Index is
(Hash mod Capacity
) + 1,
187 arg
(Index
,Table
,Bucket
),
193 delete_first_fail
(Vs
,Value
,NVs
) ->
196 setarg
(Index
,Table
,_
)
204 ( lookup_pair_eq
(Bucket
,Key
,Pair
),
206 delete_first_fail
(Vs
,Value
,NVs
) ->
209 pairlist_delete_eq
(Bucket
,Key
,NBucket
),
210 ( NBucket
= [Singleton
] ->
211 setarg
(Index
,Table
,Singleton
)
213 setarg
(Index
,Table
,NBucket
)
224 delete_first_fail
([X
| Xs
], Y
, Zs
) :-
229 delete_first_fail
(Xs
, Y
, Zs1
)
231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 value_ht
(HT
,Value
) :-
233 HT
= ht
(Capacity
,_
,Table
),
234 value_ht
(1,Capacity
,Table
,Value
).
236 value_ht
(I
,N
,Table
,Value
) :-
249 value_ht
(J
,N
,Table
,Value
)
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 expand_ht
(HT
,NewCapacity
) :-
255 HT
= ht
(Capacity
,_
,Table
),
256 NewCapacity is Capacity
* 2,
257 functor
(NewTable
,t
,NewCapacity
),
258 setarg
(1,HT
,NewCapacity
),
259 setarg
(3,HT
,NewTable
),
260 expand_copy
(Table
,1,Capacity
,NewTable
,NewCapacity
).
262 expand_copy
(Table
,I
,N
,NewTable
,NewCapacity
) :-
269 ; Bucket
= Key
- Value
->
270 expand_insert
(NewTable
,NewCapacity
,Key
,Value
)
272 expand_inserts
(Bucket
,NewTable
,NewCapacity
)
275 expand_copy
(Table
,J
,N
,NewTable
,NewCapacity
)
278 expand_inserts
([],_
,_
).
279 expand_inserts
([K
-V
|R
],Table
,Capacity
) :-
280 expand_insert
(Table
,Capacity
,K
,V
),
281 expand_inserts
(R
,Table
,Capacity
).
283 expand_insert
(Table
,Capacity
,K
,V
) :-
285 Index is
(Hash mod Capacity
) + 1,
286 arg
(Index
,Table
,Bucket
),
290 setarg
(Index
,Table
,[K
-V
,Bucket
])
292 setarg
(Index
,Table
,[K
-V
|Bucket
])
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%