diff options
-rw-r--r-- | common/hashmap.h | 2 | ||||
-rw-r--r-- | common/map.h | 15 |
2 files changed, 14 insertions, 3 deletions
diff --git a/common/hashmap.h b/common/hashmap.h index 55ed4ecc3b..3b1477b7a2 100644 --- a/common/hashmap.h +++ b/common/hashmap.h @@ -168,6 +168,8 @@ public: size_t erase(const Key &key); + uint size() { return _nele; } + const_iterator begin() const { // Find and return the first non-empty entry for (uint ctr = 0; ctr < _arrsize; ++ctr) { diff --git a/common/map.h b/common/map.h index 8ed20c571e..9279a60464 100644 --- a/common/map.h +++ b/common/map.h @@ -60,6 +60,8 @@ protected: Comparator _cmp; + uint _size; + private: Map<Key, Value, Comparator>(const Map<Key, Value, Comparator> &map); Map<Key, Value, Comparator> &operator =(const Map<Key, Value, Comparator> &map); @@ -104,7 +106,7 @@ public: }; public: - Map<Key, Value, Comparator>() : _root(0) { + Map<Key, Value, Comparator>() : _root(0), _size(0) { _header = new Node(); _header->_right = _header->_left = _header; } @@ -118,6 +120,7 @@ public: clearNodes(_root); delete _header; _root = _header = 0; + _size = 0; } /* @@ -126,10 +129,12 @@ public: */ Value &operator [](const Key &key) { Node *node; - if (!_root) + if (!_root) { node = _root = new Node(key, _header); - else + _size++; + } else { node = findOrCreateNode(_root, key); + } return node->_value; } @@ -146,12 +151,15 @@ public: void clear() { clearNodes(_root); _root = 0; + _size = 0; } bool empty() const { return (_root == 0); } + uint size() const { return _size; } + size_t erase(const Key &key) { // TODO - implement efficiently. Indeed, maybe switch to using red-black trees? // For now, just a lame, bad remove algorithm. Rule: don't remove elements @@ -259,6 +267,7 @@ protected: return node; } node = new Node(key, prevNode); + _size++; if (left) { prevNode->_left = node; } else { |