Refactor Stack class and add Set::toList() method in Set
This commit is contained in:
parent
d0fbab77ce
commit
f9451e1fe1
|
@ -542,6 +542,20 @@ class Set {
|
||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return a list with all the values of the set
|
||||||
|
List<V> toList(MemoryAllocator& listAllocator) {
|
||||||
|
|
||||||
|
List<V> list(listAllocator);
|
||||||
|
|
||||||
|
for (int i=0; i < mCapacity; i++) {
|
||||||
|
if (mEntries[i].value != nullptr) {
|
||||||
|
list.add(*(mEntries[i].value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
/// Clear the set
|
/// Clear the set
|
||||||
void clear(bool releaseMemory = false) {
|
void clear(bool releaseMemory = false) {
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#define REACTPHYSICS3D_STACK_H
|
#define REACTPHYSICS3D_STACK_H
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
|
#include <memory>
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
#include "memory/MemoryAllocator.h"
|
#include "memory/MemoryAllocator.h"
|
||||||
|
|
||||||
|
@ -34,10 +35,9 @@ namespace reactphysics3d {
|
||||||
|
|
||||||
// Class Stack
|
// Class Stack
|
||||||
/**
|
/**
|
||||||
* This class represents a simple generic stack with an initial capacity. If the number
|
* This class represents a simple generic stack.
|
||||||
* of elements exceeds the capacity, the heap will be used to allocated more memory.
|
|
||||||
*/
|
*/
|
||||||
template<typename T, uint capacity>
|
template<typename T>
|
||||||
class Stack {
|
class Stack {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -47,84 +47,134 @@ class Stack {
|
||||||
/// Reference to the memory allocator
|
/// Reference to the memory allocator
|
||||||
MemoryAllocator& mAllocator;
|
MemoryAllocator& mAllocator;
|
||||||
|
|
||||||
/// Initial array that contains the elements of the stack
|
/// Array that contains the elements of the stack
|
||||||
T mInitArray[capacity];
|
T* mArray;
|
||||||
|
|
||||||
/// Pointer to the first element of the stack
|
|
||||||
T* mElements;
|
|
||||||
|
|
||||||
/// Number of elements in the stack
|
/// Number of elements in the stack
|
||||||
uint mNbElements;
|
uint mNbElements;
|
||||||
|
|
||||||
/// Number of allocated elements in the stack
|
/// Number of allocated elements in the stack
|
||||||
uint mNbAllocatedElements;
|
uint mCapacity;
|
||||||
|
|
||||||
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
|
/// Allocate more memory
|
||||||
|
void allocate(size_t capacity) {
|
||||||
|
|
||||||
|
T* newArray = static_cast<T*>(mAllocator.allocate(capacity * sizeof(T)));
|
||||||
|
assert(newArray != nullptr);
|
||||||
|
|
||||||
|
// If there
|
||||||
|
if (mCapacity > 0) {
|
||||||
|
|
||||||
|
if (mNbElements > 0) {
|
||||||
|
|
||||||
|
// Copy the previous items in the new array
|
||||||
|
std::uninitialized_copy(mArray, mArray + mNbElements, newArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release memory of the previous array
|
||||||
|
mAllocator.release(mArray, mCapacity * sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
mArray = newArray;
|
||||||
|
|
||||||
|
mCapacity = capacity;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// -------------------- Methods -------------------- //
|
// -------------------- Methods -------------------- //
|
||||||
|
|
||||||
/// Constructor
|
/// Constructor
|
||||||
Stack(MemoryAllocator& allocator)
|
Stack(MemoryAllocator& allocator, size_t capacity = 0)
|
||||||
:mAllocator(allocator), mElements(mInitArray), mNbElements(0), mNbAllocatedElements(capacity) {
|
:mAllocator(allocator), mArray(nullptr), mNbElements(0), mCapacity(0) {
|
||||||
|
|
||||||
|
if (capacity > 0) {
|
||||||
|
allocate(capacity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy constructor
|
||||||
|
Stack(const Stack& stack)
|
||||||
|
:mAllocator(stack.mAllocator), mArray(nullptr),
|
||||||
|
mNbElements(stack.mNbElements), mCapacity(stack.mCapacity) {
|
||||||
|
|
||||||
|
if (mCapacity > 0) {
|
||||||
|
|
||||||
|
// Allocate memory for the buckets
|
||||||
|
mArray = static_cast<T*>(mAllocator.allocate(mCapacity * sizeof(T)));
|
||||||
|
assert(mArray != nullptr);
|
||||||
|
|
||||||
|
if (mNbElements > 0) {
|
||||||
|
|
||||||
|
// Copy the items
|
||||||
|
std::uninitialized_copy(stack.mArray, stack.mArray + mNbElements, mArray);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destructor
|
/// Destructor
|
||||||
~Stack() {
|
~Stack() {
|
||||||
|
|
||||||
// If elements have been allocated on the heap
|
clear();
|
||||||
if (mInitArray != mElements) {
|
|
||||||
|
|
||||||
// Release the memory allocated on the heap
|
// Release the memory allocated on the heap
|
||||||
mAllocator.release(mElements, mNbAllocatedElements * sizeof(T));
|
mAllocator.release(mArray, mCapacity * sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove all the items from the stack
|
||||||
|
void clear() {
|
||||||
|
|
||||||
|
// Destruct the items
|
||||||
|
for (size_t i = 0; i < mNbElements; i++) {
|
||||||
|
mArray[i].~T();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mNbElements = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Push an element into the stack
|
/// Push an element into the stack
|
||||||
void push(const T& element);
|
void push(const T& element) {
|
||||||
|
|
||||||
|
// If we need to allocate more elements
|
||||||
|
if (mNbElements == mCapacity) {
|
||||||
|
|
||||||
|
allocate(mCapacity > 0 ? mCapacity * 2 : 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the item into the array
|
||||||
|
new (mArray + mNbElements) T(element);
|
||||||
|
|
||||||
|
mNbElements++;
|
||||||
|
}
|
||||||
|
|
||||||
/// Pop an element from the stack (remove it from the stack and return it)
|
/// Pop an element from the stack (remove it from the stack and return it)
|
||||||
T pop();
|
T pop() {
|
||||||
|
|
||||||
/// Return the number of elments in the stack
|
assert(mNbElements > 0);
|
||||||
uint getNbElements() const;
|
|
||||||
|
|
||||||
};
|
mNbElements--;
|
||||||
|
|
||||||
// Push an element into the stack
|
// Copy the item
|
||||||
template<typename T, uint capacity>
|
T item = mArray[mNbElements];
|
||||||
inline void Stack<T, capacity>::push(const T& element) {
|
|
||||||
|
|
||||||
// If we need to allocate more elements
|
// Call the destructor
|
||||||
if (mNbElements == mNbAllocatedElements) {
|
mArray[mNbElements].~T();
|
||||||
T* oldElements = mElements;
|
|
||||||
uint oldNbAllocatedElements = mNbAllocatedElements;
|
return item;
|
||||||
mNbAllocatedElements *= 2;
|
|
||||||
mElements = static_cast<T*>(mAllocator.allocate(mNbAllocatedElements * sizeof(T)));
|
|
||||||
assert(mElements);
|
|
||||||
memcpy(mElements, oldElements, mNbElements * sizeof(T));
|
|
||||||
if (oldElements != mInitArray) {
|
|
||||||
mAllocator.release(oldElements, oldNbAllocatedElements * sizeof(T));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
mElements[mNbElements] = element;
|
/// Return the number of items in the stack
|
||||||
mNbElements++;
|
size_t size() const {
|
||||||
}
|
return mNbElements;
|
||||||
|
}
|
||||||
|
|
||||||
// Pop an element from the stack (remove it from the stack and return it)
|
/// Return the capacity of the stack
|
||||||
template<typename T, uint capacity>
|
size_t capacity() const {
|
||||||
inline T Stack<T, capacity>::pop() {
|
return mCapacity;
|
||||||
assert(mNbElements > 0);
|
}
|
||||||
mNbElements--;
|
};
|
||||||
return mElements[mNbElements];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the number of elments in the stack
|
|
||||||
template<typename T, uint capacity>
|
|
||||||
inline uint Stack<T, capacity>::getNbElements() const {
|
|
||||||
return mNbElements;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ SET (RP3D_TESTS_HEADERS
|
||||||
"tests/containers/TestList.h"
|
"tests/containers/TestList.h"
|
||||||
"tests/containers/TestMap.h"
|
"tests/containers/TestMap.h"
|
||||||
"tests/containers/TestSet.h"
|
"tests/containers/TestSet.h"
|
||||||
|
"tests/containers/TestStack.h"
|
||||||
"tests/containers/TestDeque.h"
|
"tests/containers/TestDeque.h"
|
||||||
"tests/mathematics/TestMathematicsFunctions.h"
|
"tests/mathematics/TestMathematicsFunctions.h"
|
||||||
"tests/mathematics/TestMatrix2x2.h"
|
"tests/mathematics/TestMatrix2x2.h"
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
#include "tests/containers/TestMap.h"
|
#include "tests/containers/TestMap.h"
|
||||||
#include "tests/containers/TestSet.h"
|
#include "tests/containers/TestSet.h"
|
||||||
#include "tests/containers/TestDeque.h"
|
#include "tests/containers/TestDeque.h"
|
||||||
|
#include "tests/containers/TestStack.h"
|
||||||
|
|
||||||
using namespace reactphysics3d;
|
using namespace reactphysics3d;
|
||||||
|
|
||||||
|
@ -56,6 +57,7 @@ int main() {
|
||||||
testSuite.addTest(new TestMap("Map"));
|
testSuite.addTest(new TestMap("Map"));
|
||||||
testSuite.addTest(new TestSet("Set"));
|
testSuite.addTest(new TestSet("Set"));
|
||||||
testSuite.addTest(new TestDeque("Deque"));
|
testSuite.addTest(new TestDeque("Deque"));
|
||||||
|
testSuite.addTest(new TestStack("Stack"));
|
||||||
|
|
||||||
// ---------- Mathematics tests ---------- //
|
// ---------- Mathematics tests ---------- //
|
||||||
|
|
||||||
|
|
|
@ -90,6 +90,7 @@ class TestSet : public Test {
|
||||||
testEquality();
|
testEquality();
|
||||||
testAssignment();
|
testAssignment();
|
||||||
testIterators();
|
testIterators();
|
||||||
|
testConverters();
|
||||||
}
|
}
|
||||||
|
|
||||||
void testConstructors() {
|
void testConstructors() {
|
||||||
|
@ -419,6 +420,31 @@ class TestSet : public Test {
|
||||||
}
|
}
|
||||||
rp3d_test(set1.size() == size);
|
rp3d_test(set1.size() == size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void testConverters() {
|
||||||
|
|
||||||
|
Set<int> set1(mAllocator);
|
||||||
|
|
||||||
|
rp3d_test(set1.begin() == set1.end());
|
||||||
|
|
||||||
|
set1.add(1);
|
||||||
|
set1.add(2);
|
||||||
|
set1.add(3);
|
||||||
|
set1.add(4);
|
||||||
|
|
||||||
|
List<int> list1 = set1.toList(mAllocator);
|
||||||
|
rp3d_test(list1.size() == 4);
|
||||||
|
rp3d_test(list1.find(1) != list1.end());
|
||||||
|
rp3d_test(list1.find(2) != list1.end());
|
||||||
|
rp3d_test(list1.find(3) != list1.end());
|
||||||
|
rp3d_test(list1.find(4) != list1.end());
|
||||||
|
rp3d_test(list1.find(5) == list1.end());
|
||||||
|
rp3d_test(list1.find(6) == list1.end());
|
||||||
|
|
||||||
|
Set<int> set2(mAllocator);
|
||||||
|
List<int> list2 = set2.toList(mAllocator);
|
||||||
|
rp3d_test(list2.size() == 0);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
133
test/tests/containers/TestStack.h
Normal file
133
test/tests/containers/TestStack.h
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
||||||
|
* Copyright (c) 2010-2016 Daniel Chappuis *
|
||||||
|
*********************************************************************************
|
||||||
|
* *
|
||||||
|
* This software is provided 'as-is', without any express or implied warranty. *
|
||||||
|
* In no event will the authors be held liable for any damages arising from the *
|
||||||
|
* use of this software. *
|
||||||
|
* *
|
||||||
|
* Permission is granted to anyone to use this software for any purpose, *
|
||||||
|
* including commercial applications, and to alter it and redistribute it *
|
||||||
|
* freely, subject to the following restrictions: *
|
||||||
|
* *
|
||||||
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
||||||
|
* that you wrote the original software. If you use this software in a *
|
||||||
|
* product, an acknowledgment in the product documentation would be *
|
||||||
|
* appreciated but is not required. *
|
||||||
|
* *
|
||||||
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
||||||
|
* misrepresented as being the original software. *
|
||||||
|
* *
|
||||||
|
* 3. This notice may not be removed or altered from any source distribution. *
|
||||||
|
* *
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
#ifndef TEST_STACK_H
|
||||||
|
#define TEST_STACK_H
|
||||||
|
|
||||||
|
// Libraries
|
||||||
|
#include "Test.h"
|
||||||
|
#include "containers/Stack.h"
|
||||||
|
#include "memory/DefaultAllocator.h"
|
||||||
|
|
||||||
|
/// Reactphysics3D namespace
|
||||||
|
namespace reactphysics3d {
|
||||||
|
|
||||||
|
// Class TestStack
|
||||||
|
/**
|
||||||
|
* Unit test for the Stack class
|
||||||
|
*/
|
||||||
|
class TestStack : public Test {
|
||||||
|
|
||||||
|
private :
|
||||||
|
|
||||||
|
// ---------- Atributes ---------- //
|
||||||
|
|
||||||
|
DefaultAllocator mAllocator;
|
||||||
|
|
||||||
|
public :
|
||||||
|
|
||||||
|
// ---------- Methods ---------- //
|
||||||
|
|
||||||
|
/// Constructor
|
||||||
|
TestStack(const std::string& name) : Test(name) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run the tests
|
||||||
|
void run() {
|
||||||
|
|
||||||
|
testConstructor();
|
||||||
|
testPushPop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void testConstructor() {
|
||||||
|
|
||||||
|
// ----- Constructors ----- //
|
||||||
|
|
||||||
|
Stack<std::string> stack1(mAllocator);
|
||||||
|
rp3d_test(stack1.capacity() == 0);
|
||||||
|
rp3d_test(stack1.size() == 0);
|
||||||
|
|
||||||
|
Stack<std::string> stack2(mAllocator, 100);
|
||||||
|
rp3d_test(stack2.capacity() >= 100);
|
||||||
|
rp3d_test(stack2.size() == 0);
|
||||||
|
|
||||||
|
// ----- Copy Constructors ----- //
|
||||||
|
Stack<std::string> stack3(stack2);
|
||||||
|
rp3d_test(stack3.capacity() == stack2.capacity());
|
||||||
|
rp3d_test(stack3.size() == stack2.size());
|
||||||
|
|
||||||
|
Stack<int> stack4(mAllocator);
|
||||||
|
stack4.push(10);
|
||||||
|
stack4.push(20);
|
||||||
|
stack4.push(30);
|
||||||
|
rp3d_test(stack4.capacity() >= 3);
|
||||||
|
rp3d_test(stack4.size() == 3);
|
||||||
|
stack4.push(40);
|
||||||
|
rp3d_test(stack4.size() == 4);
|
||||||
|
|
||||||
|
Stack<int> set5(stack4);
|
||||||
|
rp3d_test(set5.capacity() == stack4.capacity());
|
||||||
|
rp3d_test(set5.size() == stack4.size());
|
||||||
|
rp3d_test(set5.pop() == 40);
|
||||||
|
rp3d_test(set5.size() == 3);
|
||||||
|
rp3d_test(set5.pop() == 30);
|
||||||
|
rp3d_test(set5.pop() == 20);
|
||||||
|
rp3d_test(set5.pop() == 10);
|
||||||
|
rp3d_test(set5.size() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testPushPop() {
|
||||||
|
|
||||||
|
Stack<int> stack1(mAllocator);
|
||||||
|
stack1.push(10);
|
||||||
|
stack1.push(80);
|
||||||
|
stack1.push(130);
|
||||||
|
rp3d_test(stack1.size() == 3);
|
||||||
|
rp3d_test(stack1.pop() == 130);
|
||||||
|
rp3d_test(stack1.pop() == 80);
|
||||||
|
rp3d_test(stack1.pop() == 10);
|
||||||
|
rp3d_test(stack1.size() == 0);
|
||||||
|
stack1.push(10);
|
||||||
|
rp3d_test(stack1.pop() == 10);
|
||||||
|
stack1.push(10);
|
||||||
|
stack1.push(80);
|
||||||
|
stack1.pop();
|
||||||
|
rp3d_test(stack1.pop() == 10);
|
||||||
|
rp3d_test(stack1.size() == 0);
|
||||||
|
stack1.push(10);
|
||||||
|
stack1.push(80);
|
||||||
|
stack1.push(130);
|
||||||
|
stack1.push(56);
|
||||||
|
stack1.push(89);
|
||||||
|
stack1.push(131);
|
||||||
|
stack1.clear();
|
||||||
|
rp3d_test(stack1.size() == 0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user