|
| 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 | +} |
0 commit comments