diff options
author | Alexis211 <alexis211@gmail.com> | 2009-12-22 19:52:39 +0100 |
---|---|---|
committer | Alexis211 <alexis211@gmail.com> | 2009-12-22 19:52:39 +0100 |
commit | 7ede286ebcb845fe4bfdfb948c6073573b01c3cb (patch) | |
tree | 38a20fecb9e1ede89d2f3f7c7c9354eb700ddaa5 /Source/Library | |
parent | a7e116c4b2378f65a0a531f9b2a2102d4a80f524 (diff) | |
parent | cc1193087067c7e1105bd3d788520c034f5cf619 (diff) | |
download | Melon-7ede286ebcb845fe4bfdfb948c6073573b01c3cb.tar.gz Melon-7ede286ebcb845fe4bfdfb948c6073573b01c3cb.zip |
Merge branch 'master' of github.com:Alexis211/Melon
Conflicts:
Melon.img
Diffstat (limited to 'Source/Library')
-rw-r--r-- | Source/Library/Common/Map.class.cpp | 38 | ||||
-rw-r--r-- | Source/Library/Common/Map.class.h | 53 |
2 files changed, 91 insertions, 0 deletions
diff --git a/Source/Library/Common/Map.class.cpp b/Source/Library/Common/Map.class.cpp new file mode 100644 index 0000000..d9a4e39 --- /dev/null +++ b/Source/Library/Common/Map.class.cpp @@ -0,0 +1,38 @@ +template <typename K, typename V> +Map<K, V>::Map() { + m_root = 0; +} + +template <typename K, typename V> +Map<K, V>::~Map() { + if (m_root != 0) delete m_root; +} + +template <typename K, typename V> +bool Map<K, V>::has(const K& key) { + return (find(key, m_root) != 0); +} + +template <typename K, typename V> +void Map<K, V>::set(const K& key, const V& value) { + item_t* i = find(key, m_root); + if (i == 0) m_root = insert(key, value, m_root); + else i->value = value; +} + +template <typename K, typename V> +const V& Map<K, V>::get(const K& key) { + item_t* i = find(key); + if (i == 0) return m_null; + return i->value; +} + +template <typename K, typename V> +V& Map<K, V>::operator[] (const K& key) { + item_t* i = find(key, m_root); + if (i == 0) { + m_root = insert(key, m_null, m_root); + i = find(key, m_root); + } + return i->value; +} diff --git a/Source/Library/Common/Map.class.h b/Source/Library/Common/Map.class.h new file mode 100644 index 0000000..1270752 --- /dev/null +++ b/Source/Library/Common/Map.class.h @@ -0,0 +1,53 @@ +#ifndef DEF_MAP_CLASS_H +#define DEF_MAP_CLASS_H + +template <typename K, typename V> +class Map { + private: + V m_null; + struct item_t { + item_t *prev, *next; + K key; + V value; + item_t(const K& _key, const V& _value) : prev(0), next(0), key(_key), value(_value) {} + ~item_t() { + if (prev != 0) delete prev; + if (next != 0) delete next; + } + } *m_root; + + //Recursive functions for internal use + item_t* find(const K& key, item_t* start) { + if (start == 0) return 0; + if (start->key == key) return start; + if (start->key > key) return find(key, start->prev); + if (start->key < key) return find(key, start->next); + return 0; + } + item_t* insert(const K& key, const V& value, item_t* start) { + if (start == 0) return new item_t(key, value); + if (start->key == key) return 0; + if (start->key > key) { + start->prev = insert(key, value, start->prev); + return start; + } + if (start->key < key) { + start->next = insert(key, value, start->next); + return start; + } + return 0; + } + + public: + Map(); + ~Map(); + + bool has(const K& key); + void set(const K& key, const V& value); + const V& get(const K& key); + V& operator[] (const K& key); +}; + +#include "Map.class.cpp" + +#endif |