From cca87d2d295ef10f8f03cc1c3de2d5e943e27588 Mon Sep 17 00:00:00 2001 From: Berenger Bramas <Berenger.Bramas@inria.fr> Date: Tue, 5 May 2015 11:36:28 +0200 Subject: [PATCH] Update bool array --- Src/Containers/FBoolArray.hpp | 72 +++++++++++++++++----- UTests/utestBoolArray.cpp | 110 ++++++++++++++++++---------------- 2 files changed, 113 insertions(+), 69 deletions(-) diff --git a/Src/Containers/FBoolArray.hpp b/Src/Containers/FBoolArray.hpp index 0250f07fa..1dd9168f6 100644 --- a/Src/Containers/FBoolArray.hpp +++ b/Src/Containers/FBoolArray.hpp @@ -16,7 +16,8 @@ #ifndef FBOOLARRAY_HPP #define FBOOLARRAY_HPP - +#include "../Utils/FGlobal.hpp" +#include "../Utils/FAssert.hpp" // To get memcpy #include <cstring> @@ -31,29 +32,29 @@ */ class FBoolArray{ /** Size of a unsigned long */ - const static int BytesInBlock = sizeof(unsigned long); - const static int SizeOfBlock = BytesInBlock * 8; + const static FSize BytesInBlock = sizeof(unsigned long); + const static FSize SizeOfBlock = BytesInBlock * 8; /** The array to store bits */ - unsigned long* const array; + unsigned long* array; /** Size of the memory allocated */ - const int memSize; + FSize memSize; /** Size of the array => number of real elements */ - const int size; + FSize size; /** get size to number of long */ - int LongFromSize(const int inSize){ + FSize LongFromSize(const FSize inSize){ return ((inSize + SizeOfBlock - 1) / SizeOfBlock); } /** Alloc an array */ - unsigned long * AllocArray(const int inSize){ + unsigned long * AllocArray(const FSize inSize){ return new unsigned long[LongFromSize(inSize)]; } public : /** Constructor with size */ - FBoolArray(const int inSize) : array(AllocArray(inSize)), memSize(LongFromSize(inSize)*BytesInBlock), size(inSize) { + explicit FBoolArray(const FSize inSize = 0) : array(AllocArray(inSize)), memSize(LongFromSize(inSize)*BytesInBlock), size(inSize) { setToZeros(); } @@ -62,6 +63,25 @@ public : *this = other; } + /** Move the data */ + FBoolArray(FBoolArray&& other): array(nullptr), memSize(0), size(0){ + array = other.array; + memSize = other.memSize; + size = other.size; + other.array = nullptr; + other.memSize = 0; + other.size = 0; + } + + /** remove all values and allocate new array */ + void reset(const FSize inSize){ + delete [] array; + array = (AllocArray(inSize)); + memSize = (LongFromSize(inSize)*BytesInBlock); + size = (inSize); + setToZeros(); + } + /** Destructor */ ~FBoolArray(){ delete [] array; @@ -72,10 +92,25 @@ public : * Array must have the same size */ FBoolArray& operator=(const FBoolArray& other){ + FAssertLF(size == other.size); memcpy(array, other.array, memSize); return *this; } + /** + * Move the data from one array to the other + */ + FBoolArray& operator=(FBoolArray&& other){ + delete [] array; + array = other.array; + memSize = other.memSize; + size = other.size; + other.array = nullptr; + other.memSize = 0; + other.size = 0; + return *this; + } + /** * Operator == * Array must have the same size @@ -93,22 +128,22 @@ public : } /** To get a value */ - bool get(const int inPos) const { - const int posInArray = inPos / SizeOfBlock; - const int bytePosition = inPos - (posInArray * 8); + bool get(const FSize inPos) const { + const FSize posInArray = inPos / SizeOfBlock; + const FSize bytePosition = inPos - (posInArray * 8); return (array[posInArray] >> bytePosition) & 1; } /** To set a value */ - void set(const int inPos, const bool inVal){ - const int posInArray = inPos / SizeOfBlock; - const int bytePosition = inPos - (posInArray * 8); + void set(const FSize inPos, const bool inVal){ + const FSize posInArray = inPos / SizeOfBlock; + const FSize bytePosition = inPos - (posInArray * 8); if(inVal) array[posInArray] |= (1UL << bytePosition); else array[posInArray] &= ~(1UL << bytePosition); } /** To get the size of the array */ - int getSize() const { + FSize getSize() const { return size; } @@ -116,6 +151,11 @@ public : void setToZeros() const { memset( array, 0, memSize); } + + /** Set all the memory to 1 */ + void setToOnes() const { + memset( array, (unsigned char)0xFF, memSize); + } }; diff --git a/UTests/utestBoolArray.cpp b/UTests/utestBoolArray.cpp index abf7873ae..7f830df22 100644 --- a/UTests/utestBoolArray.cpp +++ b/UTests/utestBoolArray.cpp @@ -26,66 +26,70 @@ /** this class test the bool array container */ class TestArray : public FUTester<TestArray> { - void TestGetSet(){ - FBoolArray array(500); - for(int idx = 0 ; idx < 500 ; ++idx){ - uassert(!array.get(idx)); - } - - for(int idx = 0 ; idx < 500 ; ++idx){ - array.set(idx, true); - uassert(array.get(idx)); - array.set(idx, false); - uassert(!array.get(idx)); - } - - for(int idx = 0 ; idx < 500 ; ++idx){ - array.set(idx, true); - } - array.setToZeros(); - for(int idx = 0 ; idx < 500 ; ++idx){ - uassert(!array.get(idx)); - } - } - - void TestGetSet2(){ - FBoolArray array(100); - - for(int idx = 0 ; idx < 100 ; ++idx){ - if(idx%3){ - array.set(idx, true); - uassert(array.get(idx)); - } - else{ - uassert(!array.get(idx)); - } - } + void TestGetSet(){ + FBoolArray array(500); + for(int idx = 0 ; idx < 500 ; ++idx){ + uassert(!array.get(idx)); } - void TestEqual(){ - FBoolArray array1(10); - FBoolArray array2(10); + for(int idx = 0 ; idx < 500 ; ++idx){ + array.set(idx, true); + uassert(array.get(idx)); + array.set(idx, false); + uassert(!array.get(idx)); + } + for(int idx = 0 ; idx < 500 ; ++idx){ + array.set(idx, true); + } + array.setToZeros(); + for(int idx = 0 ; idx < 500 ; ++idx){ + uassert(!array.get(idx)); + } + array.setToOnes(); + for(int idx = 0 ; idx < 500 ; ++idx){ + uassert(array.get(idx)); + } + } + + void TestGetSet2(){ + FBoolArray array(100); + + for(int idx = 0 ; idx < 100 ; ++idx){ + if(idx%3){ + array.set(idx, true); + uassert(array.get(idx)); + } + else{ + uassert(!array.get(idx)); + } + } + } - uassert(array1 == array2); + void TestEqual(){ + FBoolArray array1(10); + FBoolArray array2(10); - array1.set(1, true); - uassert(array1 != array2); - array2.set(1, true); - uassert(array1 == array2); + uassert(array1 == array2); - array1.set(5, true); - array2 = array1; - uassert(array1 == array2); - } - - // set test - void SetTests(){ - AddTest(&TestArray::TestGetSet,"Test Get & Set"); - AddTest(&TestArray::TestGetSet2,"Test Get & Set 2"); - AddTest(&TestArray::TestEqual,"Test Equal"); - } + array1.set(1, true); + uassert(array1 != array2); + + array2.set(1, true); + uassert(array1 == array2); + + array1.set(5, true); + array2 = array1; + uassert(array1 == array2); + } + + // set test + void SetTests(){ + AddTest(&TestArray::TestGetSet,"Test Get & Set"); + AddTest(&TestArray::TestGetSet2,"Test Get & Set 2"); + AddTest(&TestArray::TestEqual,"Test Equal"); + } }; // You must do this -- GitLab