File: rdf_list.c

package info (click to toggle)
redland 1.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 27,592 kB
  • ctags: 12,328
  • sloc: ansic: 79,017; xml: 25,115; sh: 10,162; yacc: 5,985; lex: 3,682; makefile: 3,260; perl: 2,814; cpp: 59
file content (598 lines) | stat: -rw-r--r-- 13,323 bytes parent folder | download
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * rdf_list.c - RDF List Implementation
 *
 * $Id: rdf_list.c 13287 2007-12-20 12:00:50Z laalto $
 *
 * Copyright (C) 2000-2006, David Beckett http://purl.org/net/dajobe/
 * Copyright (C) 2000-2004, University of Bristol, UK http://www.bristol.ac.uk/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */


#ifdef HAVE_CONFIG_H
#include <rdf_config.h>
#endif

#ifdef WIN32
#include <win32_rdf_config.h>
#endif

#include <stdio.h>
#include <string.h>

#include <sys/types.h>

#include <redland.h>

#include <rdf_list_internal.h>


/* prototypes for local functions */
static librdf_list_node* librdf_list_find_node(librdf_list* list, void *data);

static int librdf_list_iterator_is_end(void* iterator);
static int librdf_list_iterator_next_method(void* iterator);
static void* librdf_list_iterator_get_method(void* iterator, int flags);
static void librdf_list_iterator_finished(void* iterator);

static void librdf_list_iterators_replace_node(librdf_list* list, librdf_list_node* old_node, librdf_list_node* new_node);


/* helper functions */
static librdf_list_node*
librdf_list_find_node(librdf_list* list, void *data)
{
  librdf_list_node* node;
  
  for(node=list->first; node; node=node->next) {
    if(list->equals) {
      if(list->equals(node->data, data))
        break;
    } else {
      if(node->data == data)
        break;
    }
  }
  return node;
}


/**
 * librdf_new_list:
 * @world: redland world object
 *
 * Constructor - create a new #librdf_list.
 * 
 * Return value: a new #librdf_list or NULL on failure
 **/
librdf_list*
librdf_new_list(librdf_world *world)
{
  librdf_list* new_list;
  
  librdf_world_open(world);

  new_list=(librdf_list*)LIBRDF_CALLOC(librdf_list, 1, sizeof(librdf_list));
  if(!new_list)
    return NULL;
  
  new_list->world=world;
  
  return new_list;
}


/**
 * librdf_free_list:
 * @list: #librdf_list object
 *
 * Destructor - destroy a #librdf_list object.
 * 
 **/
void
librdf_free_list(librdf_list* list) 
{
  LIBRDF_ASSERT_RETURN(list->iterator_count,
                       "Iterators were active on freeing list", );

  librdf_list_clear(list);
  LIBRDF_FREE(librdf_list, list);
}


/**
 * librdf_list_clear:
 * @list: #librdf_list object
 *
 * Empty an librdf_list.
 * 
 **/
void
librdf_list_clear(librdf_list* list) 
{
  librdf_list_node *node, *next;
  
  for(node=list->first; node; node=next) {
    next=node->next;
    LIBRDF_FREE(librdf_list_node, node);
  }
}


/**
 * librdf_list_add:
 * @list: #librdf_list object
 * @data: the data value
 *
 * Add a data item to the end of a librdf_list.
 * 
 * Equivalent to the list 'push' notion, thus if librdf_list_pop()
 * is called after this, it will return the value added here.
 *
 * Return value: non 0 on failure
 **/
int
librdf_list_add(librdf_list* list, void *data) 
{
  librdf_list_node* node;
  
  /* need new node */
  node=(librdf_list_node*)LIBRDF_CALLOC(librdf_list_node, 1,
                                        sizeof(librdf_list_node));
  if(!node)
    return 1;
  
  node->data=data;

  /* if there is a list, connect the new node to the last node  */
  if(list->last) {
    node->prev=list->last;
    list->last->next=node;
  }

  /* make this node the last node always */
  list->last=node;
  
  /* if there is no list at all, make this the first to */
  if(!list->first)
    list->first=node;

  /* node->next = NULL implicitly */

  list->length++;
  return 0;
}


/**
 * librdf_list_unshift:
 * @list: #librdf_list object
 * @data: the data value
 *
 * Add a data item to the start of a librdf_list.
 * 
 * if librdf_list_shift() is called after this, it will return the value
 * added here.
 *
 * Return value: non 0 on failure
 **/
int
librdf_list_unshift(librdf_list* list, void *data) 
{
  librdf_list_node* node;
  
  /* need new node */
  node=(librdf_list_node*)LIBRDF_CALLOC(librdf_list_node, 1,
                                        sizeof(librdf_list_node));
  if(!node)
    return 1;
  
  node->data=data;

  /* if there is a list, connect the new node to the first node  */
  if(list->first) {
    node->next=list->first;
    list->first->prev=node;
  }

  /* make this node the first node always */
  list->first=node;
  
  /* if there is no list at all, make this the last too */
  if(!list->last)
    list->last=node;

  /* node->next = NULL implicitly */

  list->length++;
  return 0;
}


/**
 * librdf_list_remove:
 * @list: #librdf_list object
 * @data: the data item
 *
 * Remove a data item from an librdf_list.
 * 
 * The search is done using the 'equals' function which may be set
 * by librdf_list_set_equals() or by straight comparison of pointers
 * if not set.
 * 
 * Return value: the data stored or NULL on failure (not found or list empty)
 **/
void *
librdf_list_remove(librdf_list* list, void *data) 
{
  librdf_list_node *node;
  
  node=librdf_list_find_node(list, data);
  if(!node)
    /* not found */
    return NULL;

  librdf_list_iterators_replace_node(list, node, node->next);
  
  if(node == list->first)
    list->first=node->next;
  if(node->prev)
    node->prev->next=node->next;

  if(node == list->last)
    list->last=node->prev;
  if(node->next)
    node->next->prev=node->prev;

  /* retrieve actual stored data */
  data=node->data;
  
  /* free node */
  LIBRDF_FREE(librdf_list_node, node);
  list->length--;

  return data;
}


/**
 * librdf_list_shift:
 * @list: #librdf_list object
 *
 * Remove and return the data at the start of the list.
 * 
 * Return value: the data object or NULL if the list is empty
 **/
void*
librdf_list_shift(librdf_list* list)
{
  librdf_list_node *node;
  void *data;

  node=list->first;
  if(!node)
    return NULL;
     
  list->first=node->next;

  if(list->first)
    /* if list not empty, fix pointers */
    list->first->prev=NULL;
  else
    /* list is now empty, zap last pointer */
    list->last=NULL;
  
  /* save data */
  data=node->data;

  /* free node */
  LIBRDF_FREE(librdf_list_node, node);

  list->length--;
  return data;
}


/**
 * librdf_list_pop:
 * @list: #librdf_list object
 *
 * Remove and return the data at the end of the list.
 * 
 * Return value: the data object or NULL if the list is empty
 **/
void*
librdf_list_pop(librdf_list* list)
{
  librdf_list_node *node;
  void *data;

  node=list->last;
  if(!node)
    return NULL;
     
  list->last=node->prev;

  if(list->last)
    /* if list not empty, fix pointers */
    list->last->next=NULL;
  else
    /* list is now empty, zap last pointer */
    list->first=NULL;
  
  /* save data */
  data=node->data;

  /* free node */
  LIBRDF_FREE(librdf_list_node, node);

  list->length--;
  return data;
}


/**
 * librdf_list_contains:
 * @list: #librdf_list object
 * @data: the data value
 *
 * Check for presence of data item in list.
 * 
 * The search is done using the 'equals' function which may be set
 * by librdf_list_set_equals() or by straight comparison of pointers
 * if not set.
 * 
 * Return value: non 0 if item was found
 **/
int
librdf_list_contains(librdf_list* list, void *data) 
{
  librdf_list_node *node;
  
  node=librdf_list_find_node(list, data);
  return (node != NULL);
}


/**
 * librdf_list_size:
 * @list: #librdf_list object
 *
 * Return the length of the list.
 * 
 * Return value: length of the list
 **/
int
librdf_list_size(librdf_list* list) 
{
  return list->length;
}


/**
 * librdf_list_set_equals:
 * @list: #librdf_list object
 * @equals: the equals function
 *
 * Set the equals function for the list.
 * 
 * The function given is used when comparing items in the list
 * during searches such as those done in librdf_list_remove() or
 * librdf_list_contains().
 * 
 **/
void
librdf_list_set_equals(librdf_list* list, 
                       int (*equals) (void* data1, void *data2)) 
{
  list->equals=equals;
}


static void
librdf_list_add_iterator_context(librdf_list* list, 
                                 librdf_list_iterator_context* node)
{
  if(list->last_iterator) {
    node->prev_ic=list->last_iterator;
    list->last_iterator->next_ic=node;
  }

  list->last_iterator=node;
  
  if(!list->first_iterator)
    list->first_iterator=node;

  list->iterator_count++;
#if LIBRDF_DEBUG > 2
  LIBRDF_DEBUG4("Added iterator %p to list %p giving %d iterators\n",
                node->iterator, list, list->iterator_count);
#endif
}


static void
librdf_list_remove_iterator_context(librdf_list* list,
                                    librdf_list_iterator_context* node)
{
  if(node == list->first_iterator)
    list->first_iterator=node->next_ic;
  if(node->prev_ic)
    node->prev_ic->next_ic=node->next_ic;

  if(node == list->last_iterator)
    list->last_iterator=node->prev_ic;
  if(node->next_ic)
    node->next_ic->prev_ic=node->prev_ic;

  list->iterator_count--;
#if LIBRDF_DEBUG > 2
  LIBRDF_DEBUG4("Removed iterator %p from list %p leaving %d iterators\n",
                node->iterator, list, list->iterator_count);
#endif
}


static void
librdf_list_iterators_replace_node(librdf_list* list, 
                                   librdf_list_node* old_node,
                                   librdf_list_node* new_node)
{
  librdf_list_iterator_context *node, *next;
  
  if(!list->iterator_count)
    return;
  
  for(node=list->first_iterator; node; node=next) {
    next=node->next_ic;
    if(node->next == old_node) {
#if LIBRDF_DEBUG > 2
      LIBRDF_DEBUG3("Moved iterator %p pointing from next node %p to %p\n", 
                    node->iterator, old_node, new_node);
#endif
      node->next = new_node;
    }
  }
}



/**
 * librdf_list_get_iterator:
 * @list: #librdf_list object
 *
 * Get an iterator for the list.
 * 
 * Return value: a new #librdf_iterator object or NULL on failure
 **/
librdf_iterator*
librdf_list_get_iterator(librdf_list* list)
{
  librdf_list_iterator_context* context;
  librdf_iterator* iterator;

  context=(librdf_list_iterator_context*)LIBRDF_CALLOC(librdf_list_iterator_context, 1, sizeof(librdf_list_iterator_context));
  if(!context)
    return NULL;

  context->list=list;
  context->current=list->first;
  context->next=context->current != NULL ? context->current->next : NULL;

  /* librdf_list_iterator_finished() calls librdf_list_remove_iterator_context(),
   * librdf_list_iterator_finished() is called if librdf_new_iterator() fails */
  librdf_list_add_iterator_context(list, context);

  iterator=librdf_new_iterator(list->world, 
                               (void*)context,
                               librdf_list_iterator_is_end,
                               librdf_list_iterator_next_method,
                               librdf_list_iterator_get_method,
                               librdf_list_iterator_finished);
  if(!iterator)
    librdf_list_iterator_finished(context);
  else
    context->iterator=iterator;

  return iterator;
}


static int
librdf_list_iterator_is_end(void* iterator) 
{
  librdf_list_iterator_context* context=(librdf_list_iterator_context*)iterator;
  librdf_list_node *node=context->current;
  
  return (node == NULL);
}


static int
librdf_list_iterator_next_method(void* iterator) 
{
  librdf_list_iterator_context* context=(librdf_list_iterator_context*)iterator;
  librdf_list_node *node=context->current;
  
  if(!node)
    return 1;
  
  context->current = context->next;
  context->next = context->current != NULL ? context->current->next : NULL;
  
  return (context->current == NULL);
}


static void*
librdf_list_iterator_get_method(void* iterator, int flags) 
{
  librdf_list_iterator_context* context=(librdf_list_iterator_context*)iterator;
  librdf_list_node *node=context->current;
  
  if(!node)
    return NULL;

  if(flags == LIBRDF_ITERATOR_GET_METHOD_GET_OBJECT)
    return node->data;

  librdf_log(context->list->world,
             0, LIBRDF_LOG_ERROR, LIBRDF_FROM_LIST, NULL,
             "Unsupported iterator method flag %d", flags);
  return NULL;
}


static void
librdf_list_iterator_finished(void* iterator)
{
  librdf_list_iterator_context* context=(librdf_list_iterator_context*)iterator;

  if(!context)
    return;
  
  librdf_list_remove_iterator_context(context->list, context);

  LIBRDF_FREE(librdf_list_iterator_context, context);
}



/**
 * librdf_list_foreach:
 * @list: #librdf_list object
 * @fn: pointer to function to apply that takes data pointer and user data parameters
 * @user_data: user data for applied function 
 *
 * Apply a function for each data item in a librdf_list.
 * 
 **/
void
librdf_list_foreach(librdf_list* list, void (*fn)(void *, void *),
                    void *user_data) 
{
  librdf_list_node *node, *next;
  
  for(node=list->first; node; node=next) {
    next=node->next;
    (*fn)(node->data, user_data);
  }
}