|
32 | 32 | namespace Java { |
33 | 33 | namespace Util { |
34 | 34 | template <typename K, typename V> |
35 | | - class HashMap { |
| 35 | + class HashMap |
| 36 | +// public HashMap |
| 37 | +// , public virtual Map |
| 38 | +// , public virtual Cloneable |
| 39 | +// , public virtual Serializable |
| 40 | + { |
36 | 41 | private: |
37 | 42 | std::map<K, V> hashMap; |
| 43 | + |
38 | 44 | public: |
39 | | - HashMap(); |
40 | | - ~HashMap(); |
41 | | - |
42 | | - V *get(K key); |
43 | | - void put(K key, V value); |
44 | | - boolean putAll(HashMap<K, V> map); //TODO: implement this later |
45 | | - boolean containsKey(K key); |
46 | | - boolean containsValue(const V value); |
47 | | - |
48 | | - void clear(); |
49 | | - boolean remove(K key); |
50 | | - boolean remove(K key, const V value); |
51 | | - void removeAll(); |
52 | | - |
53 | | - boolean isEmpty(); |
54 | | - int size(); |
55 | | - }; |
56 | | - |
57 | | - template <typename K, typename V> |
58 | | - HashMap<K, V>::HashMap() { |
59 | | - }; |
60 | | - |
61 | | - template <typename K, typename V> |
62 | | - HashMap<K, V>::~HashMap() { |
63 | | - } |
64 | | - |
65 | | - template <typename K, typename V> |
66 | | - V *HashMap<K, V>::get(K key) { |
67 | | - auto const it = hashMap.find(key); |
68 | | - |
69 | | - if (it == hashMap.end()) { |
70 | | - return NULL; |
| 45 | + HashMap(){} |
| 46 | + ~HashMap(){} |
| 47 | + |
| 48 | + public: |
| 49 | + /** |
| 50 | + * Removes all of the mappings from this map. |
| 51 | + * @param key |
| 52 | + * @return V |
| 53 | + */ |
| 54 | + V *get(K key) { |
| 55 | + auto const it = hashMap.find(key); |
| 56 | + |
| 57 | + if (it == hashMap.end()) { |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + |
| 61 | + return &hashMap[key]; |
71 | 62 | } |
72 | | - |
73 | | - return &hashMap[ key ]; |
74 | | - } |
75 | | - |
76 | | - template <typename K, typename V> |
77 | | - void HashMap<K, V>::put(K key, V value) { |
78 | | - hashMap.insert(std::make_pair(key, value)); |
79 | | - } |
80 | | - |
81 | | - template <typename K, typename V> |
82 | | - boolean HashMap<K, V>::containsKey(K key) { |
83 | | - if (NULL == get(key)) { |
84 | | - return false; |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned. |
| 66 | + * @param key |
| 67 | + * @param value |
| 68 | + */ |
| 69 | + void put(K key, V value) { |
| 70 | + hashMap.insert(std::make_pair(key, value)); |
85 | 71 | } |
86 | | - |
87 | | - return true; |
88 | | - } |
89 | | - |
90 | | - template <typename K, typename V> |
91 | | - boolean HashMap<K, V>::containsValue(const V value) { |
92 | | - for (auto const &ent1 : hashMap) { |
93 | | - if (ent1.second == value) { |
94 | | - return true; |
| 72 | + |
| 73 | + /** |
| 74 | + *C opies all of the mappings from the specified map to this map. |
| 75 | + * @param key |
| 76 | + * @return boolean |
| 77 | + */ |
| 78 | +// boolean putAll(HashMap<K, V> map); //FIXME: implement foreach this first |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns true if this map contains a mapping for the specified key |
| 82 | + * @param key |
| 83 | + * @return boolean |
| 84 | + */ |
| 85 | + boolean containsKey(K key) { |
| 86 | + if (NULL == get(key)) { |
| 87 | + return false; |
95 | 88 | } |
| 89 | + |
| 90 | + return true; |
96 | 91 | } |
97 | | - return false; |
98 | | - } |
99 | | - |
100 | | - template <typename K, typename V> |
101 | | - void HashMap<K, V>::clear() { |
102 | | - hashMap.clear(); |
103 | | - } |
104 | | - |
105 | | - template <typename K, typename V> |
106 | | - boolean HashMap<K, V>::remove(K key) { |
107 | | - if (NULL == get(key)) { |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns true if this map maps one or more keys to the specified value. |
| 95 | + * @param value |
| 96 | + * @return |
| 97 | + */ |
| 98 | + boolean containsValue(const V value) { |
| 99 | + for (auto const &ent1 : hashMap) { |
| 100 | + if (ent1.second == value) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
108 | 104 | return false; |
109 | 105 | } |
110 | | - |
111 | | - hashMap.erase(key); |
112 | | - return true; |
113 | | - } |
114 | | - |
115 | | - template <typename K, typename V> |
116 | | - boolean HashMap<K, V>::remove(K key, const V value) { |
117 | | - if (NULL == get(key) || !containsValue(value)) { |
118 | | - return false; |
| 106 | + |
| 107 | + /** |
| 108 | + * Removes all of the mappings from this map. |
| 109 | + */ |
| 110 | + void clear() { |
| 111 | + hashMap.clear(); |
119 | 112 | } |
120 | | - |
121 | | - hashMap.erase(key); |
122 | | - return true; |
123 | | - } |
124 | | - |
125 | | - template <typename K, typename V> |
126 | | - void HashMap<K, V>::removeAll() { |
127 | | - hashMap.clear(); |
128 | | - } |
129 | | - |
130 | | - template <typename K, typename V> |
131 | | - boolean HashMap<K, V>::isEmpty() { |
132 | | - return ( hashMap.size() == 0 ); |
133 | | - } |
134 | | - |
135 | | - template <typename K, typename V> |
136 | | - int HashMap<K, V>::size() { |
137 | | - return hashMap.size(); |
138 | | - } |
139 | | - |
| 113 | + |
| 114 | + /** |
| 115 | + * Removes the mapping for the specified key from this map if present. |
| 116 | + * @param key |
| 117 | + * @return boolean |
| 118 | + */ |
| 119 | + boolean remove(K key) { |
| 120 | + if (NULL == get(key)) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + hashMap.erase(key); |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Removes the mapping for the specified key & value from this map if present. |
| 130 | + * @param key |
| 131 | + * @param value |
| 132 | + * @return |
| 133 | + */ |
| 134 | + boolean remove(K key, const V value) { |
| 135 | + if (NULL == get(key) || !containsValue(value)) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + hashMap.erase(key); |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Removes all of the mappings from this map. |
| 145 | + */ |
| 146 | + void removeAll() { |
| 147 | + hashMap.clear(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Returns true if this map contains no key-value mappings. |
| 152 | + * @return boolean |
| 153 | + */ |
| 154 | + boolean isEmpty() { |
| 155 | + return ( hashMap.size() == 0 ); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Returns the number of key-value mappings in this map. |
| 160 | + * @return int |
| 161 | + */ |
| 162 | + int size() { |
| 163 | + return hashMap.size(); |
| 164 | + } |
| 165 | + }; |
140 | 166 |
|
141 | 167 | } |
142 | 168 | } |
|
0 commit comments