1 : /* -*- mode: C -*-
2 : *
3 : * File: pdf-list.h
4 : * Date: Sat Mar 1 02:14:35 2008
5 : *
6 : * GNU PDF Library - Header file for the List module
7 : *
8 : */
9 :
10 : /* Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc. */
11 :
12 : /* This program is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU General Public License as published by
14 : * the Free Software Foundation, either version 3 of the License, or
15 : * (at your option) any later version.
16 : *
17 : * This program is distributed in the hope that it will be useful,
18 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : * GNU General Public License for more details.
21 : *
22 : * You should have received a copy of the GNU General Public License
23 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : #ifndef PDF_LIST_H
27 : #define PDF_LIST_H
28 :
29 : #include <config.h>
30 :
31 : #include <pdf-types.h>
32 : #include <pdf-error.h>
33 : #include <pdf-alloc.h>
34 :
35 : /* BEGIN PUBLIC */
36 :
37 : /* List module API implementation */
38 :
39 : /* Data types */
40 :
41 : #define PDF_LIST_ITERATOR_SIZE 12
42 :
43 : struct pdf_list_s
44 : {
45 : void *gl_list;
46 : pdf_bool_t allow_duplicates;
47 : };
48 :
49 : struct pdf_list_iterator_s
50 : {
51 : void *gl_iterator[PDF_LIST_ITERATOR_SIZE];
52 : };
53 :
54 : struct pdf_list_node_s
55 : {
56 : void *gl_node;
57 : };
58 :
59 : typedef struct pdf_list_s pdf_list_t;
60 : typedef struct pdf_list_node_s pdf_list_node_t;
61 : typedef struct pdf_list_iterator_s pdf_list_iterator_t;
62 :
63 : typedef pdf_bool_t (*pdf_list_element_equals_fn_t) (const void *elt1, const void *elt2);
64 : typedef pdf_size_t (*pdf_list_element_hashcode_fn_t) (const void *elt);
65 : typedef void (*pdf_list_element_dispose_fn_t) (const void *elt);
66 : typedef int (*pdf_list_element_compar_fn_t) (const void *elt1, const void *elt2);
67 :
68 :
69 : /* END PUBLIC */
70 :
71 : #if ! defined HAVE_INLINE && ! defined COMPILING_PDF_LIST
72 :
73 : /* BEGIN PUBLIC */
74 :
75 : /* Creation and destruction functions */
76 :
77 : pdf_status_t pdf_list_new (pdf_list_element_equals_fn_t equals_fn,
78 : pdf_list_element_dispose_fn_t dispose_fn,
79 : const pdf_bool_t allow_duplicates,
80 : pdf_list_t *list);
81 :
82 : pdf_status_t pdf_list_destroy (pdf_list_t list);
83 :
84 : /* Property management functions */
85 :
86 : pdf_size_t pdf_list_size (const pdf_list_t list);
87 :
88 : /* Element searching functions */
89 :
90 : pdf_status_t pdf_list_search (const pdf_list_t list, const void* element, pdf_list_node_t *node);
91 : pdf_status_t pdf_list_search_from (const pdf_list_t list, const pdf_size_t start_index,
92 : const void* element, pdf_list_node_t *node);
93 : pdf_status_t pdf_list_search_from_to (const pdf_list_t list,
94 : const pdf_size_t start_index,
95 : const pdf_size_t end_index,
96 : const void* element,
97 : pdf_list_node_t *node);
98 : pdf_status_t pdf_list_next_node (const pdf_list_t list, const pdf_list_node_t node, pdf_list_node_t *next);
99 : pdf_status_t pdf_list_previous_node (const pdf_list_t list, const pdf_list_node_t node, pdf_list_node_t *prev);
100 : pdf_status_t pdf_list_indexof (const pdf_list_t list, const void *element, pdf_size_t *position);
101 : pdf_status_t pdf_list_indexof_from (const pdf_list_t list, const pdf_size_t start_index,
102 : const void *element, pdf_size_t *position);
103 : pdf_status_t pdf_list_indexof_from_to (const pdf_list_t list, const pdf_size_t start_index,
104 : const pdf_size_t end_index, const void* element,
105 : pdf_size_t *position);
106 :
107 : /* Element setting and getting functions */
108 :
109 : const void * pdf_list_node_value (const pdf_list_t list, const pdf_list_node_t node);
110 : pdf_status_t pdf_list_get_at (const pdf_list_t list, const pdf_size_t position, void **value);
111 : pdf_status_t pdf_list_set_at (pdf_list_t list, const pdf_size_t position,
112 : const void* element, pdf_list_node_t *node);
113 :
114 : /* Element addition and removal functions */
115 :
116 : pdf_status_t pdf_list_add_first (pdf_list_t list, const void* element, pdf_list_node_t *node);
117 : pdf_status_t pdf_list_add_last (pdf_list_t list, const void* element, pdf_list_node_t *node);
118 : pdf_status_t pdf_list_add_at (pdf_list_t list, const pdf_size_t position,
119 : const void* element, pdf_list_node_t *node);
120 : pdf_status_t pdf_list_remove_node (pdf_list_t list, const pdf_list_node_t node);
121 : pdf_status_t pdf_list_remove_at (pdf_list_t list, const pdf_size_t position);
122 : pdf_status_t pdf_list_remove (pdf_list_t list, const void * element);
123 :
124 : /* Sorted list functions */
125 :
126 : pdf_status_t
127 : pdf_list_sorted_add (pdf_list_t list, pdf_list_element_compar_fn_t compar_fn,
128 : const void* element, pdf_list_node_t * element_node);
129 :
130 : pdf_status_t
131 : pdf_list_sorted_remove (pdf_list_t list, pdf_list_element_compar_fn_t compar_fn,
132 : const void * element);
133 :
134 : pdf_status_t
135 : pdf_list_sorted_search (const pdf_list_t list, pdf_list_element_compar_fn_t compar_fn,
136 : const void* element, pdf_list_node_t *node);
137 :
138 : pdf_status_t
139 : pdf_list_sorted_search_from_to (const pdf_list_t list,
140 : pdf_list_element_compar_fn_t compar_fn,
141 : const pdf_size_t start_index, const pdf_size_t end_index,
142 : const void* element, pdf_list_node_t *node);
143 :
144 : pdf_status_t
145 : pdf_list_sorted_indexof (const pdf_list_t list,
146 : pdf_list_element_compar_fn_t compar_fn,
147 : const void* element, pdf_size_t *position);
148 :
149 : pdf_status_t
150 : pdf_list_sorted_indexof_from_to (const pdf_list_t list,
151 : pdf_list_element_compar_fn_t compar_fn,
152 : const pdf_size_t start_index, const pdf_size_t end_index,
153 : const void* element, pdf_size_t *position);
154 :
155 : /* Element iterator functions */
156 :
157 : pdf_status_t pdf_list_iterator (const pdf_list_t list, pdf_list_iterator_t *itr);
158 : pdf_status_t pdf_list_iterator_from_to (const pdf_list_t list,
159 : const pdf_size_t start_index,
160 : const pdf_size_t end_index,
161 : pdf_list_iterator_t *itr);
162 : pdf_status_t pdf_list_iterator_next (pdf_list_iterator_t *iterator,
163 : const void **element_pointer,
164 : pdf_list_node_t *node_pointer);
165 : pdf_status_t pdf_list_iterator_free (pdf_list_iterator_t *iterator);
166 :
167 :
168 : /* END PUBLIC */
169 :
170 : #else
171 :
172 : /* Inlined versions of the functions */
173 :
174 : #if defined COMPILING_PDF_LIST
175 : # define STATIC_INLINE
176 : #else
177 : # define STATIC_INLINE static inline
178 : #endif /* COMPILING_PDF_LIST */
179 :
180 : #include <gl_array_list.h>
181 :
182 : /* Creation and destruction functions */
183 :
184 : STATIC_INLINE pdf_status_t
185 : pdf_list_new (pdf_list_element_equals_fn_t equals_fn,
186 : pdf_list_element_dispose_fn_t dispose_fn,
187 : const pdf_bool_t allow_duplicates,
188 : pdf_list_t *list)
189 81 : {
190 : pdf_status_t st;
191 :
192 1076 : st = PDF_OK;
193 :
194 1076 : if (list != NULL)
195 : {
196 2150 : list->gl_list = gl_list_nx_create_empty(GL_ARRAY_LIST,
197 : equals_fn,
198 : NULL,dispose_fn, allow_duplicates);
199 1075 : list->allow_duplicates = allow_duplicates;
200 :
201 1075 : if (list->gl_list == NULL)
202 : {
203 0 : st = PDF_ENOMEM;
204 : }
205 : }
206 : else
207 : {
208 1 : st = PDF_EBADDATA;
209 : }
210 :
211 1076 : return (st);
212 : }
213 :
214 : STATIC_INLINE pdf_status_t
215 : pdf_list_destroy (pdf_list_t list)
216 79 : {
217 899 : gl_list_free ((gl_list_t) list.gl_list);
218 899 : return PDF_OK;
219 : }
220 :
221 : /* Property management functions */
222 :
223 : STATIC_INLINE pdf_size_t
224 : pdf_list_size (const pdf_list_t list)
225 43 : {
226 4068 : return ((pdf_size_t) gl_list_size((gl_list_t) list.gl_list));
227 : }
228 :
229 : /* Element searching functions */
230 :
231 : STATIC_INLINE pdf_status_t
232 : pdf_list_search (const pdf_list_t list,
233 : const void* element,
234 : pdf_list_node_t *node)
235 3 : {
236 : pdf_status_t st;
237 :
238 3 : st = PDF_OK;
239 3 : if (node != NULL && element != NULL)
240 : {
241 2 : node->gl_node = gl_list_search ((gl_list_t)list.gl_list, element);
242 2 : if (node->gl_node == NULL)
243 : {
244 1 : st = PDF_ENONODE;
245 : }
246 : }
247 : else
248 : {
249 1 : st = PDF_EBADDATA;
250 : }
251 :
252 3 : return (st);
253 : }
254 :
255 : STATIC_INLINE pdf_status_t
256 : pdf_list_search_from (const pdf_list_t list,
257 : const pdf_size_t start_index,
258 : const void* element,
259 : pdf_list_node_t *node)
260 4 : {
261 : pdf_status_t st;
262 :
263 4 : st = PDF_OK;
264 :
265 4 : if (node != NULL && element != NULL)
266 : {
267 3 : if ((start_index < pdf_list_size (list) && start_index > 0) ||
268 : (start_index == 0))
269 : {
270 4 : node->gl_node = gl_list_search_from((gl_list_t)list.gl_list,
271 : start_index, element);
272 2 : if (node->gl_node == NULL)
273 : {
274 1 : st = PDF_ENONODE;
275 : }
276 : }
277 : else
278 : {
279 1 : st = PDF_EINVRANGE;
280 : }
281 : }
282 : else
283 : {
284 1 : st = PDF_EBADDATA;
285 : }
286 :
287 4 : return (st);
288 : }
289 :
290 : STATIC_INLINE pdf_status_t
291 : pdf_list_search_from_to (const pdf_list_t list,
292 : const pdf_size_t start_index,
293 : const pdf_size_t end_index,
294 : const void* element,
295 : pdf_list_node_t *node)
296 4 : {
297 : pdf_status_t st;
298 :
299 4 : st = PDF_OK;
300 :
301 4 : if (node != NULL && element != NULL)
302 : {
303 3 : if (((start_index < pdf_list_size (list) && start_index > 0) ||
304 : (start_index == 0)) &&
305 : ((end_index <= pdf_list_size (list) && end_index > 0) ||
306 : (end_index == 0)) &&
307 : (start_index < end_index))
308 : {
309 4 : node->gl_node = gl_list_search_from_to((gl_list_t)list.gl_list,
310 : start_index, end_index,
311 : element);
312 2 : if (node->gl_node == NULL)
313 : {
314 1 : st = PDF_ENONODE;
315 : }
316 : }
317 : else
318 : {
319 1 : st = PDF_EINVRANGE;
320 : }
321 : }
322 : else
323 : {
324 1 : st = PDF_EBADDATA;
325 : }
326 :
327 4 : return (st);
328 : }
329 :
330 : STATIC_INLINE pdf_status_t
331 : pdf_list_next_node (const pdf_list_t list,
332 : const pdf_list_node_t node,
333 : pdf_list_node_t *next)
334 3 : {
335 : pdf_status_t st;
336 :
337 3 : st = PDF_OK;
338 :
339 3 : if (next != NULL)
340 : {
341 4 : next->gl_node = gl_list_next_node ((gl_list_t)list.gl_list,
342 : (gl_list_node_t)node.gl_node);
343 2 : if (next->gl_node == NULL)
344 : {
345 1 : st = PDF_ENONODE;
346 : }
347 : }
348 : else
349 : {
350 1 : st = PDF_EBADDATA;
351 : }
352 :
353 3 : return (st);
354 : }
355 :
356 :
357 : STATIC_INLINE pdf_status_t
358 : pdf_list_previous_node (const pdf_list_t list, const pdf_list_node_t node, pdf_list_node_t *prev)
359 3 : {
360 : pdf_status_t st;
361 :
362 3 : st = PDF_OK;
363 :
364 3 : if (prev != NULL)
365 : {
366 4 : prev->gl_node = gl_list_previous_node ((gl_list_t)list.gl_list,
367 : (gl_list_node_t)node.gl_node);
368 2 : if (prev->gl_node == NULL)
369 : {
370 1 : st = PDF_ENONODE;
371 : }
372 : }
373 : else
374 : {
375 1 : st = PDF_EBADDATA;
376 : }
377 :
378 3 : return (st);
379 : }
380 :
381 : STATIC_INLINE pdf_status_t
382 : pdf_list_indexof (const pdf_list_t list,
383 : const void *element,
384 : pdf_size_t *position)
385 3 : {
386 : pdf_status_t st;
387 :
388 3 : st = PDF_OK;
389 :
390 3 : if (position != NULL && element != NULL)
391 : {
392 4 : *position = (pdf_size_t) gl_list_indexof ((gl_list_t)list.gl_list,
393 : element);
394 2 : if (*position == -1)
395 : {
396 1 : st = PDF_ENONODE;
397 : }
398 : }
399 : else
400 : {
401 1 : st = PDF_EBADDATA;
402 : }
403 :
404 3 : return (st);
405 : }
406 :
407 :
408 : STATIC_INLINE pdf_status_t
409 : pdf_list_indexof_from (const pdf_list_t list,
410 : const pdf_size_t start_index,
411 : const void *element,
412 : pdf_size_t *position)
413 5 : {
414 : pdf_status_t st;
415 :
416 5 : st = PDF_OK;
417 :
418 5 : if ((position != NULL) && (element != NULL))
419 : {
420 4 : if ((start_index > 0 && start_index < pdf_list_size (list)) ||
421 : start_index == 0)
422 : {
423 4 : *position = (pdf_size_t) gl_list_indexof_from ((gl_list_t)list.gl_list,
424 : start_index, element);
425 2 : if (*position == -1)
426 : {
427 1 : st = PDF_ENONODE;
428 : }
429 : }
430 : else
431 : {
432 2 : st = PDF_EINVRANGE;
433 : }
434 : }
435 : else
436 : {
437 1 : st = PDF_EBADDATA;
438 : }
439 :
440 5 : return (st);
441 : }
442 :
443 :
444 : STATIC_INLINE pdf_status_t
445 : pdf_list_indexof_from_to (const pdf_list_t list,
446 : const pdf_size_t start_index,
447 : const pdf_size_t end_index,
448 : const void *element,
449 : pdf_size_t *position)
450 6 : {
451 : pdf_status_t st;
452 :
453 6 : st = PDF_OK;
454 :
455 6 : if ((position != NULL) && (element != NULL))
456 : {
457 5 : if (((start_index > 0 && start_index < pdf_list_size (list)) ||
458 : start_index == 0) &&
459 : (end_index > 0 && end_index <= pdf_list_size (list)) &&
460 : (start_index < end_index))
461 : {
462 4 : *position = (pdf_size_t)
463 : gl_list_indexof_from_to ((gl_list_t)list.gl_list,
464 : start_index, end_index,
465 : element);
466 2 : if (*position == -1)
467 : {
468 1 : st = PDF_ENONODE;
469 : }
470 : }
471 : else
472 : {
473 3 : st = PDF_EINVRANGE;
474 : }
475 : }
476 : else
477 : {
478 1 : st = PDF_EBADDATA;
479 : }
480 :
481 6 : return (st);
482 : }
483 :
484 :
485 : /* Element setting and getting functions */
486 :
487 : STATIC_INLINE const void *
488 : pdf_list_node_value (const pdf_list_t list, const pdf_list_node_t node)
489 3 : {
490 6 : return (gl_list_node_value ((gl_list_t)list.gl_list,
491 : (gl_list_node_t)node.gl_node));
492 : }
493 :
494 :
495 : STATIC_INLINE pdf_status_t
496 : pdf_list_get_at (const pdf_list_t list,
497 : const pdf_size_t position,
498 : const void **value)
499 387 : {
500 : pdf_status_t st;
501 :
502 543 : st = PDF_OK;
503 :
504 543 : if (value != NULL)
505 : {
506 1297 : if ((position > 0 && position < pdf_list_size (list)) ||
507 : (position == 0))
508 : {
509 1082 : *value = gl_list_get_at ((gl_list_t)list.gl_list, position);
510 : }
511 : else
512 : {
513 1 : st = PDF_EINVRANGE;
514 : }
515 : }
516 : else
517 : {
518 1 : st = PDF_EBADDATA;
519 : }
520 :
521 543 : return (st);
522 : }
523 :
524 :
525 : STATIC_INLINE pdf_status_t
526 : pdf_list_set_at (pdf_list_t list,
527 : const pdf_size_t position,
528 : const void *element,
529 : pdf_list_node_t *node)
530 2 : {
531 : pdf_status_t st;
532 :
533 2 : st = PDF_OK;
534 :
535 2 : if (list.allow_duplicates ||
536 : (gl_list_search ((gl_list_t)list.gl_list, element) == NULL))
537 : {
538 2 : if (((position > 0 && position < pdf_list_size (list)) ||
539 : (position == 0)))
540 : {
541 1 : if (node != NULL)
542 : {
543 2 : node->gl_node = gl_list_nx_set_at ((gl_list_t)list.gl_list, position,
544 : element);
545 :
546 1 : if (node->gl_node == NULL)
547 : {
548 0 : st = PDF_ENOMEM;
549 : }
550 : }
551 : else
552 : {
553 0 : if (gl_list_nx_set_at ((gl_list_t)list.gl_list, position, element)
554 : == NULL)
555 : {
556 0 : st = PDF_ENOMEM;
557 : }
558 : }
559 : }
560 : else
561 : {
562 1 : st = PDF_EINVRANGE;
563 : }
564 : }
565 : else
566 : {
567 : /* Duplicated list values are not allowed */
568 0 : st = PDF_EBADDATA;
569 : }
570 :
571 2 : return st;
572 : }
573 :
574 :
575 : /* Element addition and removal functions */
576 :
577 :
578 : STATIC_INLINE pdf_status_t
579 : pdf_list_add_first (pdf_list_t list,
580 : const void* element,
581 : pdf_list_node_t *node)
582 14 : {
583 : pdf_status_t st;
584 : gl_list_node_t gl_node;
585 :
586 14 : st = PDF_OK;
587 14 : if (list.allow_duplicates ||
588 : (gl_list_search ((gl_list_t)list.gl_list, element) == NULL))
589 : {
590 26 : gl_node = gl_list_nx_add_first ((gl_list_t)list.gl_list, element);
591 13 : if (node != NULL)
592 : {
593 1 : node->gl_node = gl_node;
594 : }
595 :
596 13 : if (gl_node == NULL)
597 : {
598 0 : st = PDF_ENOMEM;
599 : }
600 : }
601 : else
602 : {
603 : /* Duplicated list elements are not allowed */
604 1 : st = PDF_EBADDATA;
605 : }
606 :
607 14 : return (st);
608 : }
609 :
610 :
611 : STATIC_INLINE pdf_status_t
612 : pdf_list_add_last (pdf_list_t list,
613 : const void* element,
614 : pdf_list_node_t *node)
615 37 : {
616 : pdf_status_t st;
617 : gl_list_node_t gl_node;
618 :
619 387 : st = PDF_OK;
620 387 : if (list.allow_duplicates ||
621 : (gl_list_search ((gl_list_t)list.gl_list, element) == NULL))
622 : {
623 774 : gl_node = gl_list_nx_add_last ((gl_list_t)list.gl_list, element);
624 387 : if (node != NULL)
625 : {
626 7 : node->gl_node = gl_node;
627 : }
628 387 : if (gl_node == NULL)
629 : {
630 0 : st = PDF_ENOMEM;
631 : }
632 : }
633 : else
634 : {
635 : /* Duplicated list elements are not allowed */
636 0 : st = PDF_EBADDATA;
637 : }
638 :
639 387 : return (st);
640 : }
641 :
642 :
643 : STATIC_INLINE pdf_status_t
644 : pdf_list_add_at (pdf_list_t list,
645 : const pdf_size_t position,
646 : const void *element,
647 : pdf_list_node_t *node)
648 2 : {
649 : pdf_status_t st;
650 :
651 2 : st = PDF_OK;
652 :
653 2 : if (list.allow_duplicates ||
654 : (gl_list_search ((gl_list_t)list.gl_list, element) == NULL))
655 : {
656 2 : if ((position > 0 && position < pdf_list_size (list)) ||
657 : (position == 0))
658 : {
659 1 : if (node != NULL)
660 : {
661 2 : node->gl_node = gl_list_nx_add_at ((gl_list_t)list.gl_list,
662 : position, element);
663 1 : if (node->gl_node == NULL)
664 : {
665 0 : st = PDF_ENOMEM;
666 : }
667 : }
668 : else
669 : {
670 0 : if (gl_list_nx_add_at ((gl_list_t)list.gl_list, position, element)
671 : == NULL)
672 : {
673 0 : st = PDF_ENOMEM;
674 : }
675 : }
676 : }
677 : else
678 : {
679 1 : st = PDF_EINVRANGE;
680 : }
681 : }
682 : else
683 : {
684 : /* Duplicated list values are not allowed */
685 0 : st = PDF_EBADDATA;
686 : }
687 :
688 2 : return st;
689 : }
690 :
691 :
692 : STATIC_INLINE pdf_status_t
693 : pdf_list_remove_node (pdf_list_t list, const pdf_list_node_t node)
694 1 : {
695 1 : gl_list_remove_node ((gl_list_t)list.gl_list, (gl_list_node_t)node.gl_node);
696 1 : return PDF_OK;
697 : }
698 :
699 :
700 : STATIC_INLINE pdf_status_t
701 : pdf_list_remove_at (pdf_list_t list, const pdf_size_t position)
702 2 : {
703 : pdf_status_t st;
704 :
705 2 : st = PDF_OK;
706 :
707 2 : if ((position > 0 && position < pdf_list_size (list)) ||
708 : (position == 0))
709 : {
710 1 : gl_list_remove_at ((gl_list_t)list.gl_list, position);
711 : }
712 : else
713 : {
714 1 : st = PDF_EINVRANGE;
715 : }
716 :
717 2 : return st;
718 : }
719 :
720 :
721 : STATIC_INLINE pdf_status_t
722 : pdf_list_remove (pdf_list_t list, const void * element)
723 2 : {
724 : pdf_status_t st;
725 :
726 2 : st = PDF_OK;
727 :
728 4 : if (!gl_list_remove ((gl_list_t)list.gl_list, element))
729 1 : st = PDF_ENONODE;
730 :
731 2 : return st;
732 : }
733 :
734 :
735 :
736 : /* Element iterator functions */
737 :
738 : STATIC_INLINE pdf_status_t
739 : pdf_list_iterator (const pdf_list_t list,
740 : pdf_list_iterator_t *itr)
741 5 : {
742 : pdf_status_t st;
743 :
744 6 : st = PDF_OK;
745 :
746 6 : if (itr != NULL)
747 : {
748 10 : *((gl_list_iterator_t*)itr->gl_iterator) =
749 : gl_list_iterator ((gl_list_t)list.gl_list);
750 : }
751 : else
752 : {
753 1 : st = PDF_EBADDATA;
754 : }
755 :
756 6 : return (st);
757 : }
758 :
759 :
760 : STATIC_INLINE pdf_status_t
761 : pdf_list_iterator_from_to (const pdf_list_t list, const pdf_size_t start_index,
762 : const pdf_size_t end_index,
763 : pdf_list_iterator_t *itr)
764 6 : {
765 : pdf_status_t st;
766 :
767 6 : st = PDF_OK;
768 :
769 6 : if (itr != NULL)
770 : {
771 6 : if (((start_index > 0 && start_index < pdf_list_size (list)) ||
772 : start_index == 0) &&
773 : (end_index > 0 && end_index <= pdf_list_size (list)) &&
774 : (start_index < end_index))
775 : {
776 2 : *((gl_list_iterator_t*)itr->gl_iterator) =
777 : gl_list_iterator_from_to ((gl_list_t)list.gl_list, start_index,
778 : end_index);
779 : }
780 : else
781 : {
782 4 : st = PDF_EINVRANGE;
783 : }
784 : }
785 : else
786 : {
787 1 : st = PDF_EBADDATA;
788 : }
789 :
790 6 : return st;
791 : }
792 :
793 : STATIC_INLINE pdf_status_t
794 : pdf_list_iterator_next (pdf_list_iterator_t *iterator,
795 : const void **element_pointer,
796 : pdf_list_node_t *node_pointer)
797 2 : {
798 : pdf_status_t st;
799 :
800 5 : st = PDF_OK;
801 :
802 10 : if (!gl_list_iterator_next (((gl_list_iterator_t*)iterator->gl_iterator),
803 : element_pointer,
804 : ((gl_list_node_t*)&node_pointer->gl_node)))
805 2 : st = PDF_ENONODE;
806 :
807 5 : return st;
808 : }
809 :
810 : STATIC_INLINE pdf_status_t
811 : pdf_list_iterator_free (pdf_list_iterator_t *iterator)
812 5 : {
813 6 : gl_list_iterator_free ((gl_list_iterator_t*)(iterator->gl_iterator));
814 :
815 6 : return PDF_OK;
816 : }
817 :
818 :
819 : STATIC_INLINE pdf_status_t
820 : pdf_list_sorted_add (pdf_list_t list, pdf_list_element_compar_fn_t compar_fn,
821 : const void* element, pdf_list_node_t * element_node)
822 20 : {
823 : pdf_list_node_t node;
824 : pdf_status_t st;
825 :
826 20 : st = PDF_OK;
827 :
828 20 : if (compar_fn != NULL)
829 : {
830 38 : node.gl_node = gl_sortedlist_nx_add ((gl_list_t)list.gl_list, compar_fn,
831 : element);
832 19 : if (element_node != NULL)
833 : {
834 0 : *element_node = node;
835 : }
836 :
837 19 : if (node.gl_node == NULL)
838 : {
839 0 : st = PDF_ENOMEM;
840 : }
841 : }
842 : else
843 : {
844 1 : st = PDF_EBADDATA;
845 : }
846 :
847 20 : return (st);
848 :
849 : }
850 :
851 : STATIC_INLINE pdf_status_t
852 : pdf_list_sorted_remove (pdf_list_t list, pdf_list_element_compar_fn_t compar_fn,
853 : const void * element)
854 3 : {
855 : pdf_status_t st;
856 :
857 3 : st = PDF_OK;
858 :
859 3 : if (compar_fn != NULL)
860 : {
861 4 : if (!gl_sortedlist_remove ((gl_list_t)list.gl_list, compar_fn, element))
862 1 : st = PDF_ENONODE;
863 : }
864 : else
865 : {
866 1 : st = PDF_EBADDATA;
867 : }
868 :
869 3 : return (st);
870 :
871 : }
872 :
873 : STATIC_INLINE pdf_status_t
874 : pdf_list_sorted_search (const pdf_list_t list, pdf_list_element_compar_fn_t compar_fn,
875 : const void* element, pdf_list_node_t *node)
876 4 : {
877 :
878 : pdf_status_t st;
879 :
880 4 : st = PDF_OK;
881 :
882 4 : if (compar_fn != NULL && node != NULL && element != NULL)
883 : {
884 4 : node->gl_node = gl_sortedlist_search ((gl_list_t)list.gl_list,
885 : compar_fn, element);
886 2 : if (node->gl_node == NULL)
887 : {
888 1 : st = PDF_ENONODE;
889 : }
890 : }
891 : else
892 : {
893 2 : st = PDF_EBADDATA;
894 : }
895 :
896 4 : return (st);
897 : }
898 :
899 : STATIC_INLINE pdf_status_t
900 : pdf_list_sorted_search_from_to (const pdf_list_t list,
901 : pdf_list_element_compar_fn_t compar_fn,
902 : const pdf_size_t start_index, const pdf_size_t end_index,
903 : const void* element, pdf_list_node_t *node)
904 5 : {
905 : pdf_status_t st;
906 :
907 5 : st = PDF_OK;
908 :
909 5 : if (compar_fn != NULL && node != NULL && element != NULL)
910 : {
911 3 : if (((start_index < pdf_list_size (list) && start_index > 0) ||
912 : (start_index == 0)) &&
913 : ((end_index <= pdf_list_size (list) && end_index > 0) ||
914 : (end_index == 0)) &&
915 : (start_index < end_index))
916 : {
917 4 : node->gl_node = gl_sortedlist_search_from_to((gl_list_t)list.gl_list,
918 : compar_fn, start_index,
919 : end_index, element);
920 2 : if (node->gl_node == NULL)
921 : {
922 1 : st = PDF_ENONODE;
923 : }
924 : }
925 : else
926 : {
927 1 : st = PDF_EINVRANGE;
928 : }
929 : }
930 : else
931 : {
932 2 : st = PDF_EBADDATA;
933 : }
934 :
935 5 : return (st);
936 : }
937 :
938 : STATIC_INLINE pdf_status_t
939 : pdf_list_sorted_indexof (const pdf_list_t list,
940 : pdf_list_element_compar_fn_t compar_fn,
941 : const void* element, pdf_size_t *position)
942 4 : {
943 :
944 : pdf_status_t st;
945 :
946 4 : st = PDF_OK;
947 :
948 4 : if (compar_fn != NULL && position != NULL && element != NULL )
949 : {
950 4 : *position = (pdf_size_t) gl_sortedlist_indexof ((gl_list_t)list.gl_list,
951 : compar_fn, element);
952 2 : if (*position == -1)
953 : {
954 1 : st = PDF_ENONODE;
955 : }
956 : }
957 : else
958 : {
959 2 : st = PDF_EBADDATA;
960 : }
961 :
962 4 : return (st);
963 : }
964 :
965 : STATIC_INLINE pdf_status_t
966 : pdf_list_sorted_indexof_from_to (const pdf_list_t list,
967 : pdf_list_element_compar_fn_t compar_fn,
968 : const pdf_size_t start_index, const pdf_size_t end_index,
969 : const void* element, pdf_size_t *position)
970 7 : {
971 : pdf_status_t st;
972 :
973 7 : st = PDF_OK;
974 :
975 7 : if (compar_fn != NULL && position != NULL && element != NULL)
976 : {
977 5 : if (((start_index > 0 && start_index < pdf_list_size (list)) ||
978 : start_index == 0) &&
979 : (end_index > 0 && end_index <= pdf_list_size (list)) &&
980 : (start_index < end_index))
981 : {
982 4 : *position = (pdf_size_t)
983 : gl_sortedlist_indexof_from_to ((gl_list_t)list.gl_list, compar_fn,
984 : start_index, end_index,
985 : element);
986 2 : if (*position == -1)
987 : {
988 1 : st = PDF_ENONODE;
989 : }
990 : }
991 : else
992 : {
993 3 : st = PDF_EINVRANGE;
994 : }
995 : }
996 : else
997 : {
998 2 : st = PDF_EBADDATA;
999 : }
1000 :
1001 :
1002 7 : return (st);
1003 : }
1004 :
1005 : #endif /* COMPILING_PDF_LIST */
1006 : #endif /* PDF_LIST_H */
1007 :
1008 : /* End of pdf-list.h */
|