replaced inline by __inline to allow to be compiled in gcc and msvc modes
[libfirm] / ir / adt / hashset.c
index a9a6d10..b3af299 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyrigth (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
 
 /**
  * @file
- * @date    17.03.2007
- * @brief   Geberic hashset implementation
+ * @brief   Generic hashset implementation
  * @author  Matthias Braun, inspiration from densehash from google sparsehash
  *          package
+ * @date    17.03.2007
  * @version $Id$
  *
  *
 
 #ifndef HT_OCCUPANCY_FLT
 /** how full before we double size */
-#define HT_OCCUPANCY_FLT  0.5f
+#define HT_OCCUPANCY_FLT(x) ((x)/2)
 #endif /* HT_OCCUPANCY_FLT */
+#ifndef HT_1_DIV_OCCUPANCY_FLT
+#define HT_1_DIV_OCCUPANCY_FLT 2
+#endif
 
 #ifndef HT_EMPTY_FLT
 /** how empty before we half size */
-#define HT_EMPTY_FLT      (0.4f * (HT_OCCUPANCY_FLT))
+#define HT_EMPTY_FLT(x)     ((x)/5)
 #endif /* HT_EMPTY_FLT */
 
 #ifndef HT_MIN_BUCKETS
@@ -302,8 +305,8 @@ void insert_new(HashSet *self, unsigned hash, ValueType value)
 static INLINE
 void reset_thresholds(HashSet *self)
 {
-       self->enlarge_threshold = (size_t) (self->num_buckets * HT_OCCUPANCY_FLT);
-       self->shrink_threshold = (size_t) (self->num_buckets * HT_EMPTY_FLT);
+       self->enlarge_threshold = (size_t) HT_OCCUPANCY_FLT(self->num_buckets);
+       self->shrink_threshold = (size_t) HT_EMPTY_FLT(self->num_buckets);
        self->consider_shrink = 0;
 }
 
@@ -378,6 +381,9 @@ void maybe_shrink(HashSet *self)
 
        self->consider_shrink = 0;
        size = hashset_size(self);
+       if(size <= HT_MIN_BUCKETS)
+               return;
+
        if(LIKELY(size > self->shrink_threshold))
                return;
 
@@ -544,7 +550,7 @@ void hashset_init_size(HashSet *self, size_t expected_elements)
                abort();
        }
 
-       needed_size = expected_elements * (int)(1.0 / HT_OCCUPANCY_FLT);
+       needed_size = expected_elements * HT_1_DIV_OCCUPANCY_FLT;
        po2size = ceil_po2(needed_size);
        init_size(self, po2size);
 }
@@ -574,19 +580,14 @@ ValueType hashset_iterator_next(HashSetIterator *self)
        HashSetEntry *current_bucket = self->current_bucket;
        HashSetEntry *end = self->end;
 
-       if(current_bucket >= end)
-               return NullValue;
-
        /* using hashset_insert or hashset_remove is not allowed while iterating */
        assert(self->entries_version == self->set->entries_version);
 
        do {
                current_bucket++;
-       } while(current_bucket < end &&
-                       (EntryIsEmpty(*current_bucket) || EntryIsDeleted(*current_bucket)));
-
-       if(current_bucket >= end)
-               return NullValue;
+               if(current_bucket >= end)
+                       return NullValue;
+       } while(EntryIsEmpty(*current_bucket) || EntryIsDeleted(*current_bucket));
 
        self->current_bucket = current_bucket;
        return EntryGetValue(*current_bucket);