Skip to content

Commit

Permalink
Merge pull request #1 from kimkulling/StaticArraySupport
Browse files Browse the repository at this point in the history
Static array support
  • Loading branch information
kimkulling authored May 16, 2018
2 parents 6753b5f + fb59be9 commit cd922c2
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ Common stuff:

Containers:
----------
- TArray: A simple dynamic array.
- TList: A double linked list.
- TQueue: A simple queue.
- THashMap: A key-value hash map for easy lookup tables
- TStaticArray A static array.
- TArray: A simple dynamic array.
- TList: A double linked list.
- TQueue: A simple FIFO queue.
- THashMap: A key-value hash map for easy lookup tables

Memory:
-------
Expand Down
3 changes: 3 additions & 0 deletions build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ SET( cppcore_random_src
SET ( cppcore_container_src
../include/cppcore/Container/THashMap.h
../include/cppcore/Container/TArray.h
../include/cppcore/Container/TStaticArray.h
../include/cppcore/Container/TList.h
../include/cppcore/Container/TQueue.h
../include/cppcore/Container/TStaticArray.h
)

SET ( cppcore_memory_src
Expand Down Expand Up @@ -108,6 +110,7 @@ IF( BUILD_UNITTESTS )
../test/container/THashMapTest.cpp
../test/container/TListTest.cpp
../test/container/TQueueTest.cpp
../test/container/TStaticArrayTest.cpp
)

SET( cppcore_memory_test_src
Expand Down
141 changes: 141 additions & 0 deletions include/cppcore/Container/TStaticArray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*-----------------------------------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2014-2018 Kim Kulling
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-----------------------------------------------------------------------------------------------*/
#pragma once

#include <string.h>
#include <cassert>

namespace CPPCore {

//-------------------------------------------------------------------------------------------------
/// @class TArray
/// @ingroup CPPCore
///
/// @brief This template class implements a simple array with dynamic boundaries.
/// You can use it to add new items, remove them and iterate through them. The data items are
/// stores in an array.
//-------------------------------------------------------------------------------------------------
template<class T, unsigned int len>
class TStaticArray {
public:
TStaticArray();
TStaticArray(const TStaticArray<T, len> &rhs);
~TStaticArray();
void clear();
unsigned int size() const;
void set(unsigned int index, T value);
T operator[](unsigned int index) const;
T &operator[](unsigned int index);
bool operator == (const TStaticArray<T, len> &rhs) const;
TStaticArray<T, len> &operator = (const TStaticArray<T, len> &rhs);

private:
T m_array[len];
unsigned int m_len;
};

template<class T, unsigned int len>
inline
TStaticArray<T,len>::TStaticArray()
: m_len(len) {
clear();
}

template<class T, unsigned int len>
inline
TStaticArray<T, len>::TStaticArray(const TStaticArray<T, len> &rhs)
: m_len(rhs.m_len) {
assert(m_len == rhs.m_len);

for (unsigned int i = 0; i < m_len; ++i) {
m_array[i] = rhs.m_array[i];
}
}

template<class T, unsigned int len>
inline
TStaticArray<T, len>::~TStaticArray() {
// empty
}

template<class T, unsigned int len>
inline
void TStaticArray<T, len>::clear() {
::memset(m_array, 0, sizeof(T) * m_len);
}

template<class T, unsigned int len>
inline
void TStaticArray<T, len>::set(unsigned int index, T value) {
assert(index < m_len);

m_array[index] = value;
}

template<class T, unsigned int len>
inline
unsigned int TStaticArray<T, len>::size() const {
return m_len;
}

template<class T, unsigned int len>
inline
T TStaticArray<T, len>::operator[](unsigned int index) const {
assert(index < m_len);

return m_array[index];
}

template<class T, unsigned int len>
inline
T &TStaticArray<T, len>::operator[](unsigned int index) {
return m_array[index];
}

template<class T, unsigned int len>
inline
bool TStaticArray<T, len>::operator == (const TStaticArray<T, len> &rhs) const {
for (unsigned int i = 0; i < m_len; ++i) {
if (m_array[i] != rhs.m_array[i]) {
return false;
}
}

return true;
}

template<class T, unsigned int len>
inline
TStaticArray<T, len> &TStaticArray<T, len>::operator = (const TStaticArray<T, len> &rhs) {
if (*this == rhs) {
return *this;
}

for (unsigned int i = 0; i < m_len; ++i) {
m_array[i] = rhs.m_array[i];
}

return *this;
}

}
68 changes: 68 additions & 0 deletions test/container/TStaticArrayTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
-------------------------------------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2014-2016 Kim Kulling
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-------------------------------------------------------------------------------------------------
*/
#include <cppcore/Container/TStaticArray.h>

#include "gtest/gtest.h"

using namespace CPPCore;

//-------------------------------------------------------------------------------------------------
/// @class TStaticArrayTest
/// @ingroup UnitTest
///
/// @brief The queue tests.
//-------------------------------------------------------------------------------------------------
class TStaticArrayTest : public testing::Test {
// empty
};

TEST_F(TStaticArrayTest, constructTest) {
TStaticArray<int, 4> arr;
EXPECT_EQ(4, arr.size());
EXPECT_EQ(0, arr[0]);
EXPECT_EQ(0, arr[3]);
}

TEST_F(TStaticArrayTest, access_items_Test) {
TStaticArray<int, 4> arr;
for (size_t i = 0; i < 4; ++i) {
arr[i] = i;
}
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(i, arr[i]);
}
}

TEST_F(TStaticArrayTest, clear_Test) {
TStaticArray<int, 4> arr;
for (size_t i = 0; i < 4; ++i) {
arr[i] = i;
}
arr.clear();
for (size_t i = 0; i < 4; ++i) {
EXPECT_EQ(0, arr[i]);
}
}

0 comments on commit cd922c2

Please sign in to comment.