1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
Copyright (C) 2000 by David Durant
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cssysdef.h"
#include "csutil/csdllist.h"
csDLinkList::csDLinkList()
{
firstItem = NULL;
currentItem = NULL;
}
csDLinkList::~csDLinkList()
{
csDLListItem *nextItem;
if (firstItem == NULL)
return;
currentItem = firstItem->nextItem;
while (currentItem != firstItem)
{
nextItem = currentItem->nextItem;
delete currentItem;
currentItem = nextItem;
}
delete firstItem;
}
void csDLinkList::RemoveItem(void *theObj)
{
currentItem = FindListItem(theObj);
RemoveItem();
}
csDLListItem * csDLinkList::FindListItem(void *anObj)
{
csDLListItem *listItemPtr;
listItemPtr = firstItem;
if (firstItem == NULL)
return NULL;
if (anObj == listItemPtr->theObject)
return listItemPtr;
while ((listItemPtr = listItemPtr->nextItem) != firstItem)
{
if (anObj == listItemPtr->theObject)
return listItemPtr;
}
return NULL;
}
void * csDLinkList::SetCurrentItem(void *theObj)
{
csDLListItem *it = FindListItem (theObj);
if (it)
{
currentItem = it;
return currentItem;
}
else
return NULL;
}
bool csDLinkList::AddItem(void *theObj)
{
csDLListItem *newItem;
newItem = new csDLListItem;
if (!newItem)
return false;
newItem->theObject = theObj;
if (firstItem == NULL)
{
// this is the first object in the list...
firstItem = newItem;
currentItem = newItem;
newItem->prevItem = newItem;
newItem->nextItem = newItem;
}
else
{
// this is NOT the first object in the list...
newItem->prevItem = firstItem->prevItem;
newItem->nextItem = firstItem;
firstItem->prevItem->nextItem = newItem;
firstItem->prevItem = newItem;
}
return true;
}
bool csDLinkList::AddCurrentItem(void *theObj)
{
csDLListItem *newItem;
if(!currentItem || !firstItem) return false;
newItem = new csDLListItem;
if (!newItem)
return false;
newItem->theObject = theObj;
newItem->nextItem = currentItem->nextItem;
currentItem->nextItem = newItem;
newItem->prevItem = currentItem;
newItem->nextItem->prevItem = newItem;
return true;
}
void csDLinkList::RemoveItem()
{
if (!currentItem)
return;
if (currentItem->nextItem == currentItem)
{
// this is the last object in the list
firstItem = NULL;
currentItem = NULL;
delete currentItem;
}
else
{
// this is NOT the last object in the list
csDLListItem *deadItem;
deadItem = currentItem;
currentItem->nextItem->prevItem = currentItem->prevItem;
currentItem->prevItem->nextItem = currentItem->nextItem;
if (firstItem == currentItem)
firstItem = currentItem->nextItem;
currentItem = currentItem->nextItem;
delete deadItem;
}
}
void * csDLinkList::GetFirstItem()
{
if (!currentItem)
return NULL;
currentItem = firstItem;
return currentItem->theObject;
}
void * csDLinkList::PeekFirstItem()
{
if (!firstItem)
return NULL;
return firstItem->theObject;
}
void * csDLinkList::GetPrevItem()
{
if (!currentItem)
return NULL;
currentItem = currentItem->prevItem;
return currentItem->theObject;
}
void * csDLinkList::GetNextItem()
{
if (!currentItem)
return NULL;
currentItem = currentItem->nextItem;
return currentItem->theObject;
}
void * csDLinkList::GetCurrentItem()
{
if (!currentItem)
return NULL;
return currentItem->theObject;
}
|