Simple Collections
Toggle main menu visibility
Loading...
Searching...
No Matches
src
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
18
namespace
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
31
typedef
uint8_t bsize_t;
32
#elif defined(__AVR__)
33
# define DEFAULT_LIST_SIZE 5
34
# define DEFAULT_GROW_MODE GROW_BY_5
35
typedef
uint8_t bsize_t;
36
#else
37
# define DEFAULT_LIST_SIZE 10
38
# define DEFAULT_GROW_MODE GROW_BY_DOUBLE
39
typedef
size_t
bsize_t;
40
#endif
41
42
43
namespace
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
95
namespace
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
:
135
ioaTreeInternal::BtreeStorage
treeStorage;
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
283
using namespace
tccollection;
284
#endif
285
286
#endif
// SIMPLE_COLLECTIONS_H
SCThreadingSupport.h
provides the thread safety implementation for circular buffers
ioaTreeInternal::BtreeStorage
Definition
SimpleCollections.h:55
tccollection::BtreeIterator
Definition
SimpleCollections.h:102
tccollection::BtreeList::items
const V * items() const
Definition
SimpleCollections.h:217
tccollection::BtreeList::capacity
bsize_t capacity() const
Definition
SimpleCollections.h:239
tccollection::BtreeList::removeByKey
bool removeByKey(K key)
Definition
SimpleCollections.h:198
tccollection::BtreeList::count
bsize_t count() const
Definition
SimpleCollections.h:232
tccollection::BtreeList::removeIndex
void removeIndex(bsize_t index)
Definition
SimpleCollections.h:204
tccollection::BtreeList::add
bool add(const V &item)
Definition
SimpleCollections.h:184
tccollection::BtreeList::getByKey
V * getByKey(K key) const
Definition
SimpleCollections.h:191
tccollection::BtreeList::BtreeList
BtreeList(bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
Definition
SimpleCollections.h:143
tccollection::BtreeList::end
BtreeIterator< V > end() const
Definition
SimpleCollections.h:262
tccollection::BtreeList::nearestLocation
bsize_t nearestLocation(const K &key) const
Definition
SimpleCollections.h:212
tccollection::BtreeList::BtreeList
BtreeList(ioaTreeInternal::KeyAccessor customAccessor, bsize_t size=DEFAULT_LIST_SIZE, GrowByMode howToGrow=DEFAULT_GROW_MODE)
Definition
SimpleCollections.h:164
tccollection::BtreeList::clear
void clear()
Definition
SimpleCollections.h:244
tccollection::BtreeList::itemAtIndex
V * itemAtIndex(bsize_t idx) const
Definition
SimpleCollections.h:224