"Fossies" - the Fresh Open Source Software Archive

Member "nanomsg-1.1.5/src/utils/hash.c" (15 Oct 2018, 4926 Bytes) of package /linux/misc/nanomsg-1.1.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hash.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.1.4_vs_1.1.5.

    1 /*
    2     Copyright (c) 2012-2013 Martin Sustrik  All rights reserved.
    3 
    4     Permission is hereby granted, free of charge, to any person obtaining a copy
    5     of this software and associated documentation files (the "Software"),
    6     to deal in the Software without restriction, including without limitation
    7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
    8     and/or sell copies of the Software, and to permit persons to whom
    9     the Software is furnished to do so, subject to the following conditions:
   10 
   11     The above copyright notice and this permission notice shall be included
   12     in all copies or substantial portions of the Software.
   13 
   14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   20     IN THE SOFTWARE.
   21 */
   22 
   23 #include "hash.h"
   24 #include "fast.h"
   25 #include "alloc.h"
   26 #include "cont.h"
   27 #include "err.h"
   28 
   29 #define NN_HASH_INITIAL_SLOTS 32
   30 
   31 static uint32_t nn_hash_key (uint32_t key);
   32 
   33 void nn_hash_init (struct nn_hash *self)
   34 {
   35     uint32_t i;
   36 
   37     self->slots = NN_HASH_INITIAL_SLOTS;
   38     self->items = 0;
   39     self->array = nn_alloc (sizeof (struct nn_list) * NN_HASH_INITIAL_SLOTS,
   40         "hash map");
   41     alloc_assert (self->array);
   42     for (i = 0; i != NN_HASH_INITIAL_SLOTS; ++i)
   43         nn_list_init (&self->array [i]);
   44 }
   45 
   46 void nn_hash_term (struct nn_hash *self)
   47 {
   48     uint32_t i;
   49 
   50     for (i = 0; i != self->slots; ++i)
   51         nn_list_term (&self->array [i]);
   52     nn_free (self->array);
   53 }
   54 
   55 static void nn_hash_rehash (struct nn_hash *self)
   56 {
   57     uint32_t i;
   58     uint32_t oldslots;
   59     struct nn_list *oldarray;
   60     struct nn_hash_item *hitm;
   61     uint32_t newslot;
   62 
   63     /*  Allocate new double-sized array of slots. */
   64     oldslots = self->slots;
   65     oldarray = self->array;
   66     self->slots *= 2;
   67     self->array = nn_alloc (sizeof (struct nn_list) * self->slots, "hash map");
   68     alloc_assert (self->array);
   69     for (i = 0; i != self->slots; ++i)
   70         nn_list_init (&self->array [i]);
   71 
   72     /*  Move the items from old slot array to new slot array. */
   73     for (i = 0; i != oldslots; ++i) {
   74         while (!nn_list_empty (&oldarray [i])) {
   75             hitm = nn_cont (nn_list_begin (&oldarray [i]),
   76                     struct nn_hash_item, list);
   77             nn_list_erase (&oldarray [i], &hitm->list);
   78             newslot = nn_hash_key (hitm->key) % self->slots;
   79             nn_list_insert (&self->array [newslot], &hitm->list,
   80                     nn_list_end (&self->array [newslot]));
   81         }
   82 
   83         nn_list_term (&oldarray [i]);
   84     }
   85 
   86     /*  Deallocate the old array of slots. */
   87     nn_free (oldarray);
   88 }
   89 
   90 void nn_hash_insert (struct nn_hash *self, uint32_t key,
   91     struct nn_hash_item *item)
   92 {
   93     struct nn_list_item *it;
   94     uint32_t i;
   95 
   96     i = nn_hash_key (key) % self->slots;
   97 
   98     for (it = nn_list_begin (&self->array [i]);
   99           it != nn_list_end (&self->array [i]);
  100           it = nn_list_next (&self->array [i], it))
  101         nn_assert (nn_cont (it, struct nn_hash_item, list)->key != key);
  102 
  103     item->key = key;
  104     nn_list_insert (&self->array [i], &item->list,
  105         nn_list_end (&self->array [i]));
  106     ++self->items;
  107 
  108     /*  If the hash is getting full, double the amount of slots and
  109         re-hash all the items. */
  110     if (nn_slow (self->items * 2 > self->slots && self->slots < 0x80000000))
  111         nn_hash_rehash(self);
  112 }
  113 
  114 void nn_hash_erase (struct nn_hash *self, struct nn_hash_item *item)
  115 {
  116     uint32_t slot;
  117 
  118     slot = nn_hash_key (item->key) % self->slots;
  119     nn_list_erase (&self->array [slot], &item->list);
  120     --self->items;
  121 }
  122 
  123 struct nn_hash_item *nn_hash_get (struct nn_hash *self, uint32_t key)
  124 {
  125     uint32_t slot;
  126     struct nn_list_item *it;
  127     struct nn_hash_item *item;
  128 
  129     slot = nn_hash_key (key) % self->slots;
  130 
  131     for (it = nn_list_begin (&self->array [slot]);
  132           it != nn_list_end (&self->array [slot]);
  133           it = nn_list_next (&self->array [slot], it)) {
  134         item = nn_cont (it, struct nn_hash_item, list);
  135         if (item->key == key)
  136             return item;
  137     }
  138 
  139     return NULL;
  140 }
  141 
  142 uint32_t nn_hash_key (uint32_t key)
  143 {
  144     /*  TODO: This is a randomly chosen hashing function. Give some thought
  145         to picking a more fitting one. */
  146     key = (key ^ 61) ^ (key >> 16);
  147     key += key << 3;
  148     key = key ^ (key >> 4);
  149     key = key * 0x27d4eb2d;
  150     key = key ^ (key >> 15);
  151 
  152     return key;
  153 }
  154 
  155 void nn_hash_item_init (struct nn_hash_item *self)
  156 {
  157     nn_list_item_init (&self->list);
  158 }
  159 
  160 void nn_hash_item_term (struct nn_hash_item *self)
  161 {
  162     nn_list_item_term (&self->list);
  163 }
  164