Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DynamicCollection.cpp
- #include "Arduino.h"
- #include "DynamicCollection.h"
- template<class T>
- inline DynamicCollection<T>::~DynamicCollection()
- {
- delete[] _pInnerCollection;
- }
- template<class T>
- inline DynamicCollection<T>::DynamicCollection()
- {
- _pInnerCollection = new T[0];
- _InnerCollLength = 0;
- }
- template<class T>
- inline DynamicCollection<T>::DynamicCollection(int length)
- {
- _pInnerCollection = new T[length];
- _InnerCollLength = length;
- }
- template<class T>
- inline DynamicCollection<T>::DynamicCollection(int length, T _default)
- {
- _pInnerCollection = new T[length];
- _InnerCollLength = length;
- for (int iter = 0; iter < Length(); iter++) this[iter] = _default;
- }
- template<class T>
- inline void DynamicCollection<T>::Add(T object)
- {
- _pInnerCollection = resize(_pInnerCollection, _InnerCollLength, _InnerCollLength + 1);
- _pInnerCollection[_InnerCollLength] = object;
- _InnerCollLength++;
- }
- template<class T>
- inline void DynamicCollection<T>::Remove(T object)
- {
- if (_InnerCollLength <= 0) return;
- for (int iter = 0; iter < _InnerCollLength; iter++) if ((*this)[iter] == object) RemoveAt(iter);
- }
- template<class T>
- inline void DynamicCollection<T>::RemoveAt(uint index)
- {
- if (index >= _InnerCollLength) return;
- memcpy(_pInnerCollection + index, _pInnerCollection + index + 1, sizeof(T) * (_InnerCollLength - index - 1));
- _pInnerCollection = resize(_pInnerCollection, _InnerCollLength, _InnerCollLength - 1);
- _InnerCollLength--;
- }
- template<class T>
- inline int DynamicCollection<T>::Length() { return _InnerCollLength; }
- template<class T>
- inline T & DynamicCollection<T>::operator[](uint index) { return *(_pInnerCollection + index); }
- template<class T>
- inline const T & DynamicCollection<T>::operator[](uint index) const { return *(_pInnerCollection + index); }
- template<class T>
- inline T * DynamicCollection<T>::resize(T * pArray, uint OldSize, uint NewSize)
- {
- T *pNewArray = new T[NewSize];
- memcpy(pNewArray, pArray, sizeof(T) * (NewSize > OldSize ? OldSize : NewSize));
- delete[] pArray;
- return pNewArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement