Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wellbelove committed Sep 28, 2015
2 parents 723962d + 7367703 commit 8d8a802
Show file tree
Hide file tree
Showing 66 changed files with 299,474 additions and 1,214 deletions.
16 changes: 7 additions & 9 deletions array.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ SOFTWARE.
#include "type_traits.h"
#include "parameter_type.h"
#include "static_assert.h"

#ifndef ETL_THROW_EXCEPTIONS
#include "error_handler.h"
#endif

///\defgroup array array
/// A replacement for std::array if you haven't got C++0x11.
Expand Down Expand Up @@ -82,7 +79,7 @@ namespace etl
///\ingroup array
/// A replacement for std::array if you haven't got C++0x11.
//***************************************************************************
template <typename T, const size_t SIZE>
template <typename T, const size_t SIZE_>
class array
{
private:
Expand All @@ -91,6 +88,11 @@ namespace etl

public:

enum
{
SIZE = SIZE_
};

typedef T value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
Expand Down Expand Up @@ -138,11 +140,7 @@ namespace etl
{
if (i >= SIZE)
{
#ifdef ETL_THROW_EXCEPTIONS
throw array_out_of_range();
#else
error_handler::error(array_out_of_range());
#endif
ETL_ERROR(array_out_of_range());
}

return _buffer[i];
Expand Down
74 changes: 74 additions & 0 deletions binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,56 @@ SOFTWARE.
#include "type_traits.h"
#include "integral_limits.h"
#include "static_assert.h"
#include "log.h"
#include "power.h"
#include "smallest.h"

namespace etl
{
//***************************************************************************
/// Maximum value that can be contained in N bits.
//***************************************************************************
namespace __private_binary__
{
/// Helper definition for non-zero NBITS.
template <const size_t NBITS>
struct max_value_for_nbits_helper
{
typedef typename etl::smallest_uint_for_bits<NBITS>::type value_type;
static const value_type value = (uint64_t(1) << (NBITS - 1)) | max_value_for_nbits_helper<NBITS - 1>::value;
};

/// Specialisation for when NBITS == 0.
template <>
struct max_value_for_nbits_helper<0>
{
typedef etl::smallest_uint_for_bits<0>::type value_type;
static const value_type value = 1;
};

template <const size_t NBITS>
const typename max_value_for_nbits_helper<NBITS>::value_type max_value_for_nbits_helper<NBITS>::value;
}

/// Definition for non-zero NBITS.
template <const size_t NBITS>
struct max_value_for_nbits
{
typedef typename etl::smallest_uint_for_bits<NBITS>::type value_type;
static const value_type value = __private_binary__::max_value_for_nbits_helper<NBITS>::value;
};

/// Specialisation for when NBITS == 0.
template <>
struct max_value_for_nbits<0>
{
typedef etl::smallest_uint_for_bits<0>::type value_type;
static const value_type value = 0;
};

template <const size_t NBITS>
const typename max_value_for_nbits<NBITS>::value_type max_value_for_nbits<NBITS>::value;

//***************************************************************************
/// Rotate left.
//***************************************************************************
Expand Down Expand Up @@ -421,6 +468,33 @@ namespace etl
return (0x69966996 >> value) & 1;
}

//***************************************************************************
/// Fold a binary number down to a set number of bits using XOR.
//***************************************************************************
template <typename TReturn, const size_t NBITS, typename TValue>
TReturn fold_bits(TValue value)
{
STATIC_ASSERT(integral_limits<TReturn>::bits >= NBITS, "Return type too small to hold result");

const TValue mask = etl::power<2, NBITS>::value - 1;
const size_t shift = NBITS;

// Fold the value down to fit the width.
TReturn folded_value = 0;

// Keep shifting down and XORing the lower bits.
while (value >= etl::max_value_for_nbits<NBITS>::value)
{
folded_value ^= value & mask;
value >>= shift;
}

// Fold the remaining bits.
folded_value ^= value;

return folded_value;
}

//***************************************************************************
/// 8 bit binary constants.
//***************************************************************************
Expand Down
19 changes: 2 additions & 17 deletions bloom_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SOFTWARE.
#include "parameter_type.h"
#include "bitset.h"
#include "type_traits.h"
#include "binary.h"
#include "log.h"
#include "power.h"

Expand Down Expand Up @@ -172,26 +173,10 @@ namespace etl
template <typename THash>
size_t get_hash(parameter_t key) const
{
const size_t mask = etl::power_of_2_round_up<WIDTH>::value - 1;

size_t hash = THash()(key);

// Fold the hash down to fit the width.
size_t folded_hash = 0;

const size_t shift = etl::log2<etl::power_of_2_round_up<WIDTH>::value>::value;

// Keep shifting down and XORing the lower bits.
while (hash >= WIDTH)
{
folded_hash ^= hash & mask;
hash >>= shift;
}

// Fold the remaining bits.
folded_hash ^= hash;

return folded_hash;
return fold_bits<size_t, etl::log2<WIDTH>::value>(hash);
}

/// The Bloom filter flags.
Expand Down
138 changes: 138 additions & 0 deletions bsd_checksum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
///\file

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
Copyright(c) 2014 jwellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef __ETL_BSDCHECKSUM__
#define __ETL_BSDCHECKSUM__

#include <stdint.h>

#include "static_assert.h"
#include "type_traits.h"
#include "binary.h"
#include "ihash.h"

///\defgroup bsdchecksum BSD Checksum calculation
///\ingroup maths

namespace etl
{
//***************************************************************************
/// Calculates the checksum.
///\tparam TSum The type used for the sum.
///\ingroup checksum
//***************************************************************************
template <typename TSum>
class bsd_checksum
{
public:

STATIC_ASSERT(is_unsigned<TSum>::value, "Signed TSum template parameter not supported");

typedef TSum value_type;

//*************************************************************************
/// Default constructor.
//*************************************************************************
bsd_checksum()
{
reset();
}

//*************************************************************************
/// Constructor from range.
/// \param begin Start of the range.
/// \param end End of the range.
//*************************************************************************
template<typename TIterator>
bsd_checksum(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Only 8 bit types supported");
reset();

while (begin != end)
{
sum = rotate_right(sum) + *begin++;
}
}

//*************************************************************************
/// Resets the CRC to the initial state.
//*************************************************************************
void reset()
{
sum = 0;
}

//*************************************************************************
/// Adds a range.
/// \param begin
/// \param end
//*************************************************************************
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Only 8 bit types supported");

while (begin != end)
{
sum = rotate_right(sum) + *begin++;
}
}

//*************************************************************************
/// \param value The uint8_t to add to the checksum.
//*************************************************************************
void add(uint8_t value)
{
sum = rotate_right(sum) + value;
}

//*************************************************************************
/// Gets the checksum value.
//*************************************************************************
value_type value()
{
return sum;
}

//*************************************************************************
/// Conversion operator to value_type.
//*************************************************************************
operator value_type ()
{
return sum;
}

private:

value_type sum;
};
}

#endif
38 changes: 13 additions & 25 deletions checksum.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ SOFTWARE.

#include "static_assert.h"
#include "type_traits.h"
#include "endian.h"
#include "ihash.h"

///\defgroup checksum Checksum calculation
Expand All @@ -45,11 +44,10 @@ namespace etl
//***************************************************************************
/// Calculates the checksum.
///\tparam TSum The type used for the sum.
///\tparam ENDIANNESS The endianness of the calculation for input types larger than uint8_t. Default = endian::little.
///\ingroup checksum
//***************************************************************************
template <typename TSum, const int ENDIANNESS = endian::little>
class checksum : public etl::ihash
template <typename TSum>
class checksum
{
public:

Expand All @@ -61,7 +59,6 @@ namespace etl
/// Default constructor.
//*************************************************************************
checksum()
: ihash(etl::endian(ENDIANNESS))
{
reset();
}
Expand All @@ -73,10 +70,14 @@ namespace etl
//*************************************************************************
template<typename TIterator>
checksum(TIterator begin, const TIterator end)
: ihash(etl::endian(ENDIANNESS))
{
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");

reset();
add(begin, end);
while (begin != end)
{
sum += *begin++;
}
}

//*************************************************************************
Expand All @@ -95,17 +96,12 @@ namespace etl
template<typename TIterator>
void add(TIterator begin, const TIterator end)
{
ihash::add(begin, end);
}
STATIC_ASSERT(sizeof(typename std::iterator_traits<TIterator>::value_type) == 1, "Type not supported");

//*************************************************************************
/// Adds a value.
/// \param value The value to add to the checksum.
//*************************************************************************
template<typename TValue>
void add(TValue value)
{
ihash::add(value);
while (begin != end)
{
sum += *begin++;
}
}

//*************************************************************************
Expand All @@ -132,14 +128,6 @@ namespace etl
return sum;
}

//*************************************************************************
/// Gets the generic digest value.
//*************************************************************************
generic_digest digest() const
{
return ihash::get_digest(sum);
}

private:

value_type sum;
Expand Down
Loading

0 comments on commit 8d8a802

Please sign in to comment.