Skip to content

Commit c53f381

Browse files
committed
#8, #7 make read only methods const, provide a way to provide a custom keyAccessor
1 parent d4eda9c commit c53f381

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

src/SimpleCollections.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool BtreeStorage::checkCapacity() {
100100
return true;
101101
}
102102

103-
bsize_t BtreeStorage::nearestLocation(uint32_t key) {
103+
bsize_t BtreeStorage::nearestLocation(uint32_t key) const {
104104
// a few short circuits, basically handling quickly nothing in list,
105105
// one item in the list and an insertion at the end of the list.
106106
if(currentSize == 0) return 0; // always first item in this case
@@ -132,7 +132,7 @@ bsize_t BtreeStorage::nearestLocation(uint32_t key) {
132132
return end;
133133
}
134134

135-
void *BtreeStorage::getByKey(uint32_t key) {
135+
void *BtreeStorage::getByKey(uint32_t key) const {
136136
if(currentSize == 0) return nullptr;
137137
bsize_t loc = nearestLocation(key);
138138
return (keyAccessor(memoryOf(binTree, loc)) == key && loc < currentSize) ? memoryOf(binTree, loc) : nullptr;

src/SimpleCollections.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ namespace ioaTreeInternal {
7272

7373
void removeIndex(bsize_t index);
7474

75-
bsize_t nearestLocation(uint32_t key);
75+
bsize_t nearestLocation(uint32_t key) const;
7676

77-
void *getByKey(uint32_t key);
77+
void *getByKey(uint32_t key) const;
7878

7979
void clear() { currentSize = 0; }
8080

81-
void *underlyingData() { return binTree; }
81+
void *underlyingData() const { return binTree; }
8282

8383
bsize_t getCapacity() const { return currentCapacity; }
8484

@@ -100,9 +100,9 @@ namespace tccollection {
100100
class BtreeIterator {
101101
private:
102102
bsize_t currentPosition;
103-
ioaTreeInternal::BtreeStorage &storage;
103+
const ioaTreeInternal::BtreeStorage &storage;
104104
public:
105-
BtreeIterator(bsize_t position, ioaTreeInternal::BtreeStorage &storage) : currentPosition(position),
105+
BtreeIterator(bsize_t position, const ioaTreeInternal::BtreeStorage &storage) : currentPosition(position),
106106
storage(storage) {}
107107

108108
void operator++() { ++currentPosition; }
@@ -132,8 +132,35 @@ namespace tccollection {
132132
private:
133133
ioaTreeInternal::BtreeStorage treeStorage;
134134
public:
135+
/**
136+
* Create a btree list that can be used to store simple objects that are not polymorphic. If no parameters are
137+
* provided it will construct with the defaults for your board.
138+
* @param size the initial size of the list, optional
139+
* @param howToGrow the way in which it should grow if space runs out, optional
140+
*/
135141
explicit BtreeList(bsize_t size = DEFAULT_LIST_SIZE, GrowByMode howToGrow = DEFAULT_GROW_MODE)
136142
: treeStorage(size, howToGrow, sizeof(V), keyAccessor, copyInternal) {}
143+
/**
144+
* Advanced usage constructor, prefer using the other constructor whenever possible.
145+
*
146+
* This constructor allows you to provide your own key accessor, some other libraries require slightly different
147+
* ways of accessing the object. For the key accessor you'll be provided with a pointer to a memory area, this
148+
* area contains the item for which we need the key, you return the key value from the accessor as a uint32_t.
149+
*
150+
* The default key accessor is as follows:
151+
*
152+
* ```
153+
* static uint32_t keyAccessor(const void *itm) {
154+
* return reinterpret_cast<const V *>(itm)->getKey();
155+
* }
156+
* ```
157+
*
158+
* @param customAccessor the custom accessor that is called to return the key
159+
* @param size the size of the list, optional parameter.
160+
* @param howToGrow the method for growing when space runs out, optional parameter
161+
*/
162+
explicit BtreeList(ioaTreeInternal::KeyAccessor customAccessor, bsize_t size = DEFAULT_LIST_SIZE, GrowByMode howToGrow = DEFAULT_GROW_MODE)
163+
: treeStorage(size, howToGrow, sizeof(V), customAccessor, copyInternal) {}
137164

138165
static uint32_t keyAccessor(const void *itm) {
139166
return reinterpret_cast<const V *>(itm)->getKey();
@@ -159,7 +186,7 @@ namespace tccollection {
159186
* @param key the key to be looked up
160187
* @return the value at that key position or null.
161188
*/
162-
V *getByKey(K key) { return reinterpret_cast<V *>(treeStorage.getByKey(key)); };
189+
V *getByKey(K key) const { return reinterpret_cast<V *>(treeStorage.getByKey(key)); };
163190

164191
/**
165192
* Remove an item using the key it was added with
@@ -180,34 +207,34 @@ namespace tccollection {
180207
* @param key the key to lookup
181208
* @return the position in the list
182209
*/
183-
bsize_t nearestLocation(const K &key) { return treeStorage.nearestLocation(key); }
210+
bsize_t nearestLocation(const K &key) const { return treeStorage.nearestLocation(key); }
184211

185212
/**
186213
* @return a list of all items
187214
*/
188-
const V *items() { return reinterpret_cast<V *>(treeStorage.underlyingData()); };
215+
const V *items() const { return reinterpret_cast<V *>(treeStorage.underlyingData()); };
189216

190217
/**
191218
* gets an item by it's index
192219
* @param idx the index to find
193220
* @return the item at the index or null.
194221
*/
195-
V *itemAtIndex(bsize_t idx) {
222+
V *itemAtIndex(bsize_t idx) const {
196223
auto *binTree = reinterpret_cast<V *>(treeStorage.underlyingData());
197224
return (idx < treeStorage.getCurrentSize()) ? &binTree[idx] : NULL;
198225
}
199226

200227
/**
201228
* @return number of items in the list
202229
*/
203-
bsize_t count() {
230+
bsize_t count() const {
204231
return treeStorage.getCurrentSize();
205232
}
206233

207234
/**
208235
* @return current capacity of the list
209236
*/
210-
bsize_t capacity() { return treeStorage.getCapacity(); }
237+
bsize_t capacity() const { return treeStorage.getCapacity(); }
211238

212239
/**
213240
* Completely clear down the list such that calls to count return 0.
@@ -224,13 +251,13 @@ namespace tccollection {
224251
*
225252
* @return an iterator for the beginning of the list.
226253
*/
227-
BtreeIterator<V> begin() { return BtreeIterator<V>(0, treeStorage); }
254+
BtreeIterator<V> begin() const { return BtreeIterator<V>(0, treeStorage); }
228255

229256
/**
230257
* An iterator that represnts the value beyond the end of the list, for use with C++ ranges.
231258
* @return an iterator that represents the end of the list. Normally used with begin().
232259
*/
233-
BtreeIterator<V> end() { return BtreeIterator<V>(treeStorage.getCurrentSize(), treeStorage); }
260+
BtreeIterator<V> end() const { return BtreeIterator<V>(treeStorage.getCurrentSize(), treeStorage); }
234261

235262
/**
236263
* Implements a simple foreach item loop, where your callback function is called once for each item in the list.

0 commit comments

Comments
 (0)