OpenSDN source code
index_map.h
Go to the documentation of this file.
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  IndexMap() { }
30  }
31 
32  ValueType *At(int index) const {
33  return values_[index];
34  }
35  ValueType *Find(const KeyType &key) const {
36  typename MapType::const_iterator loc = map_.find(key);
37  if (loc != map_.end()) {
38  return loc->second;
39  }
40  return NULL;
41  }
42 
43  void ReserveBit(int index) {
44  if (bits_.test(index))
45  assert(!values_[index]);
46  bits_.set(index);
47  values_.resize(values_.size() + 1);
48  }
49 
50  // Allocate a new index associated with the new key.
51  size_t Insert(const KeyType &key, ValueType *value, int index = -1) {
52  std::pair<typename MapType::iterator, bool> result =
53  map_.insert(std::make_pair(key, value));
54  if (!result.second) {
55  return -1;
56  }
57  size_t bit = index;
58  if (index == -1)
59  bit = bits_.find_first_clear();
60  if (bit >= values_.size()) {
61  assert(bit == values_.size());
62  values_.push_back(value);
63  } else {
64  values_[bit] = value;
65  }
66  bits_.set(bit);
67  return bit;
68  }
69 
70  void Remove(const KeyType &key, int index, bool clear_bit = true) {
71  typename MapType::iterator loc = map_.find(key);
72  assert(loc != map_.end());
73  assert(loc->second == values_[index]);
74  map_.erase(loc);
75  if (clear_bit)
76  ResetBit(index);
77  }
78 
79  void ResetBit(int index) {
80  bits_.reset(index);
81  ValueType *value = values_[index];
82  values_[index] = NULL;
83  delete value;
84  for (int64_t i = values_.size() - 1; i >= 0; i--) {
85  if (values_[i] != NULL) {
86  break;
87  }
88  values_.pop_back();
89  }
90  }
91 
92  ValueType *Locate(const KeyType &key) {
93  ValueType *value = Find(key);
94  if (value == NULL) {
95  value = new ValueType(key);
96  value->set_index(Insert(key, value));
97  }
98  return value;
99  }
100 
101  size_t size() const { return values_.size(); }
102  size_t count() const { return map_.size(); }
103  bool empty() const { return map_.empty(); }
104 
105  void clear() {
106  bits_.clear();
108  map_.clear();
109  }
110 
111  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_;
129 };
130 
131 #endif
Definition: bitset.h:18
MapType::iterator iterator
Definition: index_map.h:24
size_t count() const
Definition: index_map.h:102
ValueType * At(int index) const
Definition: index_map.h:32
void clear()
Definition: index_map.h:105
~IndexMap()
Definition: index_map.h:28
DISALLOW_COPY_AND_ASSIGN(IndexMap)
std::vector< ValueType * > VectorType
Definition: index_map.h:22
bool empty() const
Definition: index_map.h:103
iterator lower_bound(const KeyType &key)
Definition: index_map.h:115
size_t Insert(const KeyType &key, ValueType *value, int index=-1)
Definition: index_map.h:51
const BitsetType & bits() const
Definition: index_map.h:111
size_t size() const
Definition: index_map.h:101
VectorType values_
Definition: index_map.h:126
ValueType * Locate(const KeyType &key)
Definition: index_map.h:92
BitsetType bits_
Definition: index_map.h:125
MapType map_
Definition: index_map.h:127
std::map< KeyType, ValueType * > MapType
Definition: index_map.h:23
void Remove(const KeyType &key, int index, bool clear_bit=true)
Definition: index_map.h:70
void ReserveBit(int index)
Definition: index_map.h:43
iterator begin()
Definition: index_map.h:113
iterator end()
Definition: index_map.h:114
const_iterator clower_bound(const KeyType &key)
Definition: index_map.h:120
const_iterator cend()
Definition: index_map.h:119
IndexMap()
Definition: index_map.h:27
ValueType * Find(const KeyType &key) const
Definition: index_map.h:35
void ResetBit(int index)
Definition: index_map.h:79
const_iterator cbegin()
Definition: index_map.h:118
MapType::const_iterator const_iterator
Definition: index_map.h:25
void STLDeleteValues(Container *container)
Definition: util.h:102