aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.4.3/gcc/pointer-set.c
blob: d5585c68b7929a2f18c17e1e4ee591b2d09e8bcf (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* Set operations on pointers
   Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.

This file is part of GCC.

GCC 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 3, or (at your option)
any later version.

GCC 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 GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "pointer-set.h"

/* A pointer set is represented as a simple open-addressing hash
   table.  Simplifications: The hash code is based on the value of the
   pointer, not what it points to.  The number of buckets is always a
   power of 2.  Null pointers are a reserved value.  Deletion is not
   supported (yet).  There is no mechanism for user control of hash
   function, equality comparison, initial size, or resizing policy.  */

struct pointer_set_t
{
  size_t log_slots;
  size_t n_slots;		/* n_slots = 2^log_slots */
  size_t n_elements;

  const void **slots;
};

/* Use the multiplicative method, as described in Knuth 6.4, to obtain
   a hash code for P in the range [0, MAX).  MAX == 2^LOGMAX.

   Summary of this method: Multiply p by some number A that's
   relatively prime to 2^sizeof(size_t).  The result is two words.
   Discard the most significant word, and return the most significant
   N bits of the least significant word.  As suggested by Knuth, our
   choice for A is the integer part of (ULONG_MAX + 1.0) / phi, where phi
   is the golden ratio.

   We don't need to do anything special for full-width multiplication
   because we're only interested in the least significant word of the
   product, and unsigned arithmetic in C is modulo the word size.  */

static inline size_t
hash1 (const void *p, unsigned long max, unsigned long logmax)
{
#if HOST_BITS_PER_LONG == 32
  const unsigned long A = 0x9e3779b9u;
#elif HOST_BITS_PER_LONG == 64
  const unsigned long A = 0x9e3779b97f4a7c16ul;
#else
  const unsigned long A
    = (ULONG_MAX + 1.0L) * 0.6180339887498948482045868343656381177203L;
#endif
  const unsigned long shift = HOST_BITS_PER_LONG - logmax;

  return ((A * (unsigned long) p) >> shift) & (max - 1);
}

/* Allocate an empty pointer set.  */
struct pointer_set_t *
pointer_set_create (void)
{
  struct pointer_set_t *result = XNEW (struct pointer_set_t);

  result->n_elements = 0;
  result->log_slots = 8;
  result->n_slots = (size_t) 1 << result->log_slots;

  result->slots = XCNEWVEC (const void *, result->n_slots);
  return result;
}

/* Reclaims all memory associated with PSET.  */
void
pointer_set_destroy (struct pointer_set_t *pset)
{
  XDELETEVEC (pset->slots);
  XDELETE (pset);
}

/* Create a copy of PSET. */
struct pointer_set_t *
pointer_set_copy (const struct pointer_set_t *pset)
{
  struct pointer_set_t *result = XNEW (struct pointer_set_t);

  result->n_elements = pset->n_elements;
  result->log_slots = pset->log_slots;
  result->n_slots = pset->n_slots;
  result->slots = XCNEWVEC (const void *, result->n_slots);

  /* Now copy the members of the set */
  memcpy(result->slots, pset->slots, pset->n_slots * sizeof(void *));

  return result;
}

/* Returns nonzero if PSET contains P.  P must be nonnull.

   Collisions are resolved by linear probing.  */
int
pointer_set_contains (const struct pointer_set_t *pset, const void *p)
{
  size_t n = hash1 (p, pset->n_slots, pset->log_slots);

  while (true)
    {
      if (pset->slots[n] == p)
       return 1;
      else if (pset->slots[n] == 0)
       return 0;
      else
       {
         ++n;
         if (n == pset->n_slots)
           n = 0;
       }
    }
}

/* Subroutine of pointer_set_insert.  Return the insertion slot for P into
   an empty element of SLOTS, an array of length N_SLOTS.  */
static inline size_t
insert_aux (const void *p, const void **slots, size_t n_slots, size_t log_slots)
{
  size_t n = hash1 (p, n_slots, log_slots);
  while (true)
    {
      if (slots[n] == p || slots[n] == 0)
	return n;
      else
	{
	  ++n;
	  if (n == n_slots)
	    n = 0;
	}
    }
}

/* Inserts P into PSET if it wasn't already there.  Returns nonzero
   if it was already there. P must be nonnull.  */
int
pointer_set_insert (struct pointer_set_t *pset, const void *p)
{
  size_t n;

  /* For simplicity, expand the set even if P is already there.  This can be
     superfluous but can happen at most once.  */
  if (pset->n_elements > pset->n_slots / 4)
    {
      size_t new_log_slots = pset->log_slots + 1;
      size_t new_n_slots = pset->n_slots * 2;
      const void **new_slots = XCNEWVEC (const void *, new_n_slots);
      size_t i;

      for (i = 0; i < pset->n_slots; ++i)
        {
	  const void *value = pset->slots[i];
	  n = insert_aux (value, new_slots, new_n_slots, new_log_slots);
	  new_slots[n] = value;
	}

      XDELETEVEC (pset->slots);
      pset->n_slots = new_n_slots;
      pset->log_slots = new_log_slots;
      pset->slots = new_slots;
    }

  n = insert_aux (p, pset->slots, pset->n_slots, pset->log_slots);
  if (pset->slots[n])
    return 1;

  pset->slots[n] = p;
  ++pset->n_elements;
  return 0;
}

/* Delete P from PSET if PSET contains P. Returns nonzero if P is in the set.
   P must be nonnull. */
int
pointer_set_delete (struct pointer_set_t *pset, const void *p)
{
  size_t n = hash1 (p, pset->n_slots, pset->log_slots);

  while (true)
    {
      if (pset->slots[n] == p)
        {
          pset->slots[n] = 0;
          --pset->n_elements;
          return 1;
        }
      else if (pset->slots[n] == 0)
        {
          return 0;
        }
      else
        {
          ++n;
          if (n == pset->n_slots)
            n = 0;
        }
    }
}

/* Pass each pointer in PSET to the function in FN, together with the fixed
   parameter DATA.  If FN returns false, the iteration stops.  */

void pointer_set_traverse (const struct pointer_set_t *pset,
			   bool (*fn) (const void *, void *), void *data)
{
  size_t i;
  for (i = 0; i < pset->n_slots; ++i)
    if (pset->slots[i] && !fn (pset->slots[i], data))
      break;
}

/* Intersect DST_PSET with SRC_PSET and return the intersection set in
   DST_PSET, i.e.,

       DST_PSET = DST_PSET ^ SRC_PSET

   Also, if CMPL_PSET is not NULL, calculate the complement set that
   contains elements not in the intersection, i.e.,

       CMPL_PSET = (DST_PSET U SRC_PSET) - (DST_PSET ^ SRC_PSET) */
void
pointer_set_intersection_complement(struct pointer_set_t *dst_pset,
                                    const struct pointer_set_t *src_pset,
                                    struct pointer_set_t *cmpl_pset)
{
  size_t i;
  for (i = 0; i < dst_pset->n_slots; ++i)
    {
      const void *p = dst_pset->slots[i];
      if (p && !pointer_set_contains(src_pset, p))
        {
          dst_pset->slots[i] = 0;
          --dst_pset->n_elements;
          if (cmpl_pset)
            pointer_set_insert (cmpl_pset, p);
        }
    }

  if (cmpl_pset)
    {
      for (i = 0; i < src_pset->n_slots; ++i)
        {
          const void *p = src_pset->slots[i];
          if (p && !pointer_set_contains(dst_pset, p))
            pointer_set_insert (cmpl_pset, p);
        }
    }
}

/* Take the union of DST_PSET and SRC_PSET and return the union in DST_PSET. */
void
pointer_set_union_inplace (struct pointer_set_t *dst_pset,
                           const struct pointer_set_t *src_pset)
{
  size_t i;
  for (i = 0; i < src_pset->n_slots; ++i)
    {
      const void *p = src_pset->slots[i];
      if (p)
        pointer_set_insert (dst_pset, p);
    }
}

/* Return the number of elements in PSET. */
size_t
pointer_set_cardinality (const struct pointer_set_t *pset)
{
  return pset->n_elements;
}



/* A pointer map is represented the same way as a pointer_set, so
   the hash code is based on the address of the key, rather than
   its contents.  Null keys are a reserved value.  Deletion is not
   supported (yet).  There is no mechanism for user control of hash
   function, equality comparison, initial size, or resizing policy.  */

struct pointer_map_t
{
  size_t log_slots;
  size_t n_slots;		/* n_slots = 2^log_slots */
  size_t n_elements;

  const void **keys;
  void **values;
};

/* Allocate an empty pointer map.  */
struct pointer_map_t *
pointer_map_create (void)
{
  struct pointer_map_t *result = XNEW (struct pointer_map_t);

  result->n_elements = 0;
  result->log_slots = 8;
  result->n_slots = (size_t) 1 << result->log_slots;

  result->keys = XCNEWVEC (const void *, result->n_slots);
  result->values = XCNEWVEC (void *, result->n_slots);
  return result;
}

/* Reclaims all memory associated with PMAP.  */
void pointer_map_destroy (struct pointer_map_t *pmap)
{
  XDELETEVEC (pmap->keys);
  XDELETEVEC (pmap->values);
  XDELETE (pmap);
}

/* Returns a pointer to the value to which P maps, if PMAP contains P.  P
   must be nonnull.  Return NULL if PMAP does not contain P.

   Collisions are resolved by linear probing.  */
void **
pointer_map_contains (const struct pointer_map_t *pmap, const void *p)
{
  size_t n = hash1 (p, pmap->n_slots, pmap->log_slots);

  while (true)
    {
      if (pmap->keys[n] == p)
	return &pmap->values[n];
      else if (pmap->keys[n] == 0)
	return NULL;
      else
       {
         ++n;
         if (n == pmap->n_slots)
           n = 0;
       }
    }
}

/* Inserts P into PMAP if it wasn't already there.  Returns a pointer
   to the value.  P must be nonnull.  */
void **
pointer_map_insert (struct pointer_map_t *pmap, const void *p)
{
  size_t n;

  /* For simplicity, expand the map even if P is already there.  This can be
     superfluous but can happen at most once.  */
  if (pmap->n_elements > pmap->n_slots / 4)
    {
      size_t new_log_slots = pmap->log_slots + 1;
      size_t new_n_slots = pmap->n_slots * 2;
      const void **new_keys = XCNEWVEC (const void *, new_n_slots);
      void **new_values = XCNEWVEC (void *, new_n_slots);
      size_t i;

      for (i = 0; i < pmap->n_slots; ++i)
	if (pmap->keys[i])
	  {
	    const void *key = pmap->keys[i];
	    n = insert_aux (key, new_keys, new_n_slots, new_log_slots);
	    new_keys[n] = key;
	    new_values[n] = pmap->values[i];
	  }

      XDELETEVEC (pmap->keys);
      XDELETEVEC (pmap->values);
      pmap->n_slots = new_n_slots;
      pmap->log_slots = new_log_slots;
      pmap->keys = new_keys;
      pmap->values = new_values;
    }

  n = insert_aux (p, pmap->keys, pmap->n_slots, pmap->log_slots);
  if (!pmap->keys[n])
    {
      ++pmap->n_elements;
      pmap->keys[n] = p;
    }

  return &pmap->values[n];
}

/* Pass each pointer in PMAP to the function in FN, together with the pointer
   to the value and the fixed parameter DATA.  If FN returns false, the
   iteration stops.  */

void pointer_map_traverse (const struct pointer_map_t *pmap,
			   bool (*fn) (const void *, void **, void *), void *data)
{
  size_t i;
  for (i = 0; i < pmap->n_slots; ++i)
    if (pmap->keys[i] && !fn (pmap->keys[i], &pmap->values[i], data))
      break;
}