Changeset 2923 for trunk/packages/vizservers/nanovis/Chain.cpp
- Timestamp:
- Apr 1, 2012, 10:44:45 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanovis/Chain.cpp
r2798 r2923 1 1 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * Chain.cpp -- 4 * 5 * The module implements a generic linked list package. 6 * 7 * Copyright 1991-2004 George A Howlett. 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining 10 * a copy of this software and associated documentation files (the 11 * "Software"), to deal in the Software without restriction, including 12 * without limitation the rights to use, copy, modify, merge, publish, 13 * distribute, sublicense, and/or sell copies of the Software, and to 14 * permit persons to whom the Software is furnished to do so, subject to 15 * the following conditions: 16 * 17 * The above copyright notice and this permission notice shall be 18 * included in all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 */ 28 2 /* The module implements a generic linked list package. 3 * 4 * Copyright 1991-2004 George A Howlett. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 29 25 #include <stdlib.h> 30 26 #include <stdio.h> 27 31 28 #include "Chain.h" 32 29 33 30 typedef int (QSortCompareProc) (const void *, const void *); 34 31 35 /* 36 *---------------------------------------------------------------------- 37 * 38 * Reset -- 39 * 40 * Removes all the links from the chain, freeing the memory for each 41 * link. Memory pointed to by the link (clientData) is not freed. It's 42 * the caller's responsibility to deallocate it. 43 * 44 * Results: 45 * None. 46 * 47 *---------------------------------------------------------------------- 48 */ 49 void 50 Chain::Reset() /* Chain to clear */ 32 /** 33 * Removes all the links from the chain, freeing the memory for each 34 * link. Memory pointed to by the link (clientData) is not freed. It's 35 * the caller's responsibility to deallocate it. 36 */ 37 void 38 Chain::reset() 51 39 { 52 40 ChainLink *oldPtr; … … 55 43 while (linkPtr != NULL) { 56 44 oldPtr = linkPtr; 57 linkPtr = linkPtr-> Next();45 linkPtr = linkPtr->next(); 58 46 delete oldPtr; 59 47 } 60 Init(); 61 } 62 63 /* 64 *---------------------------------------------------------------------- 65 * 66 * LinkAfter -- 67 * 68 * Inserts a link after another link. If afterPtr is NULL, then the new 69 * link is prepended to the beginning of the chain. 70 * 71 * Results: 72 * None. 73 * 74 *---------------------------------------------------------------------- 75 */ 76 void 77 Chain::LinkAfter(ChainLink *linkPtr, ChainLink *afterPtr) 48 init(); 49 } 50 51 /** 52 * Inserts a link after another link. If afterPtr is NULL, then the new 53 * link is prepended to the beginning of the chain. 54 */ 55 void 56 Chain::linkAfter(ChainLink *linkPtr, ChainLink *afterPtr) 78 57 { 79 58 if (_head == NULL) { … … 87 66 _head = linkPtr; 88 67 } else { 89 linkPtr->_next = afterPtr-> Next();68 linkPtr->_next = afterPtr->next(); 90 69 linkPtr->_prev = afterPtr; 91 70 if (afterPtr == _tail) { 92 71 _tail = linkPtr; 93 72 } else { 94 afterPtr-> Next()->_prev = linkPtr;73 afterPtr->next()->_prev = linkPtr; 95 74 } 96 75 afterPtr->_next = linkPtr; … … 100 79 } 101 80 102 /* 103 *---------------------------------------------------------------------- 104 * 105 * LinkBefore -- 106 * 107 * Inserts a new link preceding a given link in a chain. If beforePtr is 108 * NULL, then the new link is placed at the beginning of the list. 109 * 110 * Results: 111 * None. 112 * 113 *---------------------------------------------------------------------- 114 */ 115 void 116 Chain::LinkBefore( 117 ChainLink *linkPtr, /* New entry to be inserted */ 118 ChainLink *beforePtr) /* Entry to link before */ 81 /** 82 * Inserts a new link preceding a given link in a chain. If beforePtr is 83 * NULL, then the new link is placed at the beginning of the list. 84 * 85 * \param linkPtr New entry to be inserted 86 * \param beforePtr Entry to link before 87 */ 88 void 89 Chain::linkBefore(ChainLink *linkPtr, 90 ChainLink *beforePtr) 119 91 { 120 92 if (_head == NULL) { … … 122 94 } else { 123 95 if (beforePtr == NULL) { 124 / * Append to the end of the chain. */96 // Append to the end of the chain. 125 97 linkPtr->_next = NULL; 126 98 linkPtr->_prev = _tail; … … 128 100 _tail = linkPtr; 129 101 } else { 130 linkPtr->_prev = beforePtr-> Prev();102 linkPtr->_prev = beforePtr->prev(); 131 103 linkPtr->_next = beforePtr; 132 104 if (beforePtr == _head) { 133 105 _head = linkPtr; 134 106 } else { 135 beforePtr-> Prev()->_next = linkPtr;107 beforePtr->prev()->_next = linkPtr; 136 108 } 137 109 beforePtr->_prev = linkPtr; … … 141 113 } 142 114 143 /* 144 *---------------------------------------------------------------------- 145 * 146 * Unlink -- 147 * 148 * Unlinks a link from the chain. The link is not deallocated, 149 * but only removed from the chain. 150 * 151 * Results: 152 * None. 153 * 154 *---------------------------------------------------------------------- 155 */ 156 void 157 Chain::Unlink(ChainLink *linkPtr) 115 /** 116 * Unlinks a link from the chain. The link is not deallocated, 117 * but only removed from the chain. 118 */ 119 void 120 Chain::unlink(ChainLink *linkPtr) 158 121 { 159 122 bool unlinked; /* Indicates if the link is actually removed … … 162 125 unlinked = false; 163 126 if (_head == linkPtr) { 164 _head = linkPtr-> Next();127 _head = linkPtr->next(); 165 128 unlinked = true; 166 129 } 167 130 if (_tail == linkPtr) { 168 _tail = linkPtr-> Prev();169 unlinked = true; 170 } 171 if (linkPtr-> Next() != NULL) {172 linkPtr-> Next()->_prev = linkPtr->Prev();173 unlinked = true; 174 } 175 if (linkPtr-> Prev() != NULL) {176 linkPtr-> Prev()->_next = linkPtr->Next();131 _tail = linkPtr->prev(); 132 unlinked = true; 133 } 134 if (linkPtr->next() != NULL) { 135 linkPtr->next()->_prev = linkPtr->prev(); 136 unlinked = true; 137 } 138 if (linkPtr->prev() != NULL) { 139 linkPtr->prev()->_next = linkPtr->next(); 177 140 unlinked = true; 178 141 } … … 183 146 } 184 147 185 /* 186 *---------------------------------------------------------------------- 187 * 188 * DeleteLink -- 189 * 190 * Unlinks and frees the given link from the chain. It's assumed that 191 * the link belong to the chain. No error checking is performed to verify 192 * this. 193 * 194 * Results: 195 * None. 196 * 197 *---------------------------------------------------------------------- 198 */ 199 void 200 Chain::DeleteLink(ChainLink *linkPtr) 201 { 202 Unlink(linkPtr); 148 /** 149 * Unlinks and frees the given link from the chain. It's assumed that 150 * the link belong to the chain. No error checking is performed to verify 151 * this. 152 */ 153 void 154 Chain::deleteLink(ChainLink *linkPtr) 155 { 156 unlink(linkPtr); 203 157 delete linkPtr; 204 158 } 205 159 206 /* 207 *---------------------------------------------------------------------- 208 * 209 * Append -- 210 * 211 * Creates and new link with the given data and appends it to the end of 212 * the chain. 213 * 214 * Results: 215 * Returns a pointer to the link created. 216 * 217 *---------------------------------------------------------------------- 160 /** 161 * Creates and new link with the given data and appends it to the end of 162 * the chain. 163 * 164 * \return a pointer to the link created. 218 165 */ 219 166 ChainLink * 220 Chain:: Append(void *clientData)167 Chain::append(void *clientData) 221 168 { 222 169 ChainLink *linkPtr; 223 170 224 171 linkPtr = new ChainLink(clientData); 225 LinkBefore(linkPtr, (ChainLink *)NULL);172 linkBefore(linkPtr, (ChainLink *)NULL); 226 173 return linkPtr; 227 174 } 228 175 229 /* 230 *---------------------------------------------------------------------- 231 * 232 * Prepend -- 233 * 234 * Creates and new link with the given data and prepends it to beginning 235 * of the chain. 236 * 237 * Results: 238 * Returns a pointer to the link created. 239 * 240 *---------------------------------------------------------------------- 176 /** 177 * Creates and new link with the given data and prepends it to beginning 178 * of the chain. 179 * 180 * \return a pointer to the link created. 241 181 */ 242 182 ChainLink * 243 Chain:: Prepend(void *clientData)183 Chain::prepend(void *clientData) 244 184 { 245 185 ChainLink *linkPtr; 246 186 247 187 linkPtr = new ChainLink(clientData); 248 LinkAfter(linkPtr, (ChainLink *)NULL);188 linkAfter(linkPtr, (ChainLink *)NULL); 249 189 return linkPtr; 250 190 } 251 191 252 /* 253 *---------------------------------------------------------------------- 254 * 255 * GetNthLink -- 256 * 257 * Find the link at the given position in the chain. 258 * 259 * Results: 260 * Returns the pointer to the link, if that numbered link 261 * exists. Otherwise NULL. 262 * 263 *---------------------------------------------------------------------- 192 /** 193 * Find the link at the given position in the chain. 194 * 195 * \param position Index of link to select from front or back of the chain. 196 * 197 * \return the pointer to the link, if that numbered link 198 * exists. Otherwise NULL. 264 199 */ 265 200 ChainLink * 266 Chain::GetNthLink(long position) /* Index of link to select from front or back 267 * of the chain. */ 268 { 269 ChainLink *linkPtr; 270 271 for (linkPtr = _head; linkPtr != NULL; linkPtr = linkPtr->Next()) { 201 Chain::getNthLink(long position) 202 { 203 ChainLink *linkPtr; 204 205 for (linkPtr = _head; linkPtr != NULL; linkPtr = linkPtr->next()) { 272 206 if (position == 0) { 273 207 return linkPtr; … … 278 212 } 279 213 280 /* 281 *---------------------------------------------------------------------- 282 * 283 * Sort -- 284 * 285 * Sorts the chain according to the given comparison routine. 286 * 287 * Results: 288 * None. 214 /** 215 * Sorts the chain according to the given comparison routine. 289 216 * 290 217 * Side Effects: 291 * The chain is reordered. 292 * 293 *---------------------------------------------------------------------- 294 */ 295 296 void 297 Chain::Sort(ChainCompareProc *proc) 218 * The chain is reordered. 219 */ 220 void 221 Chain::sort(ChainCompareProc *proc) 298 222 { 299 223 ChainLink *linkPtr; … … 308 232 } 309 233 i = 0; 310 for (linkPtr = _head; linkPtr != NULL; linkPtr = linkPtr-> Next()) {234 for (linkPtr = _head; linkPtr != NULL; linkPtr = linkPtr->next()) { 311 235 linkArr[i++] = linkPtr; 312 236 } … … 320 244 for (i = 1; i < _nLinks; i++) { 321 245 linkPtr->_next = linkArr[i]; 322 linkPtr-> Next()->_prev = linkPtr;323 linkPtr = linkPtr-> Next();246 linkPtr->next()->_prev = linkPtr; 247 linkPtr = linkPtr->next(); 324 248 } 325 249 _tail = linkPtr; … … 327 251 delete [] linkArr; 328 252 } 329
Note: See TracChangeset
for help on using the changeset viewer.