Commit 558f747e authored by berenger-bramas's avatar berenger-bramas

Add a vector file to stores particles on leafs.

But to test it now you have to use :
at the top of FList header.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/scalfmm/scalfmm/trunk@98 2616d619-271b-44dc-8df4-d4a8f33a7222
parent a0d03ae6
......@@ -101,19 +101,6 @@ public:
++this->size;
}
/**
* Push an element in the front of the list
* used when type is pointer and like [TYPE* const] object
* @param inObject the object to insert
*/
/*void pushFront(Object const inObject){
Node* newNode = new Node;
newNode->target = inObject;
newNode->next = this->root;
this->root = newNode;
++this->size;
}*/
/**
* To get front value (last pushed value)
......
#ifndef FVECTOR_HPP
#define FVECTOR_HPP
// /!\ Please, you must read the license at the bottom of this page
#include "../Utils/FGlobal.hpp"
#include <string.h>
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FList
* Please read the license
*
* This class is a vector container.
* It is a very basic vector to enable strong performance.
*
* Please refere to unit test fvectorUTest.cpp to know more.
*/
template<class T>
class FVector {
protected:
T* array; /**< memory area*/
int capacity; /**< memory capacity*/
int index; /**< index in array */
static const int DefaultSize = 10; /**< Default size */
static const int SizeOfT = sizeof(T); /**< size of the object*/
public:
/**
*@brief constructor
*/
FVector() : array(reinterpret_cast< T* >( new char[SizeOfT*DefaultSize] )), capacity(DefaultSize), index(0) {
}
/**
*@brief constructor
*@param inSize the buffer size
*@param inPointOfStart the point of start [0;1]
*/
FVector(const int inSize): array(reinterpret_cast< T* >( new char[SizeOfT*inSize]))
, capacity(inSize), index(0) {
}
FVector(const FVector& other): array(reinterpret_cast< T* >( new char[SizeOfT*other.capacity]))
, capacity(other.capacity), index(other.index) {
for(int idx = 0 ; idx < other.index ; ++idx){
new((void*)&this->array[idx]) T;
this->array[idx] = other.array[idx];
}
}
FVector& operator=(const FVector& other){
clear();
if(other.getSize() > this->capacity){
delete [] reinterpret_cast< char* >(this->array);
this->capacity = other.getSize() * 1.5;
array = reinterpret_cast< T* >( new char[SizeOfT*this->capacity]);
}
this->index = other.index;
for(int idx = 0 ; idx < other.index ; ++idx){
new((void*)&this->array[idx]) T;
this->array[idx] = other.array[idx];
}
return *this;
}
/**
*@brief destructor
*/
virtual ~FVector(){
clear();
delete [] reinterpret_cast< char* >(this->array);
}
/**
*@brief get the buffer capacity
*@return the buffer capacity
*/
int getCapacity() const{
return this->capacity;
}
/**
*@brief set the buffer capacity
*@param inthis->capacity to change the capacity
*/
void setCapacity(int inCapacity){
if( inCapacity != this->capacity ){
if( inCapacity < this->index){
inCapacity = this->index;
}
T* const buffer = reinterpret_cast< T* >( new char[SizeOfT*inCapacity]);
memcpy(buffer,this->array,SizeOfT*this->index);
delete [] reinterpret_cast< char* >(this->array);
this->array = buffer;
this->capacity = inCapacity;
}
}
/**
*@brief last data
*@return end->data
*/
const T& front() const {
return this->array[this->index - 1];
}
/**
*@brief last data
*@return end->data
*/
T& front() {
return this->array[this->index - 1];
}
/**
*@brief delete all, then size = 0
*/
void clear(){
while(this->index > 0){
(&this->array[--this->index])->~T();
}
}
/**
*@brief count the value
*@param inValue the value to test
*@return the value occured number
*/
int getSize() const{
return this->index;
}
/**
*@brief pop the first node
*/
void popFront(){
(&this->array[--this->index])->~T();
}
/**
*@brief push a new node in front
*@param inValue the new value
*/
void pushFront( const T & inValue ){
if( this->index == this->capacity ){
setCapacity(this->capacity * 1.5);
}
new((void*)&this->array[this->index]) T;
this->array[this->index] = inValue;
++this->index;
}
class BasicIterator{
protected:
FVector* const vector; /**< the vector to work on*/
int index; /**< the current node*/
public:
virtual ~BasicIterator(){}
BasicIterator(FVector<T>& inVector) : vector(&inVector), index(0){}
void progress(){
++this->index;
}
bool isValide() const{
return this->index < this->vector->index;
}
T& value(){
return this->vector->array[this->index];
}
const T& value() const{
return this->vector->array[this->index];
}
};
friend class BasicIterator;
class ConstBasicIterator{
protected:
const FVector* const vector; /**< the vector to work on*/
int index; /**< the current node*/
public:
virtual ~ConstBasicIterator(){}
ConstBasicIterator(const FVector<T>& inVector) : vector(&inVector), index(0){}
void progress(){
++this->index;
}
bool isValide() const{
return this->index < this->vector->index;
}
const T& value() const{
return this->vector->array[this->index];
}
};
friend class ConstBasicIterator;
};
#endif // FVECTOR_HPP
......@@ -109,7 +109,7 @@ int main(int argc, char ** argv){
FFmbKernels<FmbParticle, FmbCell, NbLevels> kernels(loader.getBoxWidth());
//FBasicKernels<FmbParticle, FmbCell, NbLevels> kernels;
//FFmmAlgorithm FFmmAlgorithmThread FFmmAlgorithmThreadUs
FFmmAlgorithmThread<FFmbKernels, FmbParticle, FmbCell, FSimpleLeaf, NbLevels, SizeSubLevels> algo(&tree,&kernels);
FFmmAlgorithm<FFmbKernels, FmbParticle, FmbCell, FSimpleLeaf, NbLevels, SizeSubLevels> algo(&tree,&kernels);
algo.execute();
counter.tac();
......
#include "FUTester.hpp"
#include "../Src/Containers/FVector.hpp"
/**
* This file is a unit test for the FVector class
*/
/**
* This class is simply used to count alloc dealloc
*/
class TestObject{
public:
static int counter;
static int dealloced;
TestObject(){
++counter;
}
TestObject(const TestObject&){
++counter;
}
~TestObject(){
++dealloced;
}
};
int TestObject::counter(0);
int TestObject::dealloced(0);
/** this class test the vector container */
class TestVector : public FUTester<TestVector> {
// Called before each test : simply set counter to 0
void PreTest(){
TestObject::counter = 0;
TestObject::dealloced = 0;
}
// test size
void TestSize(){
FVector<TestObject> vector;
vector.pushFront(TestObject());
vector.pushFront(TestObject());
vector.pushFront(TestObject());
assert(vector.getSize() == 3);
assert((TestObject::counter - TestObject::dealloced) == vector.getSize());
vector.clear();
assert(vector.getSize() == 0);
assert(TestObject::counter == TestObject::dealloced);
}
// test copy
void TestCopy(){
FVector<TestObject> vector;
vector.pushFront(TestObject());
vector.pushFront(TestObject());
vector.pushFront(TestObject());
{
FVector<TestObject> vector2(vector);
assert(vector.getSize() == vector2.getSize());
assert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
}
{
FVector<TestObject> vector2(vector.getSize()/2);
vector2 = vector;
assert(vector.getSize() == vector2.getSize());
assert((TestObject::counter - TestObject::dealloced) == (vector.getSize() + vector2.getSize()));
}
}
// test iter
void TestIter(){
FVector<TestObject> vector;
{
FVector<TestObject>::BasicIterator iter(vector);
assert(!iter.isValide());
}
{
vector.pushFront(TestObject());
vector.pushFront(TestObject());
vector.pushFront(TestObject());
FVector<TestObject>::BasicIterator iter(vector);
assert(iter.isValide());
int counter = 0;
while(iter.isValide()){ iter.progress(); ++counter; }
assert(!iter.isValide());
assert(counter == vector.getSize());
}
}
// set test
void SetTests(){
AddTest(&TestVector::TestSize,"Test Size");
AddTest(&TestVector::TestCopy,"Test Copy");
AddTest(&TestVector::TestIter,"Test Iter");
}
};
// You must do this
TestClass(TestVector)
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