OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
index_vector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef agent_index_vector_h
6 #define agent_index_vector_h
7 
8 #include <cassert>
9 #include <vector>
10 #include <boost/dynamic_bitset.hpp>
11 #include <base/logging.h>
12 
13 // Index management + Vector holding a pointer at allocated index
14 template <typename EntryType>
15 class IndexVector {
16 public:
17  static const size_t kGrowSize = 32;
18 
19  typedef std::vector<EntryType> EntryTable;
20 
23  // Make sure the bitmap is empty
24  if (bitmap_.count() != bitmap_.size()) {
25  LOG(ERROR, "IndexVector has " << bitmap_.size() - bitmap_.count()
26  << " entries in destructor");
27  }
28  bitmap_.clear();
29  }
30 
31  // Get entry at an index
32  EntryType At(size_t index) const {
33  if (index >= bitmap_.size()) {
34  return EntryType();
35  }
36  return entries_[index];
37  }
38 
39  // Allocate a new index and store entry in vector at allocated index
40  size_t Insert(EntryType entry) {
41  size_t index = bitmap_.find_first();
42  if (index == bitmap_.npos) {
43  size_t size = bitmap_.size();
44  bitmap_.resize(size + kGrowSize, 1);
45  entries_.resize(size + kGrowSize);
46  index = bitmap_.find_first();
47  }
48 
49  bitmap_.set(index, 0);
50  entries_[index] = entry;
51  return index;
52  }
53 
54  size_t InsertAtIndex(uint32_t index, EntryType entry) {
55  size_t size = bitmap_.size();
56  if (size == 0 || size <= index) {
57  bitmap_.resize(index + kGrowSize, 1);
58  entries_.resize(index + kGrowSize);
59  }
60 
61  // TODO(prabhjot) need to enable assertion below
62  // currently disabled due to some issue with MPLS
63  // label allocation
64  // index should not be already in use
65  // assert(bitmap_[index] == 1);
66 
67  bitmap_.set(index, 0);
68  entries_[index] = entry;
69  return index;
70  }
71 
72  void Update(size_t index, EntryType entry) {
73  assert(index < bitmap_.size());
74  assert(bitmap_[index] == 0);
75  entries_[index] = entry;
76  }
77 
78  void Remove(size_t index) {
79  assert(index < bitmap_.size());
80  assert(bitmap_[index] == 0);
81  bitmap_.set(index);
82  entries_[index] = EntryType();
83  }
84 
85  bool NoneIndexSet() {
86  return (bitmap_.count() == bitmap_.size());
87  }
88 
89  size_t InUseIndexCount() {
90  return (bitmap_.size() - bitmap_.count());
91  }
92 
93 private:
94  typedef boost::dynamic_bitset<> Bitmap;
98 };
99 
100 #endif
EntryType At(size_t index) const
Definition: index_vector.h:32
DISALLOW_COPY_AND_ASSIGN(IndexVector)
std::vector< EntryType > EntryTable
Definition: index_vector.h:19
size_t InUseIndexCount()
Definition: index_vector.h:89
static const size_t kGrowSize
Definition: index_vector.h:17
void Remove(size_t index)
Definition: index_vector.h:78
size_t Insert(EntryType entry)
Definition: index_vector.h:40
EntryTable entries_
Definition: index_vector.h:96
boost::dynamic_bitset Bitmap
Definition: index_vector.h:94
Bitmap bitmap_
Definition: index_vector.h:95
#define LOG(_Level, _Msg)
Definition: logging.h:33
void Update(size_t index, EntryType entry)
Definition: index_vector.h:72
bool NoneIndexSet()
Definition: index_vector.h:85
size_t InsertAtIndex(uint32_t index, EntryType entry)
Definition: index_vector.h:54