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 @@
#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);
}
};
......
......@@ -46,6 +46,10 @@ class TestArray : public FUTester<TestArray> {
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(){
......
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