Apama  10.1.0.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
list_t.hpp
Go to the documentation of this file.
1 /*
2  * Title: bits/list_t.hpp
3  * Description: C++ header-only wrapper for C-ABI data_t type
4  * $Copyright (c) 2015-2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.$
5  * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
6  * @Version: $Id: list_t.hpp 309474 2017-06-13 10:13:34Z mben $
7  */
8 
15 #ifndef _DATAT_BITS_INCLUDE_TAG
16 #error Must only be included from sag_connectivity_cpp.hpp, use that instead
17 #endif
18 
34 class list_t: protected sag_underlying_vector_t
35 {
36  friend class data_t;
37  friend class map_t;
38  friend class Message;
40  static size_t MIN_CAPACITY() { return 10; }
41 public:
48  template<typename DATA, typename UNDERLYING>
49  struct _iterator: public std::iterator<std::random_access_iterator_tag, DATA>
50  {
52  _iterator() : ptr(nullptr), direction(1) {}
53  explicit _iterator(UNDERLYING ptr, bool forward=true)
54  : ptr(ptr), direction(forward?1:-1)
55  {}
56  iterator &operator+=(size_t n) { ptr += direction*n; return *this; }
57  iterator &operator-=(size_t n) { ptr -= direction*n; return *this; }
58  iterator operator+(size_t n) const { return iterator(ptr+(direction*n), direction>0); }
59  iterator operator-(size_t n) const { return iterator(ptr+(direction*-n), direction > 0); }
60  ptrdiff_t operator-(const iterator &other) const { return direction*(ptr-other.ptr); }
61  DATA &operator[](size_t n) const { return static_cast<DATA&>(ptr[n*direction]); }
62  bool operator<(const iterator &other) const { return direction>0 ? ptr < other.ptr : ptr > other.ptr; }
63  bool operator>(const iterator &other) const { return direction>0 ? ptr > other.ptr : ptr < other.ptr; }
64  bool operator>=(const iterator &other) const { return direction>0 ? ptr >= other.ptr : ptr <= other.ptr; }
65  bool operator<=(const iterator &other) const { return direction>0 ? ptr <= other.ptr : ptr >= other.ptr; }
66  bool operator==(const iterator &other) const { return ptr == other.ptr; }
67  bool operator!=(const iterator &other) const { return !operator==(other); }
68  iterator &operator++() { return operator+=(1); }
69  iterator operator++(int)
70  {
71  iterator tmp(*this);
72  operator++();
73  return tmp;
74  }
75  iterator &operator--() { return operator-=(1); }
76  iterator operator--(int)
77  {
78  iterator tmp(*this);
79  operator--();
80  return tmp;
81  }
82  DATA &operator*() const { return static_cast<DATA&>(*ptr); }
83  DATA *operator->() const { return static_cast<DATA*>(ptr); }
84  UNDERLYING ptr;
85  int64_t direction;
86  };
91 
94  {
95  table = nullptr;
96  }
98  explicit list_t(size_t n);
99 
102  {
103  clear();
104  }
105 
109  {
110  list_t tmp;
111  // put us in tmp
112  swap(std::move(tmp));
113  // put other in us (and by extension the null tmp in other)
114  swap(std::move(other));
115  return *this;
116  }
118  list_t(list_t &&other)
119  {
120  table = nullptr;
121  swap(std::move(other));
122  }
123 
125  list_t(std::initializer_list<data_t> l)
126  {
127  table = nullptr;
128  for(auto it = l.begin(); it != l.end(); ++it) {
129  push_back(it->copy());
130  }
131  }
132 
134  void swap(list_t &&other)
135  {
136  std::swap(table, other.table);
137  }
139  list_t copy() const
140  {
141  list_t newlist(size());
142  for (const_iterator it = begin(); it != end(); ++it) {
143  newlist.push_back(it->copy());
144  }
145  return newlist;
146  }
147 
148 private:
150  list_t &operator=(const list_t &other) { abort(); }
152  list_t(const list_t &other) { abort(); }
153 public:
154 
156  bool operator==(const list_t &other) const;
157 
159  bool operator!=(const list_t &other) const { return !operator==(other); }
160 
161  /* Iteration:
162  * begin() and end() methods, both const and non-const, forward and backward.
163  */
165  iterator begin() { return iterator(table?table->data:0); }
167  iterator end() { return iterator(table?table->data+table->count:0); }
169  const_iterator begin() const { return const_iterator(table?table->data:0); }
171  const_iterator end() const { return const_iterator(table?table->data+table->count:0); }
173  const_iterator cbegin() const { return const_iterator(table?table->data:0); }
175  const_iterator cend() const { return const_iterator(table?table->data+table->count:0); }
177  iterator rbegin() { return iterator(table?table->data+table->count-1:0, false); }
179  iterator rend() { return iterator(table?table->data-1:0, false); }
181  const_iterator rbegin() const { return const_iterator(table?table->data+table->count-1:0, false); }
183  const_iterator rend() const { return const_iterator(table?table->data-1:0, false); }
184 
186  size_t size() const { return table?table->count:0; }
188  bool empty() const { return 0 == size(); }
189 
192  const data_t &operator[](size_t n) const;
195  data_t &operator[](size_t n);
198  const data_t &front() const { return operator[](0); }
201  const data_t &back() const { return operator[](table?table->count-1:0); }
204  data_t &front() { return operator[](0); }
207  data_t &back() { return operator[](table?table->count-1:0); }
208 
210  void reserve(size_t n)
211  {
212  if (!table) resize(n);
213  if (n > table->capacity) resize(n);
214  }
217  void clear();
218 
223  void push_back(data_t &&d);
224 
226  void pop_back();
227 
228 private:
235  void resize(size_t newsize);
236 };
237 
238 
239 
bool empty() const
Returns true if there are no items in this list.
Definition: list_t.hpp:188
list_t(list_t &&other)
Move construction.
Definition: list_t.hpp:118
list_t()
Construct an empty list.
Definition: list_t.hpp:93
list_t(std::initializer_list< data_t > l)
Initialiser list construction.
Definition: list_t.hpp:125
void push_back(data_t &&d)
Add an item to the end of the list.
A list class which implements many of the functions on std::vector.
Definition: list_t.hpp:34
A container for an payload and associated metadata.
Definition: message.hpp:27
list_t copy() const
Return a deep copy of this list.
Definition: list_t.hpp:139
A map class which implements many of the functions on std::map.
Definition: map_t.hpp:36
void pop_back()
Remove the last item in the list.
~list_t()
Free the underlying list.
Definition: list_t.hpp:101
list_t & operator=(list_t &&other)
Move assignment operator.
Definition: list_t.hpp:108
_iterator< const data_t, const sag_underlying_data_t * > const_iterator
The type of iterators over a const list.
Definition: list_t.hpp:90
const_iterator end() const
Forward const_iterator end.
Definition: list_t.hpp:171
const data_t & front() const
Returns a reference to the data_t at position 0 Throws std::runtime_error if the list is empty...
Definition: list_t.hpp:198
bool operator!=(const list_t &other) const
Returns false if this list is deep equals to other list.
Definition: list_t.hpp:159
bool operator==(const list_t &other) const
Returns true if this list is deep equals to other list.
void clear()
Clear the list and free the underlying data structure (capacity is now 0)
iterator begin()
Forward iterator begin.
Definition: list_t.hpp:165
const_iterator cbegin() const
Forward const_iterator begin.
Definition: list_t.hpp:173
const data_t & back() const
Returns a reference to the data_t at position size()-1 Throws std::runtime_error if the list is empty...
Definition: list_t.hpp:201
iterator rend()
Reverse iterator end.
Definition: list_t.hpp:179
size_t size() const
Return the number of items in this list.
Definition: list_t.hpp:186
data_t & front()
Returns a reference to the data_t at position 0 Throws std::runtime_error if the list is empty...
Definition: list_t.hpp:204
data_t & back()
Returns a reference to the data_t at position size()-1 Throws std::runtime_error if the list is empty...
Definition: list_t.hpp:207
iterator end()
Forward iterator end.
Definition: list_t.hpp:167
void swap(list_t &&other)
Swap the contents of this list with other.
Definition: list_t.hpp:134
iterator rbegin()
Reverse iterator begin.
Definition: list_t.hpp:177
Forward/reverse and const/non-const iterators are implemented using this class.
Definition: list_t.hpp:49
void reserve(size_t n)
Ensure the capacity is (at least) n.
Definition: list_t.hpp:210
const_iterator cend() const
Forward const_iterator end.
Definition: list_t.hpp:175
const_iterator begin() const
Forward const_iterator begin.
Definition: list_t.hpp:169
A variant type which can be one of the following:
Definition: data_t.hpp:42
const data_t & operator[](size_t n) const
Returns a reference to the data_t at position n.
_iterator< data_t, sag_underlying_data_t * > iterator
The type of iterators over a mutable list.
Definition: list_t.hpp:88
const_iterator rend() const
Reverse const_iterator end.
Definition: list_t.hpp:183
const_iterator rbegin() const
Reverse const_iterator begin.
Definition: list_t.hpp:181