ChaiScript / ChaiScript
1
// This file is distributed under the BSD License.
2
// See "license.txt" for details.
3
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5
// http://www.chaiscript.com
6

7
#ifndef CHAISCRIPT_UTILITY_FNV1A_HPP_
8
#define CHAISCRIPT_UTILITY_FNV1A_HPP_
9

10

11
#include <cstdint>
12
#include "../chaiscript_defines.hpp"
13

14

15
namespace chaiscript
16
{
17
  namespace utility
18
  {
19
    namespace fnv1a {
20
      template<typename Itr>
21 1
        static constexpr std::uint32_t hash(Itr begin, Itr end) noexcept {
22
#ifdef __GNUC__
23
#pragma GCC diagnostic push
24
#pragma GCC diagnostic ignored "-Wsign-conversion"
25
#endif
26

27
#ifdef CHAISCRIPT_MSVC
28
#pragma warning(push)
29
#pragma warning(disable : 4307)
30
#endif
31 1
          std::uint32_t h = 0x811c9dc5;
32

33 1
          while (begin != end) {
34 1
            h = (h ^ (*begin)) * 0x01000193;
35 1
            ++begin;
36
          }
37 1
          return h;
38

39
#ifdef CHAISCRIPT_MSVC
40
#pragma warning(pop)
41
#endif
42

43
#ifdef __GNUC__
44
#pragma GCC diagnostic pop
45
#endif
46

47
        }
48

49

50
      template<size_t N>
51
        static constexpr std::uint32_t hash(const char (&str)[N]) noexcept {
52
          return hash(std::begin(str), std::end(str)-1);
53
        }
54

55 1
      static constexpr std::uint32_t hash(const std::string_view &sv) noexcept {
56 1
        return hash(sv.begin(), sv.end());
57
      }
58

59
      static inline std::uint32_t hash(const std::string &s) noexcept {
60
        return hash(s.begin(), s.end());
61
      }
62
    }
63

64
    namespace jenkins_one_at_a_time {
65
      template<typename Itr>
66
        static constexpr std::uint32_t hash(Itr begin, Itr end) noexcept {
67
          std::uint32_t hash = 0;
68

69
          while (begin != end) {
70
            hash += *begin;
71
            hash += hash << 10;
72
            hash ^= hash >> 6;
73
            ++begin;
74
          }
75

76
          hash += hash << 3;
77
          hash ^= hash >> 11;
78
          hash += hash << 15;
79
          return hash;
80
        }
81

82
      template<size_t N>
83
        static constexpr std::uint32_t hash(const char (&str)[N]) noexcept {
84
          return hash(std::begin(str), std::end(str)-1);
85
        }
86

87
      static constexpr std::uint32_t hash(const std::string_view &sv) noexcept {
88
        return hash(sv.begin(), sv.end());
89
      }
90

91
      static inline std::uint32_t hash(const std::string &s) noexcept {
92
        return hash(s.begin(), s.end());
93
      }
94
    }
95

96
    using fnv1a::hash;
97
  }
98

99
}
100

101
#endif

Read our documentation on viewing source code .

Loading