ScummVM API documentation
qd_list.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef QDENGINE_QDCORE_QD_LIST_H
23#define QDENGINE_QDCORE_QD_LIST_H
24
25namespace QDEngine {
26
27template <class Type> class qdList {
28 int numElements;
29 Type *firstElement;
30 void test(int code);
31
32public:
33
34 qdList();
35 ~qdList();
36
37 int size();
38 Type *first();
39 Type *last();
40
41 void clear();
42 void delete_all();
43
44 void insert(Type *p);
45 void append(Type *p);
46 void insert(Type *pointer, Type *p);
47 void append(Type *pointer, Type *p);
48 void remove(Type *p);
49 Type *search(int ID);
50};
51
52template <class Type>
53inline void qdList<Type>::test(int code) {
54#ifdef _XT_TEST_LIST_
55 Type *p = first();
56 int cnt = 0;
57 while (p) {
58 cnt++;
59 p = p->next;
60 }
61 if (cnt != numElements)
62 ErrH.Abort("List", XERR_USER, code);
63#endif
64}
65
66template <class Type>
67inline qdList<Type>::qdList() {
68 numElements = 0;
69 firstElement = 0;
70}
71
72template <class Type>
73inline qdList<Type>::~qdList() {
74 clear();
75}
76
77template <class Type>
78inline void qdList<Type>::clear() {
79 while (first())
80 remove(first());
81}
82
83template <class Type>
84inline void qdList<Type>::delete_all() {
85 Type *p;
86 while ((p = first()) != 0) {
87 remove(p);
88 delete p;
89 }
90}
91
92template <class Type>
93inline int qdList<Type>::size() {
94 return numElements;
95}
96
97template <class Type>
98inline Type *qdList<Type>::first() {
99 return firstElement;
100}
101
102template <class Type>
103inline Type *qdList<Type>::last() {
104 return firstElement ? firstElement->prev : 0;
105}
106
107template <class Type>
108inline void qdList<Type>::insert(Type *p) {
109 if (p->list)
110 ErrH.Abort("Element is already in list");
111 numElements++;
112 if (firstElement) {
113 p->next = firstElement;
114 p->prev = firstElement->prev;
115 firstElement->prev = p;
116 } else {
117 p->prev = p;
118 p->next = 0;
119 }
120 firstElement = p;
121 p->list = this;
122 test(0);
123}
124
125template <class Type>
126inline void qdList<Type>::insert(Type *pointer, Type *p) {
127 if (!firstElement || firstElement == pointer) {
128 insert(p);
129 return;
130 }
131 if (!pointer) {
132 append(p);
133 return;
134 }
135
136 if (p->list)
137 ErrH.Abort("Element is already in list");
138 numElements++;
139 p->next = pointer;
140 p->prev = pointer->prev;
141 pointer->prev->next = p;
142 pointer->prev = p;
143 p->list = this;
144 test(5);
145}
146
147
148template <class Type>
149inline void qdList<Type>::append(Type *p) {
150// if(p->list)
151// ErrH.Abort("Element is already in list");
152 numElements++;
153 if (firstElement) {
154 p->next = 0;
155 p->prev = firstElement->prev;
156 firstElement->prev->next = p;
157 firstElement->prev = p;
158 } else {
159 p->next = 0;
160 p->prev = firstElement = p;
161 }
162 p->list = this;
163 test(1);
164}
165
166template <class Type>
167inline void qdList<Type>::remove(Type *p) {
168// if(p->list != this)
169// ErrH.Abort("Removed element isn't in list");
170 numElements--;
171 if (p->next)
172 p->next->prev = p->prev;
173 else
174 firstElement->prev = p->prev;
175
176 if (p != firstElement)
177 p->prev->next = p->next;
178 else {
179 firstElement = p->next;
180 if (firstElement)
181 firstElement->prev = p->prev;
182 }
183
184 p->next = p->prev = 0;
185 p->list = 0;
186 test(2);
187}
188
189template <class Type>
190inline Type *qdList<Type>::search(int ID) {
191 Type *p = first();
192 while (p) {
193 if (p->ID == ID)
194 return p;
195 p = p->next;
196 }
197 return 0;
198}
199
200} // namespace QDEngine
201
202#endif // QDENGINE_QDCORE_QD_LIST_H
Definition: qd_list.h:27
It remove(It first, It last, const T &val)
Definition: algorithm.h:466
Type
Definition: log.h:33
Базовый класс для игровых ресурсов.
Definition: console.h:28