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