From 13bcfa8bc31510b926a1f638d071dc83253179e2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 18 Aug 2018 15:55:43 +0200 Subject: Add assertions that user provided types and sizes are sane --- chilbert/Hilbert.ipp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'chilbert') 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 +#include +#include + namespace chilbert { +template +size_t +num_bits(const T&, std::enable_if_t::value>* = nullptr) +{ + return sizeof(T) * CHAR_BIT; +} + +template +size_t +num_bits(const T& vec, + std::enable_if_t::value || + std::is_same::value>* = nullptr) +{ + return vec.size(); +} + // 'Transforms' a point. template 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, 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, 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(); -- cgit v1.2.1