ScummVM API documentation
px_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  * Additional copyright for this file:
8  * Copyright (C) 1999-2000 Revolution Software Ltd.
9  * This code is based on source code created by Revolution Software,
10  * used with permission.
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef ICB_PX_LIBRARY_LIST_TEMPLATE
28 #define ICB_PX_LIBRARY_LIST_TEMPLATE
29 
30 namespace ICB {
31 
32 template <class Type> class pxList {
33 protected:
34  Type m_item; // The item at this node
35  pxList<Type> *m_tail; // The tail of the list
36 
37 public:
38  pxList() {
39  m_tail = NULL; // Construct an empty list
40  }
41  ~pxList() {
42  if (!IsEmpty())
43  delete Tail(); // Destruct the array
44  }
45 
46  Type &Head() {
47  return m_item; // The lists' head
48  }
49  pxList<Type> *Tail() {
50  return m_tail; // The lists' tail
51  }
52  bool IsEmpty() {
53  return (m_tail == NULL); // Is the list empty ?
54  }
55  void Append(const Type &); // Append item to the end of the list
56  void Insert(const Type &); // Inserts an item after the current item
57  pxList<Type> *Find(const Type &); // Returns the list whose head == entry (NULL if not found)
58  int32 Length(); // Returns the length of the list
59 };
60 
61 template <class Type> class rcSortedList {
62 protected:
63  Type m_item; // The item at this node
64  rcSortedList<Type> *m_tail; // The tail of the list
65 
66 public:
67  rcSortedList() {
68  m_tail = NULL; // Construct an empty list
69  }
70  ~rcSortedList() {
71  if (!IsEmpty())
72  delete Tail(); // Destruct the array
73  }
74 
75 public:
76  Type &Head(); // The lists' head
77  rcSortedList<Type> *Tail(); // The lists' tail
78  bool IsEmpty(); // Is the list empty ?
79  int32 Length(); // Returns the length of the list
80 
81 public:
82  rcSortedList<Type> *Insert(const Type &); // Inserts an item in the correct place
83  rcSortedList<Type> *Find(const Type &); // Returns the list whose head == entry (NULL if not found)
84 };
85 
86 template <class Type> void pxList<Type>::Append(const Type &entry) {
87  if (IsEmpty()) {
88  m_item = entry;
89  m_tail = new pxList<Type>;
90  } else {
91  Tail()->Append(entry);
92  }
93 }
94 
95 template <class Type> void pxList<Type>::Insert(const Type &entry) {
96  pxList<Type> *newNode = new pxList<Type>;
97 
98  if (IsEmpty()) { // Is the list is empty, insert the item at the start of the list
99  m_item = entry;
100  } else { // Else put the item before this node
101  newNode->m_item = m_item;
102  m_item = entry;
103  newNode->m_tail = Tail();
104  }
105  m_tail = newNode;
106 }
107 
108 template <class Type> pxList<Type> *pxList<Type>::Find(const Type &entry) {
109  // If this is the end of list marker we haven't found it
110  if (IsEmpty())
111  return NULL;
112 
113  if (Head() == entry)
114  return this;
115 
116  return (Tail()->Find(entry));
117 }
118 
119 template <class Type> int32 pxList<Type>::Length() {
120  if (IsEmpty())
121  return 0;
122 
123  return (1 + Tail()->Length());
124 }
125 
126 template <class Type> Type &rcSortedList<Type>::Head() { // The lists' head
127  return m_item;
128 }
129 
130 template <class Type> rcSortedList<Type> *rcSortedList<Type>::Tail() { // The lists' tail
131  return m_tail;
132 }
133 
134 template <class Type> bool rcSortedList<Type>::IsEmpty() { // Is the list empty ?
135  return (m_tail == NULL);
136 }
137 
138 template <class Type> rcSortedList<Type> *rcSortedList<Type>::Insert(const Type &entry) {
139  if (IsEmpty()) {
140  // End of the list so add the entry here
141  m_item = entry;
142  m_tail = new rcSortedList<Type>;
143  return this;
144  }
145  // The class being listed must have a '>' Operator defined
146  else if (m_item > entry) {
147  // The new item comes before the current one
148  rcSortedList<Type> *newNode = new rcSortedList<Type>;
149  newNode->m_tail = m_tail;
150  newNode->m_item = m_item;
151  m_item = entry;
152  m_tail = newNode;
153  return this;
154  } else {
155  // Keep going
156  return Tail()->Insert(entry);
157  }
158 }
159 
160 template <class Type> rcSortedList<Type> *rcSortedList<Type>::Find(const Type &entry) {
161  // If this is the end of list marker we haven't found it
162  if (IsEmpty())
163  return NULL;
164 
165  // this list is sorted, so we can stop when we have a higher value than entry
166  if (Head() > entry)
167  return NULL;
168 
169  if (Head() == entry)
170  return this;
171 
172  return (Tail()->Find(entry));
173 }
174 
175 template <class Type> int32 rcSortedList<Type>::Length() {
176  if (IsEmpty())
177  return 0;
178 
179  return (1 + Tail()->Length());
180 }
181 
182 } // End of namespace ICB
183 
184 #endif // #ifndef _PX_LIBRARY_LIST_TEMPLATE
Definition: actor.h:32
Type
Definition: log.h:33
Definition: px_list.h:61
Definition: px_list.h:32