|
| 1 | +#ifdef HAVE_CONFIG_H |
| 2 | +#include <../../nrnconf.h> |
| 3 | +#endif |
| 4 | +/* |
| 5 | +Based on Unidraw UList but UList changed to HTList (head tail list) |
| 6 | +for fast insertion, deletion, iteration |
| 7 | +*/ |
| 8 | + |
| 9 | +/* |
| 10 | + * Copyright (c) 1990, 1991 Stanford University |
| 11 | + * |
| 12 | + * Permission to use, copy, modify, distribute, and sell this software and its |
| 13 | + * documentation for any purpose is hereby granted without fee, provided |
| 14 | + * that the above copyright notice appear in all copies and that both that |
| 15 | + * copyright notice and this permission notice appear in supporting |
| 16 | + * documentation, and that the name of Stanford not be used in advertising or |
| 17 | + * publicity pertaining to distribution of the software without specific, |
| 18 | + * written prior permission. Stanford makes no representations about |
| 19 | + * the suitability of this software for any purpose. It is provided "as is" |
| 20 | + * without express or implied warranty. |
| 21 | + * |
| 22 | + * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 23 | + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. |
| 24 | + * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 25 | + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 26 | + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 27 | + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 28 | + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 29 | + */ |
| 30 | + |
| 31 | +/* |
| 32 | + * HTList implementation. |
| 33 | + */ |
| 34 | + |
| 35 | +#include <stdio.h> |
| 36 | +#include <OS/enter-scope.h> |
| 37 | +#include <htlist.h> |
| 38 | + |
| 39 | +/*****************************************************************************/ |
| 40 | + |
| 41 | +HTList::HTList(void* p) { |
| 42 | + _next = this; |
| 43 | + _prev = this; |
| 44 | + _object = p; |
| 45 | +} |
| 46 | + |
| 47 | +HTList::~HTList() { |
| 48 | + HTList* next = _next; |
| 49 | + if (next != this && next != NULL) { |
| 50 | + Remove(this); |
| 51 | + delete next; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void HTList::Append(HTList* e) { |
| 56 | + _prev->_next = e; |
| 57 | + e->_prev = _prev; |
| 58 | + e->_next = this; |
| 59 | + _prev = e; |
| 60 | +} |
| 61 | + |
| 62 | +void HTList::Prepend(HTList* e) { |
| 63 | + _next->_prev = e; |
| 64 | + e->_prev = this; |
| 65 | + e->_next = _next; |
| 66 | + _next = e; |
| 67 | +} |
| 68 | + |
| 69 | +void HTList::Remove(HTList* e) { |
| 70 | + e->_prev->_next = e->_next; |
| 71 | + e->_next->_prev = e->_prev; |
| 72 | + e->_prev = e->_next = NULL; |
| 73 | +} |
| 74 | + |
| 75 | +void HTList::Remove() { |
| 76 | + if (_prev) { |
| 77 | + _prev->_next = _next; |
| 78 | + } |
| 79 | + if (_next) { |
| 80 | + _next->_prev = _prev; |
| 81 | + } |
| 82 | + _prev = _next = NULL; |
| 83 | +} |
| 84 | +void HTList::RemoveAll() { |
| 85 | + while (!IsEmpty()) { |
| 86 | + Remove(First()); |
| 87 | + } |
| 88 | +} |
| 89 | +void HTList::Delete(void* p) { |
| 90 | + HTList* e; |
| 91 | + |
| 92 | + e = Find(p); |
| 93 | + if (e != NULL) { |
| 94 | + Remove(e); |
| 95 | + delete e; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +HTList* HTList::Find(void* p) { |
| 100 | + HTList* e; |
| 101 | + |
| 102 | + for (e = _next; e != this; e = e->_next) { |
| 103 | + if (e->_object == p) { |
| 104 | + return e; |
| 105 | + } |
| 106 | + } |
| 107 | + return NULL; |
| 108 | +} |
| 109 | + |
| 110 | +HTList* HTList::operator[](int count) { |
| 111 | + HTList* pos = First(); |
| 112 | + int i; |
| 113 | + |
| 114 | + for (i = 1; i < count && pos != End(); ++i) { |
| 115 | + pos = pos->Next(); |
| 116 | + } |
| 117 | + if (i == count) { |
| 118 | + return pos; |
| 119 | + } |
| 120 | + return NULL; |
| 121 | +} |
0 commit comments