diff --git a/include/Dictionary.hpp b/include/Dictionary.hpp new file mode 100644 index 0000000..c6aeb17 --- /dev/null +++ b/include/Dictionary.hpp @@ -0,0 +1,68 @@ +#include +#include "KeyValuePair.hpp" +#include "GenericArray.hpp" +#include "logger.hpp" + +template +class Dictionary : public GenericArray> +{ +public: + void Set(K key, V value) + { + if (!ContainsKey(key)) + { + this->Append(KeyValuePair(key, value)); + return; + } + this->data[IndexOf(key)] = new KeyValuePair(key, value); + } + + KeyValuePair Get(int index) + { + if (this->Count() > index) + return *this->data[index]; + return KeyValuePair(); + } + + KeyValuePair Get(K key) + { + int index = this->IndexOf(key); + if (index >= 0) + return this->Get(index); + return KeyValuePair(); + } + + bool ContainsKey(K key) + { + return this->IndexOf(key) >= 0; + } + + int IndexOf(K Key) + { + for (size_t i = 0; i < this->Count(); i++) + { + if ((*this->data[i]).GetKey() == Key) + return i; + } + return -1; + } + + V &operator[](K key) + { + int index = this->IndexOf(key); + if (index >= 0) + { + return this->Get(index).GetValue(); + } + return KeyValuePair(); + } + + KeyValuePair &operator[](int index) + { + if (index >= 0) + { + return this->Get(index).GetValue(); + } + return KeyValuePair(); + } +}; \ No newline at end of file