Good day and welcome to the FIRM XMALLOC*() macros. These macros are provided for...
[libfirm] / ir / adt / set.c
index 2db9ffa..6411e38 100644 (file)
@@ -1,5 +1,28 @@
-/* Set --- collection of entries that are unique wrt to a key.
-   Copyright (C) 1995, 1996 Markus Armbruster */
+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+/**
+ * @file
+ * @brief       implementation of set
+ * @author      Markus Armbruster
+ * @version     $Id$
+ */
 
 /*  This code is derived from:
 
 
     TODO: Fix Esmond's ugly MixedCapsIdentifiers ;->
  */
-
-/* $Id$ */
-
 #ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#ifdef USE_GCC_INLINE
-#define INLINE inline
-#else
-#define INLINE
+# include "config.h"
 #endif
 
-/* bcopy is not ISO C *
-#define bcopy(X, Y, Z) memcpy((Y), (X), (Z))
-*/
+#include "firm_config.h"
 
 #ifdef PSET
 # define SET pset
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include "misc.h"
+#include "xmalloc.h"
 #ifdef PSET
 # include "pset.h"
 #else
 # include "set.h"
 #endif
 
+
 #define TOBSTACK_ID MANGLEP(tag)
 #include "obst.h"
 
 #define SEGMENT_SIZE           (1 << SEGMENT_SIZE_SHIFT)
 #define DIRECTORY_SIZE_SHIFT   8
 #define DIRECTORY_SIZE         (1 << DIRECTORY_SIZE_SHIFT)
-#define MAX_LOAD_FACTOR        4
+#define MAX_LOAD_FACTOR                4
 
 
 typedef struct element {
-  struct element *chain;
+  struct element *chain;       /**< for chaining Elements */
   MANGLEP (entry) entry;
 } Element, *Segment;
 
 
 struct SET {
-  short        p;                      /* Next bucket to be split      */
-  short        maxp;                   /* upper bound on p during expansion    */
-  int nkey;                    /* current # keys       */
-  short        nseg;                   /* current # segments   */
+  unsigned p;                  /**< Next bucket to be split    */
+  unsigned maxp;               /**< upper bound on p during expansion  */
+  unsigned nkey;               /**< current # keys     */
+  unsigned nseg;               /**< current # segments */
   Segment *dir[DIRECTORY_SIZE];
-  MANGLEP(cmp_fun) cmp;                /* function comparing entries */
-  int iter_i, iter_j;
-  Element *iter_tail;          /* non-NULL while iterating over elts */
+  MANGLEP(cmp_fun) cmp;                /**< function comparing entries */
+  unsigned iter_i, iter_j;
+  Element *iter_tail;          /**< non-NULL while iterating over elts */
 #ifdef PSET
-  Element *free_list;
+  Element *free_list;          /**< list of free Elements */
 #endif
-  struct obstack obst;
+  struct obstack obst;         /**< obstack for allocation all data */
 #ifdef STATS
   int naccess, ncollision, ndups;
   int max_chain_len;
 #endif
 #ifdef DEBUG
-  const char *tag;
+  const char *tag;              /**< an optionally tag for distinguishing sets */
 #endif
 };
 
@@ -140,14 +153,14 @@ stat_chain_len (SET *table, int chain_len)
 const char *MANGLEP(tag);
 
 
-static void
+void
 MANGLEP(describe) (SET *table)
 {
-  int i, j, collide;
+  unsigned i, j, collide;
   Element *ptr;
   Segment *seg;
 
-  printf ("p=%d maxp=%d nkey=%d nseg=%d\n",
+  printf ("p=%u maxp=%u nkey=%u nseg=%u\n",
          table->p, table->maxp, table->nkey, table->nseg);
   for (i = 0;  i < table->nseg;  i++) {
     seg = table->dir[i];
@@ -157,12 +170,15 @@ MANGLEP(describe) (SET *table)
       while (ptr) {
        if (collide) printf ("<%3d>", collide);
        else printf ("table");
-       printf ("[%d][%3d]: %u %p\n", i, j, ptr->entry.hash, ptr->entry.dptr);
+       printf ("[%d][%3d]: %u %p\n", i, j, ptr->entry.hash, (void *)ptr->entry.dptr);
        ptr = ptr->chain;
        collide++;
       }
     }
   }
+#ifdef STATS
+  MANGLEP(stats)(table);
+#endif
 }
 
 #endif /* !DEBUG */
@@ -172,12 +188,16 @@ SET *
 (PMANGLE(new)) (MANGLEP(cmp_fun) cmp, int nslots)
 {
   int i;
-  SET *table = xmalloc (sizeof (SET));
-
-  /* Adjust nslots up to next power of 2, minimum SEGMENT_SIZE */
-  assert (nslots >= 0);
-  for (i = SEGMENT_SIZE;  i < nslots;  i <<= 1) assert (i < (i << 1));
-  nslots = i >> SEGMENT_SIZE_SHIFT;
+  SET *table = XMALLOC(SET);
+
+  if (nslots > SEGMENT_SIZE * DIRECTORY_SIZE)
+    nslots = DIRECTORY_SIZE;
+  else {
+    assert (nslots >= 0);
+    /* Adjust nslots up to next power of 2, minimum SEGMENT_SIZE */
+    for (i = SEGMENT_SIZE;  i < nslots;  i <<= 1);
+    nslots = i >> SEGMENT_SIZE_SHIFT;
+  }
 
   table->nseg = table->p = table->nkey = 0;
   table->maxp = nslots << SEGMENT_SIZE_SHIFT;
@@ -193,7 +213,7 @@ SET *
     table->dir[i] = (Segment *)obstack_alloc (&table->obst,
                                              sizeof (Segment) * SEGMENT_SIZE);
 
-    memset (table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE);
+    memset(table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE);
     table->nseg++;
   }
 
@@ -218,7 +238,16 @@ PMANGLE(del) (SET *table)
   xfree (table);
 }
 
+int
+MANGLEP(count) (SET *table)
+{
+  return table->nkey;
+}
 
+/*
+ * do one iteration step, return 1
+ * if still data in the set, 0 else
+ */
 static INLINE int
 iter_step (SET *table)
 {
@@ -232,7 +261,9 @@ iter_step (SET *table)
   return 1;
 }
 
-
+/*
+ * finds the first entry in the table
+ */
 void *
 MANGLEP(first) (SET *table)
 {
@@ -247,13 +278,19 @@ MANGLEP(first) (SET *table)
   return table->iter_tail->entry.dptr;
 }
 
-
+/*
+ * returns next entry in the table
+ */
 void *
 MANGLEP(next) (SET *table)
 {
-  assert (table->iter_tail);
+  if (!table->iter_tail)
+    return NULL;
+
+  /* follow collision chain */
   table->iter_tail = table->iter_tail->chain;
   if (!table->iter_tail) {
+    /* go to next segment */
     do {
       if (!iter_step (table)) return NULL;
     } while (!table->dir[table->iter_i][table->iter_j]);
@@ -266,23 +303,26 @@ MANGLEP(next) (SET *table)
 void
 MANGLEP(break) (SET *table)
 {
-  assert (table->iter_tail);
   table->iter_tail = NULL;
 }
 
-
+/*
+ * limit the hash value
+ */
 static INLINE unsigned
 Hash (SET *table, unsigned h)
 {
   unsigned address;
-
-  address = h & (table->maxp - 1);
+  address = h & (table->maxp - 1);          /* h % table->maxp */
   if (address < (unsigned)table->p)
     address = h & ((table->maxp << 1) - 1); /* h % (2*table->maxp) */
   return address;
 }
 
-
+/*
+ * returns non-zero if the number of elements in
+ * the set is greater then number of segments * MAX_LOAD_FACTOR
+ */
 static INLINE int
 loaded (SET *table)
 {
@@ -290,7 +330,14 @@ loaded (SET *table)
          > (table->nseg << SEGMENT_SIZE_SHIFT) * MAX_LOAD_FACTOR);
 }
 
-
+/*
+ * expand the hash-table: the algorithm is split, so on every
+ * insert, only ONE segment is rehashed!
+ *
+ * table->p contains the current segment to split
+ * after all segments were split, table->p is set to zero and
+ * table->maxp is duplicated.
+ */
 static void
 expand_table (SET *table)
 {
@@ -305,18 +352,20 @@ expand_table (SET *table)
 
   if (table->maxp + table->p < (DIRECTORY_SIZE << SEGMENT_SIZE_SHIFT)) {
     /* Locate the bucket to be split */
-    OldSegmentDir = table->p >> SEGMENT_SIZE_SHIFT;
-    OldSegment = table->dir[OldSegmentDir];
+    OldSegmentDir   = table->p >> SEGMENT_SIZE_SHIFT;
+    OldSegment      = table->dir[OldSegmentDir];
     OldSegmentIndex = table->p & (SEGMENT_SIZE-1);
 
     /* Expand address space; if necessary create a new segment */
-    NewAddress = table->maxp + table->p;
-    NewSegmentDir = NewAddress >> SEGMENT_SIZE_SHIFT;
+    NewAddress      = table->maxp + table->p;
+    NewSegmentDir   = NewAddress >> SEGMENT_SIZE_SHIFT;
     NewSegmentIndex = NewAddress & (SEGMENT_SIZE-1);
     if (NewSegmentIndex == 0) {
       table->dir[NewSegmentDir] =
        (Segment *)obstack_alloc (&table->obst,
                                  sizeof(Segment) * SEGMENT_SIZE);
+      memset(table->dir[NewSegmentDir], 0, sizeof(Segment) * SEGMENT_SIZE);
+      table->nseg++;
     }
     NewSegment = table->dir[NewSegmentDir];
 
@@ -326,7 +375,6 @@ expand_table (SET *table)
       table->maxp <<= 1;       /* table->maxp *= 2     */
       table->p = 0;
     }
-    table->nseg++;
 
     /* Relocate records to the new bucket */
     Previous = &OldSegment[OldSegmentIndex];
@@ -337,9 +385,9 @@ expand_table (SET *table)
       if (Hash (table, Current->entry.hash) == NewAddress) {
        /* move to new chain */
        *LastOfNew = Current;
-       *Previous = Current->chain;
-       LastOfNew = &Current->chain;
-       Current = Current->chain;
+       *Previous  = Current->chain;
+       LastOfNew  = &Current->chain;
+       Current    = Current->chain;
        *LastOfNew = NULL;
       } else {
        /* leave on old chain */
@@ -368,7 +416,6 @@ MANGLE(_,_search) (SET *table,
   int chain_len = 0;
 
   assert (table);
-  assert (!table->iter_tail);
   assert (key);
 #ifdef DEBUG
   MANGLEP(tag) = table->tag;
@@ -377,7 +424,7 @@ MANGLE(_,_search) (SET *table,
 
   /* Find collision chain */
   h = Hash (table, hash);
-  SegmentIndex = h & (SEGMENT_SIZE-1);
+  SegmentIndex   = h & (SEGMENT_SIZE-1);
   CurrentSegment = table->dir[h >> SEGMENT_SIZE_SHIFT];
   assert (CurrentSegment != NULL);
   q = CurrentSegment[SegmentIndex];
@@ -391,6 +438,8 @@ MANGLE(_,_search) (SET *table,
   stat_chain_len (table, chain_len);
 
   if (!q && (action != MANGLE(_,_find))) { /* not found, insert */
+    assert (!table->iter_tail && "insert an element into a set that is iterated");
+
     if (CurrentSegment[SegmentIndex]) stat_dup (table);
 
 #ifdef PSET
@@ -431,6 +480,11 @@ MANGLE(_,_search) (SET *table,
 
 #ifdef PSET
 
+int pset_default_ptr_cmp(const void *x, const void *y)
+{
+       return x != y;
+}
+
 void *
 pset_remove (SET *table, const void *key, unsigned hash)
 {
@@ -462,9 +516,24 @@ pset_remove (SET *table, const void *key, unsigned hash)
   stat_chain_len (table, chain_len);
 
   q = *p;
+
+  if (q == table->iter_tail) {
+    /* removing current element */
+    table->iter_tail = q->chain;
+    if (!table->iter_tail) {
+      /* go to next segment */
+      do {
+       if (!iter_step (table))
+         break;
+      } while (!table->dir[table->iter_i][table->iter_j]);
+      table->iter_tail = table->dir[table->iter_i][table->iter_j];
+    }
+  }
+
   *p = (*p)->chain;
   q->chain = table->free_list;
   table->free_list = q;
+  --table->nkey;
 
   return q->entry.dptr;
 }
@@ -490,6 +559,13 @@ MANGLEP(entry) *
   return pset_hinsert (se, key, hash);
 }
 
+void pset_insert_pset_ptr(pset *target, pset *src) {
+  void *elt;
+  for (elt = pset_first(src); elt; elt = pset_next(src)) {
+    pset_insert_ptr(target, elt);
+  }
+}
+
 #else /* !PSET */
 
 void *