libdballe  7.6
valuestorage.h
1 /*
2  * memdb/core - Core functions for memdb implementation
3  *
4  * Copyright (C) 2013--2014 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Author: Enrico Zini <enrico@enricozini.com>
20  */
21 
22 #ifndef DBA_MEMDB_CORE_H
23 #define DBA_MEMDB_CORE_H
24 
25 #include <vector>
26 #include <cstddef>
27 
28 namespace dballe {
29 namespace memdb {
30 
31 template<typename T> struct Match;
32 
33 template<typename T>
34 class ValueStorage
35 {
36 protected:
37  std::vector<T*> values;
38  std::vector<size_t> empty_slots;
39 
40 public:
41  typedef T value_type;
42 
43  struct index_iterator : public std::iterator<std::forward_iterator_tag, size_t>
44  {
45  const std::vector<T*>* values;
46  size_t pos;
47 
48  index_iterator(const std::vector<T*>& values, size_t pos) : values(&values), pos(pos) { skip_gaps(); }
49  // End iterator
50  index_iterator(const std::vector<T*>& values) : values(&values), pos(values.size()) {}
51  index_iterator& operator++() {
52  ++pos;
53  skip_gaps();
54  return *this;
55  }
56  void skip_gaps()
57  {
58  while (pos != values->size() && !(*values)[pos])
59  ++pos;
60  }
61  size_t operator*() const { return pos; }
62 
63  bool operator==(const index_iterator& i) const
64  {
65  return values == i.values && pos == i.pos;
66  }
67 
68  bool operator!=(const index_iterator& i) const
69  {
70  return values != i.values || pos != i.pos;
71  }
72  };
73 
74  ValueStorage() {}
75  virtual ~ValueStorage();
76 
77  void clear();
78 
85  size_t element_count() const { return values.size() - empty_slots.size(); }
86 
87  T* get_checked(size_t idx) { if (idx >= values.size()) return 0; return values[idx]; }
88  const T* get_checked(size_t idx) const { if (idx >= values.size()) return 0; return values[idx]; }
89 
90  typename std::vector<T*>::reference operator[](size_t idx) { return values[idx]; }
91  typename std::vector<T*>::const_reference operator[](size_t idx) const { return values[idx]; }
92 
93  index_iterator index_begin() const { return index_iterator(values, 0); }
94  index_iterator index_end() const { return index_iterator(values); }
95 
97  template<typename OUTITER>
98  void copy_valptrs_to(OUTITER res) const;
99 
101  template<typename OUTITER>
102  void copy_indices_to(OUTITER res) const;
103 
104 protected:
107  size_t value_add(T* value);
108 
110  void value_remove(size_t pos);
111 
112 private:
113  ValueStorage(const ValueStorage<T>&);
114  ValueStorage& operator=(const ValueStorage<T>&);
115 };
116 
117 }
118 }
119 
120 #endif
121 
Definition: valuestorage.h:43
Definition: mem/cursor.h:14
size_t element_count() const
Number of valid elements.
Definition: valuestorage.h:85
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
void value_remove(size_t pos)
Remove a value given its position.
void copy_indices_to(OUTITER res) const
Send all T pointers to the given output iterator.
size_t value_add(T *value)
Add the value to the storage and return its index take ownership of the pointer memory management...
void copy_valptrs_to(OUTITER res) const
Send all T pointers to the given output iterator.