From ad7c507a5befe12d632580a47ce15405bf126561 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 10 Sep 2020 15:35:07 +0200 Subject: [PATCH 01/37] Added fixed_vector and first tests --- .gitignore | 1 + include/nitro/lang/fixed_vector.hpp | 306 ++++++++++++++++++++++++++++ tests/CMakeLists.txt | 2 + tests/fixed_vector_test.cpp | 61 ++++++ 4 files changed, 370 insertions(+) create mode 100644 include/nitro/lang/fixed_vector.hpp create mode 100644 tests/fixed_vector_test.cpp diff --git a/.gitignore b/.gitignore index 46b49f5..77d96be 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ build .clang_complete +.vscode \ No newline at end of file diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp new file mode 100644 index 0000000..2362d6b --- /dev/null +++ b/include/nitro/lang/fixed_vector.hpp @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2015-2017, Technische Universität Dresden, Germany + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include + +#include + +namespace nitro +{ +namespace lang +{ + + template + class fixed_vector + { + public: + using iterator = typename std::array::iterator; + using reverse_iterator = typename std::array::reverse_iterator; + + fixed_vector() = default; + + fixed_vector(const std::array& array) : storage_(array) + { + size_ = N; + } + + template + fixed_vector(const std::array& array) + { + if (M > N) + raise("Array does not fit into fixed_vector!"); + + emplace(array.begin(), array.end()); + } + + fixed_vector(const fixed_vector&) = default; + ~fixed_vector() = default; + + bool empty() + { + return size_ == 0; + } + + const std::size_t& size() + { + return size_; + } + + std::size_t capacity() + { + return storage_.size(); + } + + T& operator[](const std::size_t& key) + { + if (key >= capacity()) + raise("Key is larger than capacity"); + + if (key > size()) + raise("Key is larger than size, use append() instead"); + + return storage_[key]; + } + + T& at(const std::size_t& key) + { + if (key >= capacity()) + raise("Key is larger than capacity"); + + if (key > size()) + raise("Key is larger than size, use append() instead"); + + return storage_[key]; + } + + const T& operator[](const std::size_t& key) const + { + if (key >= capacity()) + raise("Key is larger than capacity!"); + + if (key > size()) + raise("Key is larger than size, use emplace_back() instead!"); + + return storage_[key]; + } + + const T& at(const std::size_t& key) const + { + if (key >= capacity()) + raise("Key is larger than capacity!"); + + if (key > size()) + raise("Key is larger than size, use emplace_back() instead!"); + + return storage_[key]; + } + + T& front() noexcept + { + return storage_[0]; + } + + const T& front() const noexcept + { + return storage_[0]; + } + + T& back() noexcept + { + return storage_[size_ - 1]; + } + + const T& back() const noexcept + { + return storage_[size_ - 1]; + } + + std::size_t emplace(const T& value) + { + return emplace_back(value); + } + + template + void emplace(iter start, const iter& end) + { + emplace_back(start, end); + } + + std::size_t emplace_back(const T& value) + { + + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = T(value); + ++size_; + + return size_ - 1; + } + + template + void emplace_back(iter start, const iter& end) + { + while (start != end) + { + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = T(*start); + ++size_; + + ++start; + } + } + + std::size_t push_back(const T& value) + { + + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = value; + ++size_; + + return size_ - 1; + } + + T pop_back(const std::size_t& key) + { + + if (size_ == 0) + raise("Container is empty!"); + + --size_; + return storage_[size_]; + } + + fixed_vector::iterator begin() noexcept + { + return iterator(storage_.begin()); + } + + fixed_vector::reverse_iterator rbegin() noexcept + { + return reverse_iterator(storage_.rbegin()); + } + + fixed_vector::iterator end() noexcept + { + return iterator(storage_.end()); + } + + fixed_vector::reverse_iterator rend() noexcept + { + return reverse_iterator(storage_.rend()); + } + + const fixed_vector::iterator begin() const noexcept + { + return iterator(storage_.begin()); + } + + const fixed_vector::reverse_iterator rbegin() const noexcept + { + return reverse_iterator(storage_.rbegin()); + } + + const fixed_vector::iterator end() const noexcept + { + return iterator(storage_.end()); + } + + const fixed_vector::reverse_iterator rend() const noexcept + { + return reverse_iterator(storage_.rend()); + } + + const fixed_vector::iterator cbegin() const noexcept + { + return iterator(storage_.begin()); + } + + const fixed_vector::reverse_iterator crbegin() const noexcept + { + return reverse_iterator(storage_.rbegin()); + } + + const fixed_vector::iterator cend() const noexcept + { + return iterator(storage_.end()); + } + + const fixed_vector::reverse_iterator crend() const noexcept + { + return reverse_iterator(storage_.rend()); + } + + template + T& get() noexcept + { + return std::get(storage_); + } + + template + const T& get() const noexcept + { + return std::get(storage_); + } + + void erase(std::size_t key) + { + if (key >= size()) + raise("Key does not exsist!"); + + while (key + 1 < size() && key + 1 < capacity()) + { + storage_[key] = storage_[key + 1]; + ++key; + } + --size_; + } + + private: + std::size_t size_ = 0; + + std::array storage_; + }; + +} // namespace lang +} // namespace nitro + +namespace std +{ +template +T& get(nitro::lang::fixed_vector& c) noexcept +{ + return c.at(I); +} +} // namespace std \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8992f7d..d985160 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -54,3 +54,5 @@ NitroTest(is_callable_test.cpp) NitroTest(format_test.cpp) NitroTest(string_test.cpp) + +NitroTest(fixed_vector_test.cpp) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp new file mode 100644 index 0000000..7d5376c --- /dev/null +++ b/tests/fixed_vector_test.cpp @@ -0,0 +1,61 @@ +#define CATCH_CONFIG_MAIN +#include + +#include + +TEST_CASE("fixed vector init", "[lang]") +{ + nitro::lang::fixed_vector v; + + REQUIRE(v.size() == 0); + REQUIRE(v.capacity() == 10); +} + +TEST_CASE("fixed vector read and write", "[lang]") +{ + nitro::lang::fixed_vector v; + + v.emplace_back(5); + v.emplace_back(7); + + REQUIRE(v.size() == 2); + REQUIRE(v[0] == 5); + REQUIRE(v.at(1) == 7); + REQUIRE(v.back() == 7); +} + +TEST_CASE("fixed vector erase", "[lang]") +{ + nitro::lang::fixed_vector v; + + v.emplace_back(5); + v.emplace_back(7); + v.erase(0); + + REQUIRE(v.back() == 7); + REQUIRE(v.front() == 7); + REQUIRE(v.size() == 1); +} + +TEST_CASE("fixed vector array assignment", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + nitro::lang::fixed_vector v(a); + + REQUIRE(v.front() == 0); + REQUIRE(v.back() == 9); + REQUIRE(v.size() == 10); +} + +TEST_CASE("fixed vector array assignment with less elements", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5 }; + + nitro::lang::fixed_vector v(a); + + REQUIRE(v.front() == 0); + REQUIRE(v.back() == 5); + REQUIRE(v.size() == 6); + REQUIRE(v.capacity() == 10); +} \ No newline at end of file From 76c0a91358b49d7ccc913e67ffca2877dbb46c04 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Fri, 11 Sep 2020 11:04:08 +0200 Subject: [PATCH 02/37] Capacity as argument, not as template --- include/nitro/lang/fixed_vector.hpp | 255 +++++++++++++++++++++++----- tests/fixed_vector_test.cpp | 92 +++++++++- 2 files changed, 295 insertions(+), 52 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 2362d6b..279690b 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -31,51 +31,144 @@ #include #include +#include namespace nitro { namespace lang { - - template + template class fixed_vector { public: - using iterator = typename std::array::iterator; - using reverse_iterator = typename std::array::reverse_iterator; + class iterator : public std::iterator + { + T* p; + + public: + iterator(T* x) : p(x) + { + } + iterator(const iterator& mit) : p(mit.p) + { + } + iterator& operator++() + { + ++p; + return *this; + } + iterator operator++(int) + { + iterator tmp(*this); + operator++(); + return tmp; + } + bool operator==(const iterator& rhs) const + { + return p == rhs.p; + } + bool operator!=(const iterator& rhs) const + { + return p != rhs.p; + } + T& operator*() + { + return *p; + } + }; + + class reverse_iterator : public std::iterator + { + T* p; + + public: + reverse_iterator(T* x) : p(x) + { + } + reverse_iterator(const reverse_iterator& mit) : p(mit.p) + { + } + reverse_iterator& operator++() + { + --p; + return *this; + } + reverse_iterator operator++(int) + { + reverse_iterator tmp(*this); + operator++(); + return tmp; + } + bool operator==(const reverse_iterator& rhs) const + { + return p == rhs.p; + } + bool operator!=(const reverse_iterator& rhs) const + { + return p != rhs.p; + } + T& operator*() + { + return *p; + } + }; - fixed_vector() = default; + fixed_vector(const std::size_t& capacity) + { + capacity_ = capacity; + storage_ = std::make_unique(capacity); + } - fixed_vector(const std::array& array) : storage_(array) + template + fixed_vector(const std::size_t& capacity, const iterabel& array) { - size_ = N; + if (array.size() > capacity) + raise("Size larger the capacity!"); + + size_ = array.size(); + capacity_ = capacity; + storage_ = std::make_unique(capacity); + + emplace_back(array.begin(), array.end()); } - template - fixed_vector(const std::array& array) + template + fixed_vector(const std::size_t& capacity, const iterabel& array, const func& depackage_func) { - if (M > N) - raise("Array does not fit into fixed_vector!"); + if (array.size() > capacity) + raise("Size larger the capacity!"); - emplace(array.begin(), array.end()); + size_ = array.size(); + capacity_ = capacity; + storage_ = std::make_unique(capacity); + + emplace_back(array.begin(), array.end(), depackage_func); + } + + fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity(), v) + { + } + + fixed_vector operator=(const fixed_vector& v) + { + return fixed_vector(v.capacity(), v); } - fixed_vector(const fixed_vector&) = default; ~fixed_vector() = default; - bool empty() + bool empty() const { return size_ == 0; } - const std::size_t& size() + const std::size_t& size() const { return size_; } - std::size_t capacity() + std::size_t capacity() const { - return storage_.size(); + return capacity_; } T& operator[](const std::size_t& key) @@ -153,6 +246,12 @@ namespace lang emplace_back(start, end); } + template + void emplace(iter start, const iter& end, func depackage_func) + { + emplace_back(start, end, depackage_func); + } + std::size_t emplace_back(const T& value) { @@ -180,6 +279,69 @@ namespace lang } } + template + void emplace_back(iter start, const iter& end, func depackage_func) + { + while (start != end) + { + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = T(depackage_func(start)); + ++size_; + + ++start; + } + } + + std::size_t insert(const T& value) + { + + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = value; + ++size_; + + return size_ - 1; + } + + template + void insert(iter start, const iter& end) + { + while (start != end) + { + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = *start; + ++size_; + + ++start; + } + } + + template + void insert(iter start, const iter& end, func depackage_func) + { + while (start != end) + { + if (size_ >= capacity()) + raise("No capacity left!"); + + storage_[size_] = depackage_func(start); + ++size_; + + ++start; + } + } + + template + void merge(iterabel array) + { + emplace_back(array.begin(), array.end()); + } + std::size_t push_back(const T& value) { @@ -202,76 +364,76 @@ namespace lang return storage_[size_]; } - fixed_vector::iterator begin() noexcept + fixed_vector::iterator begin() noexcept { - return iterator(storage_.begin()); + return iterator(&storage_[0]); } - fixed_vector::reverse_iterator rbegin() noexcept + fixed_vector::reverse_iterator rbegin() noexcept { - return reverse_iterator(storage_.rbegin()); + return reverse_iterator(&storage_[size_ - 1]); } - fixed_vector::iterator end() noexcept + fixed_vector::iterator end() noexcept { - return iterator(storage_.end()); + return iterator(&storage_[size_]); } - fixed_vector::reverse_iterator rend() noexcept + fixed_vector::reverse_iterator rend() noexcept { - return reverse_iterator(storage_.rend()); + return reverse_iterator(&storage_[-1]); } - const fixed_vector::iterator begin() const noexcept + const fixed_vector::iterator begin() const noexcept { - return iterator(storage_.begin()); + return iterator(&storage_[0]); } - const fixed_vector::reverse_iterator rbegin() const noexcept + const fixed_vector::reverse_iterator rbegin() const noexcept { - return reverse_iterator(storage_.rbegin()); + return reverse_iterator(&storage_[size_ - 1]); } - const fixed_vector::iterator end() const noexcept + const fixed_vector::iterator end() const noexcept { - return iterator(storage_.end()); + return iterator(&storage_[size_]); } - const fixed_vector::reverse_iterator rend() const noexcept + const fixed_vector::reverse_iterator rend() const noexcept { - return reverse_iterator(storage_.rend()); + return reverse_iterator(&storage_[-1]); } - const fixed_vector::iterator cbegin() const noexcept + const fixed_vector::iterator cbegin() const noexcept { - return iterator(storage_.begin()); + return iterator(&storage_[0]); } - const fixed_vector::reverse_iterator crbegin() const noexcept + const fixed_vector::reverse_iterator crbegin() const noexcept { - return reverse_iterator(storage_.rbegin()); + return reverse_iterator(&storage_[size_ - 1]); } - const fixed_vector::iterator cend() const noexcept + const fixed_vector::iterator cend() const noexcept { - return iterator(storage_.end()); + return iterator(&storage_[size_]); } - const fixed_vector::reverse_iterator crend() const noexcept + const fixed_vector::reverse_iterator crend() const noexcept { - return reverse_iterator(storage_.rend()); + return reverse_iterator(&storage_[-1]); } template T& get() noexcept { - return std::get(storage_); + return this->at(I); } template const T& get() const noexcept { - return std::get(storage_); + return this->at(I); } void erase(std::size_t key) @@ -289,8 +451,9 @@ namespace lang private: std::size_t size_ = 0; + std::size_t capacity_ = 0; - std::array storage_; + std::unique_ptr storage_; }; } // namespace lang @@ -298,8 +461,8 @@ namespace lang namespace std { -template -T& get(nitro::lang::fixed_vector& c) noexcept +template +T& get(nitro::lang::fixed_vector& c) noexcept { return c.at(I); } diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 7d5376c..0078d9b 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -5,7 +5,7 @@ TEST_CASE("fixed vector init", "[lang]") { - nitro::lang::fixed_vector v; + nitro::lang::fixed_vector v(10); REQUIRE(v.size() == 0); REQUIRE(v.capacity() == 10); @@ -13,7 +13,7 @@ TEST_CASE("fixed vector init", "[lang]") TEST_CASE("fixed vector read and write", "[lang]") { - nitro::lang::fixed_vector v; + nitro::lang::fixed_vector v(10); v.emplace_back(5); v.emplace_back(7); @@ -26,7 +26,7 @@ TEST_CASE("fixed vector read and write", "[lang]") TEST_CASE("fixed vector erase", "[lang]") { - nitro::lang::fixed_vector v; + nitro::lang::fixed_vector v(10); v.emplace_back(5); v.emplace_back(7); @@ -41,7 +41,7 @@ TEST_CASE("fixed vector array assignment", "[lang]") { std::array a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - nitro::lang::fixed_vector v(a); + nitro::lang::fixed_vector v(10, a); REQUIRE(v.front() == 0); REQUIRE(v.back() == 9); @@ -52,10 +52,90 @@ TEST_CASE("fixed vector array assignment with less elements", "[lang]") { std::array a = { 0, 1, 2, 3, 4, 5 }; - nitro::lang::fixed_vector v(a); + nitro::lang::fixed_vector v(10, a); REQUIRE(v.front() == 0); REQUIRE(v.back() == 5); REQUIRE(v.size() == 6); REQUIRE(v.capacity() == 10); -} \ No newline at end of file +} + +TEST_CASE("fixed vector std::get access", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5 }; + + nitro::lang::fixed_vector v(6, a); + + REQUIRE(std::get<2>(v) == 2); + REQUIRE(v.get<3>() == 3); +} + +TEST_CASE("fixed vector bracket access", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5 }; + + nitro::lang::fixed_vector v(6, a); + + v[0] = 7; + v[2] = 9; + REQUIRE(v.get<0>() == 7); + REQUIRE(v.get<2>() == 9); +} + +TEST_CASE("fixed vector at() access", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5 }; + + nitro::lang::fixed_vector v(6, a); + + v.at(0) = 7; + v.at(2) = 9; + REQUIRE(v.get<0>() == 7); + REQUIRE(v.get<2>() == 9); +} + +TEST_CASE("fixed vector merge two vectors", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5 }; + std::array b = { 6, 7, 8, 9, 10, 11 }; + + nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector w(6, b); + + v.emplace(w.begin(), w.end()); + + REQUIRE(v.size() == 12); +} + +TEST_CASE("fixed vector merge two vectors", "[lang]") +{ + std::array a = { 0, 1, 2, 3, 4, 5 }; + std::array b = { 6, 7, 8, 9, 10, 11 }; + + nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector w(6, b); + + v.merge(w); + + REQUIRE(v.size() == 12); +} + +// TEST_CASE("fixed vector references in storage test", "[lang]") +// { +// +// nitro::lang::fixed_vector v(6); +// +// int64_t a = 1; +// int64_t b = 2; +// int64_t c = 3; +// +// v.insert(a); +// v.insert(b); +// v.insert(c); +// +// REQUIRE(v.get<0>() == 1); +// +// a = 4; +// +// REQUIRE(v.get<0>() == 4); +// } \ No newline at end of file From 45edff9707f3929d31e76c1f94ed2041dc4c7bcf Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Fri, 11 Sep 2020 11:30:32 +0200 Subject: [PATCH 03/37] Bug Fix --- include/nitro/lang/fixed_vector.hpp | 2 -- tests/fixed_vector_test.cpp | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 279690b..1d7dc05 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -125,7 +125,6 @@ namespace lang if (array.size() > capacity) raise("Size larger the capacity!"); - size_ = array.size(); capacity_ = capacity; storage_ = std::make_unique(capacity); @@ -138,7 +137,6 @@ namespace lang if (array.size() > capacity) raise("Size larger the capacity!"); - size_ = array.size(); capacity_ = capacity; storage_ = std::make_unique(capacity); diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 0078d9b..b0afec5 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -94,12 +94,12 @@ TEST_CASE("fixed vector at() access", "[lang]") REQUIRE(v.get<2>() == 9); } -TEST_CASE("fixed vector merge two vectors", "[lang]") +TEST_CASE("fixed vector merge two vectors #1", "[lang]") { std::array a = { 0, 1, 2, 3, 4, 5 }; std::array b = { 6, 7, 8, 9, 10, 11 }; - nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector v(12, a); nitro::lang::fixed_vector w(6, b); v.emplace(w.begin(), w.end()); @@ -107,12 +107,12 @@ TEST_CASE("fixed vector merge two vectors", "[lang]") REQUIRE(v.size() == 12); } -TEST_CASE("fixed vector merge two vectors", "[lang]") +TEST_CASE("fixed vector merge two vectors #2", "[lang]") { std::array a = { 0, 1, 2, 3, 4, 5 }; std::array b = { 6, 7, 8, 9, 10, 11 }; - nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector v(12, a); nitro::lang::fixed_vector w(6, b); v.merge(w); From f2f5216081f87527abc4b7e4d0bab8cf3406c52e Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 14 Sep 2020 08:44:28 +0200 Subject: [PATCH 04/37] Buf fix --- include/nitro/lang/fixed_vector.hpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 1d7dc05..6b6888f 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -362,62 +362,62 @@ namespace lang return storage_[size_]; } - fixed_vector::iterator begin() noexcept + typename fixed_vector::iterator begin() noexcept { return iterator(&storage_[0]); } - fixed_vector::reverse_iterator rbegin() noexcept + typename fixed_vector::reverse_iterator rbegin() noexcept { return reverse_iterator(&storage_[size_ - 1]); } - fixed_vector::iterator end() noexcept + typename fixed_vector::iterator end() noexcept { return iterator(&storage_[size_]); } - fixed_vector::reverse_iterator rend() noexcept + typename fixed_vector::reverse_iterator rend() noexcept { return reverse_iterator(&storage_[-1]); } - const fixed_vector::iterator begin() const noexcept + typename const fixed_vector::iterator begin() const noexcept { return iterator(&storage_[0]); } - const fixed_vector::reverse_iterator rbegin() const noexcept + typename const fixed_vector::reverse_iterator rbegin() const noexcept { return reverse_iterator(&storage_[size_ - 1]); } - const fixed_vector::iterator end() const noexcept + typename const fixed_vector::iterator end() const noexcept { return iterator(&storage_[size_]); } - const fixed_vector::reverse_iterator rend() const noexcept + typename const fixed_vector::reverse_iterator rend() const noexcept { return reverse_iterator(&storage_[-1]); } - const fixed_vector::iterator cbegin() const noexcept + typename const fixed_vector::iterator cbegin() const noexcept { return iterator(&storage_[0]); } - const fixed_vector::reverse_iterator crbegin() const noexcept + typename const fixed_vector::reverse_iterator crbegin() const noexcept { return reverse_iterator(&storage_[size_ - 1]); } - const fixed_vector::iterator cend() const noexcept + typename const fixed_vector::iterator cend() const noexcept { return iterator(&storage_[size_]); } - const fixed_vector::reverse_iterator crend() const noexcept + typename const fixed_vector::reverse_iterator crend() const noexcept { return reverse_iterator(&storage_[-1]); } From 8e50565c98ea453c20d4f564593f56d96f76f6af Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 14 Sep 2020 10:35:32 +0200 Subject: [PATCH 05/37] Final Fix --- include/nitro/lang/fixed_vector.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 6b6888f..320c5c7 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -382,42 +382,42 @@ namespace lang return reverse_iterator(&storage_[-1]); } - typename const fixed_vector::iterator begin() const noexcept + const typename fixed_vector::iterator begin() const noexcept { return iterator(&storage_[0]); } - typename const fixed_vector::reverse_iterator rbegin() const noexcept + const typename fixed_vector::reverse_iterator rbegin() const noexcept { return reverse_iterator(&storage_[size_ - 1]); } - typename const fixed_vector::iterator end() const noexcept + const typename fixed_vector::iterator end() const noexcept { return iterator(&storage_[size_]); } - typename const fixed_vector::reverse_iterator rend() const noexcept + const typename fixed_vector::reverse_iterator rend() const noexcept { return reverse_iterator(&storage_[-1]); } - typename const fixed_vector::iterator cbegin() const noexcept + const typename fixed_vector::iterator cbegin() const noexcept { return iterator(&storage_[0]); } - typename const fixed_vector::reverse_iterator crbegin() const noexcept + const typename fixed_vector::reverse_iterator crbegin() const noexcept { return reverse_iterator(&storage_[size_ - 1]); } - typename const fixed_vector::iterator cend() const noexcept + const typename fixed_vector::iterator cend() const noexcept { return iterator(&storage_[size_]); } - typename const fixed_vector::reverse_iterator crend() const noexcept + const typename fixed_vector::reverse_iterator crend() const noexcept { return reverse_iterator(&storage_[-1]); } From 37502826ae717c2c7fe677bd2ac489be7b94ad42 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:27:19 +0200 Subject: [PATCH 06/37] Update include/nitro/lang/fixed_vector.hpp Co-authored-by: Mario Bielert --- include/nitro/lang/fixed_vector.hpp | 38 ++--------------------------- 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 320c5c7..dc3bdbe 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -41,41 +41,7 @@ namespace lang class fixed_vector { public: - class iterator : public std::iterator - { - T* p; - - public: - iterator(T* x) : p(x) - { - } - iterator(const iterator& mit) : p(mit.p) - { - } - iterator& operator++() - { - ++p; - return *this; - } - iterator operator++(int) - { - iterator tmp(*this); - operator++(); - return tmp; - } - bool operator==(const iterator& rhs) const - { - return p == rhs.p; - } - bool operator!=(const iterator& rhs) const - { - return p != rhs.p; - } - T& operator*() - { - return *p; - } - }; + using iterator = typename std::vector::iterator; class reverse_iterator : public std::iterator { @@ -464,4 +430,4 @@ T& get(nitro::lang::fixed_vector& c) noexcept { return c.at(I); } -} // namespace std \ No newline at end of file +} // namespace std From c390b09cdff641d77dfeec517c9c757698337393 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:27:33 +0200 Subject: [PATCH 07/37] Update include/nitro/lang/fixed_vector.hpp Co-authored-by: Mario Bielert --- include/nitro/lang/fixed_vector.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index dc3bdbe..1920e1e 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -85,7 +85,7 @@ namespace lang storage_ = std::make_unique(capacity); } - template + template fixed_vector(const std::size_t& capacity, const iterabel& array) { if (array.size() > capacity) From 6b2d765fa7aed7ec336145507bc213726222a39c Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:34:10 +0200 Subject: [PATCH 08/37] Update include/nitro/lang/fixed_vector.hpp Co-authored-by: Mario Bielert --- include/nitro/lang/fixed_vector.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 1920e1e..d4f90b4 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -86,7 +86,7 @@ namespace lang } template - fixed_vector(const std::size_t& capacity, const iterabel& array) + fixed_vector(std::size_t capacity, const iterabel& array) { if (array.size() > capacity) raise("Size larger the capacity!"); From 8e8106fbb659a919173e33b0cd43aab2d07cb88c Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:39:21 +0200 Subject: [PATCH 09/37] Update include/nitro/lang/fixed_vector.hpp --- include/nitro/lang/fixed_vector.hpp | 121 +++++++++++----------------- 1 file changed, 48 insertions(+), 73 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 320c5c7..ed29c0a 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -116,33 +116,18 @@ namespace lang fixed_vector(const std::size_t& capacity) { capacity_ = capacity; - storage_ = std::make_unique(capacity); + data_ = std::make_shared(capacity); } - template - fixed_vector(const std::size_t& capacity, const iterabel& array) + template + fixed_vector(const std::size_t& capacity, const Iterabel& array) { - if (array.size() > capacity) - raise("Size larger the capacity!"); - capacity_ = capacity; - storage_ = std::make_unique(capacity); + data_ = std::make_shared(capacity); emplace_back(array.begin(), array.end()); } - template - fixed_vector(const std::size_t& capacity, const iterabel& array, const func& depackage_func) - { - if (array.size() > capacity) - raise("Size larger the capacity!"); - - capacity_ = capacity; - storage_ = std::make_unique(capacity); - - emplace_back(array.begin(), array.end(), depackage_func); - } - fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity(), v) { } @@ -177,7 +162,7 @@ namespace lang if (key > size()) raise("Key is larger than size, use append() instead"); - return storage_[key]; + return data_[key]; } T& at(const std::size_t& key) @@ -188,7 +173,7 @@ namespace lang if (key > size()) raise("Key is larger than size, use append() instead"); - return storage_[key]; + return data_[key]; } const T& operator[](const std::size_t& key) const @@ -199,7 +184,7 @@ namespace lang if (key > size()) raise("Key is larger than size, use emplace_back() instead!"); - return storage_[key]; + return data_[key]; } const T& at(const std::size_t& key) const @@ -210,27 +195,27 @@ namespace lang if (key > size()) raise("Key is larger than size, use emplace_back() instead!"); - return storage_[key]; + return data_[key]; } T& front() noexcept { - return storage_[0]; + return data_[0]; } const T& front() const noexcept { - return storage_[0]; + return data_[0]; } T& back() noexcept { - return storage_[size_ - 1]; + return data_[size_ - 1]; } const T& back() const noexcept { - return storage_[size_ - 1]; + return data_[size_ - 1]; } std::size_t emplace(const T& value) @@ -238,14 +223,14 @@ namespace lang return emplace_back(value); } - template - void emplace(iter start, const iter& end) + template + void emplace(Iter start, const Iter& end) { emplace_back(start, end); } - template - void emplace(iter start, const iter& end, func depackage_func) + template + void emplace(Iter start, const Iter& end, func depackage_func) { emplace_back(start, end, depackage_func); } @@ -256,36 +241,21 @@ namespace lang if (size_ >= capacity()) raise("No capacity left!"); - storage_[size_] = T(value); + data_[size_] = T(value); ++size_; return size_ - 1; } - template - void emplace_back(iter start, const iter& end) + template + void emplace_back(Iter start, const Iter& end) { while (start != end) { if (size_ >= capacity()) raise("No capacity left!"); - storage_[size_] = T(*start); - ++size_; - - ++start; - } - } - - template - void emplace_back(iter start, const iter& end, func depackage_func) - { - while (start != end) - { - if (size_ >= capacity()) - raise("No capacity left!"); - - storage_[size_] = T(depackage_func(start)); + data_[size_] = T(*start); ++size_; ++start; @@ -298,36 +268,36 @@ namespace lang if (size_ >= capacity()) raise("No capacity left!"); - storage_[size_] = value; + data_[size_] = value; ++size_; return size_ - 1; } - template - void insert(iter start, const iter& end) + template + void insert(Iter start, const Iter& end) { while (start != end) { if (size_ >= capacity()) raise("No capacity left!"); - storage_[size_] = *start; + data_[size_] = *start; ++size_; ++start; } } - template - void insert(iter start, const iter& end, func depackage_func) + template + void insert(Iter start, const Iter& end, func depackage_func) { while (start != end) { if (size_ >= capacity()) raise("No capacity left!"); - storage_[size_] = depackage_func(start); + data_[size_] = depackage_func(start); ++size_; ++start; @@ -346,7 +316,7 @@ namespace lang if (size_ >= capacity()) raise("No capacity left!"); - storage_[size_] = value; + data_[size_] = value; ++size_; return size_ - 1; @@ -359,67 +329,67 @@ namespace lang raise("Container is empty!"); --size_; - return storage_[size_]; + return data_[size_]; } typename fixed_vector::iterator begin() noexcept { - return iterator(&storage_[0]); + return iterator(&data_[0]); } typename fixed_vector::reverse_iterator rbegin() noexcept { - return reverse_iterator(&storage_[size_ - 1]); + return reverse_iterator(&data_[size_ - 1]); } typename fixed_vector::iterator end() noexcept { - return iterator(&storage_[size_]); + return iterator(&data_[size_]); } typename fixed_vector::reverse_iterator rend() noexcept { - return reverse_iterator(&storage_[-1]); + return reverse_iterator(&data_[-1]); } const typename fixed_vector::iterator begin() const noexcept { - return iterator(&storage_[0]); + return iterator(&data_[0]); } const typename fixed_vector::reverse_iterator rbegin() const noexcept { - return reverse_iterator(&storage_[size_ - 1]); + return reverse_iterator(&data_[size_ - 1]); } const typename fixed_vector::iterator end() const noexcept { - return iterator(&storage_[size_]); + return iterator(&data_[size_]); } const typename fixed_vector::reverse_iterator rend() const noexcept { - return reverse_iterator(&storage_[-1]); + return reverse_iterator(&data_[-1]); } const typename fixed_vector::iterator cbegin() const noexcept { - return iterator(&storage_[0]); + return iterator(&data_[0]); } const typename fixed_vector::reverse_iterator crbegin() const noexcept { - return reverse_iterator(&storage_[size_ - 1]); + return reverse_iterator(&data_[size_ - 1]); } const typename fixed_vector::iterator cend() const noexcept { - return iterator(&storage_[size_]); + return iterator(&data_[size_]); } const typename fixed_vector::reverse_iterator crend() const noexcept { - return reverse_iterator(&storage_[-1]); + return reverse_iterator(&data_[-1]); } template @@ -441,17 +411,22 @@ namespace lang while (key + 1 < size() && key + 1 < capacity()) { - storage_[key] = storage_[key + 1]; + data_[key] = data_[key + 1]; ++key; } --size_; } + std::shared_ptr data() + { + return data_; + } + private: std::size_t size_ = 0; std::size_t capacity_ = 0; - std::unique_ptr storage_; + std::shared_ptr data_; }; } // namespace lang From 42e8d3db017cb4ac08ae0653401125a053bf9d5b Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:41:44 +0200 Subject: [PATCH 10/37] Update include/nitro/lang/fixed_vector.hpp Co-authored-by: Mario Bielert --- include/nitro/lang/fixed_vector.hpp | 36 +---------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 6e1aa12..a864d61 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -43,41 +43,7 @@ namespace lang public: using iterator = typename std::vector::iterator; - class reverse_iterator : public std::iterator - { - T* p; - - public: - reverse_iterator(T* x) : p(x) - { - } - reverse_iterator(const reverse_iterator& mit) : p(mit.p) - { - } - reverse_iterator& operator++() - { - --p; - return *this; - } - reverse_iterator operator++(int) - { - reverse_iterator tmp(*this); - operator++(); - return tmp; - } - bool operator==(const reverse_iterator& rhs) const - { - return p == rhs.p; - } - bool operator!=(const reverse_iterator& rhs) const - { - return p != rhs.p; - } - T& operator*() - { - return *p; - } - }; + using reverse_iterator = typename std::vector::reverse_iterator; fixed_vector(const std::size_t& capacity) { From 5d2b5024c35a97ca2ee7be0fd8ec806f10003ecd Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:43:56 +0200 Subject: [PATCH 11/37] Update --- include/nitro/lang/fixed_vector.hpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 6e1aa12..6889550 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -80,17 +80,14 @@ namespace lang }; fixed_vector(const std::size_t& capacity) + : capacity_(capacity), data_(std::make_shared(capacity)) { - capacity_ = capacity; - data_ = std::make_shared(capacity); } template fixed_vector(std::size_t capacity, const Iterabel& array) + : capacity_(capacity), data_(std::make_shared(capacity)) { - capacity_ = capacity; - data_ = std::make_shared(capacity); - emplace_back(array.begin(), array.end()); } From 37ba9ed2f3dfdde8bf224900df9f09ebb82acbf6 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 09:57:04 +0200 Subject: [PATCH 12/37] Added Move Constructor --- include/nitro/lang/fixed_vector.hpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index ca3a824..e90144d 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -57,13 +57,12 @@ namespace lang emplace_back(array.begin(), array.end()); } - fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity(), v) + fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) { } - fixed_vector operator=(const fixed_vector& v) + fixed_vector(fixed_vector&& v) : capacity_(v.capacity_), data_(std::move(v.data_)) { - return fixed_vector(v.capacity(), v); } ~fixed_vector() = default; @@ -166,7 +165,6 @@ namespace lang std::size_t emplace_back(const T& value) { - if (size_ >= capacity()) raise("No capacity left!"); @@ -193,7 +191,6 @@ namespace lang std::size_t insert(const T& value) { - if (size_ >= capacity()) raise("No capacity left!"); @@ -241,7 +238,6 @@ namespace lang std::size_t push_back(const T& value) { - if (size_ >= capacity()) raise("No capacity left!"); @@ -253,7 +249,6 @@ namespace lang T pop_back(const std::size_t& key) { - if (size_ == 0) raise("Container is empty!"); From 8678d704ef209c25905f60362b321f98223871f3 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 14:34:46 +0200 Subject: [PATCH 13/37] Update --- include/nitro/lang/fixed_vector.hpp | 140 ++++++++++++++-------------- tests/fixed_vector_test.cpp | 39 ++++---- 2 files changed, 91 insertions(+), 88 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index e90144d..53e6fb1 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -45,16 +45,16 @@ namespace lang using reverse_iterator = typename std::vector::reverse_iterator; - fixed_vector(const std::size_t& capacity) - : capacity_(capacity), data_(std::make_shared(capacity)) + fixed_vector(std::size_t capacity) + : capacity_(capacity), data_(std::make_unique(capacity)) { } template fixed_vector(std::size_t capacity, const Iterabel& array) - : capacity_(capacity), data_(std::make_shared(capacity)) + : capacity_(capacity), data_(std::make_unique(capacity)) { - emplace_back(array.begin(), array.end()); + insert(array.begin(), array.end()); } fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) @@ -72,7 +72,7 @@ namespace lang return size_ == 0; } - const std::size_t& size() const + std::size_t size() const { return size_; } @@ -84,21 +84,15 @@ namespace lang T& operator[](const std::size_t& key) { - if (key >= capacity()) - raise("Key is larger than capacity"); - - if (key > size()) - raise("Key is larger than size, use append() instead"); - return data_[key]; } T& at(const std::size_t& key) { - if (key >= capacity()) + if (key >= capacity_) raise("Key is larger than capacity"); - if (key > size()) + if (key > size_) raise("Key is larger than size, use append() instead"); return data_[key]; @@ -106,21 +100,15 @@ namespace lang const T& operator[](const std::size_t& key) const { - if (key >= capacity()) - raise("Key is larger than capacity!"); - - if (key > size()) - raise("Key is larger than size, use emplace_back() instead!"); - return data_[key]; } const T& at(const std::size_t& key) const { - if (key >= capacity()) + if (key >= capacity_) raise("Key is larger than capacity!"); - if (key > size()) + if (key > size_) raise("Key is larger than size, use emplace_back() instead!"); return data_[key]; @@ -146,55 +134,47 @@ namespace lang return data_[size_ - 1]; } - std::size_t emplace(const T& value) + template + void emplace(iterator pos, Args&&... args) { - return emplace_back(value); - } + auto key = std::distance(begin(), pos); - template - void emplace(Iter start, const Iter& end) - { - emplace_back(start, end); - } + if (size_ >= capacity_) + raise("No capacity left!"); - template - void emplace(Iter start, const Iter& end, func depackage_func) - { - emplace_back(start, end, depackage_func); + replace(data_[key], T(args...)); + ++size_; } - std::size_t emplace_back(const T& value) + template + std::size_t emplace_back(Args&&... args) { - if (size_ >= capacity()) + if (size_ >= capacity_) raise("No capacity left!"); - data_[size_] = T(value); + replace(data_[size_], T(args...)); ++size_; return size_ - 1; } - template - void emplace_back(Iter start, const Iter& end) + std::size_t insert(const T& value) { - while (start != end) - { - if (size_ >= capacity()) - raise("No capacity left!"); + if (size_ >= capacity_) + raise("No capacity left!"); - data_[size_] = T(*start); - ++size_; + replace(data_[size_], value); + ++size_; - ++start; - } + return size_ - 1; } - std::size_t insert(const T& value) + std::size_t insert(T&& value) { - if (size_ >= capacity()) + if (size_ >= capacity_) raise("No capacity left!"); - data_[size_] = value; + replace(data_[size_], std::move(value)); ++size_; return size_ - 1; @@ -205,7 +185,7 @@ namespace lang { while (start != end) { - if (size_ >= capacity()) + if (size_ >= capacity_) raise("No capacity left!"); data_[size_] = *start; @@ -220,7 +200,7 @@ namespace lang { while (start != end) { - if (size_ >= capacity()) + if (size_ >= capacity_) raise("No capacity left!"); data_[size_] = depackage_func(start); @@ -230,15 +210,15 @@ namespace lang } } - template - void merge(iterabel array) + template + void merge(Iterabel array) { - emplace_back(array.begin(), array.end()); + insert(array.begin(), array.end()); } std::size_t push_back(const T& value) { - if (size_ >= capacity()) + if (size_ >= capacity_) raise("No capacity left!"); data_[size_] = value; @@ -316,41 +296,61 @@ namespace lang return reverse_iterator(&data_[-1]); } - template - T& get() noexcept + void erase(std::size_t key) { - return this->at(I); - } + if (key >= size_) + raise("Key does not exsist!"); - template - const T& get() const noexcept - { - return this->at(I); + while (key + 1 < size_ && key + 1 < capacity_) + { + replace(data_[key], std::forward(data_[key + 1])); + ++key; + } + --size_; } - void erase(std::size_t key) + void erase(iterator pos) { - if (key >= size()) + auto key = std::distance(begin(), pos); + + if (key >= size_) raise("Key does not exsist!"); - while (key + 1 < size() && key + 1 < capacity()) + while (key + 1 < size_ && key + 1 < capacity_) { - data_[key] = data_[key + 1]; + replace(data_[key], data_[key + 1]); ++key; } --size_; } - std::shared_ptr data() + T* data() noexcept + { + return data_.get(); + } + + const T* data() const noexcept { - return data_; + return data_.get(); } private: std::size_t size_ = 0; std::size_t capacity_ = 0; - std::shared_ptr data_; + std::unique_ptr data_; + + template + std::enable_if_t::value> replace(A& a, A&& b) + { + a = std::move(b); + } + + template + std::enable_if_t::value> replace(A& a, const A& b) + { + a = b; + } }; } // namespace lang diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index b0afec5..e0cbf8a 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -67,7 +67,7 @@ TEST_CASE("fixed vector std::get access", "[lang]") nitro::lang::fixed_vector v(6, a); REQUIRE(std::get<2>(v) == 2); - REQUIRE(v.get<3>() == 3); + REQUIRE(std::get<3>(v) == 3); } TEST_CASE("fixed vector bracket access", "[lang]") @@ -78,8 +78,8 @@ TEST_CASE("fixed vector bracket access", "[lang]") v[0] = 7; v[2] = 9; - REQUIRE(v.get<0>() == 7); - REQUIRE(v.get<2>() == 9); + REQUIRE(v[0] == 7); + REQUIRE(v[2] == 9); } TEST_CASE("fixed vector at() access", "[lang]") @@ -90,8 +90,8 @@ TEST_CASE("fixed vector at() access", "[lang]") v.at(0) = 7; v.at(2) = 9; - REQUIRE(v.get<0>() == 7); - REQUIRE(v.get<2>() == 9); + REQUIRE(v.at(0) == 7); + REQUIRE(v.at(2) == 9); } TEST_CASE("fixed vector merge two vectors #1", "[lang]") @@ -102,7 +102,7 @@ TEST_CASE("fixed vector merge two vectors #1", "[lang]") nitro::lang::fixed_vector v(12, a); nitro::lang::fixed_vector w(6, b); - v.emplace(w.begin(), w.end()); + v.insert(w.begin(), w.end()); REQUIRE(v.size() == 12); } @@ -120,22 +120,25 @@ TEST_CASE("fixed vector merge two vectors #2", "[lang]") REQUIRE(v.size() == 12); } +TEST_CASE("fixed vector with unique ptr", "[lang]") +{ + nitro::lang::fixed_vector> v(2); + v.insert(std::move(std::make_unique(5))); + v.insert(std::move(std::make_unique(6))); + + REQUIRE_NOTHROW(v.erase(0)); +} + // TEST_CASE("fixed vector references in storage test", "[lang]") // { -// -// nitro::lang::fixed_vector v(6); -// +// nitro::lang::fixed_vector> v(6); // int64_t a = 1; // int64_t b = 2; // int64_t c = 3; -// -// v.insert(a); -// v.insert(b); -// v.insert(c); -// -// REQUIRE(v.get<0>() == 1); -// +// v.emplace_back(std::ref(a)); +// v.emplace_back(std::ref(b)); +// v.emplace_back(std::ref(c)); +// REQUIRE(v[0] == 1); // a = 4; -// -// REQUIRE(v.get<0>() == 4); +// REQUIRE(v[0] == 4); // } \ No newline at end of file From 465e7e32065ae2b617c38280287514833c5a0b39 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Mon, 28 Sep 2020 14:51:19 +0200 Subject: [PATCH 14/37] Final Fix --- include/nitro/lang/fixed_vector.hpp | 40 +++++++++-------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 53e6fb1..42f83e8 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015-2017, Technische Universität Dresden, Germany + * Copyright (c) 2015-2020, Technische Universität Dresden, Germany * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted @@ -152,7 +152,7 @@ namespace lang if (size_ >= capacity_) raise("No capacity left!"); - replace(data_[size_], T(args...)); + replace(data_[size_], T(std::forward(args)...)); ++size_; return size_ - 1; @@ -181,41 +181,26 @@ namespace lang } template - void insert(Iter start, const Iter& end) + void insert(Iter pos, Iter start, Iter end) { - while (start != end) - { - if (size_ >= capacity_) - raise("No capacity left!"); - - data_[size_] = *start; - ++size_; - - ++start; - } - } + auto key = std::distance(begin(), pos); + if (key > size_) + raise("Key larger than size!"); - template - void insert(Iter start, const Iter& end, func depackage_func) - { while (start != end) { - if (size_ >= capacity_) + if (key >= capacity_) raise("No capacity left!"); - data_[size_] = depackage_func(start); - ++size_; + data_[key] = *start; + + if (key == size_) + ++size_; ++start; } } - template - void merge(Iterabel array) - { - insert(array.begin(), array.end()); - } - std::size_t push_back(const T& value) { if (size_ >= capacity_) @@ -227,13 +212,12 @@ namespace lang return size_ - 1; } - T pop_back(const std::size_t& key) + void pop_back() { if (size_ == 0) raise("Container is empty!"); --size_; - return data_[size_]; } typename fixed_vector::iterator begin() noexcept From b9fc33461a192bb408795018b058094103617683 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 1 Oct 2020 09:23:33 +0200 Subject: [PATCH 15/37] Fix --- include/nitro/lang/fixed_vector.hpp | 6 ++++++ tests/fixed_vector_test.cpp | 15 +-------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 42f83e8..af46b6e 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -212,6 +212,12 @@ namespace lang return size_ - 1; } + template + void push_back(Iter start, Iter end) + { + insert(this.end(), start, end) + } + void pop_back() { if (size_ == 0) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index e0cbf8a..2f56e24 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -102,20 +102,7 @@ TEST_CASE("fixed vector merge two vectors #1", "[lang]") nitro::lang::fixed_vector v(12, a); nitro::lang::fixed_vector w(6, b); - v.insert(w.begin(), w.end()); - - REQUIRE(v.size() == 12); -} - -TEST_CASE("fixed vector merge two vectors #2", "[lang]") -{ - std::array a = { 0, 1, 2, 3, 4, 5 }; - std::array b = { 6, 7, 8, 9, 10, 11 }; - - nitro::lang::fixed_vector v(12, a); - nitro::lang::fixed_vector w(6, b); - - v.merge(w); + v.push_back(w.begin(), w.end()); REQUIRE(v.size() == 12); } From 7fba477728da7c31f84e7d16077be8b110aeea40 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 1 Oct 2020 09:34:07 +0200 Subject: [PATCH 16/37] Fix --- include/nitro/lang/fixed_vector.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index af46b6e..3902b16 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -54,7 +54,7 @@ namespace lang fixed_vector(std::size_t capacity, const Iterabel& array) : capacity_(capacity), data_(std::make_unique(capacity)) { - insert(array.begin(), array.end()); + push_back(array.begin(), array.end()); } fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) @@ -215,7 +215,7 @@ namespace lang template void push_back(Iter start, Iter end) { - insert(this.end(), start, end) + insert(this->end(), start, end) } void pop_back() From 0b83064945f7a47741c50e29e350b427cec2729a Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 1 Oct 2020 09:57:52 +0200 Subject: [PATCH 17/37] Bug Fix --- include/nitro/lang/fixed_vector.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 3902b16..8ff799b 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -54,7 +54,7 @@ namespace lang fixed_vector(std::size_t capacity, const Iterabel& array) : capacity_(capacity), data_(std::make_unique(capacity)) { - push_back(array.begin(), array.end()); + insert(this->begin(), array.begin(), array.end()); } fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) @@ -181,9 +181,9 @@ namespace lang } template - void insert(Iter pos, Iter start, Iter end) + void insert(iterator pos, Iter start, Iter end) { - auto key = std::distance(begin(), pos); + std::size_t key = std::distance(begin(), pos); if (key > size_) raise("Key larger than size!"); @@ -215,7 +215,7 @@ namespace lang template void push_back(Iter start, Iter end) { - insert(this->end(), start, end) + insert(this->end(), start, end); } void pop_back() From bcb42494afc3f8d24e747a232a60f26e1f14353b Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 1 Oct 2020 10:13:11 +0200 Subject: [PATCH 18/37] Update --- include/nitro/lang/fixed_vector.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 8ff799b..50aa998 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -197,6 +197,7 @@ namespace lang if (key == size_) ++size_; + ++key; ++start; } } From ea8b1462aa2d07e4fd35253fc3b2c1d9113dc832 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 1 Oct 2020 12:23:56 +0200 Subject: [PATCH 19/37] Fix --- include/nitro/lang/fixed_vector.hpp | 58 ++++++++++++++--------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 50aa998..1133b5b 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -41,10 +41,6 @@ namespace lang class fixed_vector { public: - using iterator = typename std::vector::iterator; - - using reverse_iterator = typename std::vector::reverse_iterator; - fixed_vector(std::size_t capacity) : capacity_(capacity), data_(std::make_unique(capacity)) { @@ -135,7 +131,7 @@ namespace lang } template - void emplace(iterator pos, Args&&... args) + void emplace(T* const pos, Args&&... args) { auto key = std::distance(begin(), pos); @@ -181,7 +177,7 @@ namespace lang } template - void insert(iterator pos, Iter start, Iter end) + void insert(T* const pos, Iter start, Iter end) { std::size_t key = std::distance(begin(), pos); if (key > size_) @@ -227,64 +223,64 @@ namespace lang --size_; } - typename fixed_vector::iterator begin() noexcept + T* begin() noexcept { - return iterator(&data_[0]); + return &data_[0]; } - typename fixed_vector::reverse_iterator rbegin() noexcept + T* rbegin() noexcept { - return reverse_iterator(&data_[size_ - 1]); + return &data_[size_ - 1]; } - typename fixed_vector::iterator end() noexcept + T* end() noexcept { - return iterator(&data_[size_]); + return &data_[size_]; } - typename fixed_vector::reverse_iterator rend() noexcept + T* rend() noexcept { - return reverse_iterator(&data_[-1]); + return &data_[-1]; } - const typename fixed_vector::iterator begin() const noexcept + const T* begin() const noexcept { - return iterator(&data_[0]); + return &data_[0]; } - const typename fixed_vector::reverse_iterator rbegin() const noexcept + const T* rbegin() const noexcept { - return reverse_iterator(&data_[size_ - 1]); + return &data_[size_ - 1]; } - const typename fixed_vector::iterator end() const noexcept + const T* end() const noexcept { - return iterator(&data_[size_]); + return &data_[size_]; } - const typename fixed_vector::reverse_iterator rend() const noexcept + const T* rend() const noexcept { - return reverse_iterator(&data_[-1]); + return &data_[-1]; } - const typename fixed_vector::iterator cbegin() const noexcept + const T* cbegin() const noexcept { - return iterator(&data_[0]); + return &data_[0]; } - const typename fixed_vector::reverse_iterator crbegin() const noexcept + const T* crbegin() const noexcept { - return reverse_iterator(&data_[size_ - 1]); + return &data_[size_ - 1]; } - const typename fixed_vector::iterator cend() const noexcept + const T* cend() const noexcept { - return iterator(&data_[size_]); + return &data_[size_]; } - const typename fixed_vector::reverse_iterator crend() const noexcept + const T* crend() const noexcept { - return reverse_iterator(&data_[-1]); + return &data_[-1]; } void erase(std::size_t key) @@ -300,7 +296,7 @@ namespace lang --size_; } - void erase(iterator pos) + void erase(T* pos) { auto key = std::distance(begin(), pos); From 0d2fed8b4ab3a6e199dd636bbde6e4f5d824c3d1 Mon Sep 17 00:00:00 2001 From: Julian Kruchy Date: Thu, 1 Oct 2020 12:57:19 +0200 Subject: [PATCH 20/37] Fix --- include/nitro/lang/fixed_vector.hpp | 17 ++--------------- tests/fixed_vector_test.cpp | 4 ++-- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 1133b5b..e9ac587 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -283,29 +283,16 @@ namespace lang return &data_[-1]; } - void erase(std::size_t key) - { - if (key >= size_) - raise("Key does not exsist!"); - - while (key + 1 < size_ && key + 1 < capacity_) - { - replace(data_[key], std::forward(data_[key + 1])); - ++key; - } - --size_; - } - void erase(T* pos) { - auto key = std::distance(begin(), pos); + std::size_t key = std::distance(begin(), pos); if (key >= size_) raise("Key does not exsist!"); while (key + 1 < size_ && key + 1 < capacity_) { - replace(data_[key], data_[key + 1]); + replace(data_[key], std::forward(data_[key + 1])); ++key; } --size_; diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 2f56e24..cb18ab3 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -30,7 +30,7 @@ TEST_CASE("fixed vector erase", "[lang]") v.emplace_back(5); v.emplace_back(7); - v.erase(0); + v.erase(v.begin()); REQUIRE(v.back() == 7); REQUIRE(v.front() == 7); @@ -113,7 +113,7 @@ TEST_CASE("fixed vector with unique ptr", "[lang]") v.insert(std::move(std::make_unique(5))); v.insert(std::move(std::make_unique(6))); - REQUIRE_NOTHROW(v.erase(0)); + REQUIRE_NOTHROW(v.erase(v.begin())); } // TEST_CASE("fixed vector references in storage test", "[lang]") From 5572cd523efa0c45c1c89c7a80ce3c6f4e73a412 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Mon, 4 Jan 2021 10:51:22 +0100 Subject: [PATCH 21/37] Added operator= and initializer_list --- include/nitro/lang/fixed_vector.hpp | 27 +++++++++++++++++++++++++++ tests/fixed_vector_test.cpp | 1 + 2 files changed, 28 insertions(+) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index e9ac587..d625fc5 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -53,6 +53,12 @@ namespace lang insert(this->begin(), array.begin(), array.end()); } + fixed_vector(const std::initializer_list& list) + : capacity_(list.size()), data_(std::make_unique(capacity)) + { + insert(this->begin(), list.begin(), list.end()); + } + fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) { } @@ -61,6 +67,22 @@ namespace lang { } + fixed_vector operator=(const fixed_vector& v) + { + return fixed_vector(v); + } + + fixed_vector operator=(fixed_vector&& v) + { + return fixed_vector(std::move(v)); + } + + fixed_vector operator=(const std::initializer_list& l) + { + return fixed_vector(l.size(), l); + } + + ~fixed_vector() = default; bool empty() const @@ -198,6 +220,11 @@ namespace lang } } + void insert(T* const pos, std::initializer_list& list) + { + insert(pos, list.begin(), list.end()); + } + std::size_t push_back(const T& value) { if (size_ >= capacity_) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index cb18ab3..e222556 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -20,6 +20,7 @@ TEST_CASE("fixed vector read and write", "[lang]") REQUIRE(v.size() == 2); REQUIRE(v[0] == 5); + REQUIRE(v.front() == 5); REQUIRE(v.at(1) == 7); REQUIRE(v.back() == 7); } From e49334d397157926fcc32b4d0b04cb04a54fd7b5 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Mon, 4 Jan 2021 11:18:56 +0100 Subject: [PATCH 22/37] Build fix for macOS and Clang --- include/nitro/lang/fixed_vector.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index d625fc5..b48a629 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -54,7 +54,7 @@ namespace lang } fixed_vector(const std::initializer_list& list) - : capacity_(list.size()), data_(std::make_unique(capacity)) + : capacity_(list.size()), data_(std::make_unique(list.size())) { insert(this->begin(), list.begin(), list.end()); } From a48d64ae231e7afad32ca7c75679463d08bccfb8 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Mon, 4 Jan 2021 11:19:46 +0100 Subject: [PATCH 23/37] Code Formatting --- include/nitro/lang/fixed_vector.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index b48a629..340d99a 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -82,7 +82,6 @@ namespace lang return fixed_vector(l.size(), l); } - ~fixed_vector() = default; bool empty() const From 3efd5bfe2d78ae2b84efa4065d45d174194ce473 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Tue, 5 Jan 2021 12:29:31 +0100 Subject: [PATCH 24/37] constexpr (requires Cppstd 20) --- include/nitro/lang/fixed_vector.hpp | 90 ++++++++++++++--------------- tests/fixed_vector_test.cpp | 6 +- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 340d99a..80ba70e 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -41,70 +41,70 @@ namespace lang class fixed_vector { public: - fixed_vector(std::size_t capacity) + constexpr fixed_vector(std::size_t capacity) : capacity_(capacity), data_(std::make_unique(capacity)) { } template - fixed_vector(std::size_t capacity, const Iterabel& array) + constexpr fixed_vector(std::size_t capacity, const Iterabel& array) : capacity_(capacity), data_(std::make_unique(capacity)) { insert(this->begin(), array.begin(), array.end()); } - fixed_vector(const std::initializer_list& list) + constexpr fixed_vector(const std::initializer_list& list) : capacity_(list.size()), data_(std::make_unique(list.size())) { insert(this->begin(), list.begin(), list.end()); } - fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) + constexpr fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) { } - fixed_vector(fixed_vector&& v) : capacity_(v.capacity_), data_(std::move(v.data_)) + constexpr fixed_vector(fixed_vector&& v) : capacity_(v.capacity_), data_(std::move(v.data_)) { } - fixed_vector operator=(const fixed_vector& v) + constexpr fixed_vector operator=(const fixed_vector& v) { return fixed_vector(v); } - fixed_vector operator=(fixed_vector&& v) + constexpr fixed_vector operator=(fixed_vector&& v) { return fixed_vector(std::move(v)); } - fixed_vector operator=(const std::initializer_list& l) + constexpr fixed_vector operator=(const std::initializer_list& l) { return fixed_vector(l.size(), l); } ~fixed_vector() = default; - bool empty() const + constexpr bool empty() const { return size_ == 0; } - std::size_t size() const + constexpr std::size_t size() const { return size_; } - std::size_t capacity() const + constexpr std::size_t capacity() const { return capacity_; } - T& operator[](const std::size_t& key) + constexpr T& operator[](const std::size_t& key) { return data_[key]; } - T& at(const std::size_t& key) + constexpr T& at(const std::size_t& key) { if (key >= capacity_) raise("Key is larger than capacity"); @@ -115,12 +115,12 @@ namespace lang return data_[key]; } - const T& operator[](const std::size_t& key) const + constexpr const T& operator[](const std::size_t& key) const { return data_[key]; } - const T& at(const std::size_t& key) const + constexpr const T& at(const std::size_t& key) const { if (key >= capacity_) raise("Key is larger than capacity!"); @@ -131,28 +131,28 @@ namespace lang return data_[key]; } - T& front() noexcept + constexpr T& front() noexcept { return data_[0]; } - const T& front() const noexcept + constexpr const T& front() const noexcept { return data_[0]; } - T& back() noexcept + constexpr T& back() noexcept { return data_[size_ - 1]; } - const T& back() const noexcept + constexpr const T& back() const noexcept { return data_[size_ - 1]; } template - void emplace(T* const pos, Args&&... args) + constexpr void emplace(T* const pos, Args&&... args) { auto key = std::distance(begin(), pos); @@ -164,7 +164,7 @@ namespace lang } template - std::size_t emplace_back(Args&&... args) + constexpr std::size_t emplace_back(Args&&... args) { if (size_ >= capacity_) raise("No capacity left!"); @@ -175,7 +175,7 @@ namespace lang return size_ - 1; } - std::size_t insert(const T& value) + constexpr std::size_t insert(const T& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -186,7 +186,7 @@ namespace lang return size_ - 1; } - std::size_t insert(T&& value) + constexpr std::size_t insert(T&& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -198,7 +198,7 @@ namespace lang } template - void insert(T* const pos, Iter start, Iter end) + constexpr void insert(T* const pos, Iter start, Iter end) { std::size_t key = std::distance(begin(), pos); if (key > size_) @@ -219,12 +219,12 @@ namespace lang } } - void insert(T* const pos, std::initializer_list& list) + constexpr void insert(T* const pos, std::initializer_list& list) { insert(pos, list.begin(), list.end()); } - std::size_t push_back(const T& value) + constexpr std::size_t push_back(const T& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -236,12 +236,12 @@ namespace lang } template - void push_back(Iter start, Iter end) + constexpr void push_back(Iter start, Iter end) { insert(this->end(), start, end); } - void pop_back() + constexpr void pop_back() { if (size_ == 0) raise("Container is empty!"); @@ -249,67 +249,67 @@ namespace lang --size_; } - T* begin() noexcept + constexpr T* begin() noexcept { return &data_[0]; } - T* rbegin() noexcept + constexpr T* rbegin() noexcept { return &data_[size_ - 1]; } - T* end() noexcept + constexpr T* end() noexcept { return &data_[size_]; } - T* rend() noexcept + constexpr T* rend() noexcept { return &data_[-1]; } - const T* begin() const noexcept + constexpr const T* begin() const noexcept { return &data_[0]; } - const T* rbegin() const noexcept + constexpr const T* rbegin() const noexcept { return &data_[size_ - 1]; } - const T* end() const noexcept + constexpr const T* end() const noexcept { return &data_[size_]; } - const T* rend() const noexcept + constexpr const T* rend() const noexcept { return &data_[-1]; } - const T* cbegin() const noexcept + constexpr const T* cbegin() const noexcept { return &data_[0]; } - const T* crbegin() const noexcept + constexpr const T* crbegin() const noexcept { return &data_[size_ - 1]; } - const T* cend() const noexcept + constexpr const T* cend() const noexcept { return &data_[size_]; } - const T* crend() const noexcept + constexpr const T* crend() const noexcept { return &data_[-1]; } - void erase(T* pos) + constexpr void erase(T* pos) { std::size_t key = std::distance(begin(), pos); @@ -324,12 +324,12 @@ namespace lang --size_; } - T* data() noexcept + constexpr T* data() noexcept { return data_.get(); } - const T* data() const noexcept + constexpr const T* data() const noexcept { return data_.get(); } @@ -341,13 +341,13 @@ namespace lang std::unique_ptr data_; template - std::enable_if_t::value> replace(A& a, A&& b) + constexpr std::enable_if_t::value> replace(A& a, A&& b) { a = std::move(b); } template - std::enable_if_t::value> replace(A& a, const A& b) + constexpr std::enable_if_t::value> replace(A& a, const A& b) { a = b; } diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index e222556..bf71c78 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -97,11 +97,11 @@ TEST_CASE("fixed vector at() access", "[lang]") TEST_CASE("fixed vector merge two vectors #1", "[lang]") { - std::array a = { 0, 1, 2, 3, 4, 5 }; - std::array b = { 6, 7, 8, 9, 10, 11 }; + constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr std::array b = { 6, 7, 8, 9, 10, 11 }; nitro::lang::fixed_vector v(12, a); - nitro::lang::fixed_vector w(6, b); + constexpr nitro::lang::fixed_vector w(6, b); v.push_back(w.begin(), w.end()); From af50e9a330d45f3c3ae61acb2f0ca01fd1e16620 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Tue, 5 Jan 2021 12:36:37 +0100 Subject: [PATCH 25/37] Formatted --- include/nitro/lang/fixed_vector.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 80ba70e..a0fce0c 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -63,7 +63,8 @@ namespace lang { } - constexpr fixed_vector(fixed_vector&& v) : capacity_(v.capacity_), data_(std::move(v.data_)) + constexpr fixed_vector(fixed_vector&& v) + : capacity_(v.capacity_), data_(std::move(v.data_)) { } From ebb0b6d06a7f5f15dfb7d07edb96f0de35631891 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Tue, 5 Jan 2021 12:39:35 +0100 Subject: [PATCH 26/37] More constexpr --- tests/fixed_vector_test.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index bf71c78..14e69b4 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -5,7 +5,7 @@ TEST_CASE("fixed vector init", "[lang]") { - nitro::lang::fixed_vector v(10); + constexpr nitro::lang::fixed_vector v(10); REQUIRE(v.size() == 0); REQUIRE(v.capacity() == 10); @@ -40,7 +40,7 @@ TEST_CASE("fixed vector erase", "[lang]") TEST_CASE("fixed vector array assignment", "[lang]") { - std::array a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + constexpr std::array a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; nitro::lang::fixed_vector v(10, a); @@ -51,7 +51,7 @@ TEST_CASE("fixed vector array assignment", "[lang]") TEST_CASE("fixed vector array assignment with less elements", "[lang]") { - std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; nitro::lang::fixed_vector v(10, a); @@ -63,7 +63,7 @@ TEST_CASE("fixed vector array assignment with less elements", "[lang]") TEST_CASE("fixed vector std::get access", "[lang]") { - std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; nitro::lang::fixed_vector v(6, a); @@ -73,7 +73,7 @@ TEST_CASE("fixed vector std::get access", "[lang]") TEST_CASE("fixed vector bracket access", "[lang]") { - std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; nitro::lang::fixed_vector v(6, a); @@ -85,7 +85,7 @@ TEST_CASE("fixed vector bracket access", "[lang]") TEST_CASE("fixed vector at() access", "[lang]") { - std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; nitro::lang::fixed_vector v(6, a); From 41ef99e8832191c19544d4ccddfd23265cdd7a81 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Mon, 25 Jan 2021 11:47:36 +0100 Subject: [PATCH 27/37] Added types --- include/nitro/lang/fixed_vector.hpp | 12 ++++++++++++ tests/fixed_vector_test.cpp | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index a0fce0c..735ebeb 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -41,6 +41,18 @@ namespace lang class fixed_vector { public: + using value_type = T; + using pointer = T*; + using const_pointer = const T*; + using reference = value_type&; + using const_reference = const value_type&; + using size_type = std::size_t; + // using difference_type = /* implementation-defined */; + using iterator = T*; + using const_iterator = const T*; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + constexpr fixed_vector(std::size_t capacity) : capacity_(capacity), data_(std::make_unique(capacity)) { diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 14e69b4..972cb5c 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -5,7 +5,7 @@ TEST_CASE("fixed vector init", "[lang]") { - constexpr nitro::lang::fixed_vector v(10); + nitro::lang::fixed_vector v(10); REQUIRE(v.size() == 0); REQUIRE(v.capacity() == 10); @@ -101,7 +101,7 @@ TEST_CASE("fixed vector merge two vectors #1", "[lang]") constexpr std::array b = { 6, 7, 8, 9, 10, 11 }; nitro::lang::fixed_vector v(12, a); - constexpr nitro::lang::fixed_vector w(6, b); + nitro::lang::fixed_vector w(6, b); v.push_back(w.begin(), w.end()); From a494721d2fac7096991320d4ea8d7957fad7fc53 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Mon, 25 Jan 2021 11:48:52 +0100 Subject: [PATCH 28/37] Code Formatting --- include/nitro/lang/fixed_vector.hpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 735ebeb..16c7bbe 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -41,16 +41,15 @@ namespace lang class fixed_vector { public: - using value_type = T; - using pointer = T*; - using const_pointer = const T*; - using reference = value_type&; - using const_reference = const value_type&; - using size_type = std::size_t; - // using difference_type = /* implementation-defined */; - using iterator = T*; - using const_iterator = const T*; - using reverse_iterator = std::reverse_iterator; + using value_type = T; + using pointer = T*; + using const_pointer = const T*; + using reference = value_type&; + using const_reference = const value_type&; + using size_type = std::size_t; + using iterator = T*; + using const_iterator = const T*; + using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; constexpr fixed_vector(std::size_t capacity) From ad356060ec8443f306386fc5fb1b866e9b915176 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Tue, 26 Jan 2021 13:24:34 +0100 Subject: [PATCH 29/37] Added usings --- include/nitro/lang/fixed_vector.hpp | 88 ++++++++++++++--------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 16c7bbe..9fdbacc 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -42,39 +42,39 @@ namespace lang { public: using value_type = T; - using pointer = T*; - using const_pointer = const T*; + using pointer = value_type*; + using const_pointer = const value_type*; using reference = value_type&; using const_reference = const value_type&; using size_type = std::size_t; - using iterator = T*; - using const_iterator = const T*; + using iterator = value_type*; + using const_iterator = const value_type*; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; constexpr fixed_vector(std::size_t capacity) - : capacity_(capacity), data_(std::make_unique(capacity)) + : capacity_(capacity), data_(std::make_unique(capacity)) { } template constexpr fixed_vector(std::size_t capacity, const Iterabel& array) - : capacity_(capacity), data_(std::make_unique(capacity)) + : capacity_(capacity), data_(std::make_unique(capacity)) { insert(this->begin(), array.begin(), array.end()); } - constexpr fixed_vector(const std::initializer_list& list) - : capacity_(list.size()), data_(std::make_unique(list.size())) + constexpr fixed_vector(const std::initializer_list& list) + : capacity_(list.size()), data_(std::make_unique(list.size())) { insert(this->begin(), list.begin(), list.end()); } - constexpr fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) + constexpr fixed_vector(const fixed_vector& v) : fixed_vector(v.capacity_, v) { } - constexpr fixed_vector(fixed_vector&& v) + constexpr fixed_vector(fixed_vector&& v) : capacity_(v.capacity_), data_(std::move(v.data_)) { } @@ -89,7 +89,7 @@ namespace lang return fixed_vector(std::move(v)); } - constexpr fixed_vector operator=(const std::initializer_list& l) + constexpr fixed_vector operator=(const std::initializer_list& l) { return fixed_vector(l.size(), l); } @@ -111,12 +111,12 @@ namespace lang return capacity_; } - constexpr T& operator[](const std::size_t& key) + constexpr value_type& operator[](const std::size_t& key) { return data_[key]; } - constexpr T& at(const std::size_t& key) + constexpr value_type& at(const std::size_t& key) { if (key >= capacity_) raise("Key is larger than capacity"); @@ -127,12 +127,12 @@ namespace lang return data_[key]; } - constexpr const T& operator[](const std::size_t& key) const + constexpr const value_type& operator[](const std::size_t& key) const { return data_[key]; } - constexpr const T& at(const std::size_t& key) const + constexpr const value_type& at(const std::size_t& key) const { if (key >= capacity_) raise("Key is larger than capacity!"); @@ -143,35 +143,35 @@ namespace lang return data_[key]; } - constexpr T& front() noexcept + constexpr value_type& front() noexcept { return data_[0]; } - constexpr const T& front() const noexcept + constexpr const value_type& front() const noexcept { return data_[0]; } - constexpr T& back() noexcept + constexpr value_type& back() noexcept { return data_[size_ - 1]; } - constexpr const T& back() const noexcept + constexpr const value_type& back() const noexcept { return data_[size_ - 1]; } template - constexpr void emplace(T* const pos, Args&&... args) + constexpr void emplace(value_type* const pos, Args&&... args) { auto key = std::distance(begin(), pos); if (size_ >= capacity_) raise("No capacity left!"); - replace(data_[key], T(args...)); + replace(data_[key], value_type(args...)); ++size_; } @@ -181,13 +181,13 @@ namespace lang if (size_ >= capacity_) raise("No capacity left!"); - replace(data_[size_], T(std::forward(args)...)); + replace(data_[size_], value_type(std::forward(args)...)); ++size_; return size_ - 1; } - constexpr std::size_t insert(const T& value) + constexpr std::size_t insert(const value_type& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -198,7 +198,7 @@ namespace lang return size_ - 1; } - constexpr std::size_t insert(T&& value) + constexpr std::size_t insert(value_type&& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -210,7 +210,7 @@ namespace lang } template - constexpr void insert(T* const pos, Iter start, Iter end) + constexpr void insert(value_type* const pos, Iter start, Iter end) { std::size_t key = std::distance(begin(), pos); if (key > size_) @@ -231,12 +231,12 @@ namespace lang } } - constexpr void insert(T* const pos, std::initializer_list& list) + constexpr void insert(value_type* const pos, std::initializer_list& list) { insert(pos, list.begin(), list.end()); } - constexpr std::size_t push_back(const T& value) + constexpr std::size_t push_back(const value_type& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -261,67 +261,67 @@ namespace lang --size_; } - constexpr T* begin() noexcept + constexpr value_type* begin() noexcept { return &data_[0]; } - constexpr T* rbegin() noexcept + constexpr value_type* rbegin() noexcept { return &data_[size_ - 1]; } - constexpr T* end() noexcept + constexpr value_type* end() noexcept { return &data_[size_]; } - constexpr T* rend() noexcept + constexpr value_type* rend() noexcept { return &data_[-1]; } - constexpr const T* begin() const noexcept + constexpr const value_type* begin() const noexcept { return &data_[0]; } - constexpr const T* rbegin() const noexcept + constexpr const value_type* rbegin() const noexcept { return &data_[size_ - 1]; } - constexpr const T* end() const noexcept + constexpr const value_type* end() const noexcept { return &data_[size_]; } - constexpr const T* rend() const noexcept + constexpr const value_type* rend() const noexcept { return &data_[-1]; } - constexpr const T* cbegin() const noexcept + constexpr const value_type* cbegin() const noexcept { return &data_[0]; } - constexpr const T* crbegin() const noexcept + constexpr const value_type* crbegin() const noexcept { return &data_[size_ - 1]; } - constexpr const T* cend() const noexcept + constexpr const value_type* cend() const noexcept { return &data_[size_]; } - constexpr const T* crend() const noexcept + constexpr const value_type* crend() const noexcept { return &data_[-1]; } - constexpr void erase(T* pos) + constexpr void erase(value_type* pos) { std::size_t key = std::distance(begin(), pos); @@ -330,18 +330,18 @@ namespace lang while (key + 1 < size_ && key + 1 < capacity_) { - replace(data_[key], std::forward(data_[key + 1])); + replace(data_[key], std::forward(data_[key + 1])); ++key; } --size_; } - constexpr T* data() noexcept + constexpr value_type* data() noexcept { return data_.get(); } - constexpr const T* data() const noexcept + constexpr const value_type* data() const noexcept { return data_.get(); } @@ -350,7 +350,7 @@ namespace lang std::size_t size_ = 0; std::size_t capacity_ = 0; - std::unique_ptr data_; + std::unique_ptr data_; template constexpr std::enable_if_t::value> replace(A& a, A&& b) From 9220b1c1428c6dc71571d9e2f57c348011f095d4 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Tue, 26 Jan 2021 13:33:07 +0100 Subject: [PATCH 30/37] Added test for init list --- tests/fixed_vector_test.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 972cb5c..93e7bb5 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -108,6 +108,13 @@ TEST_CASE("fixed vector merge two vectors #1", "[lang]") REQUIRE(v.size() == 12); } +TEST_CASE("fixed vector init with init list", "[lang]") +{ + nitro::lang::fixed_vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + + REQUIRE(v.size() == 12); +} + TEST_CASE("fixed vector with unique ptr", "[lang]") { nitro::lang::fixed_vector> v(2); From 9356b7d5a9b106f797361d0829a76e351eaf946f Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Tue, 26 Jan 2021 13:33:22 +0100 Subject: [PATCH 31/37] Added more constexpr --- tests/fixed_vector_test.cpp | 55 ++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 93e7bb5..19b7c0f 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -5,7 +5,9 @@ TEST_CASE("fixed vector init", "[lang]") { - nitro::lang::fixed_vector v(10); + constexpr int SIZE = 10; + + nitro::lang::fixed_vector v(SIZE); REQUIRE(v.size() == 0); REQUIRE(v.capacity() == 10); @@ -13,7 +15,9 @@ TEST_CASE("fixed vector init", "[lang]") TEST_CASE("fixed vector read and write", "[lang]") { - nitro::lang::fixed_vector v(10); + constexpr int SIZE = 10; + + nitro::lang::fixed_vector v(SIZE); v.emplace_back(5); v.emplace_back(7); @@ -27,7 +31,9 @@ TEST_CASE("fixed vector read and write", "[lang]") TEST_CASE("fixed vector erase", "[lang]") { - nitro::lang::fixed_vector v(10); + constexpr int SIZE = 10; + + nitro::lang::fixed_vector v(SIZE); v.emplace_back(5); v.emplace_back(7); @@ -40,9 +46,11 @@ TEST_CASE("fixed vector erase", "[lang]") TEST_CASE("fixed vector array assignment", "[lang]") { - constexpr std::array a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + constexpr int SIZE = 10; + constexpr std::array INIT_VALUES = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - nitro::lang::fixed_vector v(10, a); + nitro::lang::fixed_vector v(SIZE, INIT_VALUES); REQUIRE(v.front() == 0); REQUIRE(v.back() == 9); @@ -51,9 +59,10 @@ TEST_CASE("fixed vector array assignment", "[lang]") TEST_CASE("fixed vector array assignment with less elements", "[lang]") { - constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr int SIZE = 10; + constexpr std::array INIT_VALUES = { 0, 1, 2, 3, 4, 5 }; - nitro::lang::fixed_vector v(10, a); + nitro::lang::fixed_vector v(SIZE, INIT_VALUES); REQUIRE(v.front() == 0); REQUIRE(v.back() == 5); @@ -63,9 +72,10 @@ TEST_CASE("fixed vector array assignment with less elements", "[lang]") TEST_CASE("fixed vector std::get access", "[lang]") { - constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr int SIZE = 6; + constexpr std::array INIT_VALUES = { 0, 1, 2, 3, 4, 5 }; - nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector v(SIZE, INIT_VALUES); REQUIRE(std::get<2>(v) == 2); REQUIRE(std::get<3>(v) == 3); @@ -73,9 +83,10 @@ TEST_CASE("fixed vector std::get access", "[lang]") TEST_CASE("fixed vector bracket access", "[lang]") { - constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr int SIZE = 10; + constexpr std::array INIT_VALUES = { 0, 1, 2, 3, 4, 5 }; - nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector v(SIZE, INIT_VALUES); v[0] = 7; v[2] = 9; @@ -85,9 +96,10 @@ TEST_CASE("fixed vector bracket access", "[lang]") TEST_CASE("fixed vector at() access", "[lang]") { - constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; + constexpr int SIZE = 10; + constexpr std::array INIT_VALUES = { 0, 1, 2, 3, 4, 5 }; - nitro::lang::fixed_vector v(6, a); + nitro::lang::fixed_vector v(SIZE, INIT_VALUES); v.at(0) = 7; v.at(2) = 9; @@ -95,13 +107,16 @@ TEST_CASE("fixed vector at() access", "[lang]") REQUIRE(v.at(2) == 9); } -TEST_CASE("fixed vector merge two vectors #1", "[lang]") +TEST_CASE("fixed vector merge two vectors", "[lang]") { - constexpr std::array a = { 0, 1, 2, 3, 4, 5 }; - constexpr std::array b = { 6, 7, 8, 9, 10, 11 }; + constexpr int SIZE_DOUBLE = 12; + constexpr int SIZE_SINGLE = 6; - nitro::lang::fixed_vector v(12, a); - nitro::lang::fixed_vector w(6, b); + constexpr std::array INIT_VALUES_A = { 0, 1, 2, 3, 4, 5 }; + constexpr std::array INIT_VALUES_B = { 6, 7, 8, 9, 10, 11 }; + + nitro::lang::fixed_vector v(SIZE_DOUBLE, INIT_VALUES_A); + nitro::lang::fixed_vector w(SIZE_SINGLE, INIT_VALUES_B); v.push_back(w.begin(), w.end()); @@ -117,7 +132,9 @@ TEST_CASE("fixed vector init with init list", "[lang]") TEST_CASE("fixed vector with unique ptr", "[lang]") { - nitro::lang::fixed_vector> v(2); + constexpr int SIZE = 2; + + nitro::lang::fixed_vector> v(SIZE); v.insert(std::move(std::make_unique(5))); v.insert(std::move(std::make_unique(6))); From 41ae6325fa675ab8a7b9d7a7662623b4abb29955 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Wed, 19 May 2021 12:17:57 +0200 Subject: [PATCH 32/37] Formatted test file --- tests/fixed_vector_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 19b7c0f..42c2504 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -46,7 +46,7 @@ TEST_CASE("fixed vector erase", "[lang]") TEST_CASE("fixed vector array assignment", "[lang]") { - + constexpr int SIZE = 10; constexpr std::array INIT_VALUES = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; @@ -125,7 +125,7 @@ TEST_CASE("fixed vector merge two vectors", "[lang]") TEST_CASE("fixed vector init with init list", "[lang]") { - nitro::lang::fixed_vector v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + nitro::lang::fixed_vector v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; REQUIRE(v.size() == 12); } From 2d2304ffa5c97c81cee2fbcc8e3a711e340a32df Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Wed, 19 May 2021 12:22:50 +0200 Subject: [PATCH 33/37] Added Tests for copy and move --- tests/fixed_vector_test.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 42c2504..45313b2 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -13,6 +13,28 @@ TEST_CASE("fixed vector init", "[lang]") REQUIRE(v.capacity() == 10); } +TEST_CASE("fixed vector copy", "[lang]") +{ + constexpr int SIZE = 10; + + nitro::lang::fixed_vector v(SIZE); + nitro::lang::fixed_vector w = v; + + REQUIRE(v.size() == 0); + REQUIRE(v.capacity() == 10); +} + +TEST_CASE("fixed vector move", "[lang]") +{ + constexpr int SIZE = 10; + + nitro::lang::fixed_vector v(SIZE); + nitro::lang::fixed_vector w = std::move(v); + + REQUIRE(v.size() == 0); + REQUIRE(v.capacity() == 10); +} + TEST_CASE("fixed vector read and write", "[lang]") { constexpr int SIZE = 10; From 6638af5958cb858787ba211d3dc4d7a4b8b27c05 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Wed, 19 May 2021 12:33:34 +0200 Subject: [PATCH 34/37] Replaced raw types with typedefs --- include/nitro/lang/fixed_vector.hpp | 76 ++++++++++++++--------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index 9fdbacc..c3f961a 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -52,13 +52,13 @@ namespace lang using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - constexpr fixed_vector(std::size_t capacity) + constexpr fixed_vector(size_type capacity) : capacity_(capacity), data_(std::make_unique(capacity)) { } template - constexpr fixed_vector(std::size_t capacity, const Iterabel& array) + constexpr fixed_vector(size_type capacity, const Iterabel& array) : capacity_(capacity), data_(std::make_unique(capacity)) { insert(this->begin(), array.begin(), array.end()); @@ -101,22 +101,22 @@ namespace lang return size_ == 0; } - constexpr std::size_t size() const + constexpr size_type size() const { return size_; } - constexpr std::size_t capacity() const + constexpr size_type capacity() const { return capacity_; } - constexpr value_type& operator[](const std::size_t& key) + constexpr reference operator[](const size_type& key) { return data_[key]; } - constexpr value_type& at(const std::size_t& key) + constexpr reference at(const size_type& key) { if (key >= capacity_) raise("Key is larger than capacity"); @@ -127,12 +127,12 @@ namespace lang return data_[key]; } - constexpr const value_type& operator[](const std::size_t& key) const + constexpr const_reference operator[](const size_type& key) const { return data_[key]; } - constexpr const value_type& at(const std::size_t& key) const + constexpr const_reference at(const size_type& key) const { if (key >= capacity_) raise("Key is larger than capacity!"); @@ -143,28 +143,28 @@ namespace lang return data_[key]; } - constexpr value_type& front() noexcept + constexpr reference front() noexcept { return data_[0]; } - constexpr const value_type& front() const noexcept + constexpr const_reference front() const noexcept { return data_[0]; } - constexpr value_type& back() noexcept + constexpr reference back() noexcept { return data_[size_ - 1]; } - constexpr const value_type& back() const noexcept + constexpr const_reference back() const noexcept { return data_[size_ - 1]; } template - constexpr void emplace(value_type* const pos, Args&&... args) + constexpr void emplace(pointer const pos, Args&&... args) { auto key = std::distance(begin(), pos); @@ -176,7 +176,7 @@ namespace lang } template - constexpr std::size_t emplace_back(Args&&... args) + constexpr size_type emplace_back(Args&&... args) { if (size_ >= capacity_) raise("No capacity left!"); @@ -187,7 +187,7 @@ namespace lang return size_ - 1; } - constexpr std::size_t insert(const value_type& value) + constexpr size_type insert(const_reference value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -198,7 +198,7 @@ namespace lang return size_ - 1; } - constexpr std::size_t insert(value_type&& value) + constexpr size_type insert(reference& value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -210,9 +210,9 @@ namespace lang } template - constexpr void insert(value_type* const pos, Iter start, Iter end) + constexpr void insert(iterator const pos, Iter start, Iter end) { - std::size_t key = std::distance(begin(), pos); + size_type key = std::distance(begin(), pos); if (key > size_) raise("Key larger than size!"); @@ -231,12 +231,12 @@ namespace lang } } - constexpr void insert(value_type* const pos, std::initializer_list& list) + constexpr void insert(iterator const pos, std::initializer_list& list) { insert(pos, list.begin(), list.end()); } - constexpr std::size_t push_back(const value_type& value) + constexpr size_type push_back(const_reference value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -261,69 +261,69 @@ namespace lang --size_; } - constexpr value_type* begin() noexcept + constexpr iterator begin() noexcept { return &data_[0]; } - constexpr value_type* rbegin() noexcept + constexpr iterator rbegin() noexcept { return &data_[size_ - 1]; } - constexpr value_type* end() noexcept + constexpr iterator end() noexcept { return &data_[size_]; } - constexpr value_type* rend() noexcept + constexpr iterator rend() noexcept { return &data_[-1]; } - constexpr const value_type* begin() const noexcept + constexpr const iterator begin() const noexcept { return &data_[0]; } - constexpr const value_type* rbegin() const noexcept + constexpr const iterator rbegin() const noexcept { return &data_[size_ - 1]; } - constexpr const value_type* end() const noexcept + constexpr const iterator end() const noexcept { return &data_[size_]; } - constexpr const value_type* rend() const noexcept + constexpr const iterator rend() const noexcept { return &data_[-1]; } - constexpr const value_type* cbegin() const noexcept + constexpr const iterator cbegin() const noexcept { return &data_[0]; } - constexpr const value_type* crbegin() const noexcept + constexpr const iterator crbegin() const noexcept { return &data_[size_ - 1]; } - constexpr const value_type* cend() const noexcept + constexpr const iterator cend() const noexcept { return &data_[size_]; } - constexpr const value_type* crend() const noexcept + constexpr const iterator crend() const noexcept { return &data_[-1]; } - constexpr void erase(value_type* pos) + constexpr void erase(iterator pos) { - std::size_t key = std::distance(begin(), pos); + size_type key = std::distance(begin(), pos); if (key >= size_) raise("Key does not exsist!"); @@ -336,19 +336,19 @@ namespace lang --size_; } - constexpr value_type* data() noexcept + constexpr pointer data() noexcept { return data_.get(); } - constexpr const value_type* data() const noexcept + constexpr const_pointer data() const noexcept { return data_.get(); } private: - std::size_t size_ = 0; - std::size_t capacity_ = 0; + size_type size_ = 0; + size_type capacity_ = 0; std::unique_ptr data_; From 229da4530660398fa2134071292614b7af95f963 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Wed, 19 May 2021 12:38:25 +0200 Subject: [PATCH 35/37] Fix --- include/nitro/lang/fixed_vector.hpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index c3f961a..a473e62 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -46,6 +46,7 @@ namespace lang using const_pointer = const value_type*; using reference = value_type&; using const_reference = const value_type&; + using move = value_type&&; using size_type = std::size_t; using iterator = value_type*; using const_iterator = const value_type*; @@ -198,7 +199,7 @@ namespace lang return size_ - 1; } - constexpr size_type insert(reference& value) + constexpr size_type insert(move value) { if (size_ >= capacity_) raise("No capacity left!"); @@ -281,42 +282,42 @@ namespace lang return &data_[-1]; } - constexpr const iterator begin() const noexcept + constexpr const_iterator begin() const noexcept { return &data_[0]; } - constexpr const iterator rbegin() const noexcept + constexpr const_iterator rbegin() const noexcept { return &data_[size_ - 1]; } - constexpr const iterator end() const noexcept + constexpr const_iterator end() const noexcept { return &data_[size_]; } - constexpr const iterator rend() const noexcept + constexpr const_iterator rend() const noexcept { return &data_[-1]; } - constexpr const iterator cbegin() const noexcept + constexpr const_iterator cbegin() const noexcept { return &data_[0]; } - constexpr const iterator crbegin() const noexcept + constexpr const_iterator crbegin() const noexcept { return &data_[size_ - 1]; } - constexpr const iterator cend() const noexcept + constexpr const_iterator cend() const noexcept { return &data_[size_]; } - constexpr const iterator crend() const noexcept + constexpr const_iterator crend() const noexcept { return &data_[-1]; } From 70eabc33e4da93f9a93344cba4b2a9eaf659005e Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Fri, 21 May 2021 13:55:57 +0200 Subject: [PATCH 36/37] removed move --- include/nitro/lang/fixed_vector.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/nitro/lang/fixed_vector.hpp b/include/nitro/lang/fixed_vector.hpp index a473e62..a1613e4 100644 --- a/include/nitro/lang/fixed_vector.hpp +++ b/include/nitro/lang/fixed_vector.hpp @@ -46,7 +46,6 @@ namespace lang using const_pointer = const value_type*; using reference = value_type&; using const_reference = const value_type&; - using move = value_type&&; using size_type = std::size_t; using iterator = value_type*; using const_iterator = const value_type*; @@ -199,7 +198,7 @@ namespace lang return size_ - 1; } - constexpr size_type insert(move value) + constexpr size_type insert(value_type&& value) { if (size_ >= capacity_) raise("No capacity left!"); From 68d7d97b890a90b922f991871502a2a110f47ae7 Mon Sep 17 00:00:00 2001 From: Daddelhai Date: Fri, 21 May 2021 14:01:03 +0200 Subject: [PATCH 37/37] deleted CATCH_CONFIG_MAIN --- tests/fixed_vector_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/fixed_vector_test.cpp b/tests/fixed_vector_test.cpp index 45313b2..fe64fcb 100644 --- a/tests/fixed_vector_test.cpp +++ b/tests/fixed_vector_test.cpp @@ -1,4 +1,3 @@ -#define CATCH_CONFIG_MAIN #include #include