1 /* Set operations on pointers
2 Copyright (C) 2004, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "pointer-set.h"
24 /* A pointer set is represented as a simple open-addressing hash
25 table. Simplifications: The hash code is based on the value of the
26 pointer, not what it points to. The number of buckets is always a
27 power of 2. Null pointers are a reserved value. Deletion is not
28 supported (yet). There is no mechanism for user control of hash
29 function, equality comparison, initial size, or resizing policy. */
34 size_t n_slots
; /* n_slots = 2^log_slots */
40 /* Use the multiplicative method, as described in Knuth 6.4, to obtain
41 a hash code for P in the range [0, MAX). MAX == 2^LOGMAX.
43 Summary of this method: Multiply p by some number A that's
44 relatively prime to 2^sizeof(size_t). The result is two words.
45 Discard the most significant word, and return the most significant
46 N bits of the least significant word. As suggested by Knuth, our
47 choice for A is the integer part of (ULONG_MAX + 1.0) / phi, where phi
50 We don't need to do anything special for full-width multiplication
51 because we're only interested in the least significant word of the
52 product, and unsigned arithmetic in C is modulo the word size. */
55 hash1 (const void *p
, unsigned long max
, unsigned long logmax
)
57 #if HOST_BITS_PER_LONG == 32
58 const unsigned long A
= 0x9e3779b9u
;
59 #elif HOST_BITS_PER_LONG == 64
60 const unsigned long A
= 0x9e3779b97f4a7c16ul
;
63 = (ULONG_MAX
+ 1.0L) * 0.6180339887498948482045868343656381177203L;
65 const unsigned long shift
= HOST_BITS_PER_LONG
- logmax
;
67 return ((A
* (unsigned long) p
) >> shift
) & (max
- 1);
70 /* Allocate an empty pointer set. */
71 struct pointer_set_t
*
72 pointer_set_create (void)
74 struct pointer_set_t
*result
= XNEW (struct pointer_set_t
);
76 result
->n_elements
= 0;
77 result
->log_slots
= 8;
78 result
->n_slots
= (size_t) 1 << result
->log_slots
;
80 result
->slots
= XCNEWVEC (const void *, result
->n_slots
);
84 /* Reclaims all memory associated with PSET. */
86 pointer_set_destroy (struct pointer_set_t
*pset
)
88 XDELETEVEC (pset
->slots
);
92 /* Returns nonzero if PSET contains P. P must be nonnull.
94 Collisions are resolved by linear probing. */
96 pointer_set_contains (const struct pointer_set_t
*pset
, const void *p
)
98 size_t n
= hash1 (p
, pset
->n_slots
, pset
->log_slots
);
102 if (pset
->slots
[n
] == p
)
104 else if (pset
->slots
[n
] == 0)
109 if (n
== pset
->n_slots
)
115 /* Subroutine of pointer_set_insert. Return the insertion slot for P into
116 an empty element of SLOTS, an array of length N_SLOTS. */
118 insert_aux (const void *p
, const void **slots
, size_t n_slots
, size_t log_slots
)
120 size_t n
= hash1 (p
, n_slots
, log_slots
);
123 if (slots
[n
] == p
|| slots
[n
] == 0)
134 /* Inserts P into PSET if it wasn't already there. Returns nonzero
135 if it was already there. P must be nonnull. */
137 pointer_set_insert (struct pointer_set_t
*pset
, const void *p
)
141 /* For simplicity, expand the set even if P is already there. This can be
142 superfluous but can happen at most once. */
143 if (pset
->n_elements
> pset
->n_slots
/ 4)
145 size_t new_log_slots
= pset
->log_slots
+ 1;
146 size_t new_n_slots
= pset
->n_slots
* 2;
147 const void **new_slots
= XCNEWVEC (const void *, new_n_slots
);
150 for (i
= 0; i
< pset
->n_slots
; ++i
)
152 const void *value
= pset
->slots
[i
];
153 n
= insert_aux (value
, new_slots
, new_n_slots
, new_log_slots
);
154 new_slots
[n
] = value
;
157 XDELETEVEC (pset
->slots
);
158 pset
->n_slots
= new_n_slots
;
159 pset
->log_slots
= new_log_slots
;
160 pset
->slots
= new_slots
;
163 n
= insert_aux (p
, pset
->slots
, pset
->n_slots
, pset
->log_slots
);
172 /* Pass each pointer in PSET to the function in FN, together with the fixed
173 parameter DATA. If FN returns false, the iteration stops. */
175 void pointer_set_traverse (const struct pointer_set_t
*pset
,
176 bool (*fn
) (const void *, void *), void *data
)
179 for (i
= 0; i
< pset
->n_slots
; ++i
)
180 if (pset
->slots
[i
] && !fn (pset
->slots
[i
], data
))
185 /* A pointer map is represented the same way as a pointer_set, so
186 the hash code is based on the address of the key, rather than
187 its contents. Null keys are a reserved value. Deletion is not
188 supported (yet). There is no mechanism for user control of hash
189 function, equality comparison, initial size, or resizing policy. */
194 size_t n_slots
; /* n_slots = 2^log_slots */
201 /* Allocate an empty pointer map. */
202 struct pointer_map_t
*
203 pointer_map_create (void)
205 struct pointer_map_t
*result
= XNEW (struct pointer_map_t
);
207 result
->n_elements
= 0;
208 result
->log_slots
= 8;
209 result
->n_slots
= (size_t) 1 << result
->log_slots
;
211 result
->keys
= XCNEWVEC (const void *, result
->n_slots
);
212 result
->values
= XCNEWVEC (void *, result
->n_slots
);
216 /* Reclaims all memory associated with PMAP. */
217 void pointer_map_destroy (struct pointer_map_t
*pmap
)
219 XDELETEVEC (pmap
->keys
);
220 XDELETEVEC (pmap
->values
);
224 /* Returns a pointer to the value to which P maps, if PMAP contains P. P
225 must be nonnull. Return NULL if PMAP does not contain P.
227 Collisions are resolved by linear probing. */
229 pointer_map_contains (const struct pointer_map_t
*pmap
, const void *p
)
231 size_t n
= hash1 (p
, pmap
->n_slots
, pmap
->log_slots
);
235 if (pmap
->keys
[n
] == p
)
236 return &pmap
->values
[n
];
237 else if (pmap
->keys
[n
] == 0)
242 if (n
== pmap
->n_slots
)
248 /* Inserts P into PMAP if it wasn't already there. Returns a pointer
249 to the value. P must be nonnull. */
251 pointer_map_insert (struct pointer_map_t
*pmap
, const void *p
)
255 /* For simplicity, expand the map even if P is already there. This can be
256 superfluous but can happen at most once. */
257 if (pmap
->n_elements
> pmap
->n_slots
/ 4)
259 size_t new_log_slots
= pmap
->log_slots
+ 1;
260 size_t new_n_slots
= pmap
->n_slots
* 2;
261 const void **new_keys
= XCNEWVEC (const void *, new_n_slots
);
262 void **new_values
= XCNEWVEC (void *, new_n_slots
);
265 for (i
= 0; i
< pmap
->n_slots
; ++i
)
268 const void *key
= pmap
->keys
[i
];
269 n
= insert_aux (key
, new_keys
, new_n_slots
, new_log_slots
);
271 new_values
[n
] = pmap
->values
[i
];
274 XDELETEVEC (pmap
->keys
);
275 XDELETEVEC (pmap
->values
);
276 pmap
->n_slots
= new_n_slots
;
277 pmap
->log_slots
= new_log_slots
;
278 pmap
->keys
= new_keys
;
279 pmap
->values
= new_values
;
282 n
= insert_aux (p
, pmap
->keys
, pmap
->n_slots
, pmap
->log_slots
);
289 return &pmap
->values
[n
];
292 /* Pass each pointer in PMAP to the function in FN, together with the pointer
293 to the value and the fixed parameter DATA. If FN returns false, the
296 void pointer_map_traverse (const struct pointer_map_t
*pmap
,
297 bool (*fn
) (const void *, void **, void *), void *data
)
300 for (i
= 0; i
< pmap
->n_slots
; ++i
)
301 if (pmap
->keys
[i
] && !fn (pmap
->keys
[i
], &pmap
->values
[i
], data
))