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

collection.h
1 /* file: collection.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 #ifndef __COLLECTION_H__
43 #define __COLLECTION_H__
44 
45 #include <new>
46 #include "services/daal_shared_ptr.h"
47 #include "services/error_id.h"
48 
49 namespace daal
50 {
51 namespace services
52 {
53 
57 namespace interface1
58 {
68 template<class T>
69 class Collection
70 {
71 public:
75  Collection() : _array(NULL), _size(0), _capacity(0)
76  {}
77 
82  explicit Collection(size_t n) : _array(NULL), _size(0), _capacity(0)
83  {
84  if(!resize(n)) {return;}
85  _size = n;
86  }
87 
93  Collection(size_t n, const T *array) : _array(NULL), _size(0), _capacity(0)
94  {
95  if(!resize(n)) {return;}
96  for(size_t i = 0; i < n; i++)
97  {
98  _array[i] = array[i];
99  }
100  _size = n;
101  }
102 
107  Collection(const Collection<T> &other) : _array(NULL), _size(0), _capacity(0)
108  {
109  if(!resize(other.capacity())) {return;}
110  for(size_t i = 0; i < other.size(); i++)
111  {
112  this->push_back(other[i]);
113  }
114  }
115 
116  Collection &operator = (const Collection<T> &other)
117  {
118  if(!resize(other.capacity())) {return *this;}
119  _size = 0;
120  for(size_t i = 0; i < other.size(); i++)
121  {
122  this->push_back(other[i]);
123  }
124  return *this;
125  }
126 
130  virtual ~Collection()
131  {
132  for(size_t i = 0; i < _capacity; i++)
133  {
134  _array[i].~T();
135  }
136 
137  services::daal_free(_array);
138  }
139 
144  size_t size() const {return _size;}
145 
150  size_t capacity() const {return _capacity;}
151 
157  T &operator [] (size_t index)
158  {
159  return _array[index];
160  }
161 
167  const T &operator [] (size_t index) const
168  {
169  return _array[index];
170  }
171 
177  T &get(size_t index)
178  {
179  return _array[index];
180  }
181 
182 
188  const T &get(size_t index) const
189  {
190  return _array[index];
191  }
192 
197  Collection &push_back(const T &x)
198  {
199  if(_size >= _capacity)
200  {
201  _resize();
202  }
203  _array[_size] = x;
204  _size++;
205 
206  return *this;
207  }
208 
213  Collection &operator << (const T &x)
214  {
215  return this->push_back(x);
216  }
217 
222  bool resize(size_t newCapacity)
223  {
224  if(newCapacity <= _capacity) { return true; }
225  T *newArray = (T *)services::daal_malloc(sizeof(T) * newCapacity);
226  if(!newArray) {return false;}
227  for(size_t i = 0; i < newCapacity; i++)
228  {
229  T *elementMemory = &(newArray[i]);
230  ::new(elementMemory) T;
231  }
232 
233  size_t minSize = newCapacity < _size ? newCapacity : _size;
234  for(size_t i = 0; i < minSize; i++)
235  {
236  newArray[i] = _array[i];
237  }
238 
239  for(size_t i = 0; i < _capacity; i++)
240  {
241  _array[i].~T();
242  }
243 
244  services::daal_free(_array);
245  _array = newArray;
246  _capacity = newCapacity;
247  return true;
248  }
249 
253  void clear()
254  {
255  for(size_t i = 0; i < _capacity; i++)
256  {
257  _array[i].~T();
258  }
259 
260  services::daal_free(_array);
261 
262  _array = NULL;
263  _size = 0;
264  _capacity = 0;
265  }
266 
272  bool insert(const size_t pos, const T &x)
273  {
274  if(pos > this->size())
275  {
276  return true;
277  }
278 
279  size_t newSize = 1 + this->size();
280  if(newSize > _capacity)
281  {
282  if(!_resize()) {return false;}
283  }
284 
285  size_t tail = _size - pos;
286  for(size_t i = 0; i < tail; i++)
287  {
288  _array[_size - i] = _array[_size - 1 - i];
289  }
290  _array[pos] = x;
291  _size = newSize;
292  return true;
293  }
294 
300  bool insert(const size_t pos, Collection<T> &other)
301  {
302  if(pos > this->size())
303  {
304  return true;
305  }
306 
307  size_t newSize = other.size() + this->size();
308  if(newSize > _capacity)
309  {
310  if(!resize(newSize)) {return false;}
311  }
312 
313  size_t length = other.size();
314  size_t tail = _size - pos;
315  for(size_t i = 0; i < tail; i++)
316  {
317  _array[_size + length - 1 - i] = _array[_size - 1 - i];
318  }
319  for(size_t i = 0; i < length; i++)
320  {
321  _array[pos + i] = other[i];
322  }
323  _size = newSize;
324  return true;
325  }
326 
331  void erase(size_t pos)
332  {
333  if(pos >= this->size())
334  {
335  return;
336  }
337 
338  _size--;
339 
340  for(size_t i = 0; i < _size - pos; i++)
341  {
342  _array[pos + i] = _array[pos + 1 + i];
343  }
344  }
345 
346 private:
347  static const size_t _default_capacity = 16;
348  bool _resize()
349  {
350  size_t newCapacity = 2 * _capacity;
351  if(_capacity == 0) { newCapacity = _default_capacity; }
352  return resize(newCapacity);
353  }
354 
355 protected:
356  T *_array;
357  size_t _size;
358  size_t _capacity;
359 };
361 } // namespace interface1
362 using interface1::Collection;
363 
364 } // namespace services
365 } // namespace daal
366 
367 #endif
daal::services::interface1::Collection::erase
void erase(size_t pos)
Definition: collection.h:331
daal
Definition: algorithm_base_common.h:57
daal::services::interface1::Collection::~Collection
virtual ~Collection()
Definition: collection.h:130
daal::services::interface1::Collection::Collection
Collection(size_t n, const T *array)
Definition: collection.h:93
daal::services::interface1::Collection::operator[]
T & operator[](size_t index)
Definition: collection.h:157
daal::services::interface1::Collection::push_back
Collection & push_back(const T &x)
Definition: collection.h:197
daal::services::interface1::Collection::Collection
Collection(const Collection< T > &other)
Definition: collection.h:107
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:213
daal::services::interface1::Collection::clear
void clear()
Definition: collection.h:253
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:300
daal::services::interface1::Collection::Collection
Collection(size_t n)
Definition: collection.h:82
daal::services::interface1::Collection::size
size_t size() const
Definition: collection.h:144
daal::services::interface1::Collection::Collection
Collection()
Definition: collection.h:75
daal::services::interface1::Collection::capacity
size_t capacity() const
Definition: collection.h:150
daal::services::interface1::Collection::resize
bool resize(size_t newCapacity)
Definition: collection.h:222
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:69
daal::services::interface1::Collection::insert
bool insert(const size_t pos, const T &x)
Definition: collection.h:272

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