X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fhmap.c;h=f3cbf23a6161ccade1ce5dc879d44b9b5a65cf6e;hb=f8dc847024749f595c17e7384c519c329a39070d;hp=9050cc08f5571a6f2e8908201444d834a96a6398;hpb=91c50c1bf38fa2377a685343608b88915aa0168a;p=pspp diff --git a/src/libpspp/hmap.c b/src/libpspp/hmap.c index 9050cc08f5..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, 2010 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); @@ -69,9 +70,9 @@ hmap_clear (struct hmap *map) 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); } @@ -82,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; @@ -90,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) @@ -105,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); @@ -127,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 @@ -145,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); @@ -171,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) @@ -184,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)