File: Obj.c

package info (click to toggle)
xfireworks 1.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 416 kB
  • ctags: 465
  • sloc: ansic: 3,110; makefile: 122; sh: 52
file content (396 lines) | stat: -rw-r--r-- 9,925 bytes parent folder | download | duplicates (11)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*****************************************************************************/
/* Obj.c - A library for object list.                                        */
/*                                                                           */
/* Obj.c Copyright (c) 2000 Sakai Hiroaki.                                   */
/* All Rights Reserved.                                                      */
/*****************************************************************************/

#include "ObjP.h"

/*****************************************************************************/
/* ObjListData                                                       */
/*****************************************************************************/

static ObjListData ObjListData_Create(Obj obj, Obj (*destructor)())
{
  ObjListData list_data;

  list_data = (ObjListData)malloc(sizeof(_ObjListData));

  list_data->obj = obj;
  list_data->destructor = destructor;

  return (list_data);
}

static ObjListData ObjListData_Destroy(ObjListData list_data)
{
  if (list_data == NULL) return (NULL);

  /* ǥȥ饯μ¹ */
  if (list_data->destructor)
    (*(list_data->destructor))(list_data->obj);

  free(list_data);

  return (NULL);
}

/*****************************************************************************/
/* ObjList ֥Ȥ                                              */
/*****************************************************************************/

Obj ObjListData_GetObj(ObjListData data)
{
  if (data == NULL) return (NULL);
  return (data->obj);
}

Obj ObjListData_GetPrev(ObjListData data)
{
  if (data == NULL) return (NULL);
  return (data->prev);
}

Obj ObjListData_GetNext(ObjListData data)
{
  if (data == NULL) return (NULL);
  return (data->next);
}

int ObjList_GetLength(ObjList list)
{
  if (list == NULL) return (-1);
  return (list->length);
}

ObjListData ObjList_GetStartEdge(ObjList list)
{
  if (list == NULL) return (NULL);
  return (list->start_edge);
}

ObjListData ObjList_GetEndEdge(ObjList list)
{
  if (list == NULL) return (NULL);
  return (list->end_edge);
}

ObjListData ObjList_GetStart(ObjList list)
{
  if (list == NULL) return (NULL);
  return (list->start_edge->next);
}

ObjListData ObjList_GetEnd(ObjList list)
{
  if (list == NULL) return (NULL);
  return (list->end_edge->prev);
}

int ObjList_IsEmpty(ObjList list)
{
  if (list == NULL) return (1);
  return (list->start_edge->next == list->end_edge);
}

int ObjList_IsStartEdge(ObjList list, ObjListData data)
{
  if (list == NULL) return (0);
  return (data == list->start_edge);
}

int ObjList_IsEndEdge(ObjList list, ObjListData data)
{
  if (list == NULL) return (0);
  return (data == list->end_edge);
}

int ObjList_IsStart(ObjList list, ObjListData data)
{
  if (list == NULL) return (0);
  return (data == list->start_edge->next);
}

int ObjList_IsEnd(ObjList list, ObjListData data)
{
  if (list == NULL) return (0);
  return (data == list->end_edge->prev);
}

ObjListData ObjList_InsertObjToPrev(ObjList list, ObjListData current,
				    Obj obj, Obj (*destructor)())
{
  ObjListData data;

  if (list == NULL) return (NULL);
  if (ObjList_IsStartEdge(list, current)) return (NULL);

  data = ObjListData_Create(obj, destructor);
  if (data == NULL) return (NULL);

  data->prev = current->prev;
  data->next = current;
  current->prev->next = data;
  current->prev = data;
  (list->length)++;

  return (data);
}

ObjListData ObjList_InsertObjToNext(ObjList list, ObjListData current,
				    Obj obj, Obj (*destructor)())
{
  ObjListData data;

  if (list == NULL) return (NULL);
  if (ObjList_IsEndEdge(list, current)) return (NULL);

  data = ObjListData_Create(obj, destructor);
  if (data == NULL) return (NULL);

  data->next = current->next;
  data->prev = current;
  current->next->prev = data;
  current->next = data;
  (list->length)++;

  return (data);
}

ObjListData ObjList_InsertObjToStart(ObjList list, Obj obj,
				     Obj (*destructor)())
{
  ObjListData current;
  current = ObjList_GetStart(list);
  return (ObjList_InsertObjToPrev(list, current, obj, destructor));
}

ObjListData ObjList_InsertObjToEnd(ObjList list, Obj obj,
				   Obj (*destructor)())
{
  ObjListData current;
  current = ObjList_GetEnd(list);
  return (ObjList_InsertObjToNext(list, current, obj, destructor));
}

ObjListData ObjList_DeleteObjToPrev(ObjList list, ObjListData current)
{
  ObjListData ret;

  if (list == NULL) return (NULL);
  if (ObjList_IsStartEdge(list, current) || ObjList_IsEndEdge(list, current))
    return (NULL);

  current->prev->next = current->next;
  current->next->prev = current->prev;

  ret = current->prev;
  ObjListData_Destroy(current);
  (list->length)--;

  return (ret);
}

ObjListData ObjList_DeleteObjToNext(ObjList list, ObjListData current)
{
  ObjListData ret;

  if (list == NULL) return (NULL);
  if (ObjList_IsStartEdge(list, current) || ObjList_IsEndEdge(list, current))
    return (NULL);

  current->prev->next = current->next;
  current->next->prev = current->prev;

  ret = current->next;
  ObjListData_Destroy(current);
  (list->length)--;

  return (ret);
}

ObjListData ObjList_DeleteObjFromStart(ObjList list)
{
  ObjListData current;
  if (list == NULL) return (NULL);
  current = ObjList_GetStart(list);
  return (ObjList_DeleteObjToNext(list, current));
}

ObjListData ObjList_DeleteObjFromEnd(ObjList list)
{
  ObjListData current;
  if (list == NULL) return (NULL);
  current = ObjList_GetEnd(list);
  return (ObjList_DeleteObjToPrev(list, current));
}

ObjListData ObjList_MoveObjToPrev(ObjList list,
				  ObjListData current,
				  ObjListData to)
{
  if (list == NULL) return (NULL);
  return (ObjList_MoveObjToPrevOfOtherList(list, current, list, to));
}

ObjListData ObjList_MoveObjToNext(ObjList list,
				  ObjListData current,
				  ObjListData to)
{
  if (list == NULL) return (NULL);
  return (ObjList_MoveObjToNextOfOtherList(list, current, list, to));
}

ObjListData ObjList_MoveObjToStart(ObjList list, ObjListData current)
{
  if (list == NULL) return (NULL);
  return (ObjList_MoveObjToStartOfOtherList(list, current, list));
}

ObjListData ObjList_MoveObjToEnd(ObjList list, ObjListData current)
{
  if (list == NULL) return (NULL);
  return (ObjList_MoveObjToEndOfOtherList(list, current, list));
}

ObjList ObjList_Create() /* ObjList ֥Ȥ */
{
  ObjList list;

  list = (ObjList)malloc(sizeof(_ObjList));
  if (list == NULL) return (NULL);

  list->start_edge   = ObjListData_Create(NULL, NULL);
  list->end_edge     = ObjListData_Create(NULL, NULL);
  list->length  = 0;    /* ¸ߤƤǡο */

  list->start_edge->prev = NULL;
  list->start_edge->next = list->end_edge;
  list->end_edge->prev   = list->start_edge;
  list->end_edge->next   = NULL;

  return (list);
}

ObjList ObjList_Destroy(ObjList list) /*  */
{
  if (list == NULL) return (NULL);

  while (!ObjList_IsEmpty(list))
    ObjList_DeleteObjFromStart(list);

  if (list->start_edge)
    list->start_edge = ObjListData_Destroy(list->start_edge);
  if (list->end_edge)
    list->end_edge = ObjListData_Destroy(list->end_edge);

  free(list);

  return (NULL);
}

/*===========================================================================*/
/* ʣΥꥹȴ֤Ǥ                                                    */
/*===========================================================================*/

ObjListData ObjList_MoveObjToPrevOfOtherList(ObjList list, ObjListData current,
					     ObjList to_list, ObjListData to)
{
  if (list == NULL) return (NULL);
  if (to_list == NULL) return (NULL);
  if (ObjList_IsStartEdge(list, current) || ObjList_IsEndEdge(list, current))
    return (NULL);

  if (ObjList_IsStartEdge(to_list, to)) return (NULL);
  if ((list == to_list) && (current == to)) return (current);

  current->prev->next = current->next;
  current->next->prev = current->prev;

  current->prev = to->prev;
  current->next = to;
  to->prev->next = current;
  to->prev = current;

  (list->length)--;
  (to_list->length)++;

  return (current);
}

ObjListData ObjList_MoveObjToNextOfOtherList(ObjList list, ObjListData current,
					     ObjList to_list, ObjListData to)
{
  if (list == NULL) return (NULL);
  if (ObjList_IsStartEdge(list, current) || ObjList_IsEndEdge(list, current))
    return (NULL);

  if (ObjList_IsEndEdge(to_list, to)) return (NULL);
  if ((list == to_list) && (current == to)) return (current);

  current->prev->next = current->next;
  current->next->prev = current->prev;

  current->next = to->next;
  current->prev = to;
  to->next->prev = current;
  to->next = current;

  (list->length)--;
  (to_list->length)++;

  return (current);
}

ObjListData ObjList_MoveObjToStartOfOtherList(ObjList list,
					      ObjListData current,
					      ObjList to_list)
{
  ObjListData to;
  if (list == NULL) return (NULL);
  if (to_list == NULL) return (NULL);
  to = ObjList_GetStart(to_list);
  return (ObjList_MoveObjToPrevOfOtherList(list, current, to_list, to));
}

ObjListData ObjList_MoveObjToEndOfOtherList(ObjList list,
					    ObjListData current,
					    ObjList to_list)
{
  ObjListData to;
  if (list == NULL) return (NULL);
  if (to_list == NULL) return (NULL);
  to = ObjList_GetEnd(to_list);
  return (ObjList_MoveObjToNextOfOtherList(list, current, to_list, to));
}

ObjList ObjList_Concatenate(ObjList list1, ObjList list2)
{
  ObjListData tmp;

  if (list1 == NULL) {
    list1 = list2;
    return (list1);
  }
  if (list2 == NULL) return (list1);

  list1->end_edge->prev->next = list2->start_edge->next;
  list2->start_edge->next->prev = list1->end_edge->prev;

  tmp = list1->end_edge;
  list1->end_edge = list2->end_edge;
  list2->end_edge = tmp;

  list2->start_edge->next = list2->end_edge;
  list2->end_edge->prev = list2->start_edge;

  list1->length += list2->length;
  list2->length = 0;

  ObjList_Destroy(list2);

  return (list1);
}

/* End of File. */