]> www.ginac.de Git - ginac.git/blobdiff - ginac/utils.h
#ifndef around namespace GiNaC { }
[ginac.git] / ginac / utils.h
index 259527eef74669fadab04c35bc2bb2ab7436169b..18a5e7bd3f86c85c17216ec9ef20793546244fcf 100644 (file)
@@ -1,7 +1,8 @@
 /** @file utils.h
  *
 /** @file utils.h
  *
- *  Interface to several small and furry utilities.
- *
+ *  Interface to several small and furry utilities. */
+
+/*
  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
 #include <strstream>
 #include <string>
 #include "config.h"
 #include <strstream>
 #include <string>
 #include "config.h"
+#include "assertion.h"
+
+#ifndef NO_GINAC_NAMESPACE
+namespace GiNaC {
+#endif // ndef NO_GINAC_NAMESPACE
 
 template<class T>
 string ToString(T const & t)
 
 template<class T>
 string ToString(T const & t)
@@ -38,8 +44,46 @@ unsigned log2(unsigned n);
 
 int compare_pointers(void const * a, void const * b);
 
 
 int compare_pointers(void const * a, void const * b);
 
-#define DYNCONSTCAST(FINALTYPE,BASICTYPE,EXPRESSION) \
-    dynamic_cast<FINALTYPE>(const_cast<BASICTYPE>(EXPRESSION))
+/** Rotate lower 31 bits of unsigned value by one bit to the left
+ *  (upper bits get cleared). */
+inline unsigned rotate_left_31(unsigned n)
+{
+    // clear highest bit and shift 1 bit to the left
+    n=(n & 0x7FFFFFFFU) << 1;
+
+    // overflow? clear highest bit and set lowest bit
+    if (n & 0x80000000U) {
+        n=(n & 0x7FFFFFFFU) | 0x00000001U;
+    }
+    GINAC_ASSERT(n<0x80000000U);
+
+    return n;
+}
+
+/** Golden ratio hash function. */
+inline unsigned golden_ratio_hash(unsigned n)
+{
+       // This function requires arithmetic with at least 64 significant bits
+#if SIZEOF_LONG_DOUBLE > 8
+       // If "long double" is bigger than 64 bits, we assume that the mantissa
+       // has at least 64 bits. This is not guaranteed but it's a good guess.
+    const static long double golden_ratio = .618033988749894848204586834370;
+    long double m = golden_ratio * n;
+    return unsigned((m - int(m)) * 0x80000000);
+#elif SIZEOF_LONG >= 8
+       // "long" has 64 bits, so we prefer it because it might be more efficient
+       // than "long long"
+       unsigned long l = n * 0x4f1bbcddL;
+       return (l & 0x7fffffffU) ^ (l >> 32);
+#elif SIZEOF_LONG_LONG >= 8
+       // This requires ´long long´ (or an equivalent 64 bit type)---which is,
+    // unfortunately, not ANSI-compliant:
+       unsigned long long l = n * 0x4f1bbcddLL;
+       return (l & 0x7fffffffU) ^ (l >> 32);
+#else
+#error "No 64 bit data type. You lose."
+#endif
+}
 
 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
 template <class InputIterator1, class InputIterator2, class OutputIterator,
 
 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
 template <class InputIterator1, class InputIterator2, class OutputIterator,
@@ -89,4 +133,8 @@ OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
   }
 }
 
   }
 }
 
+#ifndef NO_GINAC_NAMESPACE
+} // namespace GiNaC
+#endif // ndef NO_GINAC_NAMESPACE
+
 #endif // ndef __GINAC_UTILS_H__
 #endif // ndef __GINAC_UTILS_H__