Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. //DynamicCollection.cpp
  2. #include "Arduino.h"
  3. #include "DynamicCollection.h"
  4.  
  5. template<class T>
  6. inline DynamicCollection<T>::~DynamicCollection()
  7. {
  8.     delete[] _pInnerCollection;
  9. }
  10.  
  11. template<class T>
  12. inline DynamicCollection<T>::DynamicCollection()
  13. {
  14.     _pInnerCollection = new T[0];
  15.     _InnerCollLength = 0;
  16. }
  17.  
  18. template<class T>
  19. inline DynamicCollection<T>::DynamicCollection(int length)
  20. {
  21.     _pInnerCollection = new T[length];
  22.     _InnerCollLength = length;
  23. }
  24.  
  25. template<class T>
  26. inline DynamicCollection<T>::DynamicCollection(int length, T _default)
  27. {
  28.     _pInnerCollection = new T[length];
  29.     _InnerCollLength = length;
  30.     for (int iter = 0; iter < Length(); iter++) this[iter] = _default;
  31. }
  32.  
  33. template<class T>
  34. inline void DynamicCollection<T>::Add(T object)
  35. {
  36.     _pInnerCollection = resize(_pInnerCollection, _InnerCollLength, _InnerCollLength + 1);
  37.     _pInnerCollection[_InnerCollLength] = object;
  38.     _InnerCollLength++;
  39. }
  40.  
  41. template<class T>
  42. inline void DynamicCollection<T>::Remove(T object)
  43. {
  44.     if (_InnerCollLength <= 0) return;
  45.     for (int iter = 0; iter < _InnerCollLength; iter++) if ((*this)[iter] == object) RemoveAt(iter);
  46. }
  47.  
  48. template<class T>
  49. inline void DynamicCollection<T>::RemoveAt(uint index)
  50. {
  51.     if (index >= _InnerCollLength) return;
  52.     memcpy(_pInnerCollection + index, _pInnerCollection + index + 1, sizeof(T) * (_InnerCollLength - index - 1));
  53.     _pInnerCollection = resize(_pInnerCollection, _InnerCollLength, _InnerCollLength - 1);
  54.     _InnerCollLength--;
  55. }
  56.  
  57. template<class T>
  58. inline int DynamicCollection<T>::Length() { return _InnerCollLength; }
  59.  
  60. template<class T>
  61. inline T & DynamicCollection<T>::operator[](uint index) { return *(_pInnerCollection + index); }
  62.  
  63. template<class T>
  64. inline const T & DynamicCollection<T>::operator[](uint index) const { return *(_pInnerCollection + index); }
  65.  
  66. template<class T>
  67. inline T * DynamicCollection<T>::resize(T * pArray, uint OldSize, uint NewSize)
  68. {
  69.     T *pNewArray = new T[NewSize];
  70.     memcpy(pNewArray, pArray, sizeof(T) * (NewSize > OldSize ? OldSize : NewSize));
  71.     delete[] pArray;
  72.     return pNewArray;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement