2 * "$Id: array.c 5493 2006-05-05 16:33:57Z mike $"
4 * Sorted array routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsArrayAdd() - Add an element to the array.
29 * cupsArrayClear() - Clear the array.
30 * cupsArrayCount() - Get the number of elements in the array.
31 * cupsArrayCurrent() - Return the current element in the array.
32 * cupsArrayDelete() - Free all memory used by the array.
33 * cupsArrayDup() - Duplicate the array.
34 * cupsArrayFind() - Find an element in the array.
35 * cupsArrayFirst() - Get the first element in the array.
36 * cupsArrayIndex() - Get the N-th element in the array.
37 * cupsArrayInsert() - Insert an element in the array.
38 * cupsArrayLast() - Get the last element in the array.
39 * cupsArrayNew() - Create a new array.
40 * cupsArrayNext() - Get the next element in the array.
41 * cupsArrayPrev() - Get the previous element in the array.
42 * cupsArrayRemove() - Remove an element from the array.
43 * cupsArrayRestore() - Reset the current element to the last cupsArraySave.
44 * cupsArraySave() - Mark the current element for a later
46 * cupsArrayUserData() - Return the user data for an array.
47 * cups_array_add() - Insert or append an element to the array...
48 * cups_array_find() - Find an element in the array...
52 * Include necessary headers...
64 #define _CUPS_MAXSAVE 32 /**** Maximum number of saves ****/
68 * Types and structures...
71 struct _cups_array_s
/**** CUPS array structure ****/
74 * The current implementation uses an insertion sort into an array of
75 * sorted pointers. We leave the array type private/opaque so that we
76 * can change the underlying implementation without affecting the users
80 int num_elements
, /* Number of array elements */
81 alloc_elements
, /* Allocated array elements */
82 current
, /* Current element */
83 insert
, /* Last inserted element */
84 unique
, /* Are all elements unique? */
85 num_saved
, /* Number of saved elements */
88 void **elements
; /* Array elements */
89 cups_array_func_t compare
; /* Element comparison function */
90 void *data
; /* User data passed to compare */
98 static int cups_array_add(cups_array_t
*a
, void *e
, int insert
);
99 static int cups_array_find(cups_array_t
*a
, void *e
, int prev
, int *rdiff
);
103 * 'cupsArrayAdd()' - Add an element to the array.
105 * When adding an element to a sorted array, non-unique elements are
106 * appended at the end of the run. For unsorted arrays, the element
107 * is inserted at the end of the array.
110 int /* O - 1 on success, 0 on failure */
111 cupsArrayAdd(cups_array_t
*a
, /* I - Array */
112 void *e
) /* I - Element */
114 DEBUG_printf(("cupsArrayAdd(a=%p, e=%p)\n", a
, e
));
117 * Range check input...
122 DEBUG_puts("cupsArrayAdd: returning 0");
127 * Append the element...
130 return (cups_array_add(a
, e
, 0));
135 * 'cupsArrayClear()' - Clear the array.
139 cupsArrayClear(cups_array_t
*a
) /* I - Array */
142 * Range check input...
149 * Set the number of elements to 0; we don't actually free the memory
150 * here - that is done in cupsArrayDelete()...
162 * 'cupsArrayCount()' - Get the number of elements in the array.
165 int /* O - Number of elements */
166 cupsArrayCount(cups_array_t
*a
) /* I - Array */
169 * Range check input...
176 * Return the number of elements...
179 return (a
->num_elements
);
184 * 'cupsArrayCurrent()' - Return the current element in the array.
187 void * /* O - Element */
188 cupsArrayCurrent(cups_array_t
*a
) /* I - Array */
191 * Range check input...
198 * Return the current element...
201 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
202 return (a
->elements
[a
->current
]);
209 * 'cupsArrayDelete()' - Free all memory used by the array.
213 cupsArrayDelete(cups_array_t
*a
) /* I - Array */
216 * Range check input...
223 * Free the array of element pointers - the caller is responsible
224 * for freeing the elements themselves...
227 if (a
->alloc_elements
)
235 * 'cupsArrayDup()' - Duplicate the array.
238 cups_array_t
* /* O - Duplicate array */
239 cupsArrayDup(cups_array_t
*a
) /* I - Array */
241 cups_array_t
*da
; /* Duplicate array */
245 * Range check input...
252 * Allocate memory for the array...
255 da
= calloc(1, sizeof(cups_array_t
));
259 da
->compare
= a
->compare
;
261 da
->current
= a
->current
;
262 da
->insert
= a
->insert
;
263 da
->unique
= a
->unique
;
264 da
->num_saved
= a
->num_saved
;
266 memcpy(da
->saved
, a
->saved
, sizeof(a
->saved
));
271 * Allocate memory for the elements...
274 da
->elements
= malloc(a
->num_elements
* sizeof(void *));
282 * Copy the element pointers...
285 memcpy(da
->elements
, a
->elements
, a
->num_elements
* sizeof(void *));
286 da
->num_elements
= a
->num_elements
;
287 da
->alloc_elements
= a
->num_elements
;
291 * Return the new array...
299 * 'cupsArrayFind()' - Find an element in the array.
302 void * /* O - Element found or NULL */
303 cupsArrayFind(cups_array_t
*a
, /* I - Array */
304 void *e
) /* I - Element */
306 int current
, /* Current element */
307 diff
; /* Difference */
311 * Range check input...
318 * See if we have any elements...
321 if (!a
->num_elements
)
325 * Yes, look for a match...
328 current
= cups_array_find(a
, e
, a
->current
, &diff
);
332 * Found a match! If the array does not contain unique values, find
333 * the first element that is the same...
336 if (!a
->unique
&& a
->compare
)
339 * The array is not unique, find the first match...
342 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
347 a
->current
= current
;
349 return (a
->elements
[current
]);
365 * 'cupsArrayFirst()' - Get the first element in the array.
368 void * /* O - First element or NULL */
369 cupsArrayFirst(cups_array_t
*a
) /* I - Array */
372 * Range check input...
379 * Return the first element...
384 return (cupsArrayCurrent(a
));
389 * 'cupsArrayIndex()' - Get the N-th element in the array.
392 void * /* O - N-th element or NULL */
393 cupsArrayIndex(cups_array_t
*a
, /* I - Array */
394 int n
) /* I - Index into array, starting at 0 */
401 return (cupsArrayCurrent(a
));
406 * 'cupsArrayInsert()' - Insert an element in the array.
408 * When inserting an element in a sorted array, non-unique elements are
409 * inserted at the beginning of the run. For unsorted arrays, the element
410 * is inserted at the beginning of the array.
413 int /* O - 0 on failure, 1 on success */
414 cupsArrayInsert(cups_array_t
*a
, /* I - Array */
415 void *e
) /* I - Element */
417 DEBUG_printf(("cupsArrayInsert(a=%p, e=%p)\n", a
, e
));
420 * Range check input...
425 DEBUG_puts("cupsArrayInsert: returning 0");
430 * Insert the element...
433 return (cups_array_add(a
, e
, 1));
438 * 'cupsArrayLast()' - Get the last element in the array.
441 void * /* O - Last element or NULL */
442 cupsArrayLast(cups_array_t
*a
) /* I - Array */
445 * Range check input...
452 * Return the last element...
455 a
->current
= a
->num_elements
- 1;
457 return (cupsArrayCurrent(a
));
462 * 'cupsArrayNew()' - Create a new array.
465 cups_array_t
* /* O - Array */
466 cupsArrayNew(cups_array_func_t f
, /* I - Comparison function */
467 void *d
) /* I - User data */
469 cups_array_t
*a
; /* Array */
473 * Allocate memory for the array...
476 a
= calloc(1, sizeof(cups_array_t
));
492 * 'cupsArrayNext()' - Get the next element in the array.
495 void * /* O - Next element or NULL */
496 cupsArrayNext(cups_array_t
*a
) /* I - Array */
499 * Range check input...
506 * Return the next element...
509 if (a
->current
< a
->num_elements
)
512 return (cupsArrayCurrent(a
));
517 * 'cupsArrayPrev()' - Get the previous element in the array.
520 void * /* O - Previous element or NULL */
521 cupsArrayPrev(cups_array_t
*a
) /* I - Array */
524 * Range check input...
531 * Return the previous element...
537 return (cupsArrayCurrent(a
));
542 * 'cupsArrayRemove()' - Remove an element from the array.
545 int /* O - 1 on success, 0 on failure */
546 cupsArrayRemove(cups_array_t
*a
, /* I - Array */
547 void *e
) /* I - Element */
549 int i
, /* Looping var */
550 current
, /* Current element */
551 diff
; /* Difference */
555 * Range check input...
562 * See if the element is in the array...
565 if (!a
->num_elements
)
568 current
= cups_array_find(a
, e
, a
->current
, &diff
);
573 * Yes, now remove it...
578 if (current
< a
->num_elements
)
579 memmove(a
->elements
+ current
, a
->elements
+ current
+ 1,
580 (a
->num_elements
- current
) * sizeof(void *));
582 if (current
<= a
->current
)
585 if (current
<= a
->insert
)
588 for (i
= 0; i
< a
->num_saved
; i
++)
589 if (current
<= a
->saved
[i
])
592 if (a
->num_elements
<= 1)
600 * 'cupsArrayRestore()' - Reset the current element to the last cupsArraySave.
603 void * /* O - New current element */
604 cupsArrayRestore(cups_array_t
*a
) /* I - Array */
609 if (a
->num_saved
<= 0)
613 a
->current
= a
->saved
[a
->num_saved
];
615 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
616 return (a
->elements
[a
->current
]);
623 * 'cupsArraySave()' - Mark the current element for a later cupsArrayRestore.
625 * The save/restore stack is guaranteed to be at least 32 elements deep.
628 int /* O - 1 on success, 0 on failure */
629 cupsArraySave(cups_array_t
*a
) /* I - Array */
634 if (a
->num_saved
>= _CUPS_MAXSAVE
)
637 a
->saved
[a
->num_saved
] = a
->current
;
645 * 'cupsArrayUserData()' - Return the user data for an array.
648 void * /* O - User data */
649 cupsArrayUserData(cups_array_t
*a
) /* I - Array */
659 * 'cups_array_add()' - Insert or append an element to the array...
662 static int /* O - 1 on success, 0 on failure */
663 cups_array_add(cups_array_t
*a
, /* I - Array */
664 void *e
, /* I - Element to add */
665 int insert
) /* I - 1 = insert, 0 = append */
667 int i
, /* Looping var */
668 current
, /* Current element */
669 diff
; /* Comparison with current element */
672 DEBUG_printf(("cups_array_add(a=%p, e=%p, insert=%d)\n", a
, e
, insert
));
675 * Verify we have room for the new element...
678 if (a
->num_elements
>= a
->alloc_elements
)
681 * Allocate additional elements; start with 16 elements, then
682 * double the size until 1024 elements, then add 1024 elements
686 void **temp
; /* New array elements */
687 int count
; /* New allocation count */
690 if (a
->alloc_elements
== 0)
693 temp
= malloc(count
* sizeof(void *));
697 if (a
->alloc_elements
< 1024)
698 count
= a
->alloc_elements
* 2;
700 count
= a
->alloc_elements
+ 1024;
702 temp
= realloc(a
->elements
, count
* sizeof(void *));
705 DEBUG_printf(("cups_array_add: count=%d\n", count
));
709 DEBUG_puts("cupsAddAdd: allocation failed, returning 0");
713 a
->alloc_elements
= count
;
718 * Find the insertion point for the new element; if there is no
719 * compare function or elements, just add it to the beginning or end...
722 if (!a
->num_elements
|| !a
->compare
)
725 * No elements or comparison function, insert/append as needed...
729 current
= 0; /* Insert at beginning */
731 current
= a
->num_elements
; /* Append to the end */
736 * Do a binary search for the insertion point...
739 current
= cups_array_find(a
, e
, a
->insert
, &diff
);
744 * Insert after the current element...
752 * Compared equal, make sure we add to the begining or end of
753 * the current run of equal elements...
761 * Insert at beginning of run...
764 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
771 * Append at end of run...
778 while (current
< a
->num_elements
&&
779 !(*(a
->compare
))(e
, a
->elements
[current
], a
->data
));
785 * Insert or append the element...
788 if (current
< a
->num_elements
)
791 * Shift other elements to the right...
794 memmove(a
->elements
+ current
+ 1, a
->elements
+ current
,
795 (a
->num_elements
- current
) * sizeof(void *));
797 if (a
->current
>= current
)
800 for (i
= 0; i
< a
->num_saved
; i
++)
801 if (a
->saved
[i
] >= current
)
804 DEBUG_printf(("cups_array_add: insert element at index %d...\n", current
));
808 printf("cups_array_add: append element at %d...\n", current
);
811 a
->elements
[current
] = e
;
816 for (current
= 0; current
< a
->num_elements
; current
++)
817 printf("cups_array_add: a->elements[%d]=%p\n", current
, a
->elements
[current
]);
820 DEBUG_puts("cups_array_add: returning 1");
827 * 'cups_array_find()' - Find an element in the array...
830 static int /* O - Index of match */
831 cups_array_find(cups_array_t
*a
, /* I - Array */
832 void *e
, /* I - Element */
833 int prev
, /* I - Previous index */
834 int *rdiff
) /* O - Difference of match */
836 int left
, /* Left side of search */
837 right
, /* Right side of search */
838 current
, /* Current element */
839 diff
; /* Comparison with current element */
842 DEBUG_printf(("cups_array_find(a=%p, e=%p, prev=%d, rdiff=%p)\n", a
, e
, prev
,
848 * Do a binary search for the element...
851 DEBUG_puts("cups_array_find: binary search");
853 if (prev
>= 0 && prev
< a
->num_elements
)
856 * Start search on either side of previous...
859 if ((diff
= (*(a
->compare
))(e
, a
->elements
[prev
], a
->data
)) == 0 ||
860 (diff
< 0 && prev
== 0) ||
861 (diff
> 0 && prev
== (a
->num_elements
- 1)))
864 * Exact or edge match, return it!
867 DEBUG_printf(("cups_array_find: Returning %d, diff=%d\n", prev
, diff
));
876 * Start with previous on right side...
885 * Start wih previous on left side...
889 right
= a
->num_elements
- 1;
895 * Start search in the middle...
899 right
= a
->num_elements
- 1;
904 current
= (left
+ right
) / 2;
905 diff
= (*(a
->compare
))(e
, a
->elements
[current
], a
->data
);
907 DEBUG_printf(("cups_array_find: left=%d, right=%d, current=%d, diff=%d\n",
908 left
, right
, current
, diff
));
917 while ((right
- left
) > 1);
922 * Check the last 1 or 2 elements...
925 if ((diff
= (*(a
->compare
))(e
, a
->elements
[left
], a
->data
)) <= 0)
929 diff
= (*(a
->compare
))(e
, a
->elements
[right
], a
->data
);
937 * Do a linear pointer search...
940 DEBUG_puts("cups_array_find: linear search");
944 for (current
= 0; current
< a
->num_elements
; current
++)
945 if (a
->elements
[current
] == e
)
953 * Return the closest element and the difference...
956 DEBUG_printf(("cups_array_find: Returning %d, diff=%d\n", current
, diff
));
965 * End of "$Id: array.c 5493 2006-05-05 16:33:57Z mike $".