16 #ifndef __COLLECTION_H__
17 #define __COLLECTION_H__
20 #include "services/daal_shared_ptr.h"
49 Collection() : _array(NULL), _size(0), _capacity(0)
56 explicit Collection(
size_t n) : _array(NULL), _size(0), _capacity(0)
58 if(!resize(n)) {
return;}
67 Collection(
size_t n,
const T *array) : _array(NULL), _size(0), _capacity(0)
69 if(!resize(n)) {
return;}
70 for(
size_t i = 0; i < n; i++)
81 Collection(
const Collection<T> &other) : _array(NULL), _size(0), _capacity(0)
83 if(!resize(other.capacity())) {
return;}
84 for(
size_t i = 0; i < other.size(); i++)
86 this->push_back(other[i]);
90 Collection &operator = (
const Collection<T> &other)
92 if(!resize(other.capacity())) {
return *
this;}
94 for(
size_t i = 0; i < other.size(); i++)
96 this->push_back(other[i]);
104 virtual ~Collection()
106 for(
size_t i = 0; i < _capacity; i++)
111 services::daal_free(_array);
118 size_t size()
const {
return _size;}
124 size_t capacity()
const {
return _capacity;}
131 T &operator [] (
size_t index)
133 return _array[index];
141 const T &operator [] (
size_t index)
const
143 return _array[index];
153 return _array[index];
162 const T &
get(
size_t index)
const
164 return _array[index];
180 const T* data()
const
189 Collection &push_back(
const T &x)
200 bool safe_push_back(
const T &x)
202 if (_size >= _capacity)
204 if (!_resize()) {
return false; }
216 Collection &operator << (
const T &x)
218 return this->push_back(x);
225 bool resize(
size_t newCapacity)
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++)
232 T *elementMemory = &(newArray[i]);
233 ::new(elementMemory) T;
236 size_t minSize = newCapacity < _size ? newCapacity : _size;
237 for(
size_t i = 0; i < minSize; i++)
239 newArray[i] = _array[i];
242 for(
size_t i = 0; i < _capacity; i++)
247 services::daal_free(_array);
249 _capacity = newCapacity;
258 for(
size_t i = 0; i < _capacity; i++)
263 services::daal_free(_array);
275 bool insert(
const size_t pos,
const T &x)
277 if(pos > this->size())
282 size_t newSize = 1 + this->size();
283 if(newSize > _capacity)
285 if(!_resize()) {
return false;}
288 size_t tail = _size - pos;
289 for(
size_t i = 0; i < tail; i++)
291 _array[_size - i] = _array[_size - 1 - i];
303 bool insert(
const size_t pos, Collection<T> &other)
305 if(pos > this->size())
310 size_t newSize = other.size() + this->size();
311 if(newSize > _capacity)
313 if(!resize(newSize)) {
return false;}
316 size_t length = other.size();
317 size_t tail = _size - pos;
318 for(
size_t i = 0; i < tail; i++)
320 _array[_size + length - 1 - i] = _array[_size - 1 - i];
322 for(
size_t i = 0; i < length; i++)
324 _array[pos + i] = other[i];
334 void erase(
size_t pos)
336 if(pos >= this->size())
343 for(
size_t i = 0; i < _size - pos; i++)
345 _array[pos + i] = _array[pos + 1 + i];
350 static const size_t _default_capacity = 16;
353 size_t newCapacity = 2 * _capacity;
354 if(_capacity == 0) { newCapacity = _default_capacity; }
355 return resize(newCapacity);
367 using interface1::Collection;
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