FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hash_fnv.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 namespace cpp {
22 
27 template<unsigned N>
29 { };
30 
35 template<>
37 {
38  typedef std::uint32_t uint_type;
39  static const uint_type offsetBasis = 2166136261U;
40  static const uint_type prime = 16777619U;
41 };
42 
47 template<>
49 {
50  typedef std::uint64_t uint_type;
51  static const uint_type offsetBasis = 14695981039346656037ULL;
52  static const uint_type prime = 1099511628211ULL;
53 };
54 
61 template<unsigned SIZE = sizeof(std::size_t)*8> // Target arch bit size by default
63 {
64  static_assert(SIZE == 32 || SIZE == 64, "Bit size must be 32 or 64");
65 
66 private:
67  typedef hash_fnv_1a_traits<SIZE> traits_t;
68 
69 public:
70  typedef typename traits_t::uint_type uint_type;
71 
73  template<typename BYTE>
74  uint_type operator()(const BYTE* byteSeq, std::size_t len) const
75  {
76  auto hash = traits_t::offsetBasis;
77  for (std::size_t i = 0; i < len; ++i)
78  hashStep(hash, *(byteSeq + i));
79  return hash;
80  }
81 
83  template<typename BYTE>
84  uint_type operator()(const BYTE* byteSeq) const
85  {
86  auto hash = traits_t::offsetBasis;
87  for (; *byteSeq != 0; ++byteSeq)
88  hashStep(hash, *byteSeq);
89  return hash;
90  }
91 
93  template<typename ITERATOR>
94  uint_type operator()(ITERATOR begin, ITERATOR end) const
95  {
96  auto hash = traits_t::offsetBasis;
97  for (; begin != end; ++begin)
98  hashStep(hash, *begin);
99  return hash;
100  }
101 
102 private:
103  template<typename BYTE>
104  static inline void hashStep(uint_type& hash, BYTE byte)
105  {
106  static_assert(sizeof(BYTE) == 1, "valid BYTE type");
107  hash ^= static_cast<uint_type>(byte); // XOR on the low order byte of hash
108  hash *= traits_t::prime;
109  }
110 };
111 
114 
115 } // namespace cpp
std::uint32_t uint_type
Definition: hash_fnv.h:38
Definition: basic_shared_pointer.h:20
Definition: hash_fnv.h:28
hash_fnv_1a< 64 > hash64_fnv_1a
Definition: hash_fnv.h:113
hash_fnv_1a< 32 > hash32_fnv_1a
Definition: hash_fnv.h:112
std::uint64_t uint_type
Definition: hash_fnv.h:50
Definition: hash_fnv.h:62