LCOV - code coverage report
Current view: top level - sql/include - sql_hash.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 12 12 100.0 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #ifndef SQL_HASH_H
      10             : #define SQL_HASH_H
      11             : 
      12             : /* sql_hash implementation
      13             :  * used to optimize search in expression and statement lists
      14             :  */
      15             : 
      16             : #include "sql_mem.h"
      17             : 
      18             : #define HASH_MIN_SIZE 4
      19             : 
      20             : typedef int (*fkeyvalue) (void *data);
      21             : 
      22             : typedef struct sql_hash_e {
      23             :         int key;
      24             :         void *value;
      25             :         struct sql_hash_e *chain;
      26             : } sql_hash_e;
      27             : 
      28             : typedef struct sql_hash {
      29             :         sql_allocator *sa;
      30             :         int size; /* power of 2 */
      31             :         sql_hash_e **buckets;
      32             :         fkeyvalue key;
      33             :         int entries;
      34             : } sql_hash;
      35             : 
      36             : extern sql_hash *hash_new(sql_allocator *sa, int size, fkeyvalue key);
      37             : extern int hash_entries(sql_hash *h);
      38             : extern int hash_empty(sql_hash *h);
      39             : extern void hash_del(sql_hash *ht, int key, void *value);
      40             : extern void hash_clear(sql_hash *h);
      41             : extern void hash_destroy(sql_hash *h);
      42             : 
      43             : static inline sql_hash_e*
      44    14659525 : hash_add(sql_hash *h, int key, void *value)
      45             : {
      46    14659525 :         sql_hash_e *e = (h->sa)?SA_NEW(h->sa, sql_hash_e):MNEW(sql_hash_e);
      47             : 
      48    14659525 :         if (e == NULL)
      49             :                 return NULL;
      50    14659525 :         e->chain = h->buckets[key&(h->size-1)];
      51    14659525 :         h->buckets[key&(h->size-1)] = e;
      52    14659525 :         e->key = key;
      53    14659525 :         e->value = value;
      54    14659525 :         h->entries++;
      55    14659525 :         return e;
      56             : }
      57             : 
      58             : static inline unsigned int
      59             : hash_key(const char *restrict k)
      60             : {
      61             :         unsigned int h = 37; /* prime number */
      62   586166378 :         while (*k) {
      63   500517968 :                 h = (h * 54059) ^ (k[0] * 76963); /* prime numbers */
      64   500517968 :                 k++;
      65             :         }
      66             :         return h;
      67             : }
      68             : 
      69             : #endif /* SQL_HASH_H */

Generated by: LCOV version 1.14