Add initializer list support for Dictionary.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
e4c203027b
commit
3bbb12f485
|
@ -1 +0,0 @@
|
||||||
#include "Dictionary.hpp"
|
|
|
@ -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});
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue