Scippy

SoPlex

Sequential object-oriented simPlex

array.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2020 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file array.h
17  * @brief Save arrays of arbitrary types.
18  */
19 #ifndef _ARRAY_H_
20 #define _ARRAY_H_
21 
22 #include <assert.h>
23 #include <string.h>
24 #include <vector>
25 #include "soplex/spxalloc.h"
26 
27 namespace soplex
28 {
29 /**@brief Safe arrays of arbitrary types.
30  @ingroup Elementary
31 
32  Class Array provides safe arrays of arbitrary type. Array elements are
33  accessed just like ordinary C++ array elements by means of the index
34  operator[](). Safety is provided by
35 
36  - automatic memory management in constructor and destructor
37  preventing memory leaks
38  - checking of array bound when accessing elements with the
39  indexing operator[]() (only when compiled without \c -DNDEBUG).
40 
41  Moreover, #Array%s may easily be extended by #insert%ing or
42  #append%ing elements to the Array or shrunken by
43  \ref remove() "removing"
44  elements. Method reSize(int n) resets the Array's length to \p n,
45  thereby appending elements or truncating the Array to the
46  required size.
47 
48  An Array is implemented in a C++-compliant way with respect to
49  how memory is managed: Only operators new and delete are
50  used for allocating memory. This involves some overhead for all
51  methods effecting the length of an Array, i.e., all methods
52  insert(), append(), remove() and reSize(). This involves
53  allocating a new C++ array of the new size and copying all
54  elements with the template parameters operator=().
55 
56  For this reason, it is not convenient to use class Array if its elements
57  are \ref DataObjects "Data Objects". In this case use class DataArray
58  instead.
59 
60  @see DataArray, \ref DataObjects "Data Objects"
61 */
62 template < class T >
63 class Array
64 {
65  static_assert(!std::is_same<T, bool>::value,
66  "Since Array wraps std::vector, bool is not allowed to avoid unallowed behavior");
67 protected:
68 
69  //----------------------------------------
70  /**@name Data */
71  ///@{
72  std::vector<T> data;
73  ///@}
74 
75 public:
76 
77  //----------------------------------------
78  /**@name Access / modification */
79  ///@{
80  /// reference \p n 'th element.
81  T& operator[](int n)
82  {
83  assert(n >= 0 && n < int(data.capacity()));
84  return data[n];
85  }
86  /// reference \p n 'th element.
87  const T& operator[](int n) const
88  {
89  assert(n >= 0 && n < int(data.capacity()));
90  return data[n];
91  }
92 
93  /** This function serves for using a Vector in an C-style
94  * function. It returns a pointer to the first value of the array.
95  */
96  T* get_ptr()
97  {
98  return data.data();
99  }
100  /// get a const C pointer to the data.
101  const T* get_const_ptr() const
102  {
103  return data.data();
104  }
105 
106  /// append 1 elements with value \p t.
107  void append(const T& t)
108  {
109  data.push_back(t);
110  }
111  /// append \p n uninitialized elements.
112  void append(int n)
113  {
114  T newt = T();
115  this->append(n, newt);
116  }
117  /// append \p n elements with value \p t.
118  void append(int n, const T& t)
119  {
120  data.insert(data.end(), n, t);
121  }
122  /// append \p n elements from \p t.
123  void append(int n, const T t[])
124  {
125  data.insert(data.end(), t, t + n);
126  }
127  /// append all elements from \p p_array.
128  void append(const Array<T>& t)
129  {
130  data.insert(data.end(), t.data.begin(), t.data.end());
131  }
132 
133  /// insert \p n uninitialized elements before \p i 'th element.
134  void insert(int i, int n)
135  {
136  T newt = T();
137  if( n > 0 )
138  data.insert(data.begin() + i - 1, n, newt);
139  }
140 
141  /// insert \p n elements with value \p t before \p i 'the element.
142  void insert(int i, int n, const T& t)
143  {
144  if(n > 0)
145  {
146  data.insert(data.begin() + i -1, n, t);
147  }
148  }
149 
150  /// insert \p n elements from \p p_array before \p i 'th element.
151  void insert(int i, int n, const T t[])
152  {
153  if(n > 0)
154  {
155  data.insert(data.begin() + i - 1, t, t + n);
156  }
157  }
158 
159  /// insert all elements from \p p_array before \p i 'th element.
160  void insert(int i, const Array<T>& t)
161  {
162  if(t.size())
163  {
164  data.insert(data.begin() + i - 1, t.data.begin(), t.data.end());
165  }
166  }
167 
168  /// remove \p m elements starting at \p n.
169  void remove(int n = 0, int m = 1)
170  {
171  assert(n < size() && n >= 0);
172 
173  if(n + m < size())
174  {
175  data.erase(data.begin() + n, data.begin() + n + m);
176  }
177  else
178  {
179  data.erase(data.begin() + n, data.end());
180  }
181  }
182 
183  /// remove all elements.
184  void clear()
185  {
186  data.clear();
187  }
188 
189  /// return the number of elements.
190  int size() const
191  {
192  return int(data.size());
193  }
194 
195  /// reset the number of elements.
196  void reSize(int newsize)
197  {
198  data.resize(newsize);
199  }
200 
201  ///@}
202 
203  //----------------------------------------
204  /**@name Construction / destruction */
205  ///@{
206  /// assignment operator.
208  {
209  if(this != &rhs)
210  {
211  reSize(rhs.size());
212  data = rhs.data;
213  }
214 
215  return *this;
216  }
217 
218  // Move assignment for Array
219  Array& operator=(const Array&& rhs)
220  {
221  data = std::move(rhs.data);
222  return *this;
223  }
224 
225  /// default constructor.
226  /** The constructor allocates an Array of \p n uninitialized elements.
227  */
228  explicit
229  Array(int n = 0)
230  {
231  data.resize(n);
232  }
233 
234  /// copy constructor
235  Array(const Array& old)
236  {
237  data = old.data;
238  }
239 
240  /// destructor
242  {
243  ;
244  }
245 
246  void push_back(const T& val)
247  {
248  data.push_back(val);
249  }
250 
251  void push_back(T&& val)
252  {
253  data.push_back(val);
254  }
255 
256  /// Consistency check.
257  bool isConsistent() const
258  {
259  return true;
260  }
261 
262  ///@}
263 };
264 } // namespace soplex
265 #endif // _ARRAY_H_
void append(int n, const T &t)
append n elements with value t.
Definition: array.h:118
Memory allocation routines.
int size() const
return the number of elements.
Definition: array.h:190
bool isConsistent() const
Consistency check.
Definition: array.h:257
const T & operator[](int n) const
reference n &#39;th element.
Definition: array.h:87
Safe arrays of arbitrary types.Class Array provides safe arrays of arbitrary type. Array elements are accessed just like ordinary C++ array elements by means of the index operator[](). Safety is provided by.
Definition: array.h:63
std::vector< T > data
Definition: array.h:72
void clear()
remove all elements.
Definition: array.h:184
void append(int n, const T t[])
append n elements from t.
Definition: array.h:123
Array< T > & operator=(const Array< T > &rhs)
assignment operator.
Definition: array.h:207
void insert(int i, const Array< T > &t)
insert all elements from p_array before i &#39;th element.
Definition: array.h:160
Array & operator=(const Array &&rhs)
Definition: array.h:219
T & operator[](int n)
reference n &#39;th element.
Definition: array.h:81
void insert(int i, int n, const T t[])
insert n elements from p_array before i &#39;th element.
Definition: array.h:151
~Array()
destructor
Definition: array.h:241
const T * get_const_ptr() const
get a const C pointer to the data.
Definition: array.h:101
T * get_ptr()
Definition: array.h:96
void insert(int i, int n, const T &t)
insert n elements with value t before i &#39;the element.
Definition: array.h:142
Everything should be within this namespace.
void reSize(int newsize)
reset the number of elements.
Definition: array.h:196
void append(const T &t)
append 1 elements with value t.
Definition: array.h:107
void push_back(const T &val)
Definition: array.h:246
Array(int n=0)
default constructor.
Definition: array.h:229
void append(int n)
append n uninitialized elements.
Definition: array.h:112
void push_back(T &&val)
Definition: array.h:251
void insert(int i, int n)
insert n uninitialized elements before i &#39;th element.
Definition: array.h:134
Array(const Array &old)
copy constructor
Definition: array.h:235
void append(const Array< T > &t)
append all elements from p_array.
Definition: array.h:128