diff options
author | David Robillard <d@drobilla.net> | 2018-08-18 15:55:43 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-09-29 14:46:52 +0200 |
commit | 13bcfa8bc31510b926a1f638d071dc83253179e2 (patch) | |
tree | 81f375a63a2be60c54cadfd871aed25b142eef71 | |
parent | 9be08ad2f8be8964ae72f6571d04ba38909e4388 (diff) | |
download | chilbert-13bcfa8bc31510b926a1f638d071dc83253179e2.tar.gz chilbert-13bcfa8bc31510b926a1f638d071dc83253179e2.tar.bz2 chilbert-13bcfa8bc31510b926a1f638d071dc83253179e2.zip |
Add assertions that user provided types and sizes are sane
-rw-r--r-- | chilbert/Hilbert.ipp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/chilbert/Hilbert.ipp b/chilbert/Hilbert.ipp index b4f2b63..4cf5fec 100644 --- a/chilbert/Hilbert.ipp +++ b/chilbert/Hilbert.ipp @@ -50,8 +50,28 @@ // MUST HAVE 0 <= D0 < n #define D0 1 +#include <cassert> +#include <climits> +#include <type_traits> + namespace chilbert { +template <class T> +size_t +num_bits(const T&, std::enable_if_t<std::is_integral<T>::value>* = nullptr) +{ + return sizeof(T) * CHAR_BIT; +} + +template <class T> +size_t +num_bits(const T& vec, + std::enable_if_t<std::is_same<T, CFixBitVec>::value || + std::is_same<T, CBigBitVec>::value>* = nullptr) +{ + return vec.size(); +} + // 'Transforms' a point. template <class I> inline void @@ -181,6 +201,9 @@ coordsToIndex(const P* const p, // [in ] point H& h // [out] Hilbert index ) { + assert(num_bits(h) >= n * m); + assert(num_bits(p[0]) >= m); + if (n <= FBV_BITS) { // Intermediate variables will fit in fixed width _coordsToIndex<P, H, CFixBitVec>(p, m, n, h, CFixBitVec{}); @@ -247,6 +270,11 @@ indexToCoords(P* const p, // [out] point const H& h // [out] Hilbert index ) { + assert(m > 0); + assert(n > 0); + assert(num_bits(h) >= n * m); + assert(num_bits(p[0]) >= m); + if (n <= FBV_BITS) { // Intermediate variables will fit in fixed width _indexToCoords<P, H, CFixBitVec>(p, m, n, h, CFixBitVec{}); @@ -270,6 +298,7 @@ _coordsToCompactIndex(const P* const p, if (M == 0 || m == 0) { M = m = 0; for (size_t i = 0; i < n; i++) { + assert(num_bits(p[i]) >= ms[i]); if (ms[i] > m) { m = ms[i]; } @@ -279,6 +308,8 @@ _coordsToCompactIndex(const P* const p, const size_t mn = m * n; + assert(num_bits(hc) >= M); + // If we could avoid allocation altogether (ie: have a // fixed buffer allocated on the stack) then this increases // speed by a bit (4% when n=4, m=20) @@ -354,6 +385,9 @@ _compactIndexToCoords(P* const p, } } + assert(num_bits(hc) >= M); + assert(num_bits(p[0]) >= m); + // Initialize e.reset(); l.reset(); |