Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

Commit cca87d2d authored by BRAMAS Berenger's avatar BRAMAS Berenger

Update bool array

parent bc5a1b38
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
#ifndef FBOOLARRAY_HPP #ifndef FBOOLARRAY_HPP
#define FBOOLARRAY_HPP #define FBOOLARRAY_HPP
#include "../Utils/FGlobal.hpp"
#include "../Utils/FAssert.hpp"
// To get memcpy // To get memcpy
#include <cstring> #include <cstring>
...@@ -31,29 +32,29 @@ ...@@ -31,29 +32,29 @@
*/ */
class FBoolArray{ class FBoolArray{
/** Size of a unsigned long */ /** Size of a unsigned long */
const static int BytesInBlock = sizeof(unsigned long); const static FSize BytesInBlock = sizeof(unsigned long);
const static int SizeOfBlock = BytesInBlock * 8; const static FSize SizeOfBlock = BytesInBlock * 8;
/** The array to store bits */ /** The array to store bits */
unsigned long* const array; unsigned long* array;
/** Size of the memory allocated */ /** Size of the memory allocated */
const int memSize; FSize memSize;
/** Size of the array => number of real elements */ /** Size of the array => number of real elements */
const int size; FSize size;
/** get size to number of long */ /** get size to number of long */
int LongFromSize(const int inSize){ FSize LongFromSize(const FSize inSize){
return ((inSize + SizeOfBlock - 1) / SizeOfBlock); return ((inSize + SizeOfBlock - 1) / SizeOfBlock);
} }
/** Alloc an array */ /** Alloc an array */
unsigned long * AllocArray(const int inSize){ unsigned long * AllocArray(const FSize inSize){
return new unsigned long[LongFromSize(inSize)]; return new unsigned long[LongFromSize(inSize)];
} }
public : public :
/** Constructor with size */ /** 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(); setToZeros();
} }
...@@ -62,6 +63,25 @@ public : ...@@ -62,6 +63,25 @@ public :
*this = other; *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 */ /** Destructor */
~FBoolArray(){ ~FBoolArray(){
delete [] array; delete [] array;
...@@ -72,10 +92,25 @@ public : ...@@ -72,10 +92,25 @@ public :
* Array must have the same size * Array must have the same size
*/ */
FBoolArray& operator=(const FBoolArray& other){ FBoolArray& operator=(const FBoolArray& other){
FAssertLF(size == other.size);
memcpy(array, other.array, memSize); memcpy(array, other.array, memSize);
return *this; 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 == * Operator ==
* Array must have the same size * Array must have the same size
...@@ -93,22 +128,22 @@ public : ...@@ -93,22 +128,22 @@ public :
} }
/** To get a value */ /** To get a value */
bool get(const int inPos) const { bool get(const FSize inPos) const {
const int posInArray = inPos / SizeOfBlock; const FSize posInArray = inPos / SizeOfBlock;
const int bytePosition = inPos - (posInArray * 8); const FSize bytePosition = inPos - (posInArray * 8);
return (array[posInArray] >> bytePosition) & 1; return (array[posInArray] >> bytePosition) & 1;
} }
/** To set a value */ /** To set a value */
void set(const int inPos, const bool inVal){ void set(const FSize inPos, const bool inVal){
const int posInArray = inPos / SizeOfBlock; const FSize posInArray = inPos / SizeOfBlock;
const int bytePosition = inPos - (posInArray * 8); const FSize bytePosition = inPos - (posInArray * 8);
if(inVal) array[posInArray] |= (1UL << bytePosition); if(inVal) array[posInArray] |= (1UL << bytePosition);
else array[posInArray] &= ~(1UL << bytePosition); else array[posInArray] &= ~(1UL << bytePosition);
} }
/** To get the size of the array */ /** To get the size of the array */
int getSize() const { FSize getSize() const {
return size; return size;
} }
...@@ -116,6 +151,11 @@ public : ...@@ -116,6 +151,11 @@ public :
void setToZeros() const { void setToZeros() const {
memset( array, 0, memSize); memset( array, 0, memSize);
} }
/** Set all the memory to 1 */
void setToOnes() const {
memset( array, (unsigned char)0xFF, memSize);
}
}; };
......
...@@ -26,66 +26,70 @@ ...@@ -26,66 +26,70 @@
/** this class test the bool array container */ /** this class test the bool array container */
class TestArray : public FUTester<TestArray> { class TestArray : public FUTester<TestArray> {
void TestGetSet(){ void TestGetSet(){
FBoolArray array(500); FBoolArray array(500);
for(int idx = 0 ; idx < 500 ; ++idx){ for(int idx = 0 ; idx < 500 ; ++idx){
uassert(!array.get(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 TestEqual(){ for(int idx = 0 ; idx < 500 ; ++idx){
FBoolArray array1(10); array.set(idx, true);
FBoolArray array2(10); 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); array1.set(1, true);
array2 = array1; uassert(array1 != array2);
uassert(array1 == array2);
} array2.set(1, true);
uassert(array1 == array2);
// set test
void SetTests(){ array1.set(5, true);
AddTest(&TestArray::TestGetSet,"Test Get & Set"); array2 = array1;
AddTest(&TestArray::TestGetSet2,"Test Get & Set 2"); uassert(array1 == array2);
AddTest(&TestArray::TestEqual,"Test Equal"); }
}
// 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 // You must do this
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment