source: libcf/trunk/src/cf_list.c@ 115

Last change on this file since 115 was 115, checked in by cheese, 11 years ago

#1 add comments for CF_List_Insert ()

File size: 6.7 KB
Line 
1/**
2 * @file cf_list.c
3 * @author myusgun <myusgun@gmail.com>
4 */
5#include "cf_list.h"
6#include "cf_local.h"
7#include "cf_error.h"
8
9#include <stdlib.h>
10
11#define ASSERT_CTX(__ctx) \
12 if (__ctx == NULL) \
13 return CF_ERROR_DS_INVALID_CTX
14
15#define ASSERT_TRAVERSER(__trav) \
16 if (__trav == NULL) \
17 return CF_ERROR_DS_INVALID_TRAVERSER
18
19#define ASSERT_ARGS(x) \
20 if ((x)) \
21 return CF_ERROR_DS_INVALID_ARGS
22
23/** 리스트 노드 (CF_Traverser의 구현) */
24typedef struct __cf_node__
25{
26 void * element;
27 struct __cf_node__ * prev;
28 struct __cf_node__ * next;
29} CF_NODE;
30
31/** 리스트 컨텍스트 (CF_List_Ctx의 구현) */
32typedef struct __cf_list__
33{
34 int size;
35 CF_NODE * front;
36 CF_NODE * rear;
37} CF_LIST_CTX;
38
39/**
40 * 리스트 컨텍스트 생성
41 *
42 * @return 성공 시, CF_OK; 실패 시, 오류 코드
43 *
44 * @param ctx 리스트 컨텍스트 포인터
45 */
46int
47CF_List_CreateCtx (CF_List_Ctx * ctx)
48{
49 CF_LIST_CTX * context = NULL;
50
51 ASSERT_ARGS (ctx == NULL);
52
53 context = (CF_LIST_CTX *) calloc (sizeof (CF_LIST_CTX), 1);
54 if (context == NULL)
55 return CF_ERROR_DS_CREATE_CTX;
56
57 *ctx = (CF_List_Ctx) context;
58
59 return CF_OK;
60}
61
62/**
63 * 리스트 컨텍스트 해제
64 *
65 * @return 성공 시, CF_OK; 실패 시, 오류 코드
66 *
67 * @param ctx 리스트 컨텍스트
68 */
69int
70CF_List_DestroyCtx (CF_List_Ctx ctx)
71{
72 CF_LIST_CTX * context = (CF_LIST_CTX *) ctx;
73
74 ASSERT_CTX (ctx);
75
76 CF_List_RemoveAll (ctx);
77
78 free (context);
79
80 return CF_OK;
81}
82
83/**
84 * 리스트 탐색자(Traverser)를 Front 위치로 설정
85 *
86 * @return 성공 시, CF_OK; 실패 시, 오류 코드
87 *
88 * @param ctx 리스트 컨텍스트
89 * @param traverser 리스트 탐색자 주소
90 */
91int
92CF_List_Front (CF_List_Ctx ctx,
93 CF_Traverser * traverser)
94{
95 CF_LIST_CTX * context = (CF_LIST_CTX *) ctx;
96
97 ASSERT_CTX (ctx);
98 ASSERT_TRAVERSER (traverser);
99
100 *traverser = (CF_Traverser *) context->front;
101
102 return CF_OK;
103}
104
105/**
106 * 리스트 탐색자(Traverser)를 Rear 위치로 설정
107 *
108 * @return 성공 시, CF_OK; 실패 시, 오류 코드
109 *
110 * @param ctx 리스트 컨텍스트
111 * @param traverser 리스트 탐색자 주소
112 */
113int
114CF_List_Rear (CF_List_Ctx ctx,
115 CF_Traverser * traverser)
116{
117 CF_LIST_CTX * context = (CF_LIST_CTX *) ctx;
118
119 ASSERT_CTX (ctx);
120 ASSERT_TRAVERSER (traverser);
121
122 *traverser = (CF_Traverser *) context->rear;
123
124 return CF_OK;
125}
126
127/**
128 * 리스트에 새 데이터 삽입
129 *
130 * @return 성공 시, CF_OK; 실패 시, 오류 코드
131 *
132 * @param ctx 리스트 컨텍스트
133 * @param traverser 리스트 탐색자
134 * @param direction 탐색자의 전/후를 지정
135 * @param element 삽입할 데이터 주소
136 *
137 * @remark
138 * traverser가 NULL일 때,
139 * direction이 CF_DIRECTION_BEFORE라면 Front 위치이고
140 * direction이 CF_DIRECTION_AFTER라면 Rear 위치에 삽입
141 */
142int
143CF_List_Insert (CF_List_Ctx ctx,
144 const CF_Traverser traverser,
145 const CF_DIRECTION direction,
146 const void * element)
147{
148 CF_LIST_CTX * context = (CF_LIST_CTX *) ctx;
149 CF_NODE * node = NULL;
150 CF_NODE * newnode = NULL;
151
152 ASSERT_CTX (ctx);
153
154 newnode = (CF_NODE *) calloc (sizeof (CF_NODE), 1);
155 if (newnode == NULL)
156 return CF_ERROR_DS_CREATE_NODE;
157
158 newnode->element = (void *) element;
159
160 TRY
161 {
162 if (CF_List_GetSize (ctx) == 0)
163 {
164 context->front = context->rear = newnode;
165 TRY_BREAK;
166 }
167
168 if (direction == CF_DIRECTION_BEFORE)
169 {
170 node = traverser ? (CF_NODE *) traverser : context->front;
171
172 newnode->next = node;
173 if (node)
174 {
175 newnode->prev = node->prev;
176 node->prev = newnode;
177 if (newnode->prev)
178 newnode->prev->next = newnode;
179 }
180
181 if (node == context->front)
182 context->front = newnode;
183 }
184 else /* if (direction == CF_DIRECTION_AFTER) */
185 {
186 node = traverser ? (CF_NODE *) traverser : context->rear;
187
188 newnode->prev = node;
189 if (node)
190 {
191 newnode->next = node->next;
192 node->next = newnode;
193 if (newnode->next)
194 newnode->next->prev = newnode;
195 }
196
197 if (node == context->rear)
198 context->rear = newnode;
199 }
200 } NO_CATCH;
201
202 context->size++;
203
204 return CF_OK;
205}
206
207/**
208 * 리스트에서 탐색자 위치의 항목을 삭제
209 *
210 * @return 성공 시, CF_OK; 실패 시, 오류 코드
211 *
212 * @param ctx 리스트 컨텍스트
213 * @param traverser 리스트 탐색자 주소
214 */
215int
216CF_List_Remove (CF_List_Ctx ctx,
217 CF_Traverser * traverser)
218{
219 CF_LIST_CTX * context = (CF_LIST_CTX *) ctx;
220 CF_NODE * node = NULL;
221
222 ASSERT_CTX (ctx);
223 ASSERT_TRAVERSER (traverser);
224 ASSERT_ARGS (*traverser == NULL);
225
226 node = (CF_NODE *) *traverser;
227
228 if (node->prev)
229 {
230 node->prev->next = node->next;
231 if (node == context->rear)
232 context->rear = node->prev;
233 }
234
235 if (node->next)
236 {
237 node->next->prev = node->prev;
238 if (node == context->front)
239 context->front = node->next;
240 }
241
242 free (node);
243
244 *traverser = NULL;
245
246 context->size--;
247
248 if (CF_List_GetSize (ctx) == 0)
249 context->front = context->rear = NULL;
250
251 return CF_OK;
252}
253
254/**
255 * 리스트에서 모든 항목을 삭제
256 *
257 * @return 성공 시, CF_OK; 실패 시, 오류 코드
258 *
259 * @param ctx 리스트 컨텍스트
260 */
261int
262CF_List_RemoveAll (CF_List_Ctx ctx)
263{
264 CF_Traverser traverser = NULL;
265
266 ASSERT_CTX (ctx);
267
268 while (CF_List_GetSize (ctx) > 0)
269 {
270 CF_List_Front (ctx, &traverser);
271 CF_List_Remove (ctx, &traverser);
272 }
273
274 return CF_OK;
275}
276
277/**
278 * 탐색자 위치의 데이터를 가져옴
279 *
280 * @return 성공 시, CF_OK; 실패 시, 오류 코드
281 *
282 * @param traverser 리스트 탐색자
283 * @param element 데이터 주소
284 */
285int
286CF_List_GetElement (const CF_Traverser traverser,
287 void ** element)
288{
289 CF_NODE * node = (CF_NODE *) traverser;
290
291 ASSERT_TRAVERSER (traverser);
292 ASSERT_ARGS (element == NULL);
293
294 *element = node->element;
295
296 return CF_OK;
297}
298
299/**
300 * 탐색자 위치를 이전으로 이동
301 *
302 * @return 성공 시, CF_OK; 실패 시, 오류 코드
303 *
304 * @param traverser 리스트 탐색자 주소
305 */
306int
307CF_List_Prev (CF_Traverser * traverser)
308{
309 CF_NODE * node = (CF_NODE *) *traverser;
310
311 ASSERT_TRAVERSER (traverser);
312 ASSERT_ARGS (*traverser == NULL);
313
314 node = node->prev;
315 *traverser = (CF_Traverser *) node;
316 if (node == NULL)
317 return CF_ERROR_DS_NO_MORE;
318
319 return CF_OK;
320}
321
322/**
323 * 탐색자 위치를 다음으로 이동
324 *
325 * @return 성공 시, CF_OK; 실패 시, 오류 코드
326 *
327 * @param traverser 리스트 탐색자 주소
328 */
329int
330CF_List_Next (CF_Traverser * traverser)
331{
332 CF_NODE * node = (CF_NODE *) *traverser;
333
334 ASSERT_TRAVERSER (traverser);
335 ASSERT_ARGS (*traverser == NULL);
336
337 node = node->next;
338 *traverser = (CF_Traverser *) node;
339 if (node == NULL)
340 return CF_ERROR_DS_NO_MORE;
341
342 return CF_OK;
343}
344
345/**
346 * 리스트에 등록된 항목의 수를 가져옴
347 *
348 * @return 성공 시, 항목 수; 실패 시, 오류 코드
349 *
350 * @param ctx 리스트 컨텍스트
351 */
352int
353CF_List_GetSize (CF_List_Ctx ctx)
354{
355 CF_LIST_CTX * context = (CF_LIST_CTX *) ctx;
356
357 ASSERT_CTX (ctx);
358
359 return context->size;
360}
Note: See TracBrowser for help on using the repository browser.