nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

cmetrohash.h (2384B)


      1 // metrohash.h
      2 //
      3 // The MIT License (MIT)
      4 //
      5 // Copyright (c) 2015 J. Andrew Rogers
      6 //
      7 // Updated Nov. 2015 to use safe unaligned reads and platform neutral
      8 // hash. This WILL change hashes on big endian platfors. / mikkelfj
      9 //
     10 // Permission is hereby granted, free of charge, to any person obtaining a copy
     11 // of this software and associated documentation files (the "Software"), to deal
     12 // in the Software without restriction, including without limitation the rights
     13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     14 // copies of the Software, and to permit persons to whom the Software is
     15 // furnished to do so, subject to the following conditions:
     16 //
     17 // The above copyright notice and this permission notice shall be included in all
     18 // copies or substantial portions of the Software.
     19 //
     20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     26 // SOFTWARE.
     27 //
     28 
     29 #ifndef CMETROHASH_METROHASH_H
     30 #define CMETROHASH_METROHASH_H
     31 
     32 #include "ht_portable.h"
     33 #include "unaligned.h"
     34 
     35 #pragma once
     36 
     37 #if defined (__cplusplus)
     38 extern "C" {
     39 #endif
     40 
     41 #include <stdint.h>
     42 #include <string.h>
     43 
     44 // MetroHash 64-bit hash functions
     45 void cmetrohash64_1(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out);
     46 void cmetrohash64_2(const uint8_t * key, uint64_t len, uint32_t seed, uint8_t * out);
     47 
     48 
     49 /* rotate right idiom recognized by compiler*/
     50 inline static uint64_t crotate_right(uint64_t v, unsigned k)
     51 {
     52     return (v >> k) | (v << (64 - k));
     53 }
     54 
     55 inline static uint64_t cread_u64(const void * const ptr)
     56 {
     57     return (uint64_t)unaligned_read_le64toh(ptr);
     58 }
     59 
     60 inline static uint64_t cread_u32(const void * const ptr)
     61 {
     62     return (uint64_t)unaligned_read_le32toh(ptr);
     63 }
     64 
     65 inline static uint64_t cread_u16(const void * const ptr)
     66 {
     67     return (uint64_t)unaligned_read_le16toh(ptr);
     68 }
     69 
     70 inline static uint64_t cread_u8 (const void * const ptr)
     71 {
     72     return * (uint8_t *) ptr;
     73 }
     74 
     75 #if defined (__cplusplus)
     76 }
     77 #endif
     78 #endif // #ifndef CMETROHASH_METROHASH_H