libtcod
Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
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 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef TCOD_LIST_HPP_
36#define TCOD_LIST_HPP_
37
38#include <stdlib.h> // NULL
39#include <string.h> // memcpy
40
41#include <utility>
42
43#include "list.h"
46
53
54// fast & lightweight list template
55
56
64template <class T>
65class TCODList {
66 T* array{};
67 int fillSize{};
68 int allocSize{};
69
70 public:
86 [[deprecated("TCODList is unsuitable as a C++ container.")]] TCODList() = default;
87
106 TCODList(const TCOD_list_t l) {
107 for (void** it = TCOD_list_begin(l); it != TCOD_list_end(l); ++it) {
108 push(*static_cast<T*>(static_cast<void*>(it)));
109 }
110 }
111 TCODList(const TCODList<T>& rhs) { *this = rhs; }
112 TCODList<T>& operator=(const TCODList<T>& rhs) {
113 while (allocSize < rhs.allocSize) allocate();
114 fillSize = rhs.fillSize;
115 int i = 0;
116 for (T* t = rhs.begin(); t != rhs.end(); ++t) {
117 array[i++] = *t;
118 }
119 return *this;
120 }
121 TCODList(TCODList&& rhs) noexcept { swap(*this, rhs); };
122 TCODList& operator=(TCODList&& rhs) noexcept {
123 swap(*this, rhs);
124 return *this;
125 }
126
144 virtual ~TCODList() {
145 if (array) delete[] array;
146 }
147
148 friend void swap(TCODList& lhs, TCODList& rhs) noexcept {
149 using std::swap;
150 swap(lhs.array, rhs.array);
151 swap(lhs.fillSize, rhs.fillSize);
152 swap(lhs.allocSize, rhs.allocSize);
153 }
154
166 [[deprecated("TCODList is unsuitable as a C++ container.")]] TCODList(int nbElements) {
167 fillSize = 0;
168 allocSize = nbElements;
169 array = new T[nbElements];
170 }
171 // clang-format off
172
194 void set(const T elt, int idx) {
195 if ( idx < 0 ) return;
196 while ( allocSize < idx+1 ) allocate();
197 array[idx] = elt;
198 if ( idx+1 > fillSize ) fillSize = idx+1;
199 }
200
219 T get(int idx) const {
220 return array[idx];
221 }
222
240 bool isEmpty() const {
241 return ( fillSize == 0 );
242 }
243
261 int size() const {
262 return fillSize;
263 }
264
283 bool contains(const T elt) const
284 {
285 for (T* curElt = begin(); curElt != end(); ++curElt) {
286 if (*curElt == elt) { return true; }
287 }
288 return false;
289 }
290
311 T * insertBefore(const T elt,int before) {
312 if ( fillSize+1 >= allocSize ) allocate();
313 for (int idx=fillSize; idx > before; idx--) {
314 array[idx]=array[idx-1];
315 }
316 array[before]=elt;
317 fillSize++;
318 return &array[before];
319 }
320
342 void remove(const T elt) {
343 for ( T* curElt = begin(); curElt != end(); curElt ++) {
344 if ( *curElt == elt ) {
345 remove(curElt);
346 return;
347 }
348 }
349 }
350 void removeFast(const T elt) {
351 for ( T* curElt = begin(); curElt != end(); curElt ++) {
352 if ( *curElt == elt ) {
353 removeFast(curElt);
354 return;
355 }
356 }
357 }
358
380 void addAll(const TCODList<T> &l2) {
381 for (T *t=l2.begin(); t!= l2.end(); t++) {
382 push(*t);
383 }
384 }
385
401 void clear() {
402 fillSize=0;
403 }
404
427 for ( T* curElt = begin(); curElt != end(); curElt ++ ) {
428 delete (*curElt);
429 }
430 fillSize=0;
431 }
432
453 void reverse() {
454 T* head = begin();
455 T* tail = end() - 1;
456 while (head < tail) {
457 std::swap(*head, *tail);
458 ++head;
459 --tail;
460 }
461 }
462
482 void push(const T elt) {
483 if ( fillSize+1 >= allocSize ) allocate();
484 array[fillSize++] = elt;
485 }
486
507 T pop() {
508 if ( fillSize == 0 ) return T{};
509 return array[--fillSize];
510 }
511
532 T peek() const {
533 if ( fillSize == 0 ) return T{};
534 return array[fillSize-1];
535 }
536
566 T * begin() const {
567 if ( fillSize == 0 ) return (T *)NULL;
568 return &array[0];
569 }
570 T * end() const {
571 if ( fillSize == 0 ) return (T *)NULL;
572 return &array[fillSize];
573 }
574
613 T *remove(T *elt) {
614 for ( T* curElt = elt; curElt < end()-1; curElt ++) {
615 *curElt = *(curElt+1);
616 }
617 fillSize--;
618 if ( fillSize == 0 ) return ((T *)NULL)-1;
619 else return elt-1;
620 }
621 T *removeFast(T *elt) {
622 *elt = array[fillSize-1];
623 fillSize--;
624 if ( fillSize == 0 ) return ((T *)NULL)-1;
625 else return elt-1;
626 }
627
628protected :
629 void allocate() {
630 int newSize = allocSize * 2;
631 if ( newSize == 0 ) newSize = 16;
632 T *newArray = new T[ newSize ];
633 if ( array ) {
634 if ( fillSize > 0 ) memcpy(newArray, array, sizeof(T)*fillSize);
635 delete [] array;
636 }
637 array=newArray;
638 allocSize=newSize;
639 }
640};
641
642#endif // TCOD_LIST_HPP_
Definition list.hpp:65
TCODList(const TCOD_list_t l)
You can create a list by duplicating an existing list.
Definition list.hpp:106
TCODList()=default
You can create an empty list with the default constructor.
T get(int idx) const
You can retrieve a value with get.
Definition list.hpp:219
virtual ~TCODList()
You can delete a list, freeing any allocated resources.
Definition list.hpp:144
T * insertBefore(const T elt, int before)
Definition list.hpp:311
bool contains(const T elt) const
Definition list.hpp:283
void push(const T elt)
You can push an element on the stack (append it to the end of the list) :
Definition list.hpp:482
TCODList(int nbElements)
You can also create an empty list and pre-allocate memory for elements.
Definition list.hpp:166
T peek() const
You can read the last element of the stack without removing it :
Definition list.hpp:532
void remove(const T elt)
The _fast versions replace the element to remove with the last element of the list.
Definition list.hpp:342
int size() const
Definition list.hpp:261
T pop()
You can pop an element from the stack (remove the last element of the list).
Definition list.hpp:507
void set(const T elt, int idx)
You can assign a value with set.
Definition list.hpp:194
void clearAndDelete()
For lists containing pointers, you can clear the list and delete (or free for C) the elements :
Definition list.hpp:426
void addAll(const TCODList< T > &l2)
You can concatenate two lists.
Definition list.hpp:380
void clear()
Definition list.hpp:401
T * begin() const
You can iterate through the elements of the list using an iterator.
Definition list.hpp:566
void reverse()
This function reverses the order of the elements in the list.
Definition list.hpp:453
T * remove(T *elt)
You can remove an element from the list while iterating.
Definition list.hpp:613
bool isEmpty() const
Definition list.hpp:240
Deprecated libtcod list module.