Simple Collections
Loading...
Searching...
No Matches
SimpleCollections.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry)
3 * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4 */
5
10
11#ifndef SIMPLE_COLLECTIONS_H
12#define SIMPLE_COLLECTIONS_H
13
14#include <string.h>
15#include <inttypes.h>
16#include "SCThreadingSupport.h"
17
18namespace tccollection {
19
20 enum GrowByMode : unsigned char {
21 GROW_NEVER,
22 GROW_BY_5,
23 GROW_BY_DOUBLE
24 };
25
26}
27
28#ifdef __AVR_ATmega2560__
29# define DEFAULT_LIST_SIZE 10
30# define DEFAULT_GROW_MODE GROW_BY_5
31typedef uint8_t bsize_t;
32#elif defined(__AVR__)
33# define DEFAULT_LIST_SIZE 5
34# define DEFAULT_GROW_MODE GROW_BY_5
35typedef uint8_t bsize_t;
36#else
37# define DEFAULT_LIST_SIZE 10
38# define DEFAULT_GROW_MODE GROW_BY_DOUBLE
39typedef size_t bsize_t;
40#endif
41
42
43namespace ioaTreeInternal {
44
45 typedef uint32_t (*KeyAccessor)(const void *item);
46
47 typedef void (*CopyOperator)(void *dest, const void *src);
48
55 class BtreeStorage {
56 private:
57 void *binTree;
58 void *swapperSpace;
59 KeyAccessor keyAccessor;
60 CopyOperator copier;
61 bsize_t currentCapacity;
62 bsize_t currentSize;
63 bsize_t sizeOfAnItem;
64 tccollection::GrowByMode growByMode;
65 public:
66 BtreeStorage(bsize_t size, tccollection::GrowByMode howToGrow, bsize_t eachItemSize, KeyAccessor compareOperator,
67 CopyOperator copyOperator);
68
69 ~BtreeStorage();
70
71 bool add(const void *newItem);
72
73 bool removeByKey(uint32_t key);
74
75 void removeIndex(bsize_t index);
76
77 bsize_t nearestLocation(uint32_t key) const;
78
79 void *getByKey(uint32_t key) const;
80
81 void clear() { currentSize = 0; }
82
83 void *underlyingData() const { return binTree; }
84
85 bsize_t getCapacity() const { return currentCapacity; }
86
87 bsize_t getCurrentSize() const { return currentSize; }
88
89 private:
90 bool checkCapacity();
91 void *memoryOf(void *baseMem, bsize_t item) const { return ((uint8_t *) baseMem) + (item * sizeOfAnItem); }
92 };
93}
94
95namespace tccollection {
96
101 template<class T>
102 class BtreeIterator {
103 private:
104 bsize_t currentPosition;
105 const ioaTreeInternal::BtreeStorage &storage;
106 public:
107 BtreeIterator(bsize_t position, const ioaTreeInternal::BtreeStorage &storage) : currentPosition(position),
108 storage(storage) {}
109
110 void operator++() { ++currentPosition; }
111
112 bool operator!=(const BtreeIterator &other) const { return currentPosition != other.currentPosition; }
113
114 T &operator*() { return ((T *) storage.underlyingData())[currentPosition]; }
115 };
116
132 template<class K, class V>
133 class BtreeList {
134 private:
136 public:
143 explicit BtreeList(bsize_t size = DEFAULT_LIST_SIZE, GrowByMode howToGrow = DEFAULT_GROW_MODE)
144 : treeStorage(size, howToGrow, sizeof(V), keyAccessor, copyInternal) {}
145
164 explicit BtreeList(ioaTreeInternal::KeyAccessor customAccessor, bsize_t size = DEFAULT_LIST_SIZE, GrowByMode howToGrow = DEFAULT_GROW_MODE)
165 : treeStorage(size, howToGrow, sizeof(V), customAccessor, copyInternal) {}
166
167 static uint32_t keyAccessor(const void *itm) {
168 return reinterpret_cast<const V *>(itm)->getKey();
169 }
170
171 static void copyInternal(void *dest, const void *src) {
172 auto *itemDest = reinterpret_cast<V *>(dest);
173 auto *itemSrc = reinterpret_cast<const V *>(src);
174 *itemDest = *itemSrc;
175 }
176
184 bool add(const V &item) { return treeStorage.add(&item); }
185
191 V *getByKey(K key) const { return reinterpret_cast<V *>(treeStorage.getByKey(key)); };
192
198 bool removeByKey(K key) { return treeStorage.removeByKey(key); }
199
204 void removeIndex(bsize_t index) { treeStorage.removeIndex(index); }
205
212 bsize_t nearestLocation(const K &key) const { return treeStorage.nearestLocation(key); }
213
217 const V *items() const { return reinterpret_cast<V *>(treeStorage.underlyingData()); };
218
224 V *itemAtIndex(bsize_t idx) const {
225 auto *binTree = reinterpret_cast<V *>(treeStorage.underlyingData());
226 return (idx < treeStorage.getCurrentSize()) ? &binTree[idx] : NULL;
227 }
228
232 bsize_t count() const {
233 return treeStorage.getCurrentSize();
234 }
235
239 bsize_t capacity() const { return treeStorage.getCapacity(); }
240
244 void clear() { treeStorage.clear(); }
245
256 BtreeIterator<V> begin() const { return BtreeIterator<V>(0, treeStorage); }
257
262 BtreeIterator<V> end() const { return BtreeIterator<V>(treeStorage.getCurrentSize(), treeStorage); }
263
274 void forEachItem(void(*paramFn)(V *)) {
275 for (bsize_t i = 0; i < count(); ++i) {
276 paramFn(itemAtIndex(i));
277 }
278 }
279 };
280}
281
282#ifndef TC_COLLECTION_MANUAL_NAMESPACE
283using namespace tccollection;
284#endif
285
286#endif // SIMPLE_COLLECTIONS_H
provides the thread safety implementation for circular buffers
Definition SimpleCollections.h:55
Definition SimpleCollections.h:102
const V * items() const
Definition SimpleCollections.h:217
bsize_t capacity() const
Definition SimpleCollections.h:239
bool removeByKey(K key)
Definition SimpleCollections.h:198
bsize_t count() const
Definition SimpleCollections.h:232
void removeIndex(bsize_t index)
Definition SimpleCollections.h:204
bool add(const V &item)
Definition SimpleCollections.h:184
V * getByKey(K key) const
Definition SimpleCollections.h:191
BtreeList(bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
Definition SimpleCollections.h:143
BtreeIterator< V > end() const
Definition SimpleCollections.h:262
bsize_t nearestLocation(const K &key) const
Definition SimpleCollections.h:212
BtreeList(ioaTreeInternal::KeyAccessor customAccessor, bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
Definition SimpleCollections.h:164
void clear()
Definition SimpleCollections.h:244
V * itemAtIndex(bsize_t idx) const
Definition SimpleCollections.h:224