source: trunk/tools/common/kList.cpp

Last change on this file was 8003, checked in by bird, 24 years ago

New kFile* classes; now in sync with os2tools.

File size: 5.0 KB
Line 
1/* $Id: kList.cpp,v 1.4 2002-02-24 02:47:28 bird Exp $ */
2/*
3 * Simple list and sorted list template class.
4 * Note: simple list is not implemented yet, as it is not yet needed.
5 *
6 * Copyright (c) 1999 knut st. osmundsen
7 *
8 */
9#ifndef _kList_cpp_
10#define _kList_cpp_
11
12#ifndef DEBUG
13 #define DEBUG
14#endif
15
16/**
17 * Constructor.
18 */
19template <class kEntry>
20kSortedList<kEntry>::kSortedList() : pFirst(NULL), pLast(NULL), cEntries(0)
21{
22}
23
24
25/**
26 * Destructor.
27 */
28template <class kEntry>
29kSortedList<kEntry>::~kSortedList()
30{
31 destroy();
32}
33
34
35/**
36 * Removes all entries in the list.
37 */
38template <class kEntry>
39void kSortedList<kEntry>::destroy()
40{
41 while (pFirst != NULL)
42 {
43 kEntry *p = pFirst;
44 pFirst = (kEntry*)pFirst->getNext();
45 delete p;
46 #ifdef DEBUG
47 cEntries--;
48 #endif
49 }
50 #ifdef DEBUG
51 kASSERT(cEntries == 0);
52 #endif
53 cEntries = 0;
54 pLast = NULL;
55}
56
57
58/**
59 * Inserts entry into the list.
60 * @param pEntry Pointer to entry to insert.
61 */
62template <class kEntry>
63void kSortedList<kEntry>::insert(kEntry *pEntry)
64{
65 if (pEntry == NULL)
66 return;
67
68 if (pFirst == NULL)
69 {
70 pEntry->setNext(NULL);
71 pLast = pFirst = pEntry;
72 }
73 else if (*pLast <= *pEntry)
74 {
75 pEntry->setNext(NULL);
76 pLast->setNext(pEntry);
77 pLast = pEntry;
78 }
79 else
80 {
81 kEntry *pPrev = NULL;
82 kEntry *p = pFirst;
83 while (p != NULL && *p < *pEntry)
84 {
85 pPrev = p;
86 p = (kEntry*)p->getNext();
87 }
88
89 assert(p != NULL);
90
91 pEntry->setNext(p);
92 if (pPrev != NULL)
93 pPrev->setNext(pEntry);
94 else
95 pFirst = pEntry;
96 }
97 cEntries++;
98}
99
100
101/**
102 * Get an element from the list without removing it.
103 * Also see function remove(...)
104 * @returns pointer to matching object. NULL if not found.
105 * @param entry Reference to match object.
106 * @remark
107 */
108template <class kEntry>
109kEntry *kSortedList<kEntry>::get(const kEntry &entry) const
110{
111 kEntry *p = pFirst;
112 while (p != NULL && *p < entry)
113 p = (kEntry*)p->getNext();
114
115 return p != NULL && *p == entry ? p : NULL;
116}
117
118
119/**
120 * Get first element from the list without removing it.
121 * @returns pointer to matching object. NULL if empty list.
122 * @remark
123 */
124template <class kEntry>
125kEntry *kSortedList<kEntry>::getFirst(void) const
126{
127
128 return pFirst;
129}
130
131
132/**
133 * Get first element from the list without removing it.
134 * @returns pointer to matching object. NULL if empty list.
135 * @remark
136 */
137template <class kEntry>
138kEntry *kSortedList<kEntry>::getLast(void) const
139{
140 return pLast;
141}
142
143
144/**
145 * Gets count of entries in the list.
146 * @returns Entry count.
147 */
148template <class kEntry>
149unsigned long kSortedList<kEntry>::getCount(void) const
150{
151 return cEntries;
152}
153
154
155
156
157
158
159/**
160 * Constructor.
161 */
162template <class kEntry>
163kList<kEntry>::kList() : pFirst(NULL), pLast(NULL), cEntries(0)
164{
165}
166
167
168/**
169 * Destructor.
170 */
171template <class kEntry>
172kList<kEntry>::~kList()
173{
174 destroy();
175}
176
177
178/**
179 * Removes all entries in the list.
180 */
181template <class kEntry>
182void kList<kEntry>::destroy()
183{
184 while (pFirst != NULL)
185 {
186 kEntry *p = pFirst;
187 pFirst = (kEntry*)pFirst->getNext();
188 delete p;
189 #ifdef DEBUG
190 cEntries--;
191 #endif
192 }
193 #ifdef DEBUG
194 kASSERT(cEntries == 0);
195 #endif
196 cEntries = 0;
197 pLast = NULL;
198}
199
200
201/**
202 * Inserts an entry into the end of the list.
203 * @param pEntry Pointer to entry to insert.
204 */
205template <class kEntry>
206void kList<kEntry>::insert(kEntry *pEntry)
207{
208 if (pEntry == NULL)
209 return;
210
211 if (pFirst == NULL)
212 {
213 pEntry->setNext(NULL);
214 pLast = pFirst = pEntry;
215 }
216 else
217 {
218 pEntry->setNext(NULL);
219 pLast->setNext(pEntry);
220 pLast = pEntry;
221 }
222 cEntries++;
223}
224
225#if 0
226/**
227 * Inserts an entry into the list in an ascending sorted fashion.
228 * @param pEntry Pointer to entry to insert.
229 */
230template <class kEntry>
231void kList<kEntry>::insertSorted(kEntry *pEntry)
232{
233 if (pEntry == NULL)
234 return;
235 if (pFirst == NULL)
236 {
237 pEntry->setNext(NULL);
238 pLast = pFirst = pEntry;
239 }
240 else
241 { /* linear search */
242 kEntry *pCur = pFirst;
243
244 pCur->
245
246 pEntry->setNext(NULL);
247 pLast->setNext(pEntry);
248 pLast = pEntry;
249 }
250 cEntries++;
251}
252#endif
253
254
255/**
256 * Get first element from the list without removing it.
257 * @returns pointer to matching object. NULL if empty list.
258 * @remark
259 */
260template <class kEntry>
261kEntry *kList<kEntry>::getFirst(void) const
262{
263 return pFirst;
264}
265
266
267/**
268 * Get first element from the list without removing it.
269 * @returns pointer to matching object. NULL if empty list.
270 * @remark
271 */
272template <class kEntry>
273kEntry *kList<kEntry>::getLast(void) const
274{
275 return pLast;
276}
277
278
279/**
280 * Gets count of entries in the list.
281 * @returns Entry count.
282 */
283template <class kEntry>
284unsigned long kList<kEntry>::getCount(void) const
285{
286 return cEntries;
287}
288
289
290#endif
Note: See TracBrowser for help on using the repository browser.