|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// A template-ized lookup table implementation |
| 4 | +// |
| 5 | +// this is this stupidest table implementation ever but it's |
| 6 | +// an MVP for unit testing. O(n). |
| 7 | +template <typename K, typename V> |
| 8 | +class Table { |
| 9 | + private: |
| 10 | + struct Node { |
| 11 | + K key; |
| 12 | + V val; |
| 13 | + Node *next; |
| 14 | + }; |
| 15 | + |
| 16 | + Node* mStart; |
| 17 | + unsigned long mSize; |
| 18 | + // to alow const reference signatures, pre-allocate nil values |
| 19 | + K mNilK; |
| 20 | + V mNilV; |
| 21 | + |
| 22 | + void init() { |
| 23 | + mStart = NULL; |
| 24 | + mSize = 0; |
| 25 | + } |
| 26 | + |
| 27 | + public: |
| 28 | + Table() : mNilK(), mNilV() { init(); } |
| 29 | + |
| 30 | + // number of things in the table |
| 31 | + inline unsigned long size() const { return mSize; } |
| 32 | + |
| 33 | + // whether there are no things |
| 34 | + inline bool empty() const { return 0 == mSize; } |
| 35 | + |
| 36 | + // whether there is a thing stored at the given key |
| 37 | + bool has(K const key) const { |
| 38 | + for (Node* p = mStart; p; p = p->next) { |
| 39 | + if (p->key == key) return true; |
| 40 | + } |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + // allow find operations on keys |
| 45 | + template <typename T> |
| 46 | + const K& getMatchingKey(T const firstArg, bool (*isMatch)(const T, const K)) const { |
| 47 | + for (Node* p = mStart; p; p = p->next) { |
| 48 | + if (isMatch(firstArg, p->key)) return p->key; |
| 49 | + } |
| 50 | + return mNilK; |
| 51 | + } |
| 52 | + |
| 53 | + // allow iteration over entire table, with a work function that takes key/value pairs |
| 54 | + void iterate(void (*work)(const K&, const V&)) const { |
| 55 | + for (Node* p = mStart; p; p = p->next) work(p->key, p->val); |
| 56 | + } |
| 57 | + void iterate(void (*work)(K, V)) const { |
| 58 | + for (Node* p = mStart; p; p = p->next) work(p->key, p->val); |
| 59 | + } |
| 60 | + |
| 61 | + // allow iteration over entire table, with a work function that takes key/value pairs |
| 62 | + // plus an initial argument. this enables member function passing (via workaround) |
| 63 | + template <typename T> |
| 64 | + void iterate(void (*work)(T&, const K&, const V&), T& firstArg) const { |
| 65 | + for (Node* p = mStart; p; p = p->next) work(firstArg, p->key, p->val); |
| 66 | + } |
| 67 | + |
| 68 | + template <typename T> |
| 69 | + void iterate(void (*work)(T&, K, V), T& firstArg) const { |
| 70 | + for (Node* p = mStart; p; p = p->next) work(firstArg, p->key, p->val); |
| 71 | + } |
| 72 | + |
| 73 | + template <typename T> |
| 74 | + void iterate(void (*work)(T, K, V), T firstArg) const { |
| 75 | + for (Node* p = mStart; p; p = p->next) work(firstArg, p->key, p->val); |
| 76 | + } |
| 77 | + |
| 78 | + // return the value for a given key |
| 79 | + const V& get(K const key) const { |
| 80 | + for (Node* p = mStart; p; p = p->next) { |
| 81 | + if (p->key == key) return p->val; |
| 82 | + } |
| 83 | + return mNilV; |
| 84 | + } |
| 85 | + |
| 86 | + // remove an item by key |
| 87 | + bool remove(K const key) { |
| 88 | + Node *o = NULL; |
| 89 | + for (Node* p = mStart; p; p = p->next) { |
| 90 | + if (p->key == key) { |
| 91 | + (o ? o->next : mStart) = p->next; |
| 92 | + delete p; |
| 93 | + --mSize; |
| 94 | + return true; |
| 95 | + } |
| 96 | + o = p; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + // add a key/value pair. deletes any existing key by that name. |
| 102 | + bool add(K const key, V const val) { |
| 103 | + remove(key); |
| 104 | + Node *n = new Node; |
| 105 | + if (n == NULL) return false; |
| 106 | + n->key = key; |
| 107 | + n->val = val; |
| 108 | + n->next = mStart; |
| 109 | + mStart = n; |
| 110 | + ++mSize; |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + // remove everything |
| 115 | + void clear() { |
| 116 | + Node* p; |
| 117 | + while (mStart) { |
| 118 | + p = mStart; |
| 119 | + mStart = mStart->next; |
| 120 | + delete p; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + ~Table() { clear(); } |
| 125 | +}; |
0 commit comments