From 0bcaa46d0fb0af324fc2991ddcdf3d34c594f741 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 7 Aug 2018 17:28:27 +0200 Subject: Raise responsibility of allocating scratch Use copy constructors instead of size-based initialization in deeper methods instead. This avoids the assumption that the types take a dynamic size parameter. --- chilbert/Algorithm.hpp | 52 ++++++++++++++++++++++++++++++++++---------------- chilbert/FixBitVec.hpp | 2 +- test/test_fsb.cpp | 10 +++++----- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/chilbert/Algorithm.hpp b/chilbert/Algorithm.hpp index cee638a..bd4a9a1 100644 --- a/chilbert/Algorithm.hpp +++ b/chilbert/Algorithm.hpp @@ -149,10 +149,14 @@ namespace chilbert int m, int n, H &h, + I&& scratch, int *ds = nullptr // #HACK ) { - I e(n), l(n), t(n), w(n); + I e{std::move(scratch)}; + I l{e}; + I t{e}; + I w{e}; int d, i; int ho = m*n; @@ -211,10 +215,10 @@ namespace chilbert { // Intermediate variables will fit in fixed width? if ( n <= FBV_BITS ) - _coordsToIndex(p,m,n,h); + _coordsToIndex(p,m,n,h, CFixBitVec{}); // Otherwise, they must be BigBitVecs. else - _coordsToIndex(p,m,n,h); + _coordsToIndex(p,m,n,h, CBigBitVec(n)); return; } @@ -227,10 +231,14 @@ namespace chilbert P *p, int m, int n, - const H &h + const H &h, + I&& scratch ) { - I e(n), l(n), t(n), w(n); + I e{std::move(scratch)}; + I l{e}; + I t{e}; + I w{e}; int d, i, j, ho; // Initialize @@ -288,10 +296,10 @@ namespace chilbert { // Intermediate variables will fit in fixed width? if ( n <= FBV_BITS ) - _indexToCoords(p,m,n,h); + _indexToCoords(p,m,n,h,CFixBitVec{}); // Otherwise, they must be BigBitVecs. else - _indexToCoords(p,m,n,h); + _indexToCoords(p,m,n,h,CBigBitVec(n)); return; } @@ -304,6 +312,7 @@ namespace chilbert const int *ms, int n, HC &hc, + I&& scratch, int M = 0, int m = 0 ) @@ -333,13 +342,13 @@ namespace chilbert if ( mn > FBV_BITS ) { CBigBitVec h(mn); - _coordsToIndex(p,m,n,h,ds); + _coordsToIndex(p,m,n,h,std::move(scratch),ds); compactIndex(ms,ds,n,m,h,hc); } else { CFixBitVec h; - _coordsToIndex(p,m,n,h,ds); + _coordsToIndex(p,m,n,h,std::move(scratch),ds); compactIndex(ms,ds,n,m,h,hc); } @@ -368,10 +377,10 @@ namespace chilbert { // Intermediate variables will fit in fixed width? if ( n <= FBV_BITS ) - _coordsToCompactIndex(p,ms,n,hc,M,m); + _coordsToCompactIndex(p,ms,n,hc,CFixBitVec{},M,m); // Otherwise, they must be BigBitVecs. else - _coordsToCompactIndex(p,ms,n,hc,M,m); + _coordsToCompactIndex(p,ms,n,hc,CBigBitVec(n),M,m); return; } @@ -384,11 +393,19 @@ namespace chilbert const int *ms, int n, const HC &hc, + I&& scratch, int M = 0, int m = 0 ) { - I e(n), l(n), t(n), w(n), r(n), mask(n), ptrn(n); + I e{std::move(scratch)}; + I l{e}; + I t{e}; + I w{e}; + I r{e}; + I mask{e}; + I ptrn{e}; + int d, i, j, b; // Get total precision and max precision @@ -463,11 +480,14 @@ namespace chilbert ) { // Intermediate variables will fit in fixed width? - if ( n <= FBV_BITS ) - _compactIndexToCoords(p,ms,n,hc,M,m); + if ( n <= FBV_BITS ) { + CFixBitVec scratch; + _compactIndexToCoords(p,ms,n,hc,CFixBitVec{},M,m); // Otherwise, they must be BigBitVecs. - else - _compactIndexToCoords(p,ms,n,hc,M,m); + } else { + CBigBitVec scratch(n); + _compactIndexToCoords(p,ms,n,hc,std::move(scratch),M,m); + } return; } diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp index bea1c1a..d8fee9a 100644 --- a/chilbert/FixBitVec.hpp +++ b/chilbert/FixBitVec.hpp @@ -62,7 +62,7 @@ public: // Returns the current size in bits. int - size() + size() const { return FBV_BITS; } diff --git a/test/test_fsb.cpp b/test/test_fsb.cpp index 473438f..c55d9cb 100644 --- a/test/test_fsb.cpp +++ b/test/test_fsb.cpp @@ -26,11 +26,11 @@ using namespace chilbert; template int -test_fsb(const int size) +test_fsb(const T& empty_field) { - for (int i = 0; i < size; ++i) { - T field{size}; - field.reset(); + assert(empty_field.none()); + for (int i = 0; i < empty_field.size(); ++i) { + T field = empty_field; field.set(i); assert(field.fsb() == i + 1); } @@ -41,5 +41,5 @@ test_fsb(const int size) int main() { - return test_fsb(FBV_BITS) || test_fsb(1024); + return test_fsb(CFixBitVec{}) || test_fsb(CBigBitVec{1024}); } -- cgit v1.2.1