1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The Map class |
---|
13 | // ------------- |
---|
14 | // A very simple Map class. |
---|
15 | // |
---|
16 | |
---|
17 | #if !defined __LOL_BASE_MAP_H__ |
---|
18 | #define __LOL_BASE_MAP_H__ |
---|
19 | |
---|
20 | namespace lol |
---|
21 | { |
---|
22 | |
---|
23 | /* A stupidly linear map for now. */ |
---|
24 | template<typename K, typename V> class Map : protected Hash<K> |
---|
25 | { |
---|
26 | public: |
---|
27 | /* If E is different from K, Hash<K> must implement operator()(E const&) |
---|
28 | * and an equality operator between K and E must exist in order to use this method. |
---|
29 | */ |
---|
30 | |
---|
31 | /* I choose to make this inline because passing the key by reference |
---|
32 | * is usually suboptimal. */ |
---|
33 | template <typename E> |
---|
34 | inline V const& operator[] (E const &key) const |
---|
35 | { |
---|
36 | /* Look for the hash in our table and return the value. */ |
---|
37 | uint32_t hash = ((Hash<K> const &)*this)(key); |
---|
38 | for (int i = 0; i < m_array.Count(); ++i) |
---|
39 | if (m_array[i].m1 == hash) |
---|
40 | if (m_array[i].m2 == key) |
---|
41 | return m_array[i].m3; |
---|
42 | /* XXX: this in an error! */ |
---|
43 | ASSERT(0, "trying to read a nonexistent key in map"); |
---|
44 | return V(); |
---|
45 | } |
---|
46 | |
---|
47 | template <typename E> |
---|
48 | inline V & operator[] (E const &key) |
---|
49 | { |
---|
50 | /* Look for the hash in our table and return the value if found. */ |
---|
51 | uint32_t hash = ((Hash<K> const &)*this)(key); |
---|
52 | for (int i = 0; i < m_array.Count(); ++i) |
---|
53 | if (m_array[i].m1 == hash) |
---|
54 | if (m_array[i].m2 == key) |
---|
55 | return m_array[i].m3; |
---|
56 | /* If not found, insert a new value. */ |
---|
57 | m_array.Push(hash, key, V()); |
---|
58 | return m_array.Last().m3; |
---|
59 | } |
---|
60 | |
---|
61 | template <typename E> |
---|
62 | inline void Remove(E const &key) |
---|
63 | { |
---|
64 | uint32_t hash = ((Hash<K> const &)*this)(key); |
---|
65 | for (int i = 0; i < m_array.Count(); ++i) |
---|
66 | if (m_array[i].m1 == hash) |
---|
67 | if (m_array[i].m2 == key) |
---|
68 | { |
---|
69 | m_array.Remove(i); |
---|
70 | return; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | template <typename E> |
---|
75 | inline bool HasKey(E const &key) |
---|
76 | { |
---|
77 | uint32_t hash = ((Hash<K> const &)*this)(key); |
---|
78 | for (int i = 0; i < m_array.Count(); ++i) |
---|
79 | if (m_array[i].m1 == hash) |
---|
80 | if (m_array[i].m2 == key) |
---|
81 | return true; |
---|
82 | return false; |
---|
83 | } |
---|
84 | |
---|
85 | template <typename E> |
---|
86 | inline bool TryGetValue(E const &key, V& value) |
---|
87 | { |
---|
88 | uint32_t hash = ((Hash<K> const &)*this)(key); |
---|
89 | for (int i = 0; i < m_array.Count(); ++i) |
---|
90 | if (m_array[i].m1 == hash) |
---|
91 | if (m_array[i].m2 == key) |
---|
92 | { |
---|
93 | value = m_array[i].m3; |
---|
94 | return true; |
---|
95 | } |
---|
96 | |
---|
97 | return false; |
---|
98 | } |
---|
99 | |
---|
100 | inline int Count() const { return m_array.Count(); } |
---|
101 | |
---|
102 | private: |
---|
103 | Array<uint32_t, K, V> m_array; |
---|
104 | }; |
---|
105 | |
---|
106 | } /* namespace lol */ |
---|
107 | |
---|
108 | #endif // __LOL_BASE_MAP_H__ |
---|
109 | |
---|