C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

collection.h
1 /* file: collection.h */
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *******************************************************************************/
17 
18 #ifndef __COLLECTION_H__
19 #define __COLLECTION_H__
20 
21 #include <new>
22 #include "services/daal_shared_ptr.h"
23 
24 namespace daal
25 {
26 namespace services
27 {
28 
32 namespace interface1
33 {
44 template<class T>
45 class Collection
46 {
47 public:
51  Collection() : _array(NULL), _size(0), _capacity(0)
52  {}
53 
58  explicit Collection(size_t n) : _array(NULL), _size(0), _capacity(0)
59  {
60  if(!resize(n)) {return;}
61  _size = n;
62  }
63 
69  Collection(size_t n, const T *array) : _array(NULL), _size(0), _capacity(0)
70  {
71  if(!resize(n)) {return;}
72  for(size_t i = 0; i < n; i++)
73  {
74  _array[i] = array[i];
75  }
76  _size = n;
77  }
78 
83  Collection(const Collection<T> &other) : _array(NULL), _size(0), _capacity(0)
84  {
85  if(!resize(other.capacity())) {return;}
86  for(size_t i = 0; i < other.size(); i++)
87  {
88  this->push_back(other[i]);
89  }
90  }
91 
92  Collection &operator = (const Collection<T> &other)
93  {
94  if(!resize(other.capacity())) {return *this;}
95  _size = 0;
96  for(size_t i = 0; i < other.size(); i++)
97  {
98  this->push_back(other[i]);
99  }
100  return *this;
101  }
102 
106  virtual ~Collection()
107  {
108  for(size_t i = 0; i < _capacity; i++)
109  {
110  _array[i].~T();
111  }
112 
113  services::daal_free(_array);
114  _array = NULL;
115  }
116 
121  size_t size() const {return _size;}
122 
127  size_t capacity() const {return _capacity;}
128 
134  T &operator [] (size_t index)
135  {
136  return _array[index];
137  }
138 
144  const T &operator [] (size_t index) const
145  {
146  return _array[index];
147  }
148 
154  T &get(size_t index)
155  {
156  return _array[index];
157  }
158 
159 
165  const T &get(size_t index) const
166  {
167  return _array[index];
168  }
169 
174  T* data()
175  {
176  return _array;
177  }
178 
183  const T* data() const
184  {
185  return _array;
186  }
187 
192  Collection &push_back(const T &x)
193  {
194  safe_push_back(x);
195  return *this;
196  }
197 
203  bool safe_push_back(const T &x)
204  {
205  if (_size >= _capacity)
206  {
207  if (!_resize()) { return false; }
208  }
209  _array[_size] = x;
210  _size++;
211 
212  return true;
213  }
214 
219  Collection &operator << (const T &x)
220  {
221  return this->push_back(x);
222  }
223 
228  bool resize(size_t newCapacity)
229  {
230  if(newCapacity <= _capacity) { return true; }
231  T *newArray = (T *)services::daal_calloc(sizeof(T) * newCapacity);
232  if(!newArray) {return false;}
233  for(size_t i = 0; i < newCapacity; i++)
234  {
235  T *elementMemory = &(newArray[i]);
236  ::new(elementMemory) T;
237  }
238 
239  size_t minSize = newCapacity < _size ? newCapacity : _size;
240  for(size_t i = 0; i < minSize; i++)
241  {
242  newArray[i] = _array[i];
243  }
244 
245  for(size_t i = 0; i < _capacity; i++)
246  {
247  _array[i].~T();
248  }
249 
250  services::daal_free(_array);
251  _array = newArray;
252  _capacity = newCapacity;
253  return true;
254  }
255 
259  void clear()
260  {
261  for(size_t i = 0; i < _capacity; i++)
262  {
263  _array[i].~T();
264  }
265 
266  services::daal_free(_array);
267 
268  _array = NULL;
269  _size = 0;
270  _capacity = 0;
271  }
272 
278  bool insert(const size_t pos, const T &x)
279  {
280  if(pos > this->size())
281  {
282  return true;
283  }
284 
285  size_t newSize = 1 + this->size();
286  if(newSize > _capacity)
287  {
288  if(!_resize()) {return false;}
289  }
290 
291  size_t tail = _size - pos;
292  for(size_t i = 0; i < tail; i++)
293  {
294  _array[_size - i] = _array[_size - 1 - i];
295  }
296  _array[pos] = x;
297  _size = newSize;
298  return true;
299  }
300 
306  bool insert(const size_t pos, Collection<T> &other)
307  {
308  if(pos > this->size())
309  {
310  return true;
311  }
312 
313  size_t newSize = other.size() + this->size();
314  if(newSize > _capacity)
315  {
316  if(!resize(newSize)) {return false;}
317  }
318 
319  size_t length = other.size();
320  size_t tail = _size - pos;
321  for(size_t i = 0; i < tail; i++)
322  {
323  _array[_size + length - 1 - i] = _array[_size - 1 - i];
324  }
325  for(size_t i = 0; i < length; i++)
326  {
327  _array[pos + i] = other[i];
328  }
329  _size = newSize;
330  return true;
331  }
332 
337  void erase(size_t pos)
338  {
339  if(pos >= this->size())
340  {
341  return;
342  }
343 
344  _size--;
345 
346  for(size_t i = 0; i < _size - pos; i++)
347  {
348  _array[pos + i] = _array[pos + 1 + i];
349  }
350  }
351 
352 private:
353  static const size_t _default_capacity = 16;
354  bool _resize()
355  {
356  size_t newCapacity = 2 * _capacity;
357  if(_capacity == 0) { newCapacity = _default_capacity; }
358  return resize(newCapacity);
359  }
360 
361 protected:
362  T *_array;
363  size_t _size;
364  size_t _capacity;
365 };
366 
368 } // namespace interface1
369 
370 using interface1::Collection;
371 
372 } // namespace services
373 } // namespace daal
374 
375 #endif
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:45
daal::services::interface1::Collection::Collection
Collection(size_t n)
Definition: collection.h:58
daal::services::daal_calloc
DAAL_EXPORT void * daal_calloc(size_t size, size_t alignment=DAAL_MALLOC_DEFAULT_ALIGNMENT)
daal::services::interface1::Collection::Collection
Collection()
Definition: collection.h:51
daal::services::interface1::Collection::erase
void erase(size_t pos)
Definition: collection.h:337
daal::services::interface1::Collection::data
T * data()
Definition: collection.h:174
daal::services::interface1::Collection::resize
bool resize(size_t newCapacity)
Definition: collection.h:228
daal::services::interface1::Collection::clear
void clear()
Definition: collection.h:259
daal::services::interface1::Collection::data
const T * data() const
Definition: collection.h:183
daal::services::interface1::Collection::insert
bool insert(const size_t pos, Collection< T > &other)
Definition: collection.h:306
daal::services::interface1::Collection::capacity
size_t capacity() const
Definition: collection.h:127
daal::services::interface1::Collection::safe_push_back
bool safe_push_back(const T &x)
Definition: collection.h:203
daal::services::interface1::Collection::insert
bool insert(const size_t pos, const T &x)
Definition: collection.h:278
daal::services::interface1::Collection::Collection
Collection(size_t n, const T *array)
Definition: collection.h:69
daal::services::interface1::Collection::push_back
Collection & push_back(const T &x)
Definition: collection.h:192
daal::services::interface1::Collection::size
size_t size() const
Definition: collection.h:121
daal::services::interface1::Collection::Collection
Collection(const Collection< T > &other)
Definition: collection.h:83
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::services::interface1::Collection::operator[]
T & operator[](size_t index)
Definition: collection.h:134
daal::services::interface1::Collection::~Collection
virtual ~Collection()
Definition: collection.h:106
daal::services::interface1::Collection::operator<<
Collection & operator<<(const T &x)
Definition: collection.h:219

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