]> mj.ucw.cz Git - libucw.git/blobdiff - lib/hashtable.h
Fix calculation of internal sorting buffer and add unification to s-fixint.
[libucw.git] / lib / hashtable.h
index 486fe30bb83e5a00b01183c7866a78c2b6794778..ec581ed777bb9743826b963204bb6e940965837f 100644 (file)
@@ -2,7 +2,7 @@
  *     UCW Library -- Universal Hash Table
  *
  *     (c) 2002--2004 Martin Mares <mj@ucw.cz>
- *     (c) 2002 Robert Spalek <robert@ucw.cz>
+ *     (c) 2002--2005 Robert Spalek <robert@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -39,6 +39,9 @@
  *                     That is, `type1 k1, type2 k2, ... typen kn'.
  *                     With complex keys, HASH_GIVE_HASHFN and HASH_GIVE_EQ
  *                     are mandatory.
+ *  | HASH_KEY_MEMORY=f        use node->f as a raw data key, compared using
+ *                     memcmp
+ *    HASH_KEY_SIZE    the length of the key block
  *
  *  Then specify what operations you request (all names are automatically
  *  prefixed by calling HASH_PREFIX):
@@ -204,6 +207,30 @@ struct P(table) P(table);
    { HASH_KEY(n->) = k; }
 #endif
 
+#elif defined(HASH_KEY_MEMORY)
+
+#define HASH_KEY(x) x HASH_KEY_MEMORY
+
+#define HASH_KEY_DECL byte HASH_KEY( )[HASH_KEY_SIZE]
+
+#ifndef HASH_GIVE_HASHFN
+#  define HASH_GIVE_HASHFN
+   static inline int P(hash) (TAUC byte *x)
+   { return hash_block(x, HASH_KEY_SIZE); }
+#endif
+
+#ifndef HASH_GIVE_EQ
+#  define HASH_GIVE_EQ
+   static inline int P(eq) (TAUC byte *x, byte *y)
+   { return !memcmp(x, y, HASH_KEY_SIZE); }
+#endif
+
+#ifndef HASH_GIVE_INIT_KEY
+#  define HASH_GIVE_INIT_KEY
+   static inline void P(init_key) (TAUC P(node) *n, byte *k)
+   { memcpy(HASH_KEY(n->), k, HASH_KEY_SIZE); }
+#endif
+
 #elif defined(HASH_KEY_STRING) || defined(HASH_KEY_ENDSTRING)
 
 #ifdef HASH_KEY_STRING
@@ -290,8 +317,6 @@ static inline void P(init_data) (TAUC P(node) *n UNUSED)
 }
 #endif
 
-#include <stdlib.h>
-
 #ifdef HASH_GIVE_ALLOC
 /* If the caller has requested to use his own allocation functions, do so */
 static inline void P(init_alloc) (TAU) { }
@@ -354,7 +379,7 @@ static inline void * P(new_bucket)(TAUC uns size) { return P(alloc)(TTC size); }
 
 static void P(alloc_table) (TAU)
 {
-  T.hash_size = nextprime(T.hash_size);
+  T.hash_size = next_table_prime(T.hash_size);
   T.ht = P(table_alloc)(TTC sizeof(void *) * T.hash_size);
   bzero(T.ht, sizeof(void *) * T.hash_size);
   if (2*T.hash_size < T.hash_hard_max)
@@ -586,7 +611,7 @@ do {                                                                                        \
        GLUE_(h_px,node) *h_var = &h_buck->n;
 #define HASH_FOR_ALL(h_px, h_var) HASH_FOR_ALL_DYNAMIC(h_px, &GLUE_(h_px,table), h_var)
 #define HASH_END_FOR } } while(0)
-#define HASH_BREAK 
+#define HASH_BREAK
 #define HASH_CONTINUE continue
 
 #endif