Fix some typos (found by codespell)
[pspp] / src / libpspp / hmap.c
index 081d7cbba5a5f3e607f6a0dc3bfb90176a796a06..f3cbf23a6161ccade1ce5dc879d44b9b5a65cf6e 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2008 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
 #include <config.h>
 #endif
 
-#include <libpspp/hmap.h>
+#include "libpspp/hmap.h"
+
 #include <assert.h>
 #include <stdlib.h>
 
+#include "gl/xalloc.h"
+
 static size_t capacity_to_mask (size_t capacity);
 
 /* Initializes MAP as a new hash map that is initially empty. */
@@ -47,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
@@ -55,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);
 }
 
@@ -68,20 +83,20 @@ 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;
 
   assert ((new_mask & (new_mask + 1)) == 0);
   if (new_mask)
-    new_buckets = calloc (new_mask + 1, sizeof *new_buckets);
-  else 
+    new_buckets = xcalloc (new_mask + 1, sizeof *new_buckets);
+  else
     {
       new_buckets = &map->one;
       new_buckets[0] = NULL;
     }
-      
+
   if (map->count > 0)
     {
       for (node = hmap_first (map); node != NULL; node = next)
@@ -91,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);
@@ -113,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
@@ -131,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);
@@ -157,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)
@@ -170,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)