| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | #ifndef INCLUDE_COMPACTLIST_IMPL_HPP | |||
| 2 | #define INCLUDE_COMPACTLIST_IMPL_HPP | |||
| 3 | #include <iostream> | |||
| 4 | #include "singleton_pool.hpp" | |||
| 5 | ||||
| 6 | // Properties: | |||
| 7 | //- data remains at the same memory address | |||
| 8 | //- overhead vector assures quick access by index | |||
| 9 | //- indices in vector change to keep a closed row | |||
| 10 | // CompactListItems contain their index number; this is updated when changed. | |||
| 11 | template <class T, int AllocatorGranularity = 32> | |||
| 12 | class CompactList | |||
| 13 | { | |||
| 14 | private: | |||
| 15 | ||||
| 16 | std::vector<T*> elements; | |||
| 17 | ||||
| 18 | // Memory management: choose to place items (T) together in blocks of | |||
| 19 | // AllocatorGranularity. IMPORTANT NOTE: Not tread-safe! | |||
| 20 | struct PoolTag | |||
| 21 | { | |||
| 22 | }; | |||
| 23 | ||||
| 24 | using MemPool = crts::SingletonPool<T>; | |||
| 25 | T* insert_element(T* new_element); | |||
| 26 | void remove(T*); | |||
| 27 | void remove_all(); | |||
| 28 | ||||
| 29 | public: | |||
| 30 | ||||
| 31 | ✗ | CompactList(): | ||
| 32 | elements() | |||
| 33 | { | |||
| 34 | ✗ | elements.reserve(AllocatorGranularity); | ||
| 35 | ✗ | }; | ||
| 36 | ||||
| 37 | ~CompactList(); | |||
| 38 | ||||
| 39 | ✗ | T* element_address(int idx) { return elements[idx]; }; | ||
| 40 | ||||
| 41 | void remove_element(int idx); | |||
| 42 | ||||
| 43 | ✗ | int size() { return elements.size(); }; | ||
| 44 | ||||
| 45 | T& operator[](int idx) { return *(elements[idx]); }; | |||
| 46 | ||||
| 47 | T* create(); | |||
| 48 | template <class Q> | |||
| 49 | T* create(Q); | |||
| 50 | template <class Q, class R> | |||
| 51 | T* create(Q, R); | |||
| 52 | template <class Q, class R, class S> | |||
| 53 | T* create(Q, R, S); | |||
| 54 | }; | |||
| 55 | ||||
| 56 | template <class T, int I> | |||
| 57 | ✗ | CompactList<T, I>::~CompactList() | ||
| 58 | { | |||
| 59 | ✗ | remove_all(); | ||
| 60 | ✗ | } | ||
| 61 | ||||
| 62 | template <class T, int I> | |||
| 63 | ✗ | void CompactList<T, I>::remove_all() | ||
| 64 | // private function (called by ~) | |||
| 65 | { | |||
| 66 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | for (int j = elements.size() - 1; j >= 0; j--) | |
| 67 | { | |||
| 68 | ✗ | remove(elements[j]); | ||
| 69 | } | |||
| 70 | ✗ | elements.pop_back(); | ||
| 71 | ✗ | } | ||
| 72 | ||||
| 73 | template <class T, int I> | |||
| 74 | inline void CompactList<T, I>::remove(T* element) | |||
| 75 | // private function (called by removeAll (~) and RemoveElement) | |||
| 76 | // Removes a single element. No checking is performed on the pointer. | |||
| 77 | { | |||
| 78 | ✗ | element->~T(); | ||
| 79 | MemPool::free(element); | |||
| 80 | return; | |||
| 81 | } | |||
| 82 | ||||
| 83 | template <class T, int I> | |||
| 84 | inline T* CompactList<T, I>::create() | |||
| 85 | { | |||
| 86 | T* new_element = new (MemPool::malloc()) T(); | |||
| 87 | return insert_element(new_element); | |||
| 88 | } | |||
| 89 | ||||
| 90 | template <class T, int I> | |||
| 91 | template <class Q> | |||
| 92 | ✗ | inline T* CompactList<T, I>::create(Q par1) | ||
| 93 | { | |||
| 94 | ✗ | T* new_element = new (MemPool::malloc()) T(par1); | ||
| 95 | ✗ | return insert_element(new_element); | ||
| 96 | } | |||
| 97 | ||||
| 98 | template <class T, int I> | |||
| 99 | template <class Q, class R> | |||
| 100 | inline T* CompactList<T, I>::create(Q par1, R par2) | |||
| 101 | { | |||
| 102 | T* new_element = new (MemPool::malloc()) T(par1, par2); | |||
| 103 | return insert_element(new_element); | |||
| 104 | } | |||
| 105 | ||||
| 106 | template <class T, int I> | |||
| 107 | template <class Q, class R, class S> | |||
| 108 | inline T* CompactList<T, I>::create(Q par1, R par2, S par3) | |||
| 109 | { | |||
| 110 | T* new_element = new (MemPool::malloc()) T(par1, par2, par3); | |||
| 111 | return insert_element(new_element); | |||
| 112 | } | |||
| 113 | ||||
| 114 | template <class T, int I> | |||
| 115 | ✗ | inline T* CompactList<T, I>::insert_element(T* new_element) | ||
| 116 | // private function! | |||
| 117 | { | |||
| 118 | ✗ | new_element->index = elements.size(); | ||
| 119 | ✗ | elements.push_back(new_element); | ||
| 120 | ✗ | return new_element; | ||
| 121 | } | |||
| 122 | ||||
| 123 | template <class T, int I> | |||
| 124 | ✗ | inline void CompactList<T, I>::remove_element(int idx) | ||
| 125 | // check (only the outer!!) can eliminate? | |||
| 126 | { | |||
| 127 | ✗ | int size = elements.size(); | ||
| 128 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (idx < size) | |
| 129 | { | |||
| 130 | ✗ | remove(elements[idx]); | ||
| 131 | // if idx was not the last element, move last element to the gap | |||
| 132 |
0/2✗ Decision 'true' not taken.
✗ Decision 'false' not taken.
|
✗ | if (idx != size - 1) | |
| 133 | { | |||
| 134 | ✗ | elements[idx] = elements.back(); | ||
| 135 | ✗ | elements[idx]->index = idx; | ||
| 136 | } | |||
| 137 | ✗ | elements.pop_back(); | ||
| 138 | } | |||
| 139 | else | |||
| 140 | { | |||
| 141 | std::cerr << "ERROR CompactList: trying to remove non-existing element. " | |||
| 142 | "(index = " | |||
| 143 | ✗ | << idx << ", size = " << size << ")\n"; | ||
| 144 | } | |||
| 145 | ✗ | return; | ||
| 146 | } | |||
| 147 | ||||
| 148 | template <class T> | |||
| 149 | class CompactListItem | |||
| 150 | { | |||
| 151 | public: | |||
| 152 | ||||
| 153 | ✗ | CompactListItem(): | ||
| 154 | ✗ | index(0) {}; | ||
| 155 | int index; | |||
| 156 | ||||
| 157 | protected: | |||
| 158 | ||||
| 159 | virtual ~CompactListItem() {}; | |||
| 160 | template <class Q, int I> | |||
| 161 | friend class CompactList; | |||
| 162 | }; | |||
| 163 | ||||
| 164 | #endif // INCLUDE_COMPACTLIST_IMPL_HPP | |||
| 165 |