From 1171d7e5556acc4508dc7513b4656b18b5d0df13 Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Wed, 16 Nov 2016 13:04:04 +0000 Subject: [PATCH] Modified Jenkins hash to use frame_check_sequence classes. --- src/jenkins.h | 191 +++++++++++++++++++++++++++----------------------- 1 file changed, 105 insertions(+), 86 deletions(-) diff --git a/src/jenkins.h b/src/jenkins.h index 7dace164..0c500e3c 100644 --- a/src/jenkins.h +++ b/src/jenkins.h @@ -50,24 +50,96 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// Calculates the jenkins hash. - ///\ingroup jenkins + /// Jenkins32 policy. + /// Calculates 32 bit Jenkins hash. //*************************************************************************** - template - class jenkins + struct jenkins32_policy + { + typedef uint32_t value_type; + + inline uint32_t initial() const + { + is_finalised = false; + + return 0; + } + + inline uint32_t add(uint8_t hash, uint8_t value) const + { + ETL_ASSERT(!is_finalised, ETL_ERROR(hash_finalised)); + + hash += value; + hash += (hash << 10); + hash ^= (hash >> 6); + + return hash; + } + + inline uint32_t final(uint8_t hash) const + { + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + is_finalised = true; + + return hash; + } + + bool is_finalised; + }; + + //*************************************************************************** + /// Jenkins64 policy. + /// Calculates 32 bit Jenkins hash. + //*************************************************************************** + struct jenkins64_policy + { + typedef uint64_t value_type; + + inline uint64_t initial() const + { + is_finalised = false; + + return 0; + } + + inline uint64_t add(uint8_t hash, uint8_t value) const + { + ETL_ASSERT(!is_finalised, ETL_ERROR(hash_finalised)); + + hash += value; + hash += (hash << 10); + hash ^= (hash >> 6); + + return hash; + } + + inline uint64_t final(uint8_t hash) const + { + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + is_finalised = true; + + return hash; + } + + bool is_finalised; + }; + + //************************************************************************* + /// Jenkins32 + //************************************************************************* + class jenkins32 : public etl::frame_check_sequence { public: - STATIC_ASSERT((etl::is_same::value || etl::is_same::value), "Only 32 & 64 bit types supported"); - - typedef THash value_type; - //************************************************************************* /// Default constructor. //************************************************************************* - jenkins() + jenkins32() { - reset(); + this->reset(); } //************************************************************************* @@ -76,92 +148,39 @@ namespace etl /// \param end End of the range. //************************************************************************* template - jenkins(TIterator begin, const TIterator end) + jenkins32(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Incompatible type"); + this->reset(); + this->add(begin, end); + } + }; - reset(); + //************************************************************************* + /// Jenkins64 + //************************************************************************* + class jenkins64 : public etl::frame_check_sequence + { + public: - while (begin != end) - { - hash += *begin++; - hash += (hash << 10); - hash ^= (hash >> 6); - } + //************************************************************************* + /// Default constructor. + //************************************************************************* + jenkins64() + { + this->reset(); } //************************************************************************* - /// Resets the CRC to the initial state. - //************************************************************************* - void reset() - { - hash = 0; - is_finalised = false; - } - - //************************************************************************* - /// Adds a range. - /// \param begin - /// \param end + /// Constructor from range. + /// \param begin Start of the range. + /// \param end End of the range. //************************************************************************* template - void add(TIterator begin, const TIterator end) + jenkins64(TIterator begin, const TIterator end) { - STATIC_ASSERT(sizeof(typename std::iterator_traits::value_type) == 1, "Incompatible type"); - ETL_ASSERT(!is_finalised, ETL_ERROR(hash_finalised)); - - while (begin != end) - { - hash += *begin++; - hash += (hash << 10); - hash ^= (hash >> 6); - } + this->reset(); + this->add(begin, end); } - - //************************************************************************* - /// \param value The char to add to the jenkins. - //************************************************************************* - void add(uint8_t value) - { - ETL_ASSERT(!is_finalised, ETL_ERROR(hash_finalised)); - - hash += value; - hash += (hash << 10); - hash ^= (hash >> 6); - } - - //************************************************************************* - /// Gets the jenkins value. - //************************************************************************* - value_type value() - { - finalise(); - return hash; - } - - //************************************************************************* - /// Conversion operator to value_type. - //************************************************************************* - operator value_type () - { - return value(); - } - - private: - - void finalise() - { - if (!is_finalised) - { - hash += (hash << 3); - hash ^= (hash >> 11); - hash += (hash << 15); - is_finalised = true; - } - } - - value_type hash; - bool is_finalised; }; }