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