From 99e77c950d247b1f1c7c3734151ffb6769855a6e Mon Sep 17 00:00:00 2001 From: kimkulling Date: Wed, 16 May 2018 16:19:29 +0200 Subject: [PATCH 1/4] add StaticArray --- build/CMakeLists.txt | 3 + include/cppcore/Container/TStaticArray.h | 140 +++++++++++++++++++++++ test/container/TStaticArrayTest.cpp | 68 +++++++++++ 3 files changed, 211 insertions(+) create mode 100644 include/cppcore/Container/TStaticArray.h create mode 100644 test/container/TStaticArrayTest.cpp 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..2605138 --- /dev/null +++ b/include/cppcore/Container/TStaticArray.h @@ -0,0 +1,140 @@ +/*----------------------------------------------------------------------------------------------- +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 + +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) { + static_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]); + } +} + From d94c10f5ad40ca6ff42a4c97eb811298361ff22e Mon Sep 17 00:00:00 2001 From: kimkulling Date: Wed, 16 May 2018 16:20:21 +0200 Subject: [PATCH 2/4] add StaticArray doc --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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: ------- From 71ce96338803aaa15bb47ca59a86997c6f47ce8e Mon Sep 17 00:00:00 2001 From: kimkulling Date: Wed, 16 May 2018 16:36:35 +0200 Subject: [PATCH 3/4] fix linux compile issue: missing header. --- include/cppcore/Container/TStaticArray.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/cppcore/Container/TStaticArray.h b/include/cppcore/Container/TStaticArray.h index 2605138..4d4b332 100644 --- a/include/cppcore/Container/TStaticArray.h +++ b/include/cppcore/Container/TStaticArray.h @@ -22,6 +22,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -----------------------------------------------------------------------------------------------*/ #pragma once +#include #include namespace CPPCore { From fb59be93a17e935d5db03c7d46c8aa0e432c7fa7 Mon Sep 17 00:00:00 2001 From: kimkulling Date: Wed, 16 May 2018 16:39:51 +0200 Subject: [PATCH 4/4] fix build --- include/cppcore/Container/TStaticArray.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/cppcore/Container/TStaticArray.h b/include/cppcore/Container/TStaticArray.h index 4d4b332..b135b26 100644 --- a/include/cppcore/Container/TStaticArray.h +++ b/include/cppcore/Container/TStaticArray.h @@ -65,7 +65,7 @@ template inline TStaticArray::TStaticArray(const TStaticArray &rhs) : m_len(rhs.m_len) { - static_assert(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];