| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | /** @file | ||
| 30 | * @brief Arrays and other linear sequential type field | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_ARRAYS_HPP_INCLUDED | ||
| 35 | #define GATE_ARRAYS_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/gate_core_api.hpp" | ||
| 38 | #include "gate/arrays.h" | ||
| 39 | |||
| 40 | #include "gate/gatetypes.hpp" | ||
| 41 | #include "gate/memalloc.hpp" | ||
| 42 | #include "gate/results.hpp" | ||
| 43 | #include "gate/exceptions.hpp" | ||
| 44 | #include "gate/enumerators.hpp" | ||
| 45 | #include "gate/comparers.hpp" | ||
| 46 | |||
| 47 | namespace gate | ||
| 48 | { | ||
| 49 | |||
| 50 | /// @brief | ||
| 51 | class GATE_CORE_CPP_API GenericArray | ||
| 52 | { | ||
| 53 | public: | ||
| 54 | GenericArray() noexcept; | ||
| 55 | GenericArray(GenericArray const& src) noexcept; | ||
| 56 | GenericArray(gate_array_t const& src) noexcept; | ||
| 57 | GenericArray& operator=(GenericArray const& src) noexcept; | ||
| 58 | ~GenericArray() noexcept; | ||
| 59 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 60 | GenericArray(GenericArray&& src) noexcept; | ||
| 61 | GenericArray& operator=(GenericArray&& src) noexcept; | ||
| 62 | #endif | ||
| 63 | static GenericArray createFrom(gate_array_t& other) noexcept; | ||
| 64 | |||
| 65 | void swap(GenericArray& other) noexcept; | ||
| 66 | gate_array_t const* c_impl() const noexcept; | ||
| 67 | gate_array_t* c_impl() noexcept; | ||
| 68 | |||
| 69 | bool_t empty() const noexcept; | ||
| 70 | size_t length() const noexcept; | ||
| 71 | size_t size() const noexcept; | ||
| 72 | bool_t contains(size_t index) const noexcept; | ||
| 73 | void const* getItemPtr(size_t index) const noexcept; | ||
| 74 | size_t find(gate_comparer_t comparer, void const* compareWith, size_t startAt = 0) const noexcept; | ||
| 75 | |||
| 76 | GenericArray subset(size_t index, size_t length) const; | ||
| 77 | GenericArray copy() const; | ||
| 78 | |||
| 79 | protected: | ||
| 80 | gate_array_t impl; | ||
| 81 | |||
| 82 | protected: | ||
| 83 | static void check_outofmem_nullptr(void const* ptr); | ||
| 84 | |||
| 85 | }; | ||
| 86 | |||
| 87 | |||
| 88 | /// @brief | ||
| 89 | /// @tparam T | ||
| 90 | template <class T> | ||
| 91 | class GATE_CORE_CPP_TEMPLATE_API Array : public GenericArray | ||
| 92 | { | ||
| 93 | public: | ||
| 94 | typedef T item_t; | ||
| 95 | typedef Array<T> self_t; | ||
| 96 | typedef item_t const* iterator; | ||
| 97 | typedef item_t const* const_iterator; | ||
| 98 | |||
| 99 | public: | ||
| 100 | class reverse_iterator | ||
| 101 | { | ||
| 102 | private: | ||
| 103 | item_t const* ptr; | ||
| 104 | |||
| 105 | public: | ||
| 106 | 48 | reverse_iterator(item_t const* tptr) : ptr(tptr) {} | |
| 107 | 26 | item_t const& operator*() { return *this->ptr; } | |
| 108 | 8 | item_t const* operator->() { return this->ptr; } | |
| 109 | 28 | reverse_iterator& operator++() | |
| 110 | { | ||
| 111 | 28 | --this->ptr; | |
| 112 | 28 | return *this; | |
| 113 | } | ||
| 114 | 18 | reverse_iterator operator++(int) | |
| 115 | { | ||
| 116 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
18 | reverse_iterator ret(this->ptr--); |
| 117 | 18 | return ret; | |
| 118 | } | ||
| 119 | 18 | reverse_iterator& operator--() | |
| 120 | { | ||
| 121 | 18 | ++this->ptr; | |
| 122 | 18 | return *this; | |
| 123 | } | ||
| 124 | 18 | reverse_iterator operator--(int) | |
| 125 | { | ||
| 126 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
18 | reverse_iterator ret(this->ptr++); |
| 127 | 18 | return ret; | |
| 128 | } | ||
| 129 | 1 | bool operator==(reverse_iterator const& that) const { return this->ptr == that.ptr; } | |
| 130 | 10 | bool operator!=(reverse_iterator const& that) const { return this->ptr != that.ptr; } | |
| 131 | }; | ||
| 132 | typedef reverse_iterator const_reverse_iterator; | ||
| 133 | |||
| 134 | 431 | Array() noexcept | |
| 135 | 431 | { | |
| 136 | 431 | gate_array_t* tmp = gate_array_create_static(&this->impl, NULL, sizeof(item_t), 0); | |
| 137 | #ifdef GATE_DEBUG_MODE | ||
| 138 | 431 | check_outofmem_nullptr(tmp); | |
| 139 | #else | ||
| 140 | (void)tmp; | ||
| 141 | #endif | ||
| 142 | 431 | } | |
| 143 | 13 | Array(item_t const* items, size_t count) | |
| 144 | 13 | { | |
| 145 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | gate_arraylist_t arrlist = gate_arraylist_create(sizeof(item_t), items, count, &TypeFunctions<item_t>::copyConstruct, &TypeFunctions<item_t>::destruct); |
| 146 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | check_outofmem_nullptr(arrlist.ptr); |
| 147 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | gate_array_t* result = gate_array_create(&this->impl, arrlist); |
| 148 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | gate_arraylist_release(&arrlist); |
| 149 |
1/2✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
|
13 | check_outofmem_nullptr(result); |
| 150 | 13 | } | |
| 151 | 5 | Array(self_t const& src) noexcept | |
| 152 | 5 | { | |
| 153 | 5 | gate_array_t* const tmp = gate_array_duplicate(&this->impl, &src.impl); | |
| 154 | #ifdef GATE_DEBUG_MODE | ||
| 155 | 5 | check_outofmem_nullptr(tmp); | |
| 156 | #endif | ||
| 157 | (void)tmp; | ||
| 158 | 5 | } | |
| 159 | |||
| 160 | 1 | Array(gate_array_t const& arr) | |
| 161 | 1 | { | |
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (arr.item_size != sizeof(item_t)) |
| 163 | { | ||
| 164 | ✗ | GATEXX_RAISE_ERROR(results::IncorrectType); | |
| 165 | } | ||
| 166 |
3/6✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
1 | gate_arraylist_t arrlist = gate_arraylist_create(sizeof(item_t), |
| 167 | gate_array_get(&arr, 0), gate_array_length(&arr), | ||
| 168 | &TypeFunctions<item_t>::copyConstruct, &TypeFunctions<item_t>::destruct); | ||
| 169 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | check_outofmem_nullptr(arrlist.ptr); |
| 170 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_array_t* result = gate_array_create(&this->impl, arrlist); |
| 171 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_arraylist_release(&arrlist); |
| 172 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | check_outofmem_nullptr(result); |
| 173 | 1 | } | |
| 174 | 476 | ~Array() noexcept | |
| 175 | { | ||
| 176 | 476 | } | |
| 177 | |||
| 178 | 4 | static self_t createStatic(item_t const* items, size_t count) noexcept | |
| 179 | { | ||
| 180 | 4 | self_t ret; | |
| 181 | 4 | gate_array_t* const tmp = gate_array_create_static(&ret.impl, items, sizeof(item_t), count); | |
| 182 | #ifdef GATE_DEBUG_MODE | ||
| 183 | 4 | check_outofmem_nullptr(tmp); | |
| 184 | #endif | ||
| 185 | (void)tmp; | ||
| 186 | 4 | return ret; | |
| 187 | } | ||
| 188 | |||
| 189 | #if !defined(GATE_COMPILER_SUPPORTS_CPP_ARRAY_SIZE_DEDUCTION) | ||
| 190 | template <class A> | ||
| 191 | static self_t createStaticFrom(A fixed_array) noexcept | ||
| 192 | { | ||
| 193 | return self_t::createStatic(&fixed_array[0], sizeof(fixed_array) / sizeof(fixed_array[0])); | ||
| 194 | } | ||
| 195 | #else | ||
| 196 | template <unsigned N> | ||
| 197 | 1 | static self_t createStaticFrom(item_t const (&items)[N]) noexcept | |
| 198 | { | ||
| 199 | 1 | return self_t::createStatic(&items[0], N); | |
| 200 | } | ||
| 201 | #endif | ||
| 202 | |||
| 203 | 272 | static self_t createFrom(gate_array_t& arr) | |
| 204 | { | ||
| 205 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 162 times.
|
272 | if (arr.item_size != sizeof(item_t)) |
| 206 | { | ||
| 207 | 4 | GATEXX_RAISE_ERROR(results::IncorrectType); | |
| 208 | } | ||
| 209 | 268 | self_t ret; | |
| 210 |
1/2✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
|
268 | gate_mem_copy(&ret.impl, &arr, sizeof(arr)); |
| 211 |
1/2✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
|
268 | gate_mem_clear(&arr, sizeof(arr)); |
| 212 | 268 | return ret; | |
| 213 | } | ||
| 214 | |||
| 215 | 123 | void swap(self_t& that) noexcept | |
| 216 | { | ||
| 217 | 123 | GenericArray::swap(that); | |
| 218 | 123 | } | |
| 219 | |||
| 220 | 3 | self_t& operator=(self_t const& src) noexcept | |
| 221 | { | ||
| 222 | 3 | Array that(src); | |
| 223 | 3 | this->swap(that); | |
| 224 | 3 | return *this; | |
| 225 | } | ||
| 226 | |||
| 227 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 228 | 1 | Array(self_t&& src) noexcept | |
| 229 | 1 | : GenericArray(gate::moveRef(src)) | |
| 230 | { | ||
| 231 | 1 | } | |
| 232 | |||
| 233 | 60 | self_t& operator=(self_t&& that) noexcept | |
| 234 | { | ||
| 235 | 60 | self_t tmp; | |
| 236 | 60 | that.swap(tmp); | |
| 237 | 60 | this->swap(tmp); | |
| 238 | 60 | return *this; | |
| 239 | } | ||
| 240 | #endif | ||
| 241 | |||
| 242 | 2091 | item_t const* getItemPtr(size_t ndx) const noexcept | |
| 243 | { | ||
| 244 | 2091 | item_t const* ptr = static_cast<item_t const*>(GenericArray::getItemPtr(ndx)); | |
| 245 | 2091 | return ptr; | |
| 246 | } | ||
| 247 | |||
| 248 | 2072 | item_t const& at(size_t ndx) const noexcept | |
| 249 | { | ||
| 250 | 2072 | item_t const* ptr = this->getItemPtr(ndx); | |
| 251 | #ifdef GATE_DEBUG_MODE | ||
| 252 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1054 times.
|
2072 | if (ptr == NULL) |
| 253 | { | ||
| 254 | ✗ | gate::panic(results::OutOfBounds, "Array::at()"); | |
| 255 | } | ||
| 256 | #endif | ||
| 257 | 2072 | return *ptr; | |
| 258 | } | ||
| 259 | |||
| 260 | typedef Enumerator<item_t const> enumerator_t; | ||
| 261 | |||
| 262 | 41 | enumerator_t enumerate() const noexcept | |
| 263 | { | ||
| 264 | gate_enumerator_t enumerator; | ||
| 265 | 41 | gate_enumerator_t* const tmp = gate_array_enumerate(&this->impl, &enumerator); | |
| 266 | #ifdef GATE_DEBUG_MODE | ||
| 267 | 41 | check_outofmem_nullptr(tmp); | |
| 268 | #endif | ||
| 269 | (void)tmp; | ||
| 270 | 41 | return enumerator_t(enumerator); | |
| 271 | } | ||
| 272 | |||
| 273 | 2050 | item_t const& operator[](size_t ndx) const noexcept | |
| 274 | { | ||
| 275 | 2050 | return this->at(ndx); | |
| 276 | } | ||
| 277 | |||
| 278 | 1 | self_t subset(size_t offset, size_t count) const | |
| 279 | { | ||
| 280 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | GenericArray arr = GenericArray::subset(offset, count); |
| 281 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | self_t ret = self_t::createFrom(*arr.c_impl()); |
| 282 | 2 | return ret; | |
| 283 | } | ||
| 284 | |||
| 285 | 1 | self_t copy() const | |
| 286 | { | ||
| 287 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | GenericArray arr = GenericArray::copy(); |
| 288 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | self_t ret = self_t::createFrom(*arr.c_impl()); |
| 289 | 2 | return ret; | |
| 290 | } | ||
| 291 | |||
| 292 | 64 | const_iterator begin() const noexcept { return (item_t const*)this->impl.data_ptr; } | |
| 293 | 26 | const_iterator end() const noexcept { return this->begin() + this->impl.item_count; } | |
| 294 | 1 | const_iterator cbegin() const noexcept { return this->begin(); } | |
| 295 | 1 | const_iterator cend() const noexcept { return this->end(); } | |
| 296 | |||
| 297 | 6 | const_reverse_iterator rend() const noexcept { return const_reverse_iterator(this->begin() - 1); } | |
| 298 | 6 | const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(this->begin() + this->impl.item_count - 1); } | |
| 299 | 1 | const_reverse_iterator crbegin() const noexcept { return this->rbegin(); } | |
| 300 | 1 | const_reverse_iterator crend() const noexcept { return this->rend(); } | |
| 301 | }; | ||
| 302 | |||
| 303 | /// @brief | ||
| 304 | /// @tparam T | ||
| 305 | /// @param arr | ||
| 306 | /// @return | ||
| 307 | template <class T> | ||
| 308 | Enumerator<T const> enumerate(Array<T> const& arr) noexcept | ||
| 309 | { | ||
| 310 | return arr.enumerate(); | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | class GATE_CORE_CPP_API GenericArrayList | ||
| 315 | { | ||
| 316 | public: | ||
| 317 | GenericArrayList() noexcept; | ||
| 318 | GenericArrayList(size_t itemSize, void const* source, size_t sourceLength, gate_mem_copyctor_t cctor, gate_mem_dtor_t dtor); | ||
| 319 | GenericArrayList(GenericArrayList const& src); | ||
| 320 | GenericArrayList& operator=(GenericArrayList const& src); | ||
| 321 | ~GenericArrayList() noexcept; | ||
| 322 | |||
| 323 | static GenericArrayList createFrom(gate_arraylist_t& al); | ||
| 324 | |||
| 325 | gate_arraylist_t& c_impl(); | ||
| 326 | gate_arraylist_t const& c_impl() const; | ||
| 327 | bool_t isNull() const noexcept; | ||
| 328 | void swap(GenericArrayList& that) noexcept; | ||
| 329 | |||
| 330 | size_t itemSize() const noexcept; | ||
| 331 | size_t length() const noexcept; | ||
| 332 | bool_t empty() const noexcept; | ||
| 333 | |||
| 334 | void* add(void const* ptrItem); | ||
| 335 | void* add(void const* ptrItem, size_t repeatCount); | ||
| 336 | void* insertAt(size_t index, void const* ptrItem); | ||
| 337 | VoidResult removeAt(size_t index) noexcept; | ||
| 338 | void clear() noexcept; | ||
| 339 | void* getItemPtr(size_t index) noexcept; | ||
| 340 | void const* getItemPtr(size_t index) const noexcept; | ||
| 341 | bool_t contains(size_t index) const noexcept; | ||
| 342 | |||
| 343 | GenericArrayList copy() const; | ||
| 344 | GenericArray toArray() const; | ||
| 345 | |||
| 346 | protected: | ||
| 347 | gate_arraylist_t impl; | ||
| 348 | |||
| 349 | protected: | ||
| 350 | static void check_outofmem_nullptr(void const* ptr); | ||
| 351 | }; | ||
| 352 | |||
| 353 | |||
| 354 | /// @brief | ||
| 355 | /// @tparam T | ||
| 356 | template <class T> | ||
| 357 | class GATE_CORE_CPP_TEMPLATE_API ArrayList : public GenericArrayList | ||
| 358 | { | ||
| 359 | public: | ||
| 360 | typedef T item_t; | ||
| 361 | typedef T* iterator; | ||
| 362 | typedef T const* const_iterator; | ||
| 363 | typedef ArrayList<T> self_t; | ||
| 364 | |||
| 365 | class reverse_iterator | ||
| 366 | { | ||
| 367 | private: | ||
| 368 | item_t* ptr; | ||
| 369 | |||
| 370 | public: | ||
| 371 | 12 | reverse_iterator(T* tptr) : ptr(tptr) {} | |
| 372 | 24 | item_t& operator*() { return *this->ptr; } | |
| 373 | 4 | item_t* operator->() { return this->ptr; } | |
| 374 | 18 | reverse_iterator& operator++() | |
| 375 | { | ||
| 376 | 18 | --this->ptr; | |
| 377 | 18 | return *this; | |
| 378 | } | ||
| 379 | 1 | reverse_iterator operator++(int) | |
| 380 | { | ||
| 381 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1 | reverse_iterator ret(this->ptr--); |
| 382 | 1 | return ret; | |
| 383 | } | ||
| 384 | 1 | reverse_iterator& operator--() | |
| 385 | { | ||
| 386 | 1 | ++this->ptr; | |
| 387 | 1 | return *this; | |
| 388 | } | ||
| 389 | 1 | reverse_iterator operator--(int) | |
| 390 | { | ||
| 391 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
1 | reverse_iterator ret(this->ptr++); |
| 392 | 1 | return ret; | |
| 393 | } | ||
| 394 | 1 | bool operator==(reverse_iterator const& that) const { return this->ptr == that.ptr; } | |
| 395 | 12 | bool operator!=(reverse_iterator const& that) const { return this->ptr != that.ptr; } | |
| 396 | }; | ||
| 397 | |||
| 398 | class const_reverse_iterator | ||
| 399 | { | ||
| 400 | private: | ||
| 401 | item_t const* ptr; | ||
| 402 | |||
| 403 | public: | ||
| 404 | 32 | const_reverse_iterator(item_t const* tptr) : ptr(tptr) {} | |
| 405 | 4 | item_t const& operator*() const { return *this->ptr; } | |
| 406 | 4 | item_t const* operator->() const { return this->ptr; } | |
| 407 | 4 | const_reverse_iterator& operator++() | |
| 408 | { | ||
| 409 | 4 | --this->ptr; | |
| 410 | 4 | return *this; | |
| 411 | } | ||
| 412 | 4 | const_reverse_iterator operator++(int) | |
| 413 | { | ||
| 414 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
4 | const_reverse_iterator ret(this->ptr--); |
| 415 | 4 | return ret; | |
| 416 | } | ||
| 417 | 4 | const_reverse_iterator& operator--() | |
| 418 | { | ||
| 419 | 4 | ++this->ptr; | |
| 420 | 4 | return *this; | |
| 421 | } | ||
| 422 | 8 | const_reverse_iterator operator--(int) | |
| 423 | { | ||
| 424 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
8 | const_reverse_iterator ret(this->ptr++); |
| 425 | 8 | return ret; | |
| 426 | } | ||
| 427 | 1 | bool operator==(const_reverse_iterator const& that) const { return this->ptr == that.ptr; } | |
| 428 | 5 | bool operator!=(const_reverse_iterator const& that) const { return this->ptr != that.ptr; } | |
| 429 | }; | ||
| 430 | |||
| 431 | public: | ||
| 432 | 333 | ArrayList(size_t prealloc = 0) | |
| 433 | 333 | : GenericArrayList(sizeof(item_t), NULL, prealloc, &TypeFunctions<item_t>::copyConstruct, &TypeFunctions<item_t>::destruct) | |
| 434 | { | ||
| 435 | 333 | } | |
| 436 | |||
| 437 | 3 | ArrayList(item_t const* items, size_t count) | |
| 438 | 3 | : GenericArrayList(sizeof(item_t), items, count, &TypeFunctions<item_t>::copyConstruct, &TypeFunctions<item_t>::destruct) | |
| 439 | { | ||
| 440 | 3 | } | |
| 441 | |||
| 442 | 2 | ArrayList(Array<item_t> const& copyFrom) | |
| 443 | 2 | { | |
| 444 | 2 | size_t const length = copyFrom.length(); | |
| 445 | 2 | item_t const* const first = copyFrom.getItemPtr(0); | |
| 446 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | this->impl = gate_arraylist_create(sizeof(item_t), first, length, &TypeFunctions<item_t>::copyConstruct, &TypeFunctions<item_t>::destruct); |
| 447 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | check_outofmem_nullptr(this->impl.ptr); |
| 448 | 2 | } | |
| 449 | |||
| 450 | 2 | ArrayList(Enumerator<item_t const> enumerator) | |
| 451 | 2 | { | |
| 452 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | self_t arr; |
| 453 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
|
10 | for (; enumerator.valid(); enumerator.next()) |
| 454 | { | ||
| 455 |
2/4✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
8 | arr.add(*enumerator); |
| 456 | } | ||
| 457 | 2 | this->swap(arr); | |
| 458 | 2 | } | |
| 459 | |||
| 460 | 1 | ArrayList(self_t const& src) noexcept | |
| 461 | 1 | : GenericArrayList(src) | |
| 462 | { | ||
| 463 | 1 | } | |
| 464 | |||
| 465 | 352 | ~ArrayList() noexcept | |
| 466 | { | ||
| 467 | 352 | } | |
| 468 | |||
| 469 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 470 | ✗ | ArrayList(self_t&& that) noexcept | |
| 471 | ✗ | : GenericArrayList(GenericArrayList::createFrom(that.impl)) | |
| 472 | { | ||
| 473 | ✗ | Mem::clear(that.impl); | |
| 474 | ✗ | } | |
| 475 | |||
| 476 | 1 | self_t& operator=(self_t&& that) noexcept | |
| 477 | { | ||
| 478 | 1 | this->swap(that); | |
| 479 | 1 | gate_arraylist_release(&that.impl); | |
| 480 | 1 | return *this; | |
| 481 | } | ||
| 482 | #endif | ||
| 483 | |||
| 484 | 3 | self_t copy() const | |
| 485 | { | ||
| 486 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | GenericArrayList gal = GenericArrayList::copy(); |
| 487 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | self_t ret; |
| 488 | 3 | GenericArrayList& retref = ret; | |
| 489 | 3 | retref.swap(gal); | |
| 490 | 6 | return ret; | |
| 491 | } | ||
| 492 | |||
| 493 | 1 | void constructItem(item_t* ptr_uninitialized_item) | |
| 494 | { | ||
| 495 | 1 | size_t sz = gate_arraylist_create_item(this->impl, ptr_uninitialized_item, sizeof(item_t)); | |
| 496 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (sz == 0) |
| 497 | { | ||
| 498 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 499 | } | ||
| 500 | 1 | } | |
| 501 | |||
| 502 | 1 | void constructItem(size_t index, item_t* ptr_uninitialized_item) | |
| 503 | { | ||
| 504 | 1 | size_t sz = gate_arraylist_get_value(this->impl, index, ptr_uninitialized_item, sizeof(item_t)); | |
| 505 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (sz == 0) |
| 506 | { | ||
| 507 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 508 | } | ||
| 509 | 1 | } | |
| 510 | |||
| 511 | 8 | void swap(self_t& that) noexcept | |
| 512 | { | ||
| 513 | 8 | GenericArrayList& ref = *this; | |
| 514 | 8 | ref.swap(that); | |
| 515 | 8 | } | |
| 516 | |||
| 517 | 1 | self_t& operator=(self_t const& src) noexcept | |
| 518 | { | ||
| 519 | 1 | GenericArrayList::operator=(src); | |
| 520 | 1 | return *this; | |
| 521 | } | ||
| 522 | |||
| 523 | 6 | iterator begin() noexcept | |
| 524 | { | ||
| 525 | 6 | size_t const len = this->length(); | |
| 526 | 6 | void* ptr = NULL; | |
| 527 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | if (len != 0) |
| 528 | { | ||
| 529 | 5 | ptr = gate_arraylist_get(this->impl, 0); | |
| 530 | } | ||
| 531 | 6 | return static_cast<iterator>(ptr); | |
| 532 | } | ||
| 533 | |||
| 534 | 7 | iterator end() noexcept | |
| 535 | { | ||
| 536 | 7 | size_t const len = this->length(); | |
| 537 | 7 | void* ptr = NULL; | |
| 538 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
|
7 | if (len != 0) |
| 539 | { | ||
| 540 | 4 | ptr = gate_arraylist_get(this->impl, 0); | |
| 541 | 4 | ptr = ((char*)ptr + gate_arraylist_itemsize(this->impl) * len); | |
| 542 | } | ||
| 543 | 7 | return static_cast<iterator>(ptr); | |
| 544 | } | ||
| 545 | |||
| 546 | 4 | const_iterator begin() const noexcept | |
| 547 | { | ||
| 548 | 4 | size_t const len = this->length(); | |
| 549 | 4 | void const* ptr = NULL; | |
| 550 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | if (len != 0) |
| 551 | { | ||
| 552 | 3 | ptr = gate_arraylist_get(this->impl, 0); | |
| 553 | } | ||
| 554 | 4 | return static_cast<const_iterator>(ptr); | |
| 555 | } | ||
| 556 | |||
| 557 | 6 | const_iterator end() const noexcept | |
| 558 | { | ||
| 559 | 6 | size_t const len = this->length(); | |
| 560 | 6 | char const* ptr = NULL; | |
| 561 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (len != 0) |
| 562 | { | ||
| 563 | 3 | ptr = (char const*)gate_arraylist_get(this->impl, 0); | |
| 564 | 3 | ptr += gate_arraylist_itemsize(this->impl) * len; | |
| 565 | } | ||
| 566 | 6 | return reinterpret_cast<const_iterator>(ptr); | |
| 567 | } | ||
| 568 | |||
| 569 | 1 | const_iterator cbegin() const noexcept | |
| 570 | { | ||
| 571 | 1 | return this->begin(); | |
| 572 | } | ||
| 573 | |||
| 574 | 1 | const_iterator cend() const noexcept | |
| 575 | { | ||
| 576 | 1 | return this->end(); | |
| 577 | } | ||
| 578 | |||
| 579 | 4 | reverse_iterator rbegin() noexcept | |
| 580 | { | ||
| 581 | 4 | size_t const len = this->length(); | |
| 582 | 4 | char* ptr = NULL; | |
| 583 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 | if (len != 0) |
| 584 | { | ||
| 585 | 4 | ptr = (char*)gate_arraylist_get(this->impl, len); | |
| 586 | 4 | ptr -= gate_arraylist_itemsize(this->impl); | |
| 587 | } | ||
| 588 | 4 | return reverse_iterator(static_cast<item_t*>(static_cast<void*>(ptr))); | |
| 589 | } | ||
| 590 | |||
| 591 | 4 | reverse_iterator rend() noexcept | |
| 592 | { | ||
| 593 | 4 | size_t const len = this->length(); | |
| 594 | 4 | char* ptr = NULL; | |
| 595 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
4 | if (len != 0) |
| 596 | { | ||
| 597 | 4 | ptr = (char*)gate_arraylist_get(this->impl, 0); | |
| 598 | 4 | ptr -= gate_arraylist_itemsize(this->impl); | |
| 599 | } | ||
| 600 | 4 | return reverse_iterator(static_cast<item_t*>(static_cast<void*>(ptr))); | |
| 601 | ; | ||
| 602 | } | ||
| 603 | |||
| 604 | 4 | const_reverse_iterator rbegin() const noexcept | |
| 605 | { | ||
| 606 | 4 | size_t const len = this->length(); | |
| 607 | 4 | char const* ptr = NULL; | |
| 608 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
4 | if (len != 0) |
| 609 | { | ||
| 610 | 2 | ptr = (char const*)gate_arraylist_get(this->impl, len); | |
| 611 | 2 | ptr -= gate_arraylist_itemsize(this->impl); | |
| 612 | } | ||
| 613 | 4 | return const_reverse_iterator(static_cast<item_t const*>(static_cast<void const*>(ptr))); | |
| 614 | } | ||
| 615 | |||
| 616 | 4 | const_reverse_iterator rend() const noexcept | |
| 617 | { | ||
| 618 | 4 | size_t const len = this->length(); | |
| 619 | 4 | char const* ptr = NULL; | |
| 620 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
4 | if (len != 0) |
| 621 | { | ||
| 622 | 2 | ptr = (char const*)gate_arraylist_get(this->impl, 0); | |
| 623 | 2 | ptr -= gate_arraylist_itemsize(this->impl); | |
| 624 | } | ||
| 625 | 4 | return const_reverse_iterator(static_cast<item_t const*>(static_cast<void const*>(ptr))); | |
| 626 | ; | ||
| 627 | } | ||
| 628 | |||
| 629 | 4 | const_reverse_iterator crbegin() const noexcept | |
| 630 | { | ||
| 631 | 4 | return this->rbegin(); | |
| 632 | } | ||
| 633 | |||
| 634 | 4 | const_reverse_iterator crend() const noexcept | |
| 635 | { | ||
| 636 | 4 | return this->rend(); | |
| 637 | } | ||
| 638 | |||
| 639 | 2796 | iterator add(item_t const& item) | |
| 640 | { | ||
| 641 | 2796 | iterator ret = static_cast<iterator>(GenericArrayList::add(&item)); | |
| 642 | 2796 | check_outofmem_nullptr(ret); | |
| 643 | 2796 | return ret; | |
| 644 | } | ||
| 645 | |||
| 646 | 7 | VoidResult tryAdd(item_t const& item) noexcept | |
| 647 | { | ||
| 648 | 7 | void* rawptr = GenericArrayList::add(&item); | |
| 649 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (NULL == rawptr) |
| 650 | { | ||
| 651 | ✗ | return makeErrResult(results::OutOfMemory); | |
| 652 | } | ||
| 653 | else | ||
| 654 | { | ||
| 655 | 7 | return makeOk(); | |
| 656 | } | ||
| 657 | } | ||
| 658 | |||
| 659 | 3 | iterator add(item_t const& item, size_t repeatCount) | |
| 660 | { | ||
| 661 | 3 | iterator ret = static_cast<iterator>(GenericArrayList::add(&item, repeatCount)); | |
| 662 | 3 | check_outofmem_nullptr(ret); | |
| 663 | 3 | return ret; | |
| 664 | } | ||
| 665 | |||
| 666 | template <class ITER> | ||
| 667 | 16 | void addItems(ITER begin, ITER end) | |
| 668 | { | ||
| 669 |
3/3✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
|
16 | while (begin != end) |
| 670 | { | ||
| 671 |
2/3✓ Branch 1 taken 3 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
13 | this->add(item_t(*begin)); |
| 672 | 13 | ++begin; | |
| 673 | } | ||
| 674 | 3 | } | |
| 675 | |||
| 676 | 48 | iterator insertAt(size_t index, item_t const& item) | |
| 677 | { | ||
| 678 | 48 | iterator ret = static_cast<iterator>(GenericArrayList::insertAt(index, &item)); | |
| 679 | 48 | check_outofmem_nullptr(ret); | |
| 680 | 48 | return ret; | |
| 681 | } | ||
| 682 | |||
| 683 | 1 | VoidResult remove(iterator iter) noexcept | |
| 684 | { | ||
| 685 | 1 | iterator const first = this->begin(); | |
| 686 | 1 | size_t const index = static_cast<size_t>(iter - first); | |
| 687 | 1 | return this->removeAt(index); | |
| 688 | } | ||
| 689 | |||
| 690 | 9 | item_t const* getItemPtr(size_t index) const noexcept | |
| 691 | { | ||
| 692 | 9 | void const* ptr = GenericArrayList::getItemPtr(index); | |
| 693 | 9 | return static_cast<item_t const*>(ptr); | |
| 694 | } | ||
| 695 | |||
| 696 | 6 | iterator get(size_t index) noexcept | |
| 697 | { | ||
| 698 | 6 | void* const ptr = GenericArrayList::getItemPtr(index); | |
| 699 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
|
6 | if (ptr == NULL) |
| 700 | { | ||
| 701 | 2 | return this->end(); | |
| 702 | } | ||
| 703 | 4 | return static_cast<iterator>(ptr); | |
| 704 | } | ||
| 705 | |||
| 706 | 4 | const_iterator get(size_t index) const noexcept | |
| 707 | { | ||
| 708 | 4 | void const* const ptr = GenericArrayList::getItemPtr(index); | |
| 709 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (ptr == NULL) |
| 710 | { | ||
| 711 | 1 | return this->end(); | |
| 712 | } | ||
| 713 | 3 | return static_cast<const_iterator>(ptr); | |
| 714 | } | ||
| 715 | |||
| 716 | 1063 | item_t& at(size_t index) noexcept | |
| 717 | { | ||
| 718 | 1063 | void* ptr = GenericArrayList::getItemPtr(index); | |
| 719 | #ifdef GATE_DEBUG_MODE | ||
| 720 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1059 times.
|
1063 | if (ptr == NULL) |
| 721 | { | ||
| 722 | ✗ | panic(results::OutOfBounds, "ArrayList::at()"); | |
| 723 | } | ||
| 724 | #endif | ||
| 725 | 1063 | return *static_cast<item_t*>(ptr); | |
| 726 | } | ||
| 727 | |||
| 728 | 2 | item_t const& at(size_t index) const noexcept | |
| 729 | { | ||
| 730 | 2 | void const* ptr = GenericArrayList::getItemPtr(index); | |
| 731 | #ifdef GATE_DEBUG_MODE | ||
| 732 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ptr == NULL) |
| 733 | { | ||
| 734 | ✗ | panic(results::OutOfBounds, "ArrayList::at()"); | |
| 735 | } | ||
| 736 | #endif | ||
| 737 | 2 | return *static_cast<item_t const*>(ptr); | |
| 738 | } | ||
| 739 | |||
| 740 | 1051 | item_t& operator[](size_t index) noexcept | |
| 741 | { | ||
| 742 | 1051 | return this->at(index); | |
| 743 | } | ||
| 744 | 1 | item_t const& operator[](size_t index) const noexcept | |
| 745 | { | ||
| 746 | 1 | return this->at(index); | |
| 747 | } | ||
| 748 | |||
| 749 | 1039 | self_t& push(item_t const& item) | |
| 750 | { | ||
| 751 | 1039 | this->add(item); | |
| 752 | 1039 | return *this; | |
| 753 | } | ||
| 754 | |||
| 755 | 4 | self_t& pop(item_t& item) | |
| 756 | { | ||
| 757 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if (!this->contains(0)) |
| 758 | { | ||
| 759 | 1 | GATEXX_RAISE_ERROR(results::NotAvailable); | |
| 760 | } | ||
| 761 | 3 | item = this->at(0); | |
| 762 | 3 | this->removeAt(0); | |
| 763 | 3 | return *this; | |
| 764 | } | ||
| 765 | |||
| 766 | 1039 | self_t& operator<<(item_t const& item) | |
| 767 | { | ||
| 768 | 1039 | return this->push(item); | |
| 769 | } | ||
| 770 | |||
| 771 | 3 | self_t& operator>>(item_t& item) | |
| 772 | { | ||
| 773 | 3 | return this->pop(item); | |
| 774 | } | ||
| 775 | |||
| 776 | typedef Enumerator<item_t const> enumerator_t; | ||
| 777 | |||
| 778 | 2 | enumerator_t enumerate() const | |
| 779 | { | ||
| 780 | gate_enumerator_t enumerator; | ||
| 781 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | check_outofmem_nullptr(gate_arraylist_enumerate(this->impl, &enumerator)); |
| 782 | 4 | return enumerator_t(enumerator); | |
| 783 | } | ||
| 784 | |||
| 785 | 177 | Array<item_t> toArray() const | |
| 786 | { | ||
| 787 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
354 | GenericArray arr = GenericArrayList::toArray(); |
| 788 | 177 | gate_array_t* ptrArr = arr.c_impl(); | |
| 789 |
2/2✓ Branch 1 taken 70 times.
✓ Branch 2 taken 2 times.
|
177 | Array<item_t> ret = Array<item_t>::createFrom(*ptrArr); |
| 790 | 346 | return ret; | |
| 791 | } | ||
| 792 | |||
| 793 | template <class FROM> | ||
| 794 | 11 | void importFrom(gate_arraylist_t arr) | |
| 795 | { | ||
| 796 | FROM const* ptr; | ||
| 797 | 11 | size_t length = gate_arraylist_length(arr); | |
| 798 |
2/2✓ Branch 0 taken 49 times.
✓ Branch 1 taken 11 times.
|
60 | for (size_t index = 0; index != length; ++index) |
| 799 | { | ||
| 800 | 49 | ptr = (FROM const*)gate_arraylist_get(arr, index); | |
| 801 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (ptr) |
| 802 | { | ||
| 803 |
2/3✓ Branch 1 taken 48 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
49 | this->add(item_t(*ptr)); |
| 804 | } | ||
| 805 | } | ||
| 806 | 11 | } | |
| 807 | |||
| 808 | template <class TO> | ||
| 809 | 1 | size_t exportTo(gate_arraylist_t arr) const | |
| 810 | { | ||
| 811 | 1 | size_t countedExports = 0; | |
| 812 | |||
| 813 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (sizeof(TO) != gate_arraylist_itemsize(arr)) |
| 814 | { | ||
| 815 | ✗ | GATEXX_RAISE_ERROR(results::IncorrectType); | |
| 816 | } | ||
| 817 | else | ||
| 818 | { | ||
| 819 | 1 | size_t len = this->length(); | |
| 820 | |||
| 821 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (size_t index = 0; index != len; ++index) |
| 822 | { | ||
| 823 | 3 | item_t const* ptr = this->get(index); | |
| 824 | 3 | TO const* ptr_native = (TO const*)ptr; | |
| 825 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!ptr_native) |
| 826 | { | ||
| 827 | ✗ | continue; | |
| 828 | } | ||
| 829 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (NULL != gate_arraylist_add(arr, ptr)) |
| 830 | { | ||
| 831 | 3 | ++countedExports; | |
| 832 | } | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | 1 | return countedExports; | |
| 837 | } | ||
| 838 | |||
| 839 | template <class COMPARER> | ||
| 840 | 6 | void sort(COMPARER comparer, bool descending = false) | |
| 841 | { | ||
| 842 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
6 | if (this->length() < 2) |
| 843 | { | ||
| 844 | ✗ | return; | |
| 845 | } | ||
| 846 | 6 | item_t* endPtr = this->end(); | |
| 847 | 6 | item_t* lastPtr = endPtr - 1; | |
| 848 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 3 times.
|
26 | for (item_t* current = this->begin(); current != lastPtr; ++current) |
| 849 | { | ||
| 850 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 10 times.
|
66 | for (item_t* next = current + 1; next != endPtr; ++next) |
| 851 | { | ||
| 852 | 46 | int result = comparer(*next, *current); | |
| 853 |
7/8✓ Branch 0 taken 13 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 5 times.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
|
46 | if ((!descending && (result < 0)) || (descending && (result > 0))) |
| 854 | { | ||
| 855 | 36 | gate::swapRefs(*next, *current); | |
| 856 | } | ||
| 857 | } | ||
| 858 | } | ||
| 859 | } | ||
| 860 | |||
| 861 | 1 | void sort() | |
| 862 | { | ||
| 863 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | this->sort(DefaultComparer<item_t>()); |
| 864 | 1 | } | |
| 865 | }; | ||
| 866 | |||
| 867 | |||
| 868 | |||
| 869 | /// @brief | ||
| 870 | /// @tparam T | ||
| 871 | template <class T> | ||
| 872 | class SlotList | ||
| 873 | { | ||
| 874 | public: | ||
| 875 | typedef T item_t; | ||
| 876 | typedef SlotList<T> self_t; | ||
| 877 | |||
| 878 | private: | ||
| 879 | gate_slotlist_t impl; | ||
| 880 | |||
| 881 | 138 | static void check_outofmem_nullptr(void const* ptr) | |
| 882 | { | ||
| 883 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
|
138 | if (NULL == ptr) |
| 884 | { | ||
| 885 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 886 | } | ||
| 887 | 138 | } | |
| 888 | |||
| 889 | public: | ||
| 890 | class const_iterator | ||
| 891 | { | ||
| 892 | friend class SlotList<T>; | ||
| 893 | |||
| 894 | protected: | ||
| 895 | gate_slotlist_t const* sl; | ||
| 896 | gate_slotlist_iterator_t iter; | ||
| 897 | item_t* item; | ||
| 898 | |||
| 899 | public: | ||
| 900 | 130 | const_iterator(gate_slotlist_t const* s, gate_slotlist_iterator_t it, item_t* ptr) noexcept | |
| 901 | 130 | : sl(s), iter(it), item(ptr) | |
| 902 | { | ||
| 903 | 130 | } | |
| 904 | |||
| 905 | 130 | ~const_iterator() noexcept | |
| 906 | { | ||
| 907 | 130 | } | |
| 908 | |||
| 909 | 32 | item_t const* get() const noexcept | |
| 910 | { | ||
| 911 | 32 | return this->item; | |
| 912 | } | ||
| 913 | |||
| 914 | 31 | item_t const& operator*() const | |
| 915 | { | ||
| 916 | 31 | return *this->get(); | |
| 917 | } | ||
| 918 | 1 | item_t const* operator->() const noexcept | |
| 919 | { | ||
| 920 | 1 | return this->get(); | |
| 921 | } | ||
| 922 | |||
| 923 | 32 | const_iterator& operator++() noexcept | |
| 924 | { | ||
| 925 | 32 | this->item = static_cast<item_t*>(gate_slotlist_next(this->sl, &this->iter)); | |
| 926 | 32 | return *this; | |
| 927 | } | ||
| 928 | const_iterator operator++(int) noexcept | ||
| 929 | { | ||
| 930 | const_iterator that(*this); | ||
| 931 | this->item = static_cast<item_t*>(gate_slotlist_next(this->sl, &this->iter)); | ||
| 932 | return that; | ||
| 933 | } | ||
| 934 | |||
| 935 | bool_t operator==(const_iterator const& that) const noexcept | ||
| 936 | { | ||
| 937 | return (this->sl == that.sl) && (this->item == that.item); | ||
| 938 | } | ||
| 939 | |||
| 940 | 94 | bool_t operator!=(const_iterator const& that) const noexcept | |
| 941 | { | ||
| 942 |
3/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
94 | return (this->item != that.item) || (this->sl != that.sl); |
| 943 | } | ||
| 944 | }; | ||
| 945 | |||
| 946 | class iterator : public const_iterator | ||
| 947 | { | ||
| 948 | friend class SlotList<T>; | ||
| 949 | |||
| 950 | public: | ||
| 951 | 39 | iterator(gate_slotlist_t const* s, gate_slotlist_iterator_t it, item_t* ptr) noexcept | |
| 952 | 39 | : const_iterator(s, it, ptr) | |
| 953 | { | ||
| 954 | 39 | } | |
| 955 | |||
| 956 | 18 | item_t* get() const noexcept | |
| 957 | { | ||
| 958 | 18 | return this->item; | |
| 959 | } | ||
| 960 | |||
| 961 | 18 | item_t& operator*() const | |
| 962 | { | ||
| 963 | 18 | return *this->get(); | |
| 964 | } | ||
| 965 | item_t* operator->() const noexcept | ||
| 966 | { | ||
| 967 | return this->get(); | ||
| 968 | } | ||
| 969 | }; | ||
| 970 | |||
| 971 | friend class const_iterator; | ||
| 972 | friend class iterator; | ||
| 973 | |||
| 974 | 85 | SlotList() noexcept | |
| 975 | { | ||
| 976 | 85 | check_outofmem_nullptr(gate_slotlist_create( | |
| 977 | &this->impl, sizeof(item_t), | ||
| 978 | &TypeFunctions<item_t>::copyConstruct, | ||
| 979 | &TypeFunctions<item_t>::destruct)); | ||
| 980 | 85 | } | |
| 981 | 3 | SlotList(self_t const& src) | |
| 982 | { | ||
| 983 | 3 | check_outofmem_nullptr(gate_slotlist_create_copy(&this->impl, &src.impl)); | |
| 984 | 3 | } | |
| 985 | 88 | ~SlotList() noexcept | |
| 986 | { | ||
| 987 | 88 | gate_slotlist_destroy(&this->impl); | |
| 988 | 88 | } | |
| 989 | 2 | void swap(self_t& that) noexcept | |
| 990 | { | ||
| 991 | 2 | gate::swapRefsNoExcept(this->impl, that.impl); | |
| 992 | 2 | } | |
| 993 | 1 | self_t& operator=(self_t const& src) | |
| 994 | { | ||
| 995 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 996 | { | ||
| 997 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | self_t that(src); |
| 998 | 1 | this->swap(that); | |
| 999 | } | ||
| 1000 | 1 | return *this; | |
| 1001 | } | ||
| 1002 | |||
| 1003 | 1 | gate_slotlist_t const* c_impl() const noexcept | |
| 1004 | { | ||
| 1005 | 1 | return &this->impl; | |
| 1006 | } | ||
| 1007 | |||
| 1008 | 2 | void optimize() noexcept | |
| 1009 | { | ||
| 1010 | 2 | gate_slotlist_optimize(&this->impl); | |
| 1011 | 2 | } | |
| 1012 | |||
| 1013 | 30 | item_t& add(item_t const& src) | |
| 1014 | { | ||
| 1015 | 30 | void* ptr = gate_slotlist_add(&this->impl, &src); | |
| 1016 | 30 | check_outofmem_nullptr(ptr); | |
| 1017 | 30 | return *static_cast<item_t*>(ptr); | |
| 1018 | } | ||
| 1019 | |||
| 1020 | 1 | item_t& insertAt(size_t index, item_t const& src) | |
| 1021 | { | ||
| 1022 | 1 | void* ptr = gate_slotlist_insert_at(&this->impl, index, &src); | |
| 1023 | 1 | check_outofmem_nullptr(ptr); | |
| 1024 | 1 | return *static_cast<item_t*>(ptr); | |
| 1025 | } | ||
| 1026 | |||
| 1027 | 1 | item_t* getItem(iterator const& iter) noexcept | |
| 1028 | { | ||
| 1029 | 1 | return static_cast<item_t*>(gate_slotlist_get(&this->impl, &iter.iter)); | |
| 1030 | } | ||
| 1031 | |||
| 1032 | 1 | item_t const* getItem(const_iterator const& iter) noexcept | |
| 1033 | { | ||
| 1034 | 1 | return static_cast<item_t const*>(gate_slotlist_get(&this->impl, &iter.iter)); | |
| 1035 | } | ||
| 1036 | |||
| 1037 | 8 | item_t* getPtr(size_t index) noexcept | |
| 1038 | { | ||
| 1039 | 8 | item_t* ptr = static_cast<item_t*>(gate_slotlist_at(&this->impl, index)); | |
| 1040 | 8 | return ptr; | |
| 1041 | } | ||
| 1042 | 7 | item_t const* getPtr(size_t index) const noexcept | |
| 1043 | { | ||
| 1044 | 7 | item_t const* ptr = static_cast<item_t const*>(gate_slotlist_at(&this->impl, index)); | |
| 1045 | 7 | return ptr; | |
| 1046 | } | ||
| 1047 | |||
| 1048 | 5 | bool_t contains(size_t index) const noexcept | |
| 1049 | { | ||
| 1050 | 5 | item_t const* ptr = this->getPtr(index); | |
| 1051 | 5 | return ptr != NULL; | |
| 1052 | } | ||
| 1053 | |||
| 1054 | 5 | item_t& get(size_t index) noexcept | |
| 1055 | { | ||
| 1056 | 5 | item_t* const ptr = this->getPtr(index); | |
| 1057 | #ifdef GATE_DEBUG_MODE | ||
| 1058 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (ptr == NULL) |
| 1059 | { | ||
| 1060 | ✗ | panic(results::OutOfBounds, "SlotList::get()"); | |
| 1061 | } | ||
| 1062 | #endif | ||
| 1063 | 5 | return *ptr; | |
| 1064 | } | ||
| 1065 | 1 | item_t const& get(size_t index) const | |
| 1066 | { | ||
| 1067 | 1 | item_t const* const ptr = this->getPtr(index); | |
| 1068 | #ifdef GATE_DEBUG_MODE | ||
| 1069 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ptr == NULL) |
| 1070 | { | ||
| 1071 | ✗ | panic(results::OutOfBounds, "SlotList::get()"); | |
| 1072 | } | ||
| 1073 | #endif | ||
| 1074 | 1 | return *ptr; | |
| 1075 | } | ||
| 1076 | |||
| 1077 | 14 | void clear() noexcept | |
| 1078 | { | ||
| 1079 | 14 | gate_slotlist_clear(&this->impl); | |
| 1080 | 14 | } | |
| 1081 | |||
| 1082 | 6 | size_t length() const noexcept | |
| 1083 | { | ||
| 1084 | 6 | return gate_slotlist_length(&this->impl); | |
| 1085 | } | ||
| 1086 | 1 | size_t capacity() const noexcept | |
| 1087 | { | ||
| 1088 | 1 | return gate_slotlist_capacity(&this->impl); | |
| 1089 | } | ||
| 1090 | 61 | const_iterator cend() const noexcept | |
| 1091 | { | ||
| 1092 | 61 | return const_iterator(&this->impl, gate_slotlist_capacity(&this->impl), NULL); | |
| 1093 | } | ||
| 1094 | 43 | const_iterator cbegin() const noexcept | |
| 1095 | { | ||
| 1096 | gate_slotlist_iterator_t it; | ||
| 1097 | 43 | void* const ptr = gate_slotlist_first(&this->impl, &it); | |
| 1098 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 20 times.
|
43 | if (ptr == NULL) |
| 1099 | { | ||
| 1100 | 14 | return this->cend(); | |
| 1101 | } | ||
| 1102 | 29 | return const_iterator(&this->impl, it, static_cast<item_t*>(ptr)); | |
| 1103 | } | ||
| 1104 | 19 | iterator end() noexcept | |
| 1105 | { | ||
| 1106 | 19 | return iterator(&this->impl, gate_slotlist_capacity(&this->impl), NULL); | |
| 1107 | } | ||
| 1108 | 20 | iterator begin() noexcept | |
| 1109 | { | ||
| 1110 | gate_slotlist_iterator_t it; | ||
| 1111 | 20 | void* const ptr = gate_slotlist_first(&this->impl, &it); | |
| 1112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
20 | if (ptr == NULL) |
| 1113 | { | ||
| 1114 | ✗ | return this->end(); | |
| 1115 | } | ||
| 1116 | 20 | return iterator(&this->impl, it, static_cast<item_t*>(ptr)); | |
| 1117 | } | ||
| 1118 | |||
| 1119 | 42 | const_iterator begin() const | |
| 1120 | { | ||
| 1121 | 42 | return this->cbegin(); | |
| 1122 | } | ||
| 1123 | 34 | const_iterator end() const | |
| 1124 | { | ||
| 1125 | 34 | return this->cend(); | |
| 1126 | } | ||
| 1127 | |||
| 1128 | 1 | const_iterator getIterator(size_t ndx) const | |
| 1129 | { | ||
| 1130 | 1 | return const_iterator(&this->impl, ndx, static_cast<item_t*>(gate_slotlist_at(&this->impl, ndx))); | |
| 1131 | } | ||
| 1132 | |||
| 1133 | 11 | bool_t remove(const_iterator const& it) | |
| 1134 | { | ||
| 1135 | 11 | return gate_slotlist_remove(&this->impl, &it.iter); | |
| 1136 | } | ||
| 1137 | |||
| 1138 | 1 | bool_t removeAt(size_t index) | |
| 1139 | { | ||
| 1140 | 1 | return gate_slotlist_remove_at(&this->impl, index); | |
| 1141 | } | ||
| 1142 | |||
| 1143 | 1 | Enumerator<item_t const> enumerate() const noexcept | |
| 1144 | { | ||
| 1145 | gate_enumerator_t e; | ||
| 1146 | 1 | gate_enumerator_t* ptr = gate_slotlist_enumerate(&this->impl, &e); | |
| 1147 | #ifdef GATE_DEBUG_MODE | ||
| 1148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ptr) |
| 1149 | { | ||
| 1150 | ✗ | panic(results::NullPointer, "SlotList::enumerator"); | |
| 1151 | } | ||
| 1152 | #endif | ||
| 1153 | (void)ptr; | ||
| 1154 | 1 | return Enumerator<item_t const>(e); | |
| 1155 | } | ||
| 1156 | }; | ||
| 1157 | |||
| 1158 | |||
| 1159 | |||
| 1160 | /// @brief | ||
| 1161 | /// @tparam T | ||
| 1162 | template <class T> | ||
| 1163 | class LinkedList | ||
| 1164 | { | ||
| 1165 | public: | ||
| 1166 | typedef T item_t; | ||
| 1167 | typedef LinkedList<T> self_t; | ||
| 1168 | |||
| 1169 | class iterator | ||
| 1170 | { | ||
| 1171 | protected: | ||
| 1172 | gate_linkedentry_t* ptr_entry; | ||
| 1173 | |||
| 1174 | public: | ||
| 1175 | 24 | iterator(gate_linkedentry_t* entry) noexcept : ptr_entry(entry) {} | |
| 1176 | 8 | iterator(iterator const& src) noexcept : ptr_entry(src.ptr_entry) {} | |
| 1177 | 1 | iterator& operator=(iterator const& src) noexcept | |
| 1178 | { | ||
| 1179 | 1 | this->ptr_entry = src.ptr_entry; | |
| 1180 | 1 | return *this; | |
| 1181 | } | ||
| 1182 | 32 | ~iterator() noexcept {} | |
| 1183 | |||
| 1184 | 5 | gate_linkedentry_t* c_impl() const noexcept { return this->ptr_entry; } | |
| 1185 | |||
| 1186 | 35 | item_t* getPtr() const noexcept | |
| 1187 | { | ||
| 1188 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 13 times.
|
35 | return this->ptr_entry ? static_cast<item_t*>(this->ptr_entry->data) : NULL; |
| 1189 | } | ||
| 1190 | 9 | item_t& get() const noexcept | |
| 1191 | { | ||
| 1192 | 9 | item_t* const ptr = this->getPtr(); | |
| 1193 | #ifdef GATE_DEBUG_MODE | ||
| 1194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!ptr) |
| 1195 | { | ||
| 1196 | ✗ | panic(results::NullPointer, "LinkedList::iterator::get"); | |
| 1197 | } | ||
| 1198 | #endif | ||
| 1199 | 9 | return *ptr; | |
| 1200 | } | ||
| 1201 | |||
| 1202 | 9 | item_t& operator*() const noexcept { return this->get(); } | |
| 1203 | item_t* operator->() const noexcept { return this->getPtr(); } | ||
| 1204 | |||
| 1205 | 7 | bool operator==(iterator const& src) const { return this->getPtr() == src.getPtr(); } | |
| 1206 | 6 | bool operator!=(iterator const& src) const { return this->getPtr() != src.getPtr(); } | |
| 1207 | 3 | iterator& operator++() | |
| 1208 | { | ||
| 1209 | 3 | this->ptr_entry = gate_linkedlist_next(this->ptr_entry); | |
| 1210 | 3 | return *this; | |
| 1211 | } | ||
| 1212 | 1 | iterator operator++(int) | |
| 1213 | { | ||
| 1214 | 1 | iterator tmp(*this); | |
| 1215 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ++(*this); |
| 1216 | 1 | return tmp; | |
| 1217 | } | ||
| 1218 | 2 | iterator& operator--() | |
| 1219 | { | ||
| 1220 | 2 | this->ptr_entry = gate_linkedlist_previous(this->ptr_entry); | |
| 1221 | 2 | return *this; | |
| 1222 | } | ||
| 1223 | 1 | iterator operator--(int) | |
| 1224 | { | ||
| 1225 | 1 | iterator tmp(*this); | |
| 1226 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | --(*this); |
| 1227 | 1 | return tmp; | |
| 1228 | } | ||
| 1229 | }; | ||
| 1230 | |||
| 1231 | class const_iterator | ||
| 1232 | { | ||
| 1233 | protected: | ||
| 1234 | gate_linkedentry_t const* ptr_entry; | ||
| 1235 | |||
| 1236 | public: | ||
| 1237 | 14 | const_iterator(gate_linkedentry_t const* entry) noexcept : ptr_entry(entry) {} | |
| 1238 | 4 | const_iterator(const_iterator const& src) noexcept : ptr_entry(src.ptr_entry) {} | |
| 1239 | 1 | const_iterator& operator=(const_iterator const& src) noexcept | |
| 1240 | { | ||
| 1241 | 1 | this->ptr_entry = src.ptr_entry; | |
| 1242 | 1 | return *this; | |
| 1243 | } | ||
| 1244 | 18 | ~const_iterator() noexcept {} | |
| 1245 | |||
| 1246 | gate_linkedentry_t const* c_impl() const noexcept { return this->ptr_entry; } | ||
| 1247 | |||
| 1248 | 31 | item_t const* getPtr() const noexcept | |
| 1249 | { | ||
| 1250 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 12 times.
|
31 | return this->ptr_entry ? static_cast<item_t const*>(this->ptr_entry->data) : NULL; |
| 1251 | } | ||
| 1252 | 9 | item_t const& get() const noexcept | |
| 1253 | { | ||
| 1254 | 9 | item_t const* const ptr = this->getPtr(); | |
| 1255 | #ifdef GATE_DEBUG_MODE | ||
| 1256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!ptr) |
| 1257 | { | ||
| 1258 | ✗ | panic(results::NullPointer, "LinkedList::const_iterator::get"); | |
| 1259 | } | ||
| 1260 | #endif | ||
| 1261 | 9 | return *ptr; | |
| 1262 | } | ||
| 1263 | |||
| 1264 | item_t const* operator->() const noexcept { return this->getPtr(); } | ||
| 1265 | 9 | item_t const& operator*() const noexcept { return this->get(); } | |
| 1266 | |||
| 1267 | 5 | bool operator==(const_iterator const& src) const { return this->getPtr() == src.getPtr(); } | |
| 1268 | 6 | bool operator!=(const_iterator const& src) const { return this->getPtr() != src.getPtr(); } | |
| 1269 | 6 | const_iterator& operator++() | |
| 1270 | { | ||
| 1271 | 6 | this->ptr_entry = gate_linkedlist_next(this->ptr_entry); | |
| 1272 | 6 | return *this; | |
| 1273 | } | ||
| 1274 | 1 | const_iterator operator++(int) | |
| 1275 | { | ||
| 1276 | 1 | const_iterator tmp(*this); | |
| 1277 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ++(*this); |
| 1278 | 1 | return tmp; | |
| 1279 | } | ||
| 1280 | 2 | const_iterator& operator--() | |
| 1281 | { | ||
| 1282 | 2 | this->ptr_entry = gate_linkedlist_previous(this->ptr_entry); | |
| 1283 | 2 | return *this; | |
| 1284 | } | ||
| 1285 | 1 | const_iterator operator--(int) | |
| 1286 | { | ||
| 1287 | 1 | const_iterator tmp(*this); | |
| 1288 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | --(*this); |
| 1289 | 1 | return tmp; | |
| 1290 | } | ||
| 1291 | }; | ||
| 1292 | |||
| 1293 | class reverse_iterator : public iterator | ||
| 1294 | { | ||
| 1295 | public: | ||
| 1296 | 13 | reverse_iterator(gate_linkedentry_t* entry) noexcept : iterator(entry) {} | |
| 1297 | 2 | reverse_iterator(reverse_iterator const& src) noexcept : iterator(src) {} | |
| 1298 | reverse_iterator& operator=(reverse_iterator const& src) noexcept | ||
| 1299 | { | ||
| 1300 | this->ptr_entry = src.ptr_entry; | ||
| 1301 | return *this; | ||
| 1302 | } | ||
| 1303 | |||
| 1304 | 2 | reverse_iterator& operator--() | |
| 1305 | { | ||
| 1306 | 2 | this->ptr_entry = gate_linkedlist_next(this->ptr_entry); | |
| 1307 | 2 | return *this; | |
| 1308 | } | ||
| 1309 | 1 | reverse_iterator operator--(int) | |
| 1310 | { | ||
| 1311 | 1 | reverse_iterator tmp(*this); | |
| 1312 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | --(*this); |
| 1313 | 1 | return tmp; | |
| 1314 | } | ||
| 1315 | 3 | reverse_iterator& operator++() | |
| 1316 | { | ||
| 1317 | 3 | this->ptr_entry = gate_linkedlist_previous(this->ptr_entry); | |
| 1318 | 3 | return *this; | |
| 1319 | } | ||
| 1320 | 1 | reverse_iterator operator++(int) | |
| 1321 | { | ||
| 1322 | 1 | reverse_iterator tmp(*this); | |
| 1323 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ++(*this); |
| 1324 | 1 | return tmp; | |
| 1325 | } | ||
| 1326 | }; | ||
| 1327 | |||
| 1328 | class const_reverse_iterator : public const_iterator | ||
| 1329 | { | ||
| 1330 | public: | ||
| 1331 | 6 | const_reverse_iterator(gate_linkedentry_t const* entry) noexcept : const_iterator(entry) {} | |
| 1332 | 2 | const_reverse_iterator(const_reverse_iterator const& src) noexcept : const_iterator(src) {} | |
| 1333 | const_reverse_iterator& operator=(const_reverse_iterator const& src) noexcept | ||
| 1334 | { | ||
| 1335 | this->ptr_entry = src.ptr_entry; | ||
| 1336 | return *this; | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | 2 | const_reverse_iterator& operator--() | |
| 1340 | { | ||
| 1341 | 2 | this->ptr_entry = gate_linkedlist_next(this->ptr_entry); | |
| 1342 | 2 | return *this; | |
| 1343 | } | ||
| 1344 | 1 | const_reverse_iterator operator--(int) | |
| 1345 | { | ||
| 1346 | 1 | const_reverse_iterator tmp(*this); | |
| 1347 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | --(*this); |
| 1348 | 1 | return tmp; | |
| 1349 | } | ||
| 1350 | 3 | const_reverse_iterator& operator++() | |
| 1351 | { | ||
| 1352 | 3 | this->ptr_entry = gate_linkedlist_previous(this->ptr_entry); | |
| 1353 | 3 | return *this; | |
| 1354 | } | ||
| 1355 | 1 | const_reverse_iterator operator++(int) | |
| 1356 | { | ||
| 1357 | 1 | const_reverse_iterator tmp(*this); | |
| 1358 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ++(*this); |
| 1359 | 1 | return tmp; | |
| 1360 | } | ||
| 1361 | }; | ||
| 1362 | |||
| 1363 | private: | ||
| 1364 | gate_linkedlist_t impl; | ||
| 1365 | |||
| 1366 | public: | ||
| 1367 | 1 | LinkedList() noexcept | |
| 1368 | { | ||
| 1369 | 1 | result_t ret = gate_linkedlist_create(&this->impl, sizeof(T), &TypeFunctions<T>::copyConstruct, &TypeFunctions<T>::destruct); | |
| 1370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(ret); |
| 1371 | 1 | } | |
| 1372 | 3 | LinkedList(self_t const& src) | |
| 1373 | { | ||
| 1374 | 3 | result_t ret = gate_linkedlist_create(&this->impl, sizeof(T), &TypeFunctions<T>::copyConstruct, &TypeFunctions<T>::destruct); | |
| 1375 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATEXX_CHECK_ERROR(ret); |
| 1376 | |||
| 1377 | 3 | gate_linkedentry_t* entry = gate_linkedlist_first(&src.impl); | |
| 1378 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | while (entry != NULL) |
| 1379 | { | ||
| 1380 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (NULL == gate_linkedlist_add(&this->impl, entry->data)) |
| 1381 | { | ||
| 1382 | ✗ | gate_linkedlist_destroy(&this->impl); | |
| 1383 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 1384 | } | ||
| 1385 | 6 | entry = gate_linkedlist_next(entry); | |
| 1386 | } | ||
| 1387 | 3 | } | |
| 1388 | 1 | void swap(self_t& that) noexcept | |
| 1389 | { | ||
| 1390 | 1 | gate::swapRefsNoExcept(this->impl, that.impl); | |
| 1391 | 1 | } | |
| 1392 | 1 | self_t& operator=(self_t const& src) | |
| 1393 | { | ||
| 1394 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 1395 | { | ||
| 1396 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | self_t that(src); |
| 1397 | 1 | this->swap(that); | |
| 1398 | } | ||
| 1399 | 1 | return *this; | |
| 1400 | } | ||
| 1401 | 4 | ~LinkedList() noexcept | |
| 1402 | { | ||
| 1403 | 4 | gate_linkedlist_destroy(&this->impl); | |
| 1404 | 4 | } | |
| 1405 | |||
| 1406 | 3 | iterator add(item_t const& item) noexcept | |
| 1407 | { | ||
| 1408 | 3 | gate_linkedentry_t* ptr_entry = gate_linkedlist_add(&this->impl, &item); | |
| 1409 | 3 | return iterator(ptr_entry); | |
| 1410 | } | ||
| 1411 | |||
| 1412 | 1 | iterator insert(iterator insertAt, item_t const& item) noexcept | |
| 1413 | { | ||
| 1414 | 1 | gate_linkedentry_t* ptr_entry = gate_linkedlist_insert(&this->impl, insertAt.c_impl(), &item); | |
| 1415 | 1 | return iterator(ptr_entry); | |
| 1416 | } | ||
| 1417 | |||
| 1418 | 4 | VoidResult remove(iterator iter) noexcept | |
| 1419 | { | ||
| 1420 | 4 | result_t result = gate_linkedlist_remove(&this->impl, iter.c_impl()); | |
| 1421 | 4 | return makeResult(result); | |
| 1422 | } | ||
| 1423 | |||
| 1424 | 5 | bool_t empty() const noexcept | |
| 1425 | { | ||
| 1426 | 5 | return gate_linkedlist_empty(&this->impl); | |
| 1427 | } | ||
| 1428 | |||
| 1429 | 3 | iterator begin() noexcept | |
| 1430 | { | ||
| 1431 | 3 | gate_linkedentry_t* ptr = gate_linkedlist_first(&this->impl); | |
| 1432 | 3 | return iterator(ptr); | |
| 1433 | } | ||
| 1434 | 4 | iterator end() noexcept | |
| 1435 | { | ||
| 1436 | 4 | return iterator(NULL); | |
| 1437 | } | ||
| 1438 | 3 | const_iterator cbegin() const noexcept | |
| 1439 | { | ||
| 1440 | 3 | gate_linkedentry_t const* ptr = gate_linkedlist_first(&this->impl); | |
| 1441 | 3 | return const_iterator(ptr); | |
| 1442 | } | ||
| 1443 | 5 | const_iterator cend() const noexcept | |
| 1444 | { | ||
| 1445 | 5 | return const_iterator(NULL); | |
| 1446 | } | ||
| 1447 | 1 | const_iterator begin() const noexcept { return this->cbegin(); } | |
| 1448 | const_iterator end() const noexcept { return this->cend(); } | ||
| 1449 | |||
| 1450 | 6 | reverse_iterator rbegin() noexcept | |
| 1451 | { | ||
| 1452 | 6 | gate_linkedentry_t* const ptr = gate_linkedlist_last(&this->impl); | |
| 1453 | 6 | return reverse_iterator(ptr); | |
| 1454 | } | ||
| 1455 | 7 | reverse_iterator rend() noexcept | |
| 1456 | { | ||
| 1457 | 7 | return reverse_iterator(NULL); | |
| 1458 | } | ||
| 1459 | 2 | const_reverse_iterator crbegin() const noexcept | |
| 1460 | { | ||
| 1461 | 2 | gate_linkedentry_t const* const ptr = gate_linkedlist_last(&this->impl); | |
| 1462 | 2 | return const_reverse_iterator(ptr); | |
| 1463 | } | ||
| 1464 | 4 | const_reverse_iterator crend() const noexcept | |
| 1465 | { | ||
| 1466 | 4 | return const_reverse_iterator(NULL); | |
| 1467 | } | ||
| 1468 | const_reverse_iterator rbegin() const noexcept { return this->crbegin(); } | ||
| 1469 | const_reverse_iterator rend() const noexcept { return this->crend(); } | ||
| 1470 | |||
| 1471 | 3 | iterator push(item_t const& item) noexcept | |
| 1472 | { | ||
| 1473 | 3 | return this->add(item); | |
| 1474 | } | ||
| 1475 | 3 | item_t pop() | |
| 1476 | { | ||
| 1477 | 3 | reverse_iterator iter = this->rbegin(); | |
| 1478 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | if (iter == this->rend()) |
| 1479 | { | ||
| 1480 | ✗ | GATEXX_RAISE_ERROR(results::NotAvailable); | |
| 1481 | } | ||
| 1482 | 3 | item_t ret = *iter; | |
| 1483 | 3 | this->remove(iter); | |
| 1484 | 6 | return ret; | |
| 1485 | } | ||
| 1486 | |||
| 1487 | 1 | Enumerator<item_t const> enumerate() const noexcept | |
| 1488 | { | ||
| 1489 | gate_enumerator_t e; | ||
| 1490 | 1 | gate_enumerator_t* tmp = gate_linkedlist_enumerate(&this->impl, &e); | |
| 1491 | #ifdef GATE_DEBUG_MODE | ||
| 1492 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!tmp) |
| 1493 | { | ||
| 1494 | ✗ | panic(results::NullPointer, "LinkedList::enumerate"); | |
| 1495 | } | ||
| 1496 | #else | ||
| 1497 | (void)tmp; | ||
| 1498 | #endif | ||
| 1499 | 1 | return Enumerator<item_t const>(e); | |
| 1500 | } | ||
| 1501 | |||
| 1502 | 3 | self_t& operator<<(item_t const& item) | |
| 1503 | { | ||
| 1504 | 3 | this->push(item); | |
| 1505 | 3 | return *this; | |
| 1506 | } | ||
| 1507 | 3 | self_t& operator>>(item_t& item) | |
| 1508 | { | ||
| 1509 | 3 | item = this->pop(); | |
| 1510 | 3 | return *this; | |
| 1511 | } | ||
| 1512 | }; | ||
| 1513 | |||
| 1514 | } // end of namespace gate | ||
| 1515 | |||
| 1516 | #endif | ||
| 1517 |