diff --git a/README.md b/README.md index caecf00..2cf7a40 100644 --- a/README.md +++ b/README.md @@ -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: ------- diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 8adbf21..105c2b4 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -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 @@ -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 diff --git a/include/cppcore/Container/TStaticArray.h b/include/cppcore/Container/TStaticArray.h new file mode 100644 index 0000000..b135b26 --- /dev/null +++ b/include/cppcore/Container/TStaticArray.h @@ -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 +#include + +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 TStaticArray { +public: + TStaticArray(); + TStaticArray(const TStaticArray &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 &rhs) const; + TStaticArray &operator = (const TStaticArray &rhs); + +private: + T m_array[len]; + unsigned int m_len; +}; + +template +inline +TStaticArray::TStaticArray() +: m_len(len) { + clear(); +} + +template +inline +TStaticArray::TStaticArray(const TStaticArray &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 +inline +TStaticArray::~TStaticArray() { + // empty +} + +template +inline +void TStaticArray::clear() { + ::memset(m_array, 0, sizeof(T) * m_len); +} + +template +inline +void TStaticArray::set(unsigned int index, T value) { + assert(index < m_len); + + m_array[index] = value; +} + +template +inline +unsigned int TStaticArray::size() const { + return m_len; +} + +template +inline +T TStaticArray::operator[](unsigned int index) const { + assert(index < m_len); + + return m_array[index]; +} + +template +inline +T &TStaticArray::operator[](unsigned int index) { + return m_array[index]; +} + +template +inline +bool TStaticArray::operator == (const TStaticArray &rhs) const { + for (unsigned int i = 0; i < m_len; ++i) { + if (m_array[i] != rhs.m_array[i]) { + return false; + } + } + + return true; +} + +template +inline +TStaticArray &TStaticArray::operator = (const TStaticArray &rhs) { + if (*this == rhs) { + return *this; + } + + for (unsigned int i = 0; i < m_len; ++i) { + m_array[i] = rhs.m_array[i]; + } + + return *this; +} + +} diff --git a/test/container/TStaticArrayTest.cpp b/test/container/TStaticArrayTest.cpp new file mode 100644 index 0000000..bc513db --- /dev/null +++ b/test/container/TStaticArrayTest.cpp @@ -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 + +#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 arr; + EXPECT_EQ(4, arr.size()); + EXPECT_EQ(0, arr[0]); + EXPECT_EQ(0, arr[3]); +} + +TEST_F(TStaticArrayTest, access_items_Test) { + TStaticArray 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 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]); + } +} +