]> www.ginac.de Git - cln.git/blobdiff - src/base/hash/cl_hash.h
Replace CL_REQUIRE/CL_PROVIDE(cl_symbol) with portable code.
[cln.git] / src / base / hash / cl_hash.h
index 2e92ffd2606d71ae2bb390e24b287acef8432e61..92fbe8be334cfa4593ca367cd97e1fe30aa87df2 100644 (file)
@@ -3,11 +3,13 @@
 #ifndef _CL_HASH_H
 #define _CL_HASH_H
 
-#include "cl_object.h"
-#include "cl_malloc.h"
-#include "cl_abort.h"
+#include "cln/object.h"
+#include "cln/malloc.h"
+#include "cln/exception.h"
 #include "cl_iterator.h"
 
+namespace cln {
+
 const long htentry_last = 0; // means that there is no next entry
 
 // These forward declarations are needed for Sun CC 3.0.1 and 4.0.1.
@@ -15,6 +17,7 @@ template <class htentry> struct _cl_hashtable_iterator;
 
 template <class htentry>
 struct cl_heap_hashtable : public cl_heap {
+    friend struct _cl_hashtable_iterator<htentry>;
 protected:
     typedef struct htxentry {
         long next;     // > 0: pseudo-list continues at next-1
@@ -30,20 +33,20 @@ protected:
     long * _slots;  // vector of length _modulus
     htxentry * _entries; // vector of length _size
     void* _total_vector;
-    cl_boolean (*_garcol_fun) (cl_heap*); // Function to make room in the table.
+    bool (*_garcol_fun) (cl_heap*); // Function to make room in the table.
                                // Putting some intelligent function here turns
                                // a normal hash table into a "weak" hash table.
 public:
     // Allocation.
-    void* operator new (size_t size) { return cl_malloc_hook(size); }
+    void* operator new (size_t size) { return malloc_hook(size); }
     // Deallocation.
-    void operator delete (void* ptr) { cl_free_hook(ptr); }
+    void operator delete (void* ptr) { free_hook(ptr); }
     // Constructor: build a new, empty table.
     cl_heap_hashtable (long initial_size = 5) : cl_heap (),
         _size (initial_size), _count (0), _garcol_fun (no_garcol)
     {
         _modulus = compute_modulus(_size);
-        _total_vector = cl_malloc_hook(_modulus*sizeof(long) + _size*sizeof(htxentry));
+        _total_vector = malloc_hook(_modulus*sizeof(long) + _size*sizeof(htxentry));
         _slots = (long*) ((char*)_total_vector + 0);
         _entries = (htxentry *) ((char*)_total_vector + _modulus*sizeof(long));
         for (var long hi = _modulus-1; hi >= 0; hi--)
@@ -61,7 +64,7 @@ public:
         for (long i = 0; i < _size; i++)
             if (_entries[i].next >= 0)
                 _entries[i].~htxentry();
-        cl_free_hook(_total_vector);
+        free_hook(_total_vector);
     }
     // Count number of entries.
     long num_entries ()
@@ -129,7 +132,7 @@ protected:
             return index;
         }
         #if !(defined(__hppa__) && !defined(__GNUC__)) // workaround HP CC problem
-        cl_abort();
+        throw runtime_exception();
         #endif
         return -1; // dummy
     }
@@ -141,7 +144,7 @@ protected:
     }
 private:
     // Default function to make room in a hash table.
-    static cl_boolean no_garcol (cl_heap* ht) { unused ht; return cl_false; }
+    static bool no_garcol (cl_heap* ht) { unused ht; return false; }
 };
 
 template <class htentry>
@@ -151,12 +154,12 @@ struct _cl_hashtable_iterator
   #endif
 {
 private:
-    cl_heap_hashtable<htentry>::htxentry * _entries;
+    typename cl_heap_hashtable<htentry>::htxentry * _entries;
     long _index;
 public:
     _cl_hashtable_iterator () : _entries (0), _index (-1) {}
 public: /* ugh */
-    _cl_hashtable_iterator (cl_heap_hashtable<htentry>::htxentry * e, long i)
+    _cl_hashtable_iterator (typename cl_heap_hashtable<htentry>::htxentry * e, long i)
         : _entries (e), _index (i)
     {
         do { _index--; }
@@ -167,7 +170,7 @@ public:
     htentry& next ()
     {
         if (_index < 0)
-            cl_abort();
+            throw runtime_exception();
         var long old_index = _index;
         do { _index--; }
            while (_index >= 0 && _entries[_index].next < 0);
@@ -178,13 +181,9 @@ public:
 template <class htentry>
 inline _cl_hashtable_iterator<htentry> cl_heap_hashtable<htentry>::iterator ()
 {
-#if defined(__GNUC__)
-    return _cl_hashtable_iterator<htentry>::_cl_hashtable_iterator(_entries,_size);
-#else // workaround most C++ compilers' bug
-    typedef _cl_hashtable_iterator<htentry> _cl_hashtable_iterator_type;
-    return _cl_hashtable_iterator_type(_entries,_size);
-#endif
+    return _cl_hashtable_iterator<htentry>(_entries,_size);
 }
 
+}  // namespace cln
 
 #endif /* _CL_HASH_H */