ROSE  0.11.125.0
List.h
1 #ifndef ROSE_Tree_List_H
2 #define ROSE_Tree_List_H
3 #include <Rose/Tree/Base.h>
4 
5 namespace Rose {
6 namespace Tree {
7 
11 template<class T>
12 class List: public Base {
13 private:
14  using EdgeVector = std::vector<std::unique_ptr<Edge<T>>>;
15 
16 public:
18  using Ptr = std::shared_ptr<List>;
19 
21  using Child = T;
22 
24  using ChildPtr = std::shared_ptr<T>;
25 
26  // These are for compatibility with std::vector
27  using value_type = Edge<T>;
28  using size_type = typename EdgeVector::size_type;
29  using difference_type = typename EdgeVector::difference_type;
30  using reference = value_type&;
31  using const_reference = const value_type&;
32  using pointer = value_type*;
33  using const_pointer = const value_type*;
35 private:
36  EdgeVector elmts_;
37 
38 public:
42  class iterator {
43  friend class List;
44  typename EdgeVector::iterator base_;
45  iterator() = delete;
46  iterator(typename EdgeVector::iterator base)
47  : base_(base) {}
48 
49  public:
54  ++base_;
55  return *this;
56  }
58  auto temp = *this;
59  ++base_;
60  return temp;
61  }
68  --base_;
69  return *this;
70  }
72  auto temp = *this;
73  --base_;
74  return temp;
75  }
82  base_ += n;
83  return *this;
84  }
86  iterator retval = *this;
87  retval += n;
88  return retval;
89  }
96  base_ -= n;
97  return *this;
98  }
100  iterator retval = *this;
101  retval -= n;
102  return retval;
103  }
107  difference_type operator-(const iterator &other) const {
108  return other.base_ - base_;
109  }
110 
115  ASSERT_not_null(base_[n]);
116  return *base_[n];
117  }
118 
121  ASSERT_not_null(*base_);
122  return **base_;
123  }
124 
127  ASSERT_not_null(*base_);
128  return &**base_;
129  }
130 
132  iterator& operator=(const iterator &other) {
133  base_ = other.base_;
134  return *this;
135  }
136 
140  bool operator==(const iterator &other) const {
141  return base_ == other.base_;
142  }
143  bool operator!=(const iterator &other) const {
144  return base_ != other.base_;
145  }
146  bool operator<(const iterator &other) const {
147  return base_ < other.base_;
148  }
149  bool operator<=(const iterator &other) const {
150  return base_ <= other.base_;
151  }
152  bool operator>(const iterator &other) const {
153  return base_ > other.base_;
154  }
155  bool operator>=(const iterator &other) const {
156  return base_ >= other.base_;
157  }
159  };
160 
161 protected:
162  List() {}
163 
164 public:
168  static std::shared_ptr<List> instance() {
169  return std::shared_ptr<List>(new List);
170  }
171 
175  bool empty() const {
176  return elmts_.empty();
177  }
178 
182  size_t size() const {
183  return elmts_.size();
184  }
185 
187  void reserve(size_t n) {
188  elmts_.reserve(n);
189  }
190 
192  size_t capacity() const {
193  return elmts_.capacity();
194  }
195 
200  Edge<T>& push_back(const std::shared_ptr<T>& elmt) {
201  elmts_.push_back(std::make_unique<Edge<T>>(*this, elmt));
202  return *elmts_.back();
203  }
204 
209  ASSERT_forbid(elmts_.empty());
210  BasePtr retval = (*elmts_.back())();
211  elmts_.pop_back();
212  return retval;
213  }
214 
218  const Edge<T>& operator[](size_t i) const {
219  return *elmts_.at(i);
220  }
221  Edge<T>& operator[](size_t i) {
222  return *elmts_.at(i);
223  }
224  const Edge<T>& at(size_t i) const {
225  return *elmts_.at(i);
226  }
227  Edge<T>& at(size_t i) {
228  return *elmts_.at(i);
229  }
234  return iterator(elmts_.begin());
235  }
236 
239  return iterator(elmts_.end());
240  }
241 
244  ASSERT_forbid(elmts_.empty());
245  return *elmts_.front();
246  }
247 
250  ASSERT_forbid(elmts_.empty());
251  return *elmts_.back();
252  }
253 
254 protected:
255  virtual ChildDescriptor findChild(size_t i) const override {
256  if (i < elmts_.size()) {
257  return ChildDescriptor{i, boost::lexical_cast<std::string>(i), (*elmts_[i])()};
258  } else {
259  return ChildDescriptor{elmts_.size(), "", nullptr};
260  }
261  }
262 };
263 
264 } // namespace
265 } // namespace
266 #endif
iterator & operator++()
Cause iterator to point to the next edge.
Definition: List.h:53
bool operator==(const iterator &other) const
Compare two iterators.
Definition: List.h:140
Random access iterator to non-const edges.
Definition: List.h:42
iterator & operator--()
Cause iterator to point to previous edge.
Definition: List.h:67
Edge< T > & push_back(const std::shared_ptr< T > &elmt)
Insert a child pointer at the end of this node.
Definition: List.h:200
iterator & operator=(const iterator &other)
Make this iterator point to the same element as the @ other iterator.
Definition: List.h:132
iterator begin()
Return an iterator pointing to the first edge.
Definition: List.h:233
difference_type operator-(const iterator &other) const
Distance between two iterators.
Definition: List.h:107
bool operator!=(const iterator &other) const
Compare two iterators.
Definition: List.h:143
Edge< T > & operator*()
Return a reference to the current edge.
Definition: List.h:120
T Child
Type of child.
Definition: List.h:21
const Edge< T > & at(size_t i) const
Return a reference to the Ith edge.
Definition: List.h:224
size_t size() const
Number of child edges.
Definition: List.h:182
bool operator<=(const iterator &other) const
Compare two iterators.
Definition: List.h:149
typename EdgeVector::size_type size_type
Size type.
Definition: List.h:28
Main namespace for the ROSE library.
iterator & operator-=(difference_type n)
Advance iterator in backward (or forward if negative) direction by n edges.
Definition: List.h:95
void reserve(size_t n)
Reserve space so the child edge vector can grow without being reallocated.
Definition: List.h:187
Edge< T > * operator->()
Return a pointer to the current edge.
Definition: List.h:126
std::shared_ptr< T > ChildPtr
Type of child pointer.
Definition: List.h:24
Edge< T > & front()
Return a reference to the first edge.
Definition: List.h:243
Information about a child.
Definition: Tree/Base.h:225
Edge< T > & operator[](size_t i)
Return a reference to the Ith edge.
Definition: List.h:221
iterator operator--(int)
Cause iterator to point to previous edge.
Definition: List.h:71
iterator operator-(difference_type n) const
Advance iterator in backward (or forward if negative) direction by n edges.
Definition: List.h:99
Edge< T > & back()
Return a reference to the last edge.
Definition: List.h:249
iterator end()
Return an iterator pointing to one past the last edge.
Definition: List.h:238
iterator operator++(int)
Cause iterator to point to the next edge.
Definition: List.h:57
size_t capacity() const
Reserved capacity.
Definition: List.h:192
bool operator<(const iterator &other) const
Compare two iterators.
Definition: List.h:146
BasePtr pop_back()
Erase a child edge from the end of this node.
Definition: List.h:208
Edge< T > & operator[](difference_type n) const
Return an edge relative to the current one.
Definition: List.h:114
std::shared_ptr< Base > BasePtr
Shared-ownership pointer for Base.
A parent-to-child edge in a tree.
Definition: Tree/Base.h:129
typename EdgeVector::difference_type difference_type
Distance between elements.
Definition: List.h:29
Tree vertex that points to an ordered sequence of indexable children.
Definition: List.h:12
iterator & operator+=(difference_type n)
Advance iterator in forward (or backward if negative) direction by n edges.
Definition: List.h:81
bool operator>(const iterator &other) const
Compare two iterators.
Definition: List.h:152
const Edge< T > & operator[](size_t i) const
Return a reference to the Ith edge.
Definition: List.h:218
bool empty() const
Test whether vector is empty.
Definition: List.h:175
Base class for tree vertices.
Definition: Tree/Base.h:212
virtual ChildDescriptor findChild(size_t i) const override
Finds information about an indexed child.
Definition: List.h:255
iterator operator+(difference_type n) const
Advance iterator in forward (or backward if negative) direction by n edges.
Definition: List.h:85
static std::shared_ptr< List > instance()
Allocating constructor.
Definition: List.h:168
Edge< T > & at(size_t i)
Return a reference to the Ith edge.
Definition: List.h:227
BasePtr Ptr
Shared-ownership pointer to a Base.
Definition: Tree/Base.h:215
bool operator>=(const iterator &other) const
Compare two iterators.
Definition: List.h:155