1 | /* $Id: kLIFO.cpp,v 1.2 2002-02-24 02:47:27 bird Exp $ */
|
---|
2 | /*
|
---|
3 | * Simple LIFO template class implementation.
|
---|
4 | *
|
---|
5 | * Copyright (c) 1999 knut st. osmundsen
|
---|
6 | *
|
---|
7 | */
|
---|
8 |
|
---|
9 | #ifndef _kLIFO_cpp_
|
---|
10 | #define _kLIFO_cpp_
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Creates an empty LIFO.
|
---|
14 | */
|
---|
15 | template <class kEntry>
|
---|
16 | kLIFO<kEntry>::kLIFO(void) : pTop(NULL)
|
---|
17 | {
|
---|
18 | }
|
---|
19 |
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Deletes entire LIFO.
|
---|
23 | * @remark calls destroy().
|
---|
24 | */
|
---|
25 | template <class kEntry>
|
---|
26 | kLIFO<kEntry>::~kLIFO(void)
|
---|
27 | {
|
---|
28 | destroy();
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Deletes all nodes in the LIFO.
|
---|
34 | */
|
---|
35 | template <class kEntry>
|
---|
36 | void kLIFO<kEntry>::destroy(void)
|
---|
37 | {
|
---|
38 | if (pTop)
|
---|
39 | {
|
---|
40 | #if 0
|
---|
41 | /* recursive */
|
---|
42 | delete pTop;
|
---|
43 | #else
|
---|
44 | /* iterative */
|
---|
45 | kEntry *p1;
|
---|
46 | p1 = pTop;
|
---|
47 | while (p1 != NULL)
|
---|
48 | {
|
---|
49 | kEntry *p2;
|
---|
50 | p2 = p1;
|
---|
51 | p1 = (kEntry*)p1->getNext();
|
---|
52 | p2->setNext(NULL);
|
---|
53 | delete p2;
|
---|
54 | }
|
---|
55 | #endif
|
---|
56 | pTop = NULL;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Push a node on top of the LIFO.
|
---|
63 | * @param pNewEntry Entry to push onto the stack.
|
---|
64 | */
|
---|
65 | template <class kEntry>
|
---|
66 | void kLIFO<kEntry>::push(kEntry *pNewEntry)
|
---|
67 | {
|
---|
68 | pNewEntry->setNext(pTop);
|
---|
69 | pTop = pNewEntry;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Pop the topnode from the LIFO (if any)
|
---|
75 | * @returns Topentry of the LIFO. If empty LIFO, NULL.
|
---|
76 | */
|
---|
77 | template <class kEntry>
|
---|
78 | kEntry *kLIFO<kEntry>::pop(void)
|
---|
79 | {
|
---|
80 | if (pTop)
|
---|
81 | {
|
---|
82 | kEntry *pRetEntry = pTop;
|
---|
83 | pTop = (kEntry*)pTop->getNext();
|
---|
84 | pRetEntry->setNext(NULL);
|
---|
85 | return pRetEntry;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | return NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Gets a given node from the lifo. The node does not have to be the top item.
|
---|
94 | * @returns Pointer to node. NULL on error.
|
---|
95 | * @param pGetEntry Pointer to node to get.
|
---|
96 | */
|
---|
97 | template <class kEntry>
|
---|
98 | kEntry *kLIFO<kEntry>::get(const kEntry *pGetEntry)
|
---|
99 | {
|
---|
100 | kEntry *pPrev = NULL;
|
---|
101 | kEntry *pRet = pTop;
|
---|
102 |
|
---|
103 | /* find */
|
---|
104 | while (pRet != pGetEntry)
|
---|
105 | {
|
---|
106 | pPrev = pRet;
|
---|
107 | pRet = (kEntry *)pRet->getNext();
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* unlink */
|
---|
111 | if (pRet != NULL)
|
---|
112 | {
|
---|
113 | if (pPrev == NULL)
|
---|
114 | pTop = (kEntry *)pRet->getNext();
|
---|
115 | else
|
---|
116 | pPrev->setNext(pRet->getNext());
|
---|
117 | }
|
---|
118 |
|
---|
119 | return pRet;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Unwinds the LIFO until a given node.
|
---|
125 | * @param pToEntry Pointer to the new top node.
|
---|
126 | */
|
---|
127 | template <class kEntry>
|
---|
128 | void kLIFO<kEntry>::unwind(kEntry *pToEntry)
|
---|
129 | {
|
---|
130 | if (pToEntry == NULL)
|
---|
131 | return;
|
---|
132 |
|
---|
133 | while (pTop != pToEntry && pTop != NULL)
|
---|
134 | {
|
---|
135 | kEntry *pEntry = pTop;
|
---|
136 | pTop = (kEntry*)pTop->getNext();
|
---|
137 | delete pEntry;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Pops from this lifo and pushes the items onto lifoTo.
|
---|
144 | * @param pToEntry Pointer to node to stop at. Will become the top node in this lifo.
|
---|
145 | * @param lifoTo Receiving lifo.
|
---|
146 | * @precond pToEntry must exist in this lifo.
|
---|
147 | */
|
---|
148 | template <class kEntry>
|
---|
149 | void kLIFO<kEntry>::popPush(const kEntry *pToEntry, kLIFO<kEntry> &lifoTo)
|
---|
150 | {
|
---|
151 | if (pToEntry != NULL && exists(pToEntry))
|
---|
152 | while (pTop != NULL && pTop != pToEntry)
|
---|
153 | lifoTo.push(pop());
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Is the LIFO empty?
|
---|
159 | * @returns TRUE - empty, FALSE - not empty.
|
---|
160 | */
|
---|
161 | template <class kEntry>
|
---|
162 | KBOOL kLIFO<kEntry>::isEmpty(void) const
|
---|
163 | {
|
---|
164 | return pTop == NULL;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Finds a node matching the given key.
|
---|
170 | * @returns Pointer to node if a match exists.
|
---|
171 | * @param pszKey Pointer to key string.
|
---|
172 | * @remark uses the == operator of the nodes.
|
---|
173 | */
|
---|
174 | template <class kEntry>
|
---|
175 | kEntry *kLIFO<kEntry>::find(const char *pszKey) const
|
---|
176 | {
|
---|
177 | kEntry *pCurrentEntry = pTop;
|
---|
178 |
|
---|
179 | while (pCurrentEntry != NULL && !(*pCurrentEntry == pszKey))
|
---|
180 | pCurrentEntry = (kEntry*)pCurrentEntry->getNext();
|
---|
181 |
|
---|
182 | return pCurrentEntry;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Checks if a node exists in the LIFO.
|
---|
188 | * @returns TRUE if the node exists, FALSE if it don't.
|
---|
189 | * @param pEntry Pointer to node.
|
---|
190 | */
|
---|
191 | template <class kEntry>
|
---|
192 | KBOOL kLIFO<kEntry>::exists(const kEntry *pEntry) const
|
---|
193 | {
|
---|
194 | kEntry *pCurrentEntry = pTop;
|
---|
195 |
|
---|
196 | while (pCurrentEntry != NULL && pCurrentEntry != pEntry)
|
---|
197 | pCurrentEntry = (kEntry*)pCurrentEntry->getNext();
|
---|
198 |
|
---|
199 | return pCurrentEntry != NULL;
|
---|
200 | }
|
---|
201 |
|
---|
202 | #endif
|
---|