| 103 | */ |
| 104 | |
| 105 | template <class Elem> class Dynamic_array |
| 106 | { |
| 107 | DYNAMIC_ARRAY array; |
| 108 | public: |
| 109 | Dynamic_array(uint prealloc=16, uint increment=16) |
| 110 | { |
| 111 | init(prealloc, increment); |
| 112 | } |
| 113 | |
| 114 | void init(uint prealloc=16, uint increment=16) |
| 115 | { |
| 116 | my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | @note Though formally this could be declared "const" it would be |
| 121 | misleading at it returns a non-const pointer to array's data. |
| 122 | */ |
| 123 | Elem& at(int idx) |
| 124 | { |
| 125 | return *(((Elem*)array.buffer) + idx); |
| 126 | } |
| 127 | /// Const variant of at(), which cannot change data |
| 128 | const Elem& at(int idx) const |
| 129 | { |
| 130 | return *(((Elem*)array.buffer) + idx); |
| 131 | } |
| 132 | |
| 133 | /// @returns pointer to first element; undefined behaviour if array is empty |
| 134 | Elem *front() |
| 135 | { |
| 136 | DBUG_ASSERT(array.elements >= 1); |
| 137 | return (Elem*)array.buffer; |
| 138 | } |
| 139 | |
| 140 | /// @returns pointer to first element; undefined behaviour if array is empty |
| 141 | const Elem *front() const |
| 142 | { |
| 143 | DBUG_ASSERT(array.elements >= 1); |
| 144 | return (const Elem*)array.buffer; |
| 145 | } |
| 146 | |
| 147 | /// @returns pointer to last element; undefined behaviour if array is empty. |
| 148 | Elem *back() |
| 149 | { |
| 150 | DBUG_ASSERT(array.elements >= 1); |
| 151 | return ((Elem*)array.buffer) + (array.elements - 1); |
| 152 | } |
| 153 | |
| 154 | /// @returns pointer to last element; undefined behaviour if array is empty. |
| 155 | const Elem *back() const |
| 156 | { |
| 157 | DBUG_ASSERT(array.elements >= 1); |
| 158 | return ((const Elem*)array.buffer) + (array.elements - 1); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | @retval false ok |
nothing calls this directly
no test coverage detected