git.iko.soy/rowtype-yoga/purescript-native
on 24fdff2 help sign in
42 lines 1.0 kB history raw
1///////////////////////////////////////////////////////////////////////////////
2//
3// Module : hash.hh
4// Copyright : (c) Andy Arvanitis 2015
5// License : MIT
6//
7// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8// Stability : experimental
9// Portability :
10//
11// Provides compile-time hashes for string literals.
12//
13///////////////////////////////////////////////////////////////////////////////
14//
15#ifndef PS_Hash_HH
16#define PS_Hash_HH
18#include <cstdint>
20namespace PureScript {
22 // Hash function which uses an xor version of the djb2 algorithm
23 //
24 constexpr auto djb2(const char s[], const uint32_t hash = 5381) -> uint32_t {
25 return !s[0] ? hash : djb2(s + 1, 33 * hash ^ s[0]);
26 }
28 // Compile-time string literal hash
29 //
30 template <uint32_t H>
31 struct _compile_time_hash_ {
32 static constexpr long value = H;
33 };
35 template <uint32_t H>
36 constexpr long _compile_time_hash_<H>::value;
38 #define HASH(s) _compile_time_hash_<djb2(s)>::value
40} // namespace PureScript
42#endif // PS_Hash_HH