Add initializer list support for Dictionary.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-22 16:53:42 +01:00
parent e4c203027b
commit 3bbb12f485
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 11 additions and 5 deletions

View File

@ -1 +0,0 @@
#include "Dictionary.hpp"

View File

@ -10,9 +10,9 @@ namespace Arbutils::Collections {
using iterator = typename std::unordered_map<KeyT, ValueT, std::hash<KeyT>, std::equal_to<KeyT>, using iterator = typename std::unordered_map<KeyT, ValueT, std::hash<KeyT>, std::equal_to<KeyT>,
std::allocator<std::pair<const KeyT, ValueT>>>::iterator; std::allocator<std::pair<const KeyT, ValueT>>>::iterator;
public: public:
explicit Dictionary(size_t capacity) : _map(capacity) {} explicit Dictionary(size_t capacity) : _map(capacity) {}
explicit Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l){}
inline void Insert(KeyT key, ValueT value) { inline void Insert(KeyT key, ValueT value) {
auto v = _map.insert({key, value}); auto v = _map.insert({key, value});

View File

@ -10,6 +10,14 @@ TEST_CASE("Create Dictionary, insert values", "[Utilities]") {
dic.Insert(9, 2000); dic.Insert(9, 2000);
} }
TEST_CASE("Create Dictionary with initializer list", "[Utilities]") {
auto dic = Dictionary<int, int>({{5, 100}, {10, 200}, {50, 2}});
CHECK(dic.Get(5) == 100);
CHECK(dic.Get(10) == 200);
CHECK(dic.Get(50) == 2);
}
TEST_CASE("Create Dictionary, insert values, get values", "[Utilities]") { TEST_CASE("Create Dictionary, insert values, get values", "[Utilities]") {
auto dic = Dictionary<int, int>(5); auto dic = Dictionary<int, int>(5);
dic.Insert(10, 5); dic.Insert(10, 5);
@ -101,5 +109,4 @@ TEST_CASE("Create Dictionary with different types, insert values, iterate over k
CHECK(i == 3); CHECK(i == 3);
} }
#endif #endif