diff --git a/src/Collections/Dictionary.hpp b/src/Collections/Dictionary.hpp index 7e994ba..46fd652 100644 --- a/src/Collections/Dictionary.hpp +++ b/src/Collections/Dictionary.hpp @@ -8,8 +8,7 @@ namespace Arbutils::Collections { private: std::unordered_map _map; - using iterator = typename std::unordered_map, std::equal_to, - std::allocator>>::iterator; + using iterator = typename std::unordered_map::iterator; public: Dictionary() : _map() {} explicit Dictionary(size_t capacity) : _map(capacity) {} diff --git a/src/Collections/List.hpp b/src/Collections/List.hpp new file mode 100644 index 0000000..054f3e4 --- /dev/null +++ b/src/Collections/List.hpp @@ -0,0 +1,62 @@ +#ifndef ARBUTILS_LIST_HPP +#define ARBUTILS_LIST_HPP +#include +#include +#include +#include + +namespace Arbutils::Collections { + template class List { + private: + std::vector _vector; + using iterator = typename std::vector::iterator; + + public: + List() : _vector() {} + explicit List(size_t capacity) : _vector(capacity) {} + List(const std::initializer_list& l) : _vector(l) {} + + inline const ValueT& At(size_t index) const { +#ifndef NO_ASSERT + if (index >= _vector.size()) { + std::stringstream ss; + ss << "Index " << index << " is out of bounds."; + throw std::logic_error(ss.str()); + } +#endif + return _vector.at(index); + } + inline ValueT& At(size_t index) { +#ifndef NO_ASSERT + if (index >= _vector.size()) { + std::stringstream ss; + ss << "Index " << index << " is out of bounds."; + throw std::logic_error(ss.str()); + } +#endif + return _vector.at(index); + } + + inline bool Contains(ValueT value){ + return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); + } + + inline void Append(ValueT value) { _vector.push_back(value); } + + inline void Insert(size_t index, ValueT value) { _vector.insert(index, value); } + + inline ValueT& operator[](size_t index) { return At(index); } + inline const ValueT& operator[](size_t index) const { return At(index); } + + inline size_t Count() { return _vector.size(); } + + + iterator begin() { return _vector.begin(); } + iterator begin() const { return _vector.begin(); } + + iterator end() { return _vector.end(); } + iterator end() const { return _vector.end(); } + }; +} + +#endif // ARBUTILS_LIST_HPP diff --git a/tests/ListTests.cpp b/tests/ListTests.cpp new file mode 100644 index 0000000..8274fe6 --- /dev/null +++ b/tests/ListTests.cpp @@ -0,0 +1,49 @@ +#ifdef TESTS_BUILD +#include "../extern/catch.hpp" +#include "../src/Collections/List.hpp" +using namespace Arbutils::Collections; + +TEST_CASE("Create List, insert values", "[Utilities]") { + auto ls = List(); + ls.Append(5); + ls.Append(100); + ls.Append(200); + ls.Append(500); +} + +TEST_CASE("Create List from initializer list", "[Utilities]") { + auto ls = List({5, 200, 1500, -500}); + CHECK(ls.At(0) == 5); + CHECK(ls.At(1) == 200); + CHECK(ls.At(2) == 1500); + CHECK(ls.At(3) == -500); +} + + +TEST_CASE("Create List, insert values, retrieve values", "[Utilities]") { + auto ls = List(); + ls.Append(5); + ls.Append(100); + ls.Append(200); + ls.Append(500); + + CHECK(ls.At(0) == 5); + CHECK(ls.At(1) == 100); + CHECK(ls.At(2) == 200); + CHECK(ls.At(3) == 500); +} + +TEST_CASE("Create List, insert values, iterate over values", "[Utilities]") { + auto ls = List(); + ls.Append(5); + ls.Append(100); + ls.Append(200); + ls.Append(500); + + for (auto v: ls){ + CHECK(ls.Contains(v)); + } +} + + +#endif \ No newline at end of file