X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fhmap.c;h=f3cbf23a6161ccade1ce5dc879d44b9b5a65cf6e;hb=f790dbda9d498eef9c9c0a49078adbeecf768d56;hp=4c97e2359c922034cc8389344dc2a581227042d7;hpb=8ef8acb7c70a321963d30f2264e8f91e16427fcf;p=pspp diff --git a/src/libpspp/hmap.c b/src/libpspp/hmap.c index 4c97e2359c..f3cbf23a61 100644 --- a/src/libpspp/hmap.c +++ b/src/libpspp/hmap.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2008, 2009 Free Software Foundation, Inc. + Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 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 @@ -18,11 +18,12 @@ #include #endif -#include +#include "libpspp/hmap.h" + #include #include -#include "xalloc.h" +#include "gl/xalloc.h" static size_t capacity_to_mask (size_t capacity); @@ -49,6 +50,18 @@ hmap_swap (struct hmap *a, struct hmap *b) b->buckets = &b->one; } +/* Removes all of the elements from MAP, without destroying MAP itself and + without accessing the existing elements (if any). */ +void +hmap_clear (struct hmap *map) +{ + size_t i; + + for (i = 0; i <= map->mask; i++) + map->buckets[i] = NULL; + map->count = 0; +} + /* Frees the memory, if any, allocated by hash map MAP. This has no effect on the actual data items in MAP, if any, because the client is responsible for allocating and freeing them. It @@ -57,9 +70,9 @@ hmap_swap (struct hmap *a, struct hmap *b) should iterate through the map and free the data items before destroying it. */ void -hmap_destroy (struct hmap *map) +hmap_destroy (struct hmap *map) { - if (map != NULL && map->buckets != &map->one) + if (map != NULL && map->buckets != &map->one) free (map->buckets); } @@ -70,7 +83,7 @@ hmap_destroy (struct hmap *map) NEW_MASK must be a power of 2 minus 1 (including 0), that is, its value in binary must be all 1-bits. */ static void -hmap_rehash (struct hmap *map, size_t new_mask) +hmap_rehash (struct hmap *map, size_t new_mask) { struct hmap_node **new_buckets; struct hmap_node *node, *next; @@ -78,12 +91,12 @@ hmap_rehash (struct hmap *map, size_t new_mask) assert ((new_mask & (new_mask + 1)) == 0); if (new_mask) new_buckets = xcalloc (new_mask + 1, sizeof *new_buckets); - else + else { new_buckets = &map->one; new_buckets[0] = NULL; } - + if (map->count > 0) { for (node = hmap_first (map); node != NULL; node = next) @@ -93,7 +106,7 @@ hmap_rehash (struct hmap *map, size_t new_mask) next = hmap_next (map, node); node->next = *new_bucket; *new_bucket = node; - } + } } if (map->buckets != &map->one) free (map->buckets); @@ -115,11 +128,11 @@ hmap_reserve (struct hmap *map, size_t capacity) store its current number of elements, allocating a new set of buckets and rehashing if that would save space. */ void -hmap_shrink (struct hmap *map) +hmap_shrink (struct hmap *map) { size_t new_mask = capacity_to_mask (map->count); - if (new_mask < map->mask) - hmap_rehash (map, new_mask); + if (new_mask < map->mask) + hmap_rehash (map, new_mask); } /* Moves NODE around in MAP to compensate for its hash value @@ -133,7 +146,7 @@ hmap_shrink (struct hmap *map) void hmap_changed (struct hmap *map, struct hmap_node *node, size_t new_hash) { - if ((new_hash ^ node->hash) & map->mask) + if ((new_hash ^ node->hash) & map->mask) { hmap_delete (map, node); hmap_insert_fast (map, node, new_hash); @@ -159,7 +172,7 @@ hmap_changed (struct hmap *map, struct hmap_node *node, size_t new_hash) this function runs in constant time. */ void hmap_moved (struct hmap *map, - struct hmap_node *node, const struct hmap_node *old) + struct hmap_node *node, const struct hmap_node *old) { struct hmap_node **p = &map->buckets[node->hash & map->mask]; while (*p != old) @@ -172,9 +185,9 @@ hmap_moved (struct hmap *map, a bit-mask suitable for use as the "mask" member of struct hmap, that is, a power of 2 minus 1 (including 0). */ static size_t -capacity_to_mask (size_t capacity) +capacity_to_mask (size_t capacity) { - /* Calculate the minimum mask necesary to support the given + /* Calculate the minimum mask necessary to support the given capacity. */ size_t mask = 0; while (hmap_mask_to_capacity__ (mask) < capacity)