C++ API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

collection.h
1 /* file: collection.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 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 
22 namespace daal
23 {
24 namespace services
25 {
26 
30 namespace interface1
31 {
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  T* data()
172  {
173  return _array;
174  }
175 
180  const T* data() const
181  {
182  return _array;
183  }
184 
189  Collection &push_back(const T &x)
190  {
191  safe_push_back(x);
192  return *this;
193  }
194 
200  bool safe_push_back(const T &x)
201  {
202  if (_size >= _capacity)
203  {
204  if (!_resize()) { return false; }
205  }
206  _array[_size] = x;
207  _size++;
208 
209  return true;
210  }
211 
216  Collection &operator << (const T &x)
217  {
218  return this->push_back(x);
219  }
220 
225  bool resize(size_t newCapacity)
226  {
227  if(newCapacity <= _capacity) { return true; }
228  T *newArray = (T *)services::daal_malloc(sizeof(T) * newCapacity);
229  if(!newArray) {return false;}
230  for(size_t i = 0; i < newCapacity; i++)
231  {
232  T *elementMemory = &(newArray[i]);
233  ::new(elementMemory) T;
234  }
235 
236  size_t minSize = newCapacity < _size ? newCapacity : _size;
237  for(size_t i = 0; i < minSize; i++)
238  {
239  newArray[i] = _array[i];
240  }
241 
242  for(size_t i = 0; i < _capacity; i++)
243  {
244  _array[i].~T();
245  }
246 
247  services::daal_free(_array);
248  _array = newArray;
249  _capacity = newCapacity;
250  return true;
251  }
252 
256  void clear()
257  {
258  for(size_t i = 0; i < _capacity; i++)
259  {
260  _array[i].~T();
261  }
262 
263  services::daal_free(_array);
264 
265  _array = NULL;
266  _size = 0;
267  _capacity = 0;
268  }
269 
275  bool insert(const size_t pos, const T &x)
276  {
277  if(pos > this->size())
278  {
279  return true;
280  }
281 
282  size_t newSize = 1 + this->size();
283  if(newSize > _capacity)
284  {
285  if(!_resize()) {return false;}
286  }
287 
288  size_t tail = _size - pos;
289  for(size_t i = 0; i < tail; i++)
290  {
291  _array[_size - i] = _array[_size - 1 - i];
292  }
293  _array[pos] = x;
294  _size = newSize;
295  return true;
296  }
297 
303  bool insert(const size_t pos, Collection<T> &other)
304  {
305  if(pos > this->size())
306  {
307  return true;
308  }
309 
310  size_t newSize = other.size() + this->size();
311  if(newSize > _capacity)
312  {
313  if(!resize(newSize)) {return false;}
314  }
315 
316  size_t length = other.size();
317  size_t tail = _size - pos;
318  for(size_t i = 0; i < tail; i++)
319  {
320  _array[_size + length - 1 - i] = _array[_size - 1 - i];
321  }
322  for(size_t i = 0; i < length; i++)
323  {
324  _array[pos + i] = other[i];
325  }
326  _size = newSize;
327  return true;
328  }
329 
334  void erase(size_t pos)
335  {
336  if(pos >= this->size())
337  {
338  return;
339  }
340 
341  _size--;
342 
343  for(size_t i = 0; i < _size - pos; i++)
344  {
345  _array[pos + i] = _array[pos + 1 + i];
346  }
347  }
348 
349 private:
350  static const size_t _default_capacity = 16;
351  bool _resize()
352  {
353  size_t newCapacity = 2 * _capacity;
354  if(_capacity == 0) { newCapacity = _default_capacity; }
355  return resize(newCapacity);
356  }
357 
358 protected:
359  T *_array;
360  size_t _size;
361  size_t _capacity;
362 };
363 
365 } // namespace interface1
366 
367 using interface1::Collection;
368 
369 } // namespace services
370 } // namespace daal
371 
372 #endif
daal::services::interface1::Collection::erase
void erase(size_t pos)
Definition: collection.h:334
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:189
daal::services::interface1::Collection::size
size_t size() const
Definition: collection.h:118
daal::services::interface1::Collection::data
T * data()
Definition: collection.h:171
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:216
daal::services::interface1::Collection::capacity
size_t capacity() const
Definition: collection.h:124
daal::services::interface1::Collection::clear
void clear()
Definition: collection.h:256
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:303
daal::services::interface1::Collection::Collection
Collection(size_t n)
Definition: collection.h:56
daal::services::interface1::Collection::data
const T * data() const
Definition: collection.h:180
daal::services::interface1::Collection::safe_push_back
bool safe_push_back(const T &x)
Definition: collection.h:200
daal::services::interface1::Collection::Collection
Collection()
Definition: collection.h:49
daal::services::interface1::Collection::resize
bool resize(size_t newCapacity)
Definition: collection.h:225
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:275

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