Dynalib Utils
DynaList.h
Go to the documentation of this file.
1 
12 #ifndef DYNALIST_H
13 #define DYNALIST_H
14 
15 #include <exception>
16 #include <type_traits>
17 //#include <cstring>
18 #include "TypeDefs.h"
19 #include "ICopyable.h"
20 
21 using namespace std;
22 
23 #define AUTO_PACK 0x01
24 #define PACK_NEEDED 0x02
25 #define AUTO_TRIM 0x04
26 #define OWNS_MEMBERS 0x08
27 #define END_OF_LIST -1
28 #define INVALID_INDEX -1
29 #define FIRST_FREE -2
30 
31 class String;
32 
33 #define MAKE_LISTTYPE_DEF(C, T) \
34  typedef DynaList<C> T##List
35 
36 template <class T> class DynaListIter;
37 
38 template <class T> class DynaList : public ICopyable< DynaList<T> > {
39 
40  friend class DynaListIter<T>;
41 
42  T** _members = nullptr;
43  uint _capacity = 0;
44  uint _count = 0;
45  uint8_t _flags = AUTO_PACK | AUTO_TRIM | OWNS_MEMBERS;
46 
47 protected:
48  void _insertArray(T** array, int arrayOffset, int toIndex, int count);
49  void _swap(int index1, int index2);
50  void _trimTheCount();
51  DynaList<T>* _copyRange(int frIndex, int toIndex);
52  void _deleteExcessCapacity();
53  void _deleteRange(int frIndex, int toIndex);
54  void _clearRange(int frIndex, int toIndex);
55  void _nullRange(int frIndex, int toIndex);
56  void _deleteOrClear(int frIndex, int toIndex);
57  bool _setAutoPack(bool isPack);
58  bool _setAutoTrim(bool isTrim);
59  int _nextFreeSlot();
60 
61  inline void setFlags(uint8_t flags) { _flags |= flags; }
62  inline void setAllFlags(uint8_t flags) { _flags = flags; }
63  inline void clearFlags(uint8_t flags) { _flags &= ~flags; }
64  inline void clearAllFlags() { _flags = 0x00; }
65  inline bool isFlags(uint8_t flags) { return (_flags & flags) == flags; }
66 
67 public:
68  //========================================================================================
69  // Instance Methods
70  //========================================================================================
71  DynaList();
72  explicit DynaList(uint initCapacity);
73  explicit DynaList(T* array, int count);
74  virtual ~DynaList();
75 
76  DynaList(const DynaList<T>& other);
77  DynaList<T>* copy() override;
78 
79  inline uint getCapacity() { return _capacity; }
80  inline uint getCount() { return _count; }
81  inline uint capacity() { return _capacity; }
82  inline uint count() { return _count; }
83  inline bool isFull() { return _count == _capacity; }
84  inline bool isEmpty() { return _count == 0; }
85  inline T* get(uint index) const { return _members[index]; }
86  inline T** getInternalTypedArray() { return _members; }
87  inline void** getInternalRawArray() { return (void **)_members; }
88  inline T* first() { return _count > 0 ? _members[0] : nullptr; }
89  inline T* last() { return _count > 0 ? _members[_count-1] : nullptr; }
90 
91  // inline T* operator[](uint index) { return _members[index]; }
92  // inline const T* operator[](uint index) const { return _members[index]; }
93 
94  inline T* operator [](uint index) const { return _members[index]; }
95  inline T*& operator [](uint index) { return _members[index]; }
96 
97  // void operator+=(T* value) { append(value); };
98  // void operator+=(T&& value) { append(&value); };
99  // void operator-=(int index) { deleteItem(index); };
101  DynaList<T>& operator+=(T&& value);
102  DynaList<T>& operator-=(int index);
103 
104  DynaList<T>& operator<<(T* value);
105  DynaList<T>& operator<<(T&& value);
106 
107  int firstNullSlot();
108  DynaList<T>* setCapacity(uint newCapacity);
109  DynaList<T>* setAutoPackTrim(bool pack, bool trim);
110  DynaList<T>* setOwnsMembers(bool ownsMembers);
111  void adjustSize(int delta);
112  void adjustSizeLog(int delta);
113  void trim();
114  void pack();
115  void clear();
116 
117  int indexOf(T* value, uint start, uint step);
118  int indexOf(T** value, uint arraySize, uint start, uint step);
119 
120  int insert(int index, T* value);
121  int insert(int index, T&& value);
122  int insert(int index, T** array, uint length);
123  int insert(int index, DynaList<T>* list);
124  int append(T* value);
125  int append(T&& value);
126  int append(T** array, uint length);
127  int append(DynaList<T>* list);
128  T* set(int index, T* value);
129  void move(int index, int destIndex);
130  void slide(int frIndex, int toIndex);
131  T* remove(int index);
132  DynaList<T>* remove(int frIndex, int toIndex);
133  void deleteItem(int index);
134  void deleteItems(int frIndex, int toIndex);
135  void push(T* value);
136  void pushLast(T* value);
137  T* pop();
138  T* popLast();
139  DynaListIter<T> begin() const;
140  DynaListIter<T> end() const;
141 };
142 
143 template <class T> class DynaListIter {
144  int _curIndex;
145  int _lastReturned = INVALID_INDEX;
146  const DynaList<T>* _list = nullptr;
147 
148 public:
149  DynaListIter (const DynaList<T>* list, int start)
150  : _curIndex(start), _list(list)
151  { }
152 
153  int getIndex() { return _curIndex; }
154  bool hasNext() { return _curIndex < _list->_count; }
155  bool hasPrev() { return _curIndex >= 0; }
156 
157  bool operator== (const DynaListIter<T>& other) const {
158  return _curIndex == other._curIndex;
159  }
160 
161  bool operator!= (const DynaListIter<T>& other) const {
162  return _curIndex != other._curIndex;
163  }
164 
165  T* operator* () const {
166  return _list->get( _curIndex );
167  }
168 
169  const DynaListIter<T>& operator++ () {
170  ++_curIndex;
171  return *this;
172  }
173 
174  const DynaListIter<T>& operator-- () {
175  --_curIndex;
176  return *this;
177  }
178 };
179 
181 
182 #endif //DYNALIST_H
CONSTCD11 bool operator==(const day &x, const day &y) NOEXCEPT
Definition: date.h:1274
#define OWNS_MEMBERS
Definition: DynaList.h:26
bool hasNext()
Definition: DynaList.h:154
GeneratorWrapper< T > value(T &&value)
Definition: catch.hpp:4005
void clearAllFlags()
Definition: DynaList.h:64
void setAllFlags(uint8_t flags)
Definition: DynaList.h:62
DynaListIter(const DynaList< T > *list, int start)
Definition: DynaList.h:149
uint capacity()
Definition: DynaList.h:81
Definition: String.h:60
uint getCount()
Definition: DynaList.h:80
T * get(uint index) const
Definition: DynaList.h:85
void clearFlags(uint8_t flags)
Definition: DynaList.h:63
bool isEmpty()
Definition: DynaList.h:84
#define INVALID_INDEX
Definition: DynaList.h:28
T ** getInternalTypedArray()
Definition: DynaList.h:86
std::string trim(std::string const &str)
Returns a new string without whitespace at the start/end.
Definition: DynaList.h:38
#define AUTO_PACK
Definition: DynaList.h:23
CONSTCD11 bool operator!=(const day &x, const day &y) NOEXCEPT
Definition: date.h:1282
T * last()
Definition: DynaList.h:89
auto operator+=(std::string &lhs, StringRef const &sr) -> std::string &
uint getCapacity()
Definition: DynaList.h:79
bool hasPrev()
Definition: DynaList.h:155
#define MAKE_LISTTYPE_DEF(C, T)
Definition: DynaList.h:33
void setFlags(uint8_t flags)
Definition: DynaList.h:61
bool isFlags(uint8_t flags)
Definition: DynaList.h:65
uint count()
Definition: DynaList.h:82
int getIndex()
Definition: DynaList.h:153
std::ostream & operator<<(std::ostream &, Catch_global_namespace_dummy)
T * first()
Definition: DynaList.h:88
Definition: ICopyable.h:8
#define AUTO_TRIM
Definition: DynaList.h:25
void ** getInternalRawArray()
Definition: DynaList.h:87
Definition: DynaList.h:36
bool isFull()
Definition: DynaList.h:83