Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef ctrlplane_index_map_h 7 : #define ctrlplane_index_map_h 8 : 9 : #include <cassert> 10 : #include <map> 11 : #include <vector> 12 : #include "base/bitset.h" 13 : #include "base/util.h" 14 : 15 : // 16 : // An key, value map associated with an index. 17 : // 18 : template <typename KeyType, typename ValueType, 19 : typename BitsetType = BitSet> 20 : class IndexMap { 21 : public: 22 : typedef std::vector<ValueType *> VectorType; 23 : typedef std::map<KeyType, ValueType *> MapType; 24 : typedef typename MapType::iterator iterator; 25 : typedef typename MapType::const_iterator const_iterator; 26 : 27 120047 : IndexMap() { } 28 120047 : ~IndexMap() { 29 120047 : STLDeleteValues(&values_); 30 120047 : } 31 : 32 4264272 : ValueType *At(int index) const { 33 4264272 : return values_[index]; 34 : } 35 5002042 : ValueType *Find(const KeyType &key) const { 36 5002042 : typename MapType::const_iterator loc = map_.find(key); 37 5001634 : if (loc != map_.end()) { 38 4579485 : return loc->second; 39 : } 40 422029 : return NULL; 41 : } 42 : 43 8050 : void ReserveBit(int index) { 44 8050 : if (bits_.test(index)) 45 0 : assert(!values_[index]); 46 8050 : bits_.set(index); 47 8050 : values_.resize(values_.size() + 1); 48 8050 : } 49 : 50 : // Allocate a new index associated with the new key. 51 361611 : size_t Insert(const KeyType &key, ValueType *value, int index = -1) { 52 : std::pair<typename MapType::iterator, bool> result = 53 361611 : map_.insert(std::make_pair(key, value)); 54 361611 : if (!result.second) { 55 0 : return -1; 56 : } 57 361611 : size_t bit = index; 58 361611 : if (index == -1) 59 353580 : bit = bits_.find_first_clear(); 60 361611 : if (bit >= values_.size()) { 61 340653 : assert(bit == values_.size()); 62 340653 : values_.push_back(value); 63 : } else { 64 20958 : values_[bit] = value; 65 : } 66 361611 : bits_.set(bit); 67 361611 : return bit; 68 : } 69 : 70 321333 : void Remove(const KeyType &key, int index, bool clear_bit = true) { 71 321333 : typename MapType::iterator loc = map_.find(key); 72 321333 : assert(loc != map_.end()); 73 321333 : assert(loc->second == values_[index]); 74 321333 : map_.erase(loc); 75 321333 : if (clear_bit) 76 310942 : ResetBit(index); 77 321333 : } 78 : 79 361488 : void ResetBit(int index) { 80 361488 : bits_.reset(index); 81 361488 : ValueType *value = values_[index]; 82 361488 : values_[index] = NULL; 83 361488 : delete value; 84 710041 : for (int64_t i = values_.size() - 1; i >= 0; i--) { 85 633321 : if (values_[i] != NULL) { 86 284768 : break; 87 : } 88 348553 : values_.pop_back(); 89 : } 90 361488 : } 91 : 92 829353 : ValueType *Locate(const KeyType &key) { 93 829353 : ValueType *value = Find(key); 94 829353 : if (value == NULL) { 95 310876 : value = new ValueType(key); 96 310876 : value->set_index(Insert(key, value)); 97 : } 98 829353 : return value; 99 : } 100 : 101 26075 : size_t size() const { return values_.size(); } 102 76 : size_t count() const { return map_.size(); } 103 170165 : bool empty() const { return map_.empty(); } 104 : 105 8049 : void clear() { 106 8049 : bits_.clear(); 107 8049 : STLDeleteValues(&values_); 108 8049 : map_.clear(); 109 8049 : } 110 : 111 564 : const BitsetType &bits() const { return bits_; } 112 : 113 : iterator begin() { return map_.begin(); } 114 : iterator end() { return map_.end(); } 115 : iterator lower_bound(const KeyType &key) { 116 : return map_.lower_bound(key); 117 : } 118 : const_iterator cbegin() { return map_.begin(); } 119 : const_iterator cend() { return map_.end(); } 120 : const_iterator clower_bound(const KeyType &key) { 121 : return map_.lower_bound(key); 122 : } 123 : 124 : private: 125 : BitsetType bits_; 126 : VectorType values_; 127 : MapType map_; 128 : DISALLOW_COPY_AND_ASSIGN(IndexMap); 129 : }; 130 : 131 : #endif