1/* Part of CHR (Constraint Handling Rules) 2 3 Author: Jon Sneyers 4 E-mail: Jon.Sneyers@cs.kuleuven.be 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2006-2011, K.U. Leuven 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35% based on chr_hashtable_store (by Tom Schrijvers) 36% is it safe to use nb_setarg here? 37 38:- module(chr_integertable_store, 39 [ new_iht/1, 40 lookup_iht/3, 41 insert_iht/3, 42 delete_iht/3, 43 value_iht/2 44 ]). 45:- use_module(library(lists)). 46:- use_module(library(dialect/hprolog)). 47 48%initial_capacity(65536). 49%initial_capacity(1024). 50initial_capacity(8). 51%initial_capacity(2). 52%initial_capacity(1). 53 54 55new_iht(HT) :- 56 initial_capacity(Capacity), 57 new_iht(Capacity,HT). 58 59new_iht(Capacity,HT) :- 60 functor(T1,t,Capacity), 61 HT = ht(Capacity,Table), 62 Table = T1. 63 64lookup_iht(ht(_,Table),Int,Values) :- 65 Index is Int + 1, 66 arg(Index,Table,Values), 67 Values \= []. 68% nonvar(Values). 69 70insert_iht(HT,Int,Value) :- 71 Index is Int + 1, 72 arg(2,HT,Table), 73 (arg(Index,Table,Bucket) -> 74 ( var(Bucket) -> 75 Bucket = [Value] 76 ; 77 setarg(Index,Table,[Value|Bucket]) 78 ) 79 ; % index > capacity 80 Capacity is 1<<ceil(log(Index)/log(2)), 81 expand_iht(HT,Capacity), 82 insert_iht(HT,Int,Value) 83 ). 84 85delete_iht(ht(_,Table),Int,Value) :- 86% arg(2,HT,Table), 87 Index is Int + 1, 88 arg(Index,Table,Bucket), 89 ( Bucket = [_Value] -> 90 setarg(Index,Table,[]) 91 ; 92 delete_first_fail(Bucket,Value,NBucket), 93 setarg(Index,Table,NBucket) 94 ). 95%delete_first_fail([], Y, []). 96%delete_first_fail([_], _, []) :- !. 97delete_first_fail([X | Xs], Y, Xs) :- 98 X == Y, !. 99delete_first_fail([X | Xs], Y, [X | Zs]) :- 100 delete_first_fail(Xs, Y, Zs). 101 102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103value_iht(HT,Value) :- 104 HT = ht(Capacity,Table), 105 value_iht(1,Capacity,Table,Value). 106 107value_iht(I,N,Table,Value) :- 108 I =< N, 109 arg(I,Table,Bucket), 110 ( 111 nonvar(Bucket), 112 member(Value,Bucket) 113 ; 114 J is I + 1, 115 value_iht(J,N,Table,Value) 116 ). 117 118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 119 120expand_iht(HT,NewCapacity) :- 121 HT = ht(Capacity,Table), 122 functor(NewTable,t,NewCapacity), 123 setarg(1,HT,NewCapacity), 124 setarg(2,HT,NewTable), 125 expand_copy(Table,1,Capacity,NewTable,NewCapacity). 126 127expand_copy(Table,I,N,NewTable,NewCapacity) :- 128 ( I > N -> 129 true 130 ; 131 arg(I,Table,Bucket), 132 ( var(Bucket) -> 133 true 134 ; 135 arg(I,NewTable,Bucket) 136 ), 137 J is I + 1, 138 expand_copy(Table,J,N,NewTable,NewCapacity) 139 ). 140