LCOV - code coverage report
Current view: top level - sql/common - sql_hash.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 41 56 73.2 %
Date: 2021-10-13 02:24:04 Functions: 5 6 83.3 %

          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             : #include "monetdb_config.h"
      10             : #include "sql_mem.h"
      11             : #include "sql_hash.h"
      12             : 
      13             : static unsigned int
      14             : log_base2(unsigned int n)
      15             : {
      16             :         unsigned int l ;
      17             : 
      18     6049673 :         for (l = 0; n; l++)
      19     4785106 :                 n >>= 1 ;
      20             :         return l ;
      21             : }
      22             : 
      23             : sql_hash *
      24     1264567 : hash_new(sql_allocator *sa, int size, fkeyvalue key)
      25             : {
      26     1264567 :         sql_hash *ht = (sa)?SA_NEW(sa, sql_hash):MNEW(sql_hash);
      27             : 
      28     1264567 :         if (ht == NULL)
      29             :                 return NULL;
      30     1264567 :         ht->sa = sa;
      31     1264567 :         ht->entries = 0;
      32     1264567 :         ht->size = (1<<log_base2(size-1));
      33     1264567 :         ht->key = key;
      34     1264567 :         ht->buckets = (ht->sa)?SA_ZNEW_ARRAY(sa, sql_hash_e*, ht->size):ZNEW_ARRAY(sql_hash_e*, ht->size);
      35     1264567 :         if (ht->buckets == NULL) {
      36           0 :                 if (!ht->sa)
      37           0 :                         _DELETE(ht);
      38           0 :                 return NULL;
      39             :         }
      40             :         return ht;
      41             : }
      42             : 
      43             : int
      44       13925 : hash_entries(sql_hash *h)
      45             : {
      46       13925 :         if (h)
      47       13925 :                 return h->entries;
      48             :         return 0;
      49             : }
      50             : 
      51             : int
      52       13925 : hash_empty(sql_hash *h)
      53             : {
      54       13925 :         if (h)
      55       13925 :                 return hash_entries(h) == 0;
      56             :         return 1;
      57             : }
      58             : 
      59             : void
      60        1130 : hash_del(sql_hash *h, int key, void *value)
      61             : {
      62        1130 :         sql_hash_e *e = h->buckets[key&(h->size-1)], *p = NULL;
      63             : 
      64        1191 :         while (e && (e->key != key || e->value != value)) {
      65             :                 p = e;
      66          61 :                 e = e->chain;
      67             :         }
      68        1130 :         if (e) {
      69        1130 :                 h->entries--;
      70        1130 :                 if (p)
      71          57 :                         p->chain = e->chain;
      72             :                 else
      73        1073 :                         h->buckets[key&(h->size-1)] = e->chain;
      74        1130 :                 if (!h->sa)
      75        1130 :                         _DELETE(e);
      76             :         }
      77        1130 : }
      78             : 
      79             : /* clear all hash table entries */
      80             : void
      81           0 : hash_clear(sql_hash *h) /* this code should be called for hash tables created outside SQL allocators only! */
      82             : {
      83           0 :         if (h == NULL || h->sa)
      84             :                 return;
      85           0 :         for (int i = 0; i < h->size; i++) {
      86           0 :                 sql_hash_e *e = h->buckets[i], *c = NULL;
      87             : 
      88           0 :                 if (e)
      89           0 :                         c = e->chain;
      90           0 :                 while (c) {
      91           0 :                         sql_hash_e *next = c->chain;
      92             : 
      93           0 :                         _DELETE(c);
      94             :                         c = next;
      95             :                 }
      96           0 :                 _DELETE(e);
      97           0 :                 h->buckets[i] = NULL;
      98             :         }
      99           0 :         h->entries = 0;
     100             : }
     101             : 
     102             : void
     103      187734 : hash_destroy(sql_hash *h) /* this code should be called for hash tables created outside SQL allocators only! */
     104             : {
     105      187734 :         if (h == NULL || h->sa)
     106             :                 return;
     107     3372137 :         for (int i = 0; i < h->size; i++) {
     108     3188648 :                 sql_hash_e *e = h->buckets[i], *c = NULL;
     109             : 
     110     3188648 :                 if (e)
     111      441045 :                         c = e->chain;
     112     3556709 :                 while (c) {
     113      368061 :                         sql_hash_e *next = c->chain;
     114             : 
     115      368061 :                         _DELETE(c);
     116             :                         c = next;
     117             :                 }
     118     3188648 :                 _DELETE(e);
     119             :         }
     120      183489 :         _DELETE(h->buckets);
     121      183489 :         _DELETE(h);
     122             : }
     123             : 

Generated by: LCOV version 1.14