+
+/* 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;
+}
+
+\f
+/* 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;
+}