libraries / Mitov / Mitov_SimpleList.hon commit Added link to project report (97a3ba0)
   1// SimpleList.h
   2
   3#ifndef _MITOV_SIMPLELIST_h
   4#define _MITOV_SIMPLELIST_h
   5
   6//#if defined(ARDUINO) && ARDUINO >= 100
   7#include "Arduino.h"
   8//#else
   9//#include "WProgram.h"
  10//#endif
  11
  12#ifndef NULL
  13#define NULL 0
  14#endif
  15
  16namespace Mitov
  17{
  18
  19template<typename T>
  20class SimpleList
  21{
  22public:
  23    ~SimpleList()
  24    {
  25        delete[] _list;
  26        _list = NULL;
  27        _size = 0;
  28    }
  29
  30        void AddCount( int ACount )
  31        {
  32                for( int i = 0; i < ACount; ++i )
  33                        push_back( T() );
  34        }
  35
  36        void SetCount( int ACount )
  37        {
  38                while( _size < ACount )
  39                        push_back( T() );
  40
  41                while( _size > ACount )
  42                        pop_back();
  43        }
  44
  45        void SetCount( int ACount, T AValue )
  46        {
  47                while( _size < ACount )
  48                        push_back( AValue );
  49
  50                while( _size > ACount )
  51                        pop_back();
  52        }
  53
  54    void push_back(T obj)
  55    {
  56        ++_size;
  57
  58        T* newBuffer = new T[_size];
  59
  60        if( _list )
  61        {
  62            for (unsigned int i = 0; i < _size - 1; ++i)
  63                newBuffer[i] = _list[i];
  64
  65            delete[] _list;
  66        }
  67
  68        newBuffer[_size - 1] = obj;
  69        _list = newBuffer;
  70    }
  71
  72    void append(T *obj, int ACount )
  73    {
  74                if( ! obj )
  75                        return;
  76
  77                if( !ACount )
  78                        return;
  79
  80                int AOldSize = _size;
  81        _size += ACount;
  82
  83        T* newBuffer = new T[_size];
  84        if( _list )
  85        {
  86                memcpy( newBuffer, _list, AOldSize * sizeof( T ));
  87                delete[] _list;
  88        }
  89
  90                memcpy( newBuffer + AOldSize, obj, ACount * sizeof( T ));
  91
  92        _list = newBuffer;
  93    }
  94
  95    void push_front(T obj)
  96    {
  97        ++_size;
  98        T* newBuffer = new T[_size];
  99
 100        if( _list )
 101        {
 102            for (unsigned int i = _size - 1; i > 0; --i)
 103                newBuffer[i] = _list[i - 1];
 104
 105                delete[] _list;
 106        }
 107
 108        newBuffer[0] = obj;
 109        _list = newBuffer;
 110    }
 111
 112    void pop_back()
 113    {
 114        if (empty())
 115            return;
 116
 117        --_size;
 118        if (empty())
 119        {
 120                        delete[] _list;
 121            _list = NULL;
 122            return;
 123        }
 124
 125        T* newBuffer = new T[_size];
 126                for (unsigned int i = 0; i < _size; ++i)
 127                        newBuffer[i] = _list[i];
 128
 129        delete[] _list;
 130        _list = newBuffer;
 131    }
 132
 133    void pop_front( int ACount = 1 )
 134    {
 135        if (empty())
 136            return;
 137
 138                int AOldSize = _size;
 139        _size -= ACount;
 140        if (empty())
 141        {
 142                        delete[] _list;
 143            _list = NULL;
 144            return;
 145        }
 146
 147        T* newBuffer = new T[_size];
 148
 149                memmove( newBuffer, _list + ACount, AOldSize * sizeof( T ));
 150
 151        delete[] _list;
 152        _list = newBuffer;
 153    }
 154
 155/*
 156    void reserve(unsigned int size)
 157    {
 158        if (size <= _size)
 159            return;
 160
 161        _preAlloc = size;
 162
 163        T* newBuffer = new T[_preAlloc];
 164
 165        for (unsigned int i = 0; i < _size; ++i)
 166            newBuffer[i] = _list[i];
 167
 168        delete[] _list;
 169        _list = newBuffer;
 170    }
 171*/
 172    typedef T* iterator;
 173
 174        iterator erase(iterator &itr)
 175        {
 176        if (empty())
 177            return NULL;
 178
 179        --_size;
 180        if (empty())
 181        {
 182                        delete[] _list;
 183            _list = NULL;
 184
 185            return NULL;
 186        }
 187
 188        T* newBuffer = new T[_size];
 189
 190        bool sum = false;
 191        unsigned int pos = 0;
 192        for (unsigned int i = 0; i < _size; ++i)
 193        {
 194            if (_list + i == itr)
 195            {
 196                sum = true;
 197                pos = i;
 198            }
 199
 200            if (sum)
 201                newBuffer[i] = _list[i + 1];
 202
 203            else
 204                newBuffer[i] = _list[i];
 205        }
 206
 207                delete[] _list;
 208        _list = newBuffer;
 209
 210        itr = _list + pos;
 211
 212        return itr;
 213    }
 214
 215    inline iterator begin() { return (empty() ? NULL : _list); }
 216    inline iterator end() { return (empty() ? NULL : _list + _size); }
 217
 218    void clear()
 219    {
 220        if (_list)
 221        {
 222            delete[] _list;
 223            _list = NULL;
 224        }
 225
 226        _size = 0;
 227    }
 228
 229    inline bool empty() const { return !_size; }
 230    inline unsigned int size() const { return _size; }
 231    inline operator T*() const { return _list; }
 232
 233        SimpleList<T> & operator = ( const SimpleList<T> &other )
 234        {
 235                if( &other == this )
 236                        return *this;
 237
 238                clear();
 239                append( other, other.size() );
 240
 241                return *this;
 242        }
 243
 244protected:
 245    T* _list = nullptr;
 246    unsigned int _size = 0;
 247};
 248
 249template<typename T>
 250class SimpleObjectList : public SimpleList<T>
 251{
 252        typedef SimpleList<T>   inherited;
 253
 254public:
 255    ~SimpleObjectList()
 256    {
 257                for( int i = 0; i < inherited::_size; ++i )
 258                        delete inherited::_list[ i ];
 259        }
 260};
 261
 262} // Mitov
 263
 264#endif