C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

collection.h
1 /* file: collection.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 #ifndef __COLLECTION_H__
17 #define __COLLECTION_H__
18 
19 #include <new>
20 #include "services/daal_shared_ptr.h"
21 #include "services/error_id.h"
22 
23 namespace daal
24 {
25 namespace services
26 {
27 
31 namespace interface1
32 {
42 template<class T>
43 class Collection
44 {
45 public:
49  Collection() : _array(NULL), _size(0), _capacity(0)
50  {}
51 
56  explicit Collection(size_t n) : _array(NULL), _size(0), _capacity(0)
57  {
58  if(!resize(n)) {return;}
59  _size = n;
60  }
61 
67  Collection(size_t n, const T *array) : _array(NULL), _size(0), _capacity(0)
68  {
69  if(!resize(n)) {return;}
70  for(size_t i = 0; i < n; i++)
71  {
72  _array[i] = array[i];
73  }
74  _size = n;
75  }
76 
81  Collection(const Collection<T> &other) : _array(NULL), _size(0), _capacity(0)
82  {
83  if(!resize(other.capacity())) {return;}
84  for(size_t i = 0; i < other.size(); i++)
85  {
86  this->push_back(other[i]);
87  }
88  }
89 
90  Collection &operator = (const Collection<T> &other)
91  {
92  if(!resize(other.capacity())) {return *this;}
93  _size = 0;
94  for(size_t i = 0; i < other.size(); i++)
95  {
96  this->push_back(other[i]);
97  }
98  return *this;
99  }
100 
104  virtual ~Collection()
105  {
106  for(size_t i = 0; i < _capacity; i++)
107  {
108  _array[i].~T();
109  }
110 
111  services::daal_free(_array);
112  }
113 
118  size_t size() const {return _size;}
119 
124  size_t capacity() const {return _capacity;}
125 
131  T &operator [] (size_t index)
132  {
133  return _array[index];
134  }
135 
141  const T &operator [] (size_t index) const
142  {
143  return _array[index];
144  }
145 
151  T &get(size_t index)
152  {
153  return _array[index];
154  }
155 
156 
162  const T &get(size_t index) const
163  {
164  return _array[index];
165  }
166 
171  Collection &push_back(const T &x)
172  {
173  if(_size >= _capacity)
174  {
175  _resize();
176  }
177  _array[_size] = x;
178  _size++;
179 
180  return *this;
181  }
182 
187  Collection &operator << (const T &x)
188  {
189  return this->push_back(x);
190  }
191 
196  bool resize(size_t newCapacity)
197  {
198  if(newCapacity <= _capacity) { return true; }
199  T *newArray = (T *)services::daal_malloc(sizeof(T) * newCapacity);
200  if(!newArray) {return false;}
201  for(size_t i = 0; i < newCapacity; i++)
202  {
203  T *elementMemory = &(newArray[i]);
204  ::new(elementMemory) T;
205  }
206 
207  size_t minSize = newCapacity < _size ? newCapacity : _size;
208  for(size_t i = 0; i < minSize; i++)
209  {
210  newArray[i] = _array[i];
211  }
212 
213  for(size_t i = 0; i < _capacity; i++)
214  {
215  _array[i].~T();
216  }
217 
218  services::daal_free(_array);
219  _array = newArray;
220  _capacity = newCapacity;
221  return true;
222  }
223 
227  void clear()
228  {
229  for(size_t i = 0; i < _capacity; i++)
230  {
231  _array[i].~T();
232  }
233 
234  services::daal_free(_array);
235 
236  _array = NULL;
237  _size = 0;
238  _capacity = 0;
239  }
240 
246  bool insert(const size_t pos, const T &x)
247  {
248  if(pos > this->size())
249  {
250  return true;
251  }
252 
253  size_t newSize = 1 + this->size();
254  if(newSize > _capacity)
255  {
256  if(!_resize()) {return false;}
257  }
258 
259  size_t tail = _size - pos;
260  for(size_t i = 0; i < tail; i++)
261  {
262  _array[_size - i] = _array[_size - 1 - i];
263  }
264  _array[pos] = x;
265  _size = newSize;
266  return true;
267  }
268 
274  bool insert(const size_t pos, Collection<T> &other)
275  {
276  if(pos > this->size())
277  {
278  return true;
279  }
280 
281  size_t newSize = other.size() + this->size();
282  if(newSize > _capacity)
283  {
284  if(!resize(newSize)) {return false;}
285  }
286 
287  size_t length = other.size();
288  size_t tail = _size - pos;
289  for(size_t i = 0; i < tail; i++)
290  {
291  _array[_size + length - 1 - i] = _array[_size - 1 - i];
292  }
293  for(size_t i = 0; i < length; i++)
294  {
295  _array[pos + i] = other[i];
296  }
297  _size = newSize;
298  return true;
299  }
300 
305  void erase(size_t pos)
306  {
307  if(pos >= this->size())
308  {
309  return;
310  }
311 
312  _size--;
313 
314  for(size_t i = 0; i < _size - pos; i++)
315  {
316  _array[pos + i] = _array[pos + 1 + i];
317  }
318  }
319 
320 private:
321  static const size_t _default_capacity = 16;
322  bool _resize()
323  {
324  size_t newCapacity = 2 * _capacity;
325  if(_capacity == 0) { newCapacity = _default_capacity; }
326  return resize(newCapacity);
327  }
328 
329 protected:
330  T *_array;
331  size_t _size;
332  size_t _capacity;
333 };
335 } // namespace interface1
336 using interface1::Collection;
337 
338 } // namespace services
339 } // namespace daal
340 
341 #endif
daal::services::interface1::Collection::erase
void erase(size_t pos)
Definition: collection.h:305
daal
Definition: algorithm_base_common.h:31
daal::services::interface1::Collection::~Collection
virtual ~Collection()
Definition: collection.h:104
daal::services::interface1::Collection::Collection
Collection(size_t n, const T *array)
Definition: collection.h:67
daal::services::interface1::Collection::operator[]
T & operator[](size_t index)
Definition: collection.h:131
daal::services::interface1::Collection::push_back
Collection & push_back(const T &x)
Definition: collection.h:171
daal::services::interface1::Collection::size
size_t size() const
Definition: collection.h:118
daal::services::interface1::Collection::Collection
Collection(const Collection< T > &other)
Definition: collection.h:81
daal::services::daal_malloc
DAAL_EXPORT void * daal_malloc(size_t size, size_t alignment=DAAL_MALLOC_DEFAULT_ALIGNMENT)
daal::services::interface1::Collection::operator<<
Collection & operator<<(const T &x)
Definition: collection.h:187
daal::services::interface1::Collection::capacity
size_t capacity() const
Definition: collection.h:124
daal::services::interface1::Collection::clear
void clear()
Definition: collection.h:227
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::services::interface1::Collection::insert
bool insert(const size_t pos, Collection< T > &other)
Definition: collection.h:274
daal::services::interface1::Collection::Collection
Collection(size_t n)
Definition: collection.h:56
daal::services::interface1::Collection::Collection
Collection()
Definition: collection.h:49
daal::services::interface1::Collection::resize
bool resize(size_t newCapacity)
Definition: collection.h:196
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:43
daal::services::interface1::Collection::insert
bool insert(const size_t pos, const T &x)
Definition: collection.h:246

For more complete information about compiler optimizations, see our Optimization Notice.