11#ifndef TASKMANAGER_IO_COLLECTIONS_H
12#define TASKMANAGER_IO_COLLECTIONS_H
18namespace tccollection {
20 enum GrowByMode :
unsigned char {
28#ifdef __AVR_ATmega2560__
29# define DEFAULT_LIST_SIZE 10
30# define DEFAULT_GROW_MODE GROW_BY_5
31typedef uint8_t bsize_t;
33# define DEFAULT_LIST_SIZE 5
34# define DEFAULT_GROW_MODE GROW_BY_5
35typedef uint8_t bsize_t;
37# define DEFAULT_LIST_SIZE 10
38# define DEFAULT_GROW_MODE GROW_BY_DOUBLE
39typedef size_t bsize_t;
43namespace ioaTreeInternal {
45 typedef uint32_t (*KeyAccessor)(
const void *item);
47 typedef void (*CopyOperator)(
void *dest,
const void *src);
59 KeyAccessor keyAccessor;
61 bsize_t currentCapacity;
64 tccollection::GrowByMode growByMode;
66 BtreeStorage(bsize_t size, tccollection::GrowByMode howToGrow, bsize_t eachItemSize, KeyAccessor compareOperator,
67 CopyOperator copyOperator);
71 bool add(
const void *newItem);
73 bool removeByKey(uint32_t key);
75 void removeIndex(bsize_t index);
77 bsize_t nearestLocation(uint32_t key)
const;
79 void *getByKey(uint32_t key)
const;
81 void clear() { currentSize = 0; }
83 void *underlyingData()
const {
return binTree; }
85 bsize_t getCapacity()
const {
return currentCapacity; }
87 bsize_t getCurrentSize()
const {
return currentSize; }
91 void *memoryOf(
void *baseMem, bsize_t item)
const {
return static_cast<uint8_t*
>(baseMem) + (item * sizeOfAnItem); }
95namespace tccollection {
102 class BtreeIterator {
104 bsize_t currentPosition;
110 void operator++() { ++currentPosition; }
112 bool operator!=(
const BtreeIterator &other)
const {
return currentPosition != other.currentPosition; }
114 T &operator*() {
return ((T *) storage.underlyingData())[currentPosition]; }
132 template<
class K,
class V>
143 explicit BtreeList(bsize_t size = DEFAULT_LIST_SIZE, GrowByMode howToGrow = DEFAULT_GROW_MODE)
144 : treeStorage(size, howToGrow, sizeof(V), keyAccessor, copyInternal) {}
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) {}
167 static uint32_t keyAccessor(
const void *itm) {
168 return reinterpret_cast<const V *
>(itm)->getKey();
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;
184 bool add(
const V &item) {
return treeStorage.add(&item); }
191 V *
getByKey(K key)
const {
return reinterpret_cast<V *
>(treeStorage.getByKey(key)); };
204 void removeIndex(bsize_t index) { treeStorage.removeIndex(index); }
212 bsize_t
nearestLocation(
const K &key)
const {
return treeStorage.nearestLocation(key); }
217 const V *
items()
const {
return reinterpret_cast<V *
>(treeStorage.underlyingData()); };
225 auto *binTree =
reinterpret_cast<V *
>(treeStorage.underlyingData());
226 return (idx < treeStorage.getCurrentSize()) ? &binTree[idx] : NULL;
233 return treeStorage.getCurrentSize();
239 bsize_t
capacity()
const {
return treeStorage.getCapacity(); }
244 void clear() { treeStorage.clear(); }
274 void forEachItem(
void(*paramFn)(V *)) {
275 for (bsize_t i = 0; i <
count(); ++i) {
282#ifndef TC_COLLECTION_MANUAL_NAMESPACE
283using namespace tccollection;
Definition BTreeList.h:55
Definition BTreeList.h:102
const V * items() const
Definition BTreeList.h:217
bsize_t capacity() const
Definition BTreeList.h:239
bool removeByKey(K key)
Definition BTreeList.h:198
bsize_t count() const
Definition BTreeList.h:232
void removeIndex(bsize_t index)
Definition BTreeList.h:204
bool add(const V &item)
Definition BTreeList.h:184
V * getByKey(K key) const
Definition BTreeList.h:191
BtreeList(bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
Definition BTreeList.h:143
BtreeIterator< V > end() const
Definition BTreeList.h:262
bsize_t nearestLocation(const K &key) const
Definition BTreeList.h:212
BtreeList(ioaTreeInternal::KeyAccessor customAccessor, bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
Definition BTreeList.h:164
void clear()
Definition BTreeList.h:244
V * itemAtIndex(bsize_t idx) const
Definition BTreeList.h:224