Skip to content

Commit d939528

Browse files
author
dave
committed
Preparing first revision.
1 parent ebcdc76 commit d939528

4 files changed

Lines changed: 102 additions & 1 deletion

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ Where the size is the initial capacity of the list, and the grow by mode is one
114114

115115
bsize_t capacity() // the current allocated size of the array
116116

117+
## Platforms known to work
118+
119+
| Platform | Board / Arch | State |
120+
|----------|----------------|------------------|
121+
| Arduino | Nano 33 BLE | Examples run |
122+
| Arduino | Uno, MEGA, AVR | Examples run |
123+
| Arduino | SAMD MKR1300 | Examples run |
124+
| Arduino | SAMD Seeed | Examples run |
125+
| Arduino | ESP8266 | Awaiting |
126+
| Arduino | ESP32 | Awaiting |
127+
| mbed | STM32F4 | Ran mbed example |
128+
117129
## Making changes to SimpleCollections
118130

119131
We welcome people rolling up their sleeves and helping out, but please do reach out to us before starting any work, so we can ensure its in sync with our development. We use platformIO for development and have a specific project available to help you get started, along with tests that check many elements still work as expected. See [https://github.com/davetcc/tcLibraryDev]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* This list is not designed to replace the collections in the standard library, it is a fairly light collection
3+
* that works across a very wide range of boards. It is the underpinnings of IoAbstraction, and we use it the mbed
4+
* support too. So it is very much production ready.
5+
*
6+
* Here we create a class that we are going to store in the btree list, items in btree lists are ordered so
7+
* insertion is a little slow, but retrieval, even by key is very quick - Log(NumItems). It dynamically reallocates
8+
* if needed too, so there's no need in most cases to fiddle directly with the sizes.
9+
*
10+
* The only limitation of putting an item into the btree is that the method getKey() must be implemented by the type
11+
* that is stored. You can see the default sizes of the list and other parameters in file SimpleCollections.h for
12+
* advanced users.
13+
*
14+
* It is only designed to store small, simple class types that are not polymorphic, however, you can wrap a more dynamic
15+
* class type if you needed too as a field of this class.
16+
*
17+
* Important note for advanced users: This list is not thread safe. If you use it within task manager only this is not
18+
* an issue, but if you use it elsewhere too, it must be protected by some kind of mutex or atomic block.
19+
*/
20+
21+
#include <mbed.h>
22+
#include <SimpleCollections.h>
23+
24+
BufferedSerial console(USBTX, USBRX, 115200);
25+
26+
class MbedStorage {
27+
private:
28+
uint32_t hash;
29+
char mbedData[20];
30+
public:
31+
MbedStorage() = default;
32+
MbedStorage(const MbedStorage& other) = default;
33+
MbedStorage& operator=(const MbedStorage& other) = default;
34+
MbedStorage(const char* sz) {
35+
uint32_t h = 9348451;
36+
for(size_t i=0; i<strlen(sz); i++) {
37+
h *= sz[i];
38+
}
39+
hash = h;
40+
strncpy(mbedData, sz, sizeof(mbedData));
41+
}
42+
43+
uint32_t getKey() const { return hash; }
44+
45+
const char* getData() const { return mbedData; }
46+
};
47+
48+
BtreeList<uint32_t, MbedStorage> myList;
49+
50+
51+
void log(const char* toLog) {
52+
console.write(toLog, strlen(toLog));
53+
console.write("\n", 1);
54+
}
55+
56+
int main() {
57+
auto frenchWelcome = MbedStorage("Bienvenue");
58+
myList.add(MbedStorage("Hello world"));
59+
myList.add(MbedStorage("Hello mbed"));
60+
myList.add(MbedStorage("Aloha"));
61+
myList.add(MbedStorage(frenchWelcome));
62+
63+
while(1) {
64+
log("Iterate list using C++ range");
65+
for (auto d: myList) {
66+
log(d.getData());
67+
}
68+
69+
log("Iterate list using forEach");
70+
myList.forEachItem([](MbedStorage *storage) {
71+
log(storage->getData());
72+
});
73+
74+
log("Find an item by key");
75+
auto toCheck = myList.getByKey(frenchWelcome.getKey());
76+
if (toCheck && toCheck->getKey() == frenchWelcome.getKey()) {
77+
log("Found item using key");
78+
} else {
79+
log("Find by key failed");
80+
}
81+
82+
ThisThread::sleep_for(2000ms);
83+
}
84+
return 0;
85+
}

src/SimpleCollections.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
44
*/
55

6+
#ifdef __AVR__
7+
#include <Arduino.h>
8+
#else
69
#include <malloc.h>
10+
#endif
711
#include "SimpleCollections.h"
812

913
using namespace ioaTreeInternal;

src/SimpleCollections.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace ioaTreeInternal {
8989
template<class T> class BtreeIterator {
9090
private:
9191
bsize_t currentPosition;
92-
ioaTreeInternal::BtreeStorage storage;
92+
ioaTreeInternal::BtreeStorage& storage;
9393
public:
9494
BtreeIterator(bsize_t position, ioaTreeInternal::BtreeStorage& storage) : currentPosition(position), storage(storage){}
9595
void operator++() { ++currentPosition; }

0 commit comments

Comments
 (0)