Embedded Template Library  1.0
pearson.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_PEARSON_INCLUDED
32 #define ETL_PEARSON_INCLUDED
33 
34 #include <stdint.h>
35 
36 #include "platform.h"
37 #include "static_assert.h"
38 #include "type_traits.h"
39 #include "ihash.h"
40 #include "array.h"
41 #include "container.h"
42 
43 ETL_STATIC_ASSERT(ETL_8BIT_SUPPORT, "This file does not currently support targets with no 8bit type");
44 
45 #if defined(ETL_COMPILER_KEIL)
46 #pragma diag_suppress 1300
47 #endif
48 
51 
52 namespace etl
53 {
54  //***************************************************************************
58  //***************************************************************************
59  template <const size_t HASH_LENGTH>
60  class pearson
61  {
62  public:
63 
65 
66  //*************************************************************************
68  //*************************************************************************
70  : first(true)
71  {
72  reset();
73  }
74 
75  //*************************************************************************
79  //*************************************************************************
80  template<typename TIterator>
81  pearson(TIterator begin, const TIterator end)
82  : first(true)
83  {
84  ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
85 
86  reset();
87  add(begin, end);
88  }
89 
90  //*************************************************************************
92  //*************************************************************************
93  void reset()
94  {
95  hash.fill(0);
96  }
97 
98  //*************************************************************************
102  //*************************************************************************
103  template<typename TIterator>
104  void add(TIterator begin, const TIterator end)
105  {
106  ETL_STATIC_ASSERT(sizeof(typename etl::iterator_traits<TIterator>::value_type) == 1, "Type not supported");
107 
108  while (begin != end)
109  {
110  add(*begin++);
111  }
112  }
113 
114  //*************************************************************************
116  //*************************************************************************
117  void add(uint8_t value_)
118  {
119  static const uint8_t PEARSON_LOOKUP[] =
120  {
121  228, 39, 61, 95, 227, 187, 0, 197, 31, 189, 161, 222, 34, 15, 221, 246,
122  19, 234, 6, 50, 113, 3, 91, 63, 77, 245, 144, 2, 183, 196, 25, 226,
123  97, 126, 48, 59, 217, 4, 100, 145, 12, 88, 203, 149, 80, 154, 38, 27,
124  224, 218, 158, 115, 202, 79, 53, 83, 242, 36, 139, 131, 136, 191, 42, 170,
125  23, 99, 156, 51, 143, 60, 233, 206, 62, 108, 17, 67, 81, 71, 93, 195,
126  26, 231, 247, 96, 24, 200, 176, 209, 152, 212, 138, 165, 75, 185, 130, 248,
127  125, 110, 10, 116, 201, 90, 69, 204, 85, 251, 78, 157, 47, 184, 169, 141,
128  134, 230, 89, 21, 146, 46, 55, 128, 148, 207, 216, 11, 114, 199, 103, 102,
129  166, 244, 5, 104, 225, 160, 132, 28, 172, 65, 121, 140, 153, 119, 198, 210,
130  58, 87, 117, 177, 33, 22, 13, 37, 49, 174, 109, 40, 73, 211, 18, 167,
131  164, 252, 168, 74, 30, 173, 35, 98, 66, 193, 94, 175, 86, 54, 179, 122,
132  220, 151, 192, 29, 133, 254, 155, 127, 240, 232, 190, 180, 8, 68, 236, 20,
133  137, 92, 219, 208, 52, 250, 147, 142, 111, 112, 120, 45, 135, 255, 123, 229,
134  57, 182, 243, 124, 186, 253, 7, 237, 9, 16, 70, 171, 235, 107, 223, 118,
135  215, 178, 194, 181, 43, 188, 106, 105, 64, 241, 84, 238, 159, 44, 32, 76,
136  213, 163, 150, 101, 129, 14, 249, 205, 214, 1, 41, 56, 162, 72, 239, 82
137  };
138 
139  if (first)
140  {
141  for (size_t i = 0; i < HASH_LENGTH; ++i)
142  {
143  hash[i] = PEARSON_LOOKUP[(uint32_t(value_) + i) % 256];
144  }
145 
146  first = false;
147  }
148  else
149  {
150  for (size_t i = 0; i < HASH_LENGTH; ++i)
151  {
152  hash[i] = PEARSON_LOOKUP[hash[i] ^ value_];
153  }
154  }
155  }
156 
157  //*************************************************************************
159  //*************************************************************************
161  {
162  return hash;
163  }
164 
165  //*************************************************************************
167  //*************************************************************************
168  operator value_type () const
169  {
170  return value();
171  }
172 
173  private:
174 
175  bool first;
176  value_type hash;
177  };
178 }
179 
180 #endif
ETL_CONSTEXPR TContainer::iterator begin(TContainer &container)
Definition: container.h:49
ETL_CONSTEXPR TContainer::iterator end(TContainer &container)
Definition: container.h:99
Definition: hash.h:93
void add(uint8_t value_)
Definition: pearson.h:117
void reset()
Resets the hash to the initial state.
Definition: pearson.h:93
value_type value() const
Gets the hash value.
Definition: pearson.h:160
void add(TIterator begin, const TIterator end)
Definition: pearson.h:104
pearson(TIterator begin, const TIterator end)
Definition: pearson.h:81
pearson()
Default constructor.
Definition: pearson.h:69
Definition: pearson.h:61
Definition: absolute.h:37