summaryrefslogtreecommitdiff
path: root/test/spass/hasharray.h
blob: de6e118af2e8411ce08024ffc43262429beef3ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**************************************************************/
/* ********************************************************** */
/* *                                                        * */
/* *                       HASHING                          * */
/* *                                                        * */
/* *  $Module:   HASHARRAY                                  * */ 
/* *                                                        * */
/* *  Copyright (C) 1997, 1998, 1999, 2000, 2001            * */
/* *  MPI fuer Informatik                                   * */
/* *                                                        * */
/* *  This program is free software; you can redistribute   * */
/* *  it and/or modify it under the terms of the GNU        * */
/* *  General Public License as published by the Free       * */
/* *  Software Foundation; either version 2 of the License, * */
/* *  or (at your option) any later version.                * */
/* *                                                        * */
/* *  This program is distributed in the hope that it will  * */
/* *  be useful, but WITHOUT ANY WARRANTY; without even     * */
/* *  the implied warranty of MERCHANTABILITY or FITNESS    * */
/* *  FOR A PARTICULAR PURPOSE.  See the GNU General Public * */
/* *  License for more details.                             * */
/* *                                                        * */
/* *  You should have received a copy of the GNU General    * */
/* *  Public License along with this program; if not, write * */
/* *  to the Free Software Foundation, Inc., 59 Temple      * */
/* *  Place, Suite 330, Boston, MA  02111-1307  USA         * */
/* *                                                        * */
/* *                                                        * */
/* $Revision: 21527 $                                        * */
/* $State$                                            * */
/* $Date: 2005-04-24 21:10:28 -0700 (Sun, 24 Apr 2005) $                             * */
/* $Author: duraid $                                       * */
/* *                                                        * */
/* *             Contact:                                   * */
/* *             Christoph Weidenbach                       * */
/* *             MPI fuer Informatik                        * */
/* *             Stuhlsatzenhausweg 85                      * */
/* *             66123 Saarbruecken                         * */
/* *             Email: weidenb@mpi-sb.mpg.de               * */
/* *             Germany                                    * */
/* *                                                        * */
/* ********************************************************** */
/**************************************************************/


/* $RCSfile$ */

#ifndef _HASHARRAY_
#define _HASHARRAY_

/**************************************************************/
/* Includes                                                   */
/**************************************************************/

#include "list.h"

/**************************************************************/
/* Structures                                                 */
/**************************************************************/

#define hsh__SIZE 29            /* a prime */

/* Each Entry is a list of pairs <key,list of values> */

typedef LIST* HASH;

void hsh_Check(HASH H);

/**************************************************************/
/* Inline Functions                                           */
/**************************************************************/

static __inline__ unsigned long hsh_Index(POINTER Key)
/**************************************************************
  INPUT:   A pointer
  RETURNS: A key for the hasharray
***************************************************************/
{
  return (unsigned long)Key % hsh__SIZE;
}

static __inline__ void hsh_Put(HASH H, POINTER Key, POINTER Value)
/**************************************************************
  INPUT:   A hasharray, a pointer used as key and a pointer to a data item
  EFFECT:  Add Value to the list of data items associated with the key,
           if it isn't a member already
***************************************************************/
{
  LIST Scan, Pair;
  unsigned long HashKey;
  HashKey = hsh_Index(Key);
  for (Scan = H[HashKey]; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
    Pair = list_Car(Scan);
    if (list_PairFirst(Pair) == Key) {
      if (!list_PointerMember(list_PairSecond(Pair), Value))
	list_Rplacd(Pair, list_Cons(Value, list_PairSecond(Pair)));
#ifdef CHECK
      hsh_Check(H);
#endif
      return;
    }
  }
  H[HashKey] = list_Cons(list_PairCreate(Key, list_List(Value)), H[HashKey]);
#ifdef CHECK
  hsh_Check(H);
#endif
}

static __inline__ void hsh_PutList(HASH H, POINTER Key, LIST List)
/**************************************************************
  INPUT:   A hasharray, a pointer used as key and a list of data items
  EFFECT:  Add the list to the list of data items associated with the key,
           and delete all duplicates.
***************************************************************/
{
  LIST Scan, Pair;
  unsigned long HashKey;

  HashKey = hsh_Index(Key);
  for (Scan = H[HashKey]; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
    Pair = list_Car(Scan);
    if (list_PairFirst(Pair) == Key) {
      list_Rplacd(Pair, list_Nconc(list_PairSecond(Pair), List));
#ifdef CHECK
      hsh_Check(H);
#endif
      return;
    }
  }
  H[HashKey] = list_Cons(list_PairCreate(Key, List), H[HashKey]);
#ifdef CHECK
  hsh_Check(H);
#endif
}

static __inline__ void hsh_PutListWithCompareFunc(HASH H, POINTER Key, 
						  LIST List,
						  BOOL (*Test)(POINTER, POINTER), 
						  unsigned long (*HashFunc)(POINTER))
/**************************************************************
  INPUT:   A hasharray, a pointer used as key, a list of data
           items, a test function for key equality and a
	   hashing function.
  EFFECT:  Add the list to the list of data items associated
           with the key, and delete all duplicates.
***************************************************************/
{
  LIST Scan, Pair;
  unsigned long HashKey;

  HashKey = (unsigned long) HashFunc(Key);
  for (Scan = H[HashKey]; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
    Pair = (LIST) list_Car(Scan);
    if (Test(list_PairFirst(Pair), Key)) {
      list_Rplacd(Pair, list_Nconc(list_PairSecond(Pair), List));
#ifdef CHECK
      hsh_Check(H);
#endif
      return;
    }
  }
  H[HashKey] = list_Cons(list_PairCreate(Key, List), H[HashKey]);
#ifdef CHECK
  hsh_Check(H);
#endif
}

static __inline__ LIST hsh_Get(HASH H, POINTER Key)
/**************************************************************
  INPUT:   A hasharray and a pointer used as key
  RETURNS: The list of data items associated with the key
***************************************************************/
{
  LIST Scan, Pair;

  for (Scan = H[hsh_Index(Key)]; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
    Pair = list_Car(Scan);
    if (list_PairFirst(Pair) == Key)
      return list_PairSecond(Pair);
  }
  return NULL;
}

static __inline__ void hsh_DelItem(HASH H, POINTER Key)
/**************************************************************
  INPUT:   A hasharray and a pointer used as key
  RETURNS: The information associated with the key is deleted
***************************************************************/
{
  LIST Scan, Pair;
  unsigned long k;

  k = hsh_Index(Key);
  for (Scan = H[k]; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
    Pair = list_Car(Scan);
    if (list_PairFirst(Pair) == Key) {
      list_Delete(list_PairSecond(Pair));
      list_PairFree(Pair);
      H[k] = list_PointerDeleteElement(H[k], Pair);
      return;
    }
  }
}

static __inline__ LIST hsh_GetWithCompareFunc(HASH H, POINTER Key,
					      BOOL (*Test)(POINTER, POINTER),
					      unsigned long (*HashFunc)(POINTER))
/**************************************************************
  INPUT:   A hasharray, a pointer used as key, a compare function
           for keys and a hash function for keys.
  RETURNS: The list of data items associated with the key
***************************************************************/
{
  LIST Scan, Pair;

  for (Scan = H[HashFunc(Key)]; !list_Empty(Scan); Scan = list_Cdr(Scan)) {
    Pair = list_Car(Scan);
    if (Test(list_PairFirst(Pair), Key))
      return list_PairSecond(Pair);
  }
  return NULL;
}


static __inline__ unsigned long hsh_StringHashKey(const char* Label)
{
  unsigned long i, s;
  s = 0;
  for (i = 0; i <= strlen(Label); i++)
    s += Label[i];
  s = s % hsh__SIZE;
  return s;
}

/**************************************************************/
/* Functions                                                  */
/**************************************************************/

HASH hsh_Create(void);
void hsh_Reset(HASH H);
void hsh_Delete(HASH H);
LIST hsh_GetAllEntries(HASH H);

#endif