From 79ab962e2f7ca1e661785570b94ca6524f033adf Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 22 Mar 2020 18:15:13 +0100 Subject: [PATCH] Fixed not being to iterate over const collections. --- src/Collections/Dictionary.hpp | 5 +++-- src/Collections/List.hpp | 9 ++++++--- tests/ListTests.cpp | 8 ++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Collections/Dictionary.hpp b/src/Collections/Dictionary.hpp index 3499109..bc48888 100644 --- a/src/Collections/Dictionary.hpp +++ b/src/Collections/Dictionary.hpp @@ -9,6 +9,7 @@ namespace Arbutils::Collections { std::unordered_map _map; using iterator = typename std::unordered_map::iterator; + using const_iterator = typename std::unordered_map::const_iterator; public: Dictionary() : _map() {} explicit Dictionary(size_t capacity) : _map(capacity) {} @@ -73,10 +74,10 @@ namespace Arbutils::Collections { inline const ValueT& operator[](KeyT key) const { return Get(key); } iterator begin() { return _map.begin(); } - iterator begin() const { return _map.begin(); } + const_iterator begin() const { return _map.begin(); } iterator end() { return _map.end(); } - iterator end() const { return _map.end(); } + const_iterator end() const { return _map.end(); } }; } diff --git a/src/Collections/List.hpp b/src/Collections/List.hpp index 26e95e8..f14aa19 100644 --- a/src/Collections/List.hpp +++ b/src/Collections/List.hpp @@ -10,6 +10,7 @@ namespace Arbutils::Collections { private: std::vector _vector; using iterator = typename std::vector::iterator; + using const_iterator = typename std::vector::const_iterator; public: List() : _vector() {} @@ -40,7 +41,9 @@ namespace Arbutils::Collections { return _vector.at(index); } - inline bool Contains(ValueT value) { return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); } + inline bool Contains(ValueT value) const { + return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); + } inline void Append(ValueT value) { _vector.push_back(value); } @@ -54,10 +57,10 @@ namespace Arbutils::Collections { inline void Resize(size_t size, ValueT defaultValue) { _vector.resize(size, defaultValue); } iterator begin() { return _vector.begin(); } - iterator begin() const { return _vector.begin(); } + const_iterator begin() const { return _vector.begin(); } iterator end() { return _vector.end(); } - iterator end() const { return _vector.end(); } + const_iterator end() const { return _vector.end(); } const ValueT* RawData() const { return _vector.data(); } }; diff --git a/tests/ListTests.cpp b/tests/ListTests.cpp index 8274fe6..7be267b 100644 --- a/tests/ListTests.cpp +++ b/tests/ListTests.cpp @@ -45,5 +45,13 @@ TEST_CASE("Create List, insert values, iterate over values", "[Utilities]") { } } +TEST_CASE("Create const List, iterate over values", "[Utilities]") { + const auto& ls = List({10, 100, 50}); + + for (auto v: ls){ + CHECK(ls.Contains(v)); + } +} + #endif \ No newline at end of file