1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2 This file is part of the CCUnit Library.
4 The CCUnit Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License
6 as published by the Free Software Foundation; either version 2.1 of
7 the License, or (at your option) any later version.
9 The CCUnit Library is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the CCUnit Library; see the file COPYING.LESSER.
16 If not, write to the Free Software Foundation, Inc., 59 Temple
17 Place - Suite 330, Boston, MA 02111-1307, USA.
26 * Linked list module implementation.
28 #include <ccunit/CCUnitList.h>
30 /** @addtogroup CCUnitList
34 /** @defgroup CCUnitListCell ListCell
42 typedef struct CCUnitListCell
44 struct CCUnitListCell* next; /**< next cell pointer */
45 void* contents; /**< list content object */
49 * create new list cell object.
50 * @return new list cell object.
52 static inline CCUnitListCell* ccunit_newListCell ()
54 return calloc (1, sizeof (CCUnitListCell));
58 * delete list cell object.
59 * @param cell deleting cell.
61 static inline void ccunit_deleteListCell (CCUnitListCell* cell)
68 CCUnitList* ccunit_initList (CCUnitList* list)
72 list->tailp = &list->head;
73 list->isAllocated = false;
77 inline CCUnitList* ccunit_newList ()
79 CCUnitList* newList = calloc (1, sizeof (*newList));
82 ccunit_initList (newList);
83 newList->isAllocated = true;
87 void ccunit_addList (CCUnitList* list, void* contents)
92 cell = ccunit_newListCell ();
95 cell->contents = contents;
98 list->tailp = &cell->next;
102 void ccunit_deleteList (CCUnitList* list, void (*deleteContents)(void*))
104 CCUnitListCell* cell;
108 if (deleteContents && cell->contents)
109 deleteContents (cell->contents);
110 list->head = cell->next;
111 ccunit_deleteListCell (cell);
113 if (list->isAllocated)
117 inline CCUnitListIterator* ccunit_initListIterator (const struct CCUnitList* list,
118 struct CCUnitListIterator* it)
120 it->current = list->head;
121 it->isAllocated = false;
125 CCUnitListIterator* ccunit_newListIterator (const struct CCUnitList* list)
127 CCUnitListIterator* it;
130 it = calloc (1, sizeof (*it));
133 ccunit_initListIterator (list, it);
134 it->isAllocated = true;
138 bool ccunit_hasNextListIterator (struct CCUnitListIterator* it)
142 return it->current != NULL;
145 void* ccunit_nextListIterator (struct CCUnitListIterator* it)
148 if (!ccunit_hasNextListIterator (it))
150 contents = it->current->contents;
151 it->current = it->current->next;
155 inline void ccunit_deleteListIterator (struct CCUnitListIterator* it)
157 if (it && it->isAllocated)