forked from Atlantis-PBEM/Atlantis
-
Notifications
You must be signed in to change notification settings - Fork 3
/
alist.h
129 lines (111 loc) · 3.11 KB
/
alist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef ALIST_CLASS
#define ALIST_CLASS
// START A3HEADER
//
// This source file is part of the Atlantis PBM game program.
// Copyright (C) 1995-1999 Geoff Dunbar
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program, in the file license.txt. If not, write
// to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
// See the Atlantis Project web page for details:
// http://www.prankster.com/project
//
// END A3HEADER
#include <vector>
/**
* Base class for intrusive single-linked list
*/
class AListElem
{
public:
/// destructor
virtual ~AListElem();
AListElem *next; ///< next element in the list
};
/**
* a collection of AListElem
*/
class AList
{
public:
/// constructor
AList();
/// destructor
~AList();
/// delete all elements
void DeleteAll() { deleteAll(); }
void deleteAll();
/// clear this without deleting members
void Empty() { clear(); }
void clear();
///@return 'e' if it is in this, else NULL
AListElem* Get(AListElem *e) { return get(e); }
AListElem* get(AListElem *e);
///@return the element after 'e'
AListElem* Next(AListElem *e) const { return next(e); }
AListElem* next(AListElem *e) const;
///@return true if 'e' was in this (before being removed)
bool Remove(AListElem *e) { return remove(e); }
bool remove(AListElem *e);
/// into the front
void Insert(AListElem *e) { push_front(e); }
void push_front(AListElem *e);
/// to the back
void Add(AListElem *e) { push_back(e); }
void push_back(AListElem *e);
///@return the first element
AListElem* First() const { return front(); }
AListElem* front() const;
///@return number of elements in this
int Num() const { return size(); }
int size() const;
// Helper function for forlist_safe
int NextLive(AListElem **copy, int size, int pos) const
{
return nextLive(copy, size, pos);
}
int nextLive(AListElem **copy, int size, int pos) const;
private:
AListElem *list;
AListElem *lastelem;
int num;
};
template<typename T>
void deleteAll(std::vector<T*> &v)
{
for (auto i : v)
delete i;
v.clear();
}
#define forlist(l) \
AListElem *elem, *_elem2; \
for (elem=(l)->First(), \
_elem2 = (elem ? (l)->Next(elem) : 0); \
elem; \
elem = _elem2, \
_elem2 = (_elem2 ? ((l)->Next(_elem2)) : 0))
#define forlist_safe(l) \
int size = (l)->Num(); \
AListElem **copy = new AListElem*[size]; \
AListElem *elem; \
int pos; \
for (pos = 0, elem = (l)->First(); elem; elem = elem->next, pos++) { \
copy[pos] = elem; \
} \
for (pos = 0; \
pos < size ? (elem = copy[pos], 1) : (delete [] copy, 0); \
pos = (l)->NextLive(copy, size, pos))
#endif