OSDN Git Service

upgraded version
[ccunit/ccunit.git] / src / ccunit / CCUnitList.c
1 /* Copyright (C) 2003 TSUTSUMI Kikuo.
2    This file is part of the CCUnit Library.
3
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.
8
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.
13
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.  
18 */
19
20 /*
21  * $Id$
22  */
23
24 /**
25  * @file
26  * Linked list module implementation.
27  */
28 #include <ccunit/CCUnitList.h>
29
30 /** @addtogroup CCUnitList
31  * @{
32  */
33
34 /** @defgroup CCUnitListCell ListCell
35  * Linked list cell.
36  * @{
37  */
38
39 /**
40  * List Cell class.
41  */
42 typedef struct CCUnitListCell
43 {
44   struct CCUnitListCell* next;                  /**< next cell pointer  */
45   void* contents;                               /**< list content object  */
46 } CCUnitListCell;
47
48 /**
49  * create new list cell object.
50  * @return new list cell object.
51  */
52 static inline CCUnitListCell* ccunit_newListCell ()
53 {
54   return calloc (1, sizeof (CCUnitListCell));
55 }
56
57 /**
58  * delete list cell object.
59  * @param cell deleting cell.
60  */
61 static inline void ccunit_deleteListCell (CCUnitListCell* cell)
62 {
63   safe_free (cell);
64 }
65
66 /** @} */
67
68 CCUnitList* ccunit_initList (CCUnitList* list)
69 {
70   list->length = 0;
71   list->head = NULL;
72   list->tailp = &list->head;
73   list->isAllocated = false;
74   return list;
75 }
76
77 inline CCUnitList* ccunit_newList ()
78 {
79   CCUnitList* newList = calloc (1, sizeof (*newList));
80   if (!newList)
81     return NULL;
82   ccunit_initList (newList);
83   newList->isAllocated = true;
84   return newList;
85 }
86
87 void ccunit_addList (CCUnitList* list, void* contents)
88 {
89   CCUnitListCell* cell;
90   if (!list)
91     return;
92   cell = ccunit_newListCell ();
93   if (!cell)
94     return;
95   cell->contents = contents;
96   cell->next = NULL;
97   *list->tailp = cell;
98   list->tailp = &cell->next;
99   list->length ++;
100 }
101
102 void ccunit_deleteList (CCUnitList* list, void (*deleteContents)(void*))
103 {
104   CCUnitListCell* cell;
105   while (list->head)
106     {
107       cell = list->head;
108       if (deleteContents && cell->contents)
109         deleteContents (cell->contents);
110       list->head = cell->next;
111       ccunit_deleteListCell (cell);
112     }
113   if (list->isAllocated)
114     safe_free (list);
115 }
116
117 inline CCUnitListIterator* ccunit_initListIterator (const struct CCUnitList* list,
118                                                     struct CCUnitListIterator* it)
119 {
120   it->current = list->head;
121   it->isAllocated = false;
122   return it;
123 }
124
125 CCUnitListIterator* ccunit_newListIterator (const struct CCUnitList* list)
126 {
127   CCUnitListIterator* it;
128   if (!list)
129     return NULL;
130   it = calloc (1, sizeof (*it));
131   if (!it)
132     return NULL;
133   ccunit_initListIterator (list, it);
134   it->isAllocated = true;
135   return it;
136 }
137
138 bool ccunit_hasNextListIterator (struct CCUnitListIterator* it)
139 {
140   if (!it)
141     return false;
142   return it->current != NULL;
143 }
144
145 void* ccunit_nextListIterator (struct CCUnitListIterator* it)
146 {
147   void* contents;
148   if (!ccunit_hasNextListIterator (it))
149     return NULL;
150   contents = it->current->contents;
151   it->current = it->current->next;
152   return contents;
153 }
154
155 inline void ccunit_deleteListIterator (struct CCUnitListIterator* it)
156 {
157   if (it && it->isAllocated)
158     safe_free (it);
159 }
160
161 /** @} */