2 * "$Id: array.c 6649 2007-07-11 21:46:42Z mike $"
4 * Sorted array routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsArrayAdd() - Add an element to the array.
20 * cupsArrayClear() - Clear the array.
21 * cupsArrayCount() - Get the number of elements in the array.
22 * cupsArrayCurrent() - Return the current element in the array.
23 * cupsArrayDelete() - Free all memory used by the array.
24 * cupsArrayDup() - Duplicate the array.
25 * cupsArrayFind() - Find an element in the array.
26 * cupsArrayFirst() - Get the first element in the array.
27 * cupsArrayGetIndex() - Get the index of the current element.
28 * cupsArrayGetInsert() - Get the index of the last inserted element.
29 * cupsArrayIndex() - Get the N-th element in the array.
30 * cupsArrayInsert() - Insert an element in the array.
31 * cupsArrayLast() - Get the last element in the array.
32 * cupsArrayNew() - Create a new array.
33 * cupsArrayNext() - Get the next element in the array.
34 * cupsArrayPrev() - Get the previous element in the array.
35 * cupsArrayRemove() - Remove an element from the array.
36 * cupsArrayRestore() - Reset the current element to the last cupsArraySave.
37 * cupsArraySave() - Mark the current element for a later
39 * cupsArrayUserData() - Return the user data for an array.
40 * cups_array_add() - Insert or append an element to the array...
41 * cups_array_find() - Find an element in the array...
45 * Include necessary headers...
57 #define _CUPS_MAXSAVE 32 /**** Maximum number of saves ****/
61 * Types and structures...
64 struct _cups_array_s
/**** CUPS array structure ****/
67 * The current implementation uses an insertion sort into an array of
68 * sorted pointers. We leave the array type private/opaque so that we
69 * can change the underlying implementation without affecting the users
73 int num_elements
, /* Number of array elements */
74 alloc_elements
, /* Allocated array elements */
75 current
, /* Current element */
76 insert
, /* Last inserted element */
77 unique
, /* Are all elements unique? */
78 num_saved
, /* Number of saved elements */
81 void **elements
; /* Array elements */
82 cups_array_func_t compare
; /* Element comparison function */
83 void *data
; /* User data passed to compare */
84 cups_ahash_func_t hashfunc
; /* Hash function */
85 int hashsize
, /* Size of hash */
86 *hash
; /* Hash array */
94 static int cups_array_add(cups_array_t
*a
, void *e
, int insert
);
95 static int cups_array_find(cups_array_t
*a
, void *e
, int prev
, int *rdiff
);
99 * 'cupsArrayAdd()' - Add an element to the array.
101 * When adding an element to a sorted array, non-unique elements are
102 * appended at the end of the run. For unsorted arrays, the element
103 * is inserted at the end of the array.
106 int /* O - 1 on success, 0 on failure */
107 cupsArrayAdd(cups_array_t
*a
, /* I - Array */
108 void *e
) /* I - Element */
110 DEBUG_printf(("cupsArrayAdd(a=%p, e=%p)\n", a
, e
));
113 * Range check input...
118 DEBUG_puts("cupsArrayAdd: returning 0");
123 * Append the element...
126 return (cups_array_add(a
, e
, 0));
131 * 'cupsArrayClear()' - Clear the array.
135 cupsArrayClear(cups_array_t
*a
) /* I - Array */
138 * Range check input...
145 * Set the number of elements to 0; we don't actually free the memory
146 * here - that is done in cupsArrayDelete()...
158 * 'cupsArrayCount()' - Get the number of elements in the array.
161 int /* O - Number of elements */
162 cupsArrayCount(cups_array_t
*a
) /* I - Array */
165 * Range check input...
172 * Return the number of elements...
175 return (a
->num_elements
);
180 * 'cupsArrayCurrent()' - Return the current element in the array.
183 void * /* O - Element */
184 cupsArrayCurrent(cups_array_t
*a
) /* I - Array */
187 * Range check input...
194 * Return the current element...
197 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
198 return (a
->elements
[a
->current
]);
205 * 'cupsArrayDelete()' - Free all memory used by the array.
209 cupsArrayDelete(cups_array_t
*a
) /* I - Array */
212 * Range check input...
219 * Free the array of element pointers - the caller is responsible
220 * for freeing the elements themselves...
223 if (a
->alloc_elements
)
234 * 'cupsArrayDup()' - Duplicate the array.
237 cups_array_t
* /* O - Duplicate array */
238 cupsArrayDup(cups_array_t
*a
) /* I - Array */
240 cups_array_t
*da
; /* Duplicate array */
244 * Range check input...
251 * Allocate memory for the array...
254 da
= calloc(1, sizeof(cups_array_t
));
258 da
->compare
= a
->compare
;
260 da
->current
= a
->current
;
261 da
->insert
= a
->insert
;
262 da
->unique
= a
->unique
;
263 da
->num_saved
= a
->num_saved
;
265 memcpy(da
->saved
, a
->saved
, sizeof(a
->saved
));
270 * Allocate memory for the elements...
273 da
->elements
= malloc(a
->num_elements
* sizeof(void *));
281 * Copy the element pointers...
284 memcpy(da
->elements
, a
->elements
, a
->num_elements
* sizeof(void *));
285 da
->num_elements
= a
->num_elements
;
286 da
->alloc_elements
= a
->num_elements
;
290 * Return the new array...
298 * 'cupsArrayFind()' - Find an element in the array.
301 void * /* O - Element found or NULL */
302 cupsArrayFind(cups_array_t
*a
, /* I - Array */
303 void *e
) /* I - Element */
305 int current
, /* Current element */
306 diff
, /* Difference */
307 hash
; /* Hash index */
311 * Range check input...
318 * See if we have any elements...
321 if (!a
->num_elements
)
325 * Yes, look for a match...
330 hash
= (*(a
->hashfunc
))(e
, a
->data
);
332 if (hash
< 0 || hash
>= a
->hashsize
)
334 current
= a
->current
;
339 current
= a
->hash
[hash
];
341 if (current
< 0 || current
>= a
->num_elements
)
342 current
= a
->current
;
347 current
= a
->current
;
351 current
= cups_array_find(a
, e
, current
, &diff
);
355 * Found a match! If the array does not contain unique values, find
356 * the first element that is the same...
359 if (!a
->unique
&& a
->compare
)
362 * The array is not unique, find the first match...
365 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
370 a
->current
= current
;
373 a
->hash
[hash
] = current
;
375 return (a
->elements
[current
]);
391 * 'cupsArrayFirst()' - Get the first element in the array.
394 void * /* O - First element or NULL */
395 cupsArrayFirst(cups_array_t
*a
) /* I - Array */
398 * Range check input...
405 * Return the first element...
410 return (cupsArrayCurrent(a
));
415 * 'cupsArrayGetIndex()' - Get the index of the current element.
420 int /* O - Index of the current element */
421 cupsArrayGetIndex(cups_array_t
*a
) /* I - Array */
431 * 'cupsArrayGetInsert()' - Get the index of the last inserted element.
436 int /* O - Index of the last inserted element */
437 cupsArrayGetInsert(cups_array_t
*a
) /* I - Array */
447 * 'cupsArrayIndex()' - Get the N-th element in the array.
450 void * /* O - N-th element or NULL */
451 cupsArrayIndex(cups_array_t
*a
, /* I - Array */
452 int n
) /* I - Index into array, starting at 0 */
459 return (cupsArrayCurrent(a
));
464 * 'cupsArrayInsert()' - Insert an element in the array.
466 * When inserting an element in a sorted array, non-unique elements are
467 * inserted at the beginning of the run. For unsorted arrays, the element
468 * is inserted at the beginning of the array.
471 int /* O - 0 on failure, 1 on success */
472 cupsArrayInsert(cups_array_t
*a
, /* I - Array */
473 void *e
) /* I - Element */
475 DEBUG_printf(("cupsArrayInsert(a=%p, e=%p)\n", a
, e
));
478 * Range check input...
483 DEBUG_puts("cupsArrayInsert: returning 0");
488 * Insert the element...
491 return (cups_array_add(a
, e
, 1));
496 * 'cupsArrayLast()' - Get the last element in the array.
499 void * /* O - Last element or NULL */
500 cupsArrayLast(cups_array_t
*a
) /* I - Array */
503 * Range check input...
510 * Return the last element...
513 a
->current
= a
->num_elements
- 1;
515 return (cupsArrayCurrent(a
));
520 * 'cupsArrayNew()' - Create a new array.
523 cups_array_t
* /* O - Array */
524 cupsArrayNew(cups_array_func_t f
, /* I - Comparison function */
525 void *d
) /* I - User data */
527 return (cupsArrayNew2(f
, d
, 0, 0));
532 * 'cupsArrayNew2()' - Create a new array with hash.
537 cups_array_t
* /* O - Array */
538 cupsArrayNew2(cups_array_func_t f
, /* I - Comparison function */
539 void *d
, /* I - User data */
540 cups_ahash_func_t h
, /* I - Hash function*/
541 int hsize
) /* I - Hash size */
543 cups_array_t
*a
; /* Array */
547 * Allocate memory for the array...
550 a
= calloc(1, sizeof(cups_array_t
));
565 a
->hash
= malloc(hsize
* sizeof(int));
573 memset(a
->hash
, -1, hsize
* sizeof(int));
581 * 'cupsArrayNext()' - Get the next element in the array.
584 void * /* O - Next element or NULL */
585 cupsArrayNext(cups_array_t
*a
) /* I - Array */
588 * Range check input...
595 * Return the next element...
598 if (a
->current
< a
->num_elements
)
601 return (cupsArrayCurrent(a
));
606 * 'cupsArrayPrev()' - Get the previous element in the array.
609 void * /* O - Previous element or NULL */
610 cupsArrayPrev(cups_array_t
*a
) /* I - Array */
613 * Range check input...
620 * Return the previous element...
626 return (cupsArrayCurrent(a
));
631 * 'cupsArrayRemove()' - Remove an element from the array.
634 int /* O - 1 on success, 0 on failure */
635 cupsArrayRemove(cups_array_t
*a
, /* I - Array */
636 void *e
) /* I - Element */
638 int i
, /* Looping var */
639 current
, /* Current element */
640 diff
; /* Difference */
644 * Range check input...
651 * See if the element is in the array...
654 if (!a
->num_elements
)
657 current
= cups_array_find(a
, e
, a
->current
, &diff
);
662 * Yes, now remove it...
667 if (current
< a
->num_elements
)
668 memmove(a
->elements
+ current
, a
->elements
+ current
+ 1,
669 (a
->num_elements
- current
) * sizeof(void *));
671 if (current
<= a
->current
)
674 if (current
< a
->insert
)
676 else if (current
== a
->insert
)
679 for (i
= 0; i
< a
->num_saved
; i
++)
680 if (current
<= a
->saved
[i
])
683 if (a
->num_elements
<= 1)
691 * 'cupsArrayRestore()' - Reset the current element to the last cupsArraySave.
694 void * /* O - New current element */
695 cupsArrayRestore(cups_array_t
*a
) /* I - Array */
700 if (a
->num_saved
<= 0)
704 a
->current
= a
->saved
[a
->num_saved
];
706 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
707 return (a
->elements
[a
->current
]);
714 * 'cupsArraySave()' - Mark the current element for a later cupsArrayRestore.
716 * The save/restore stack is guaranteed to be at least 32 elements deep.
719 int /* O - 1 on success, 0 on failure */
720 cupsArraySave(cups_array_t
*a
) /* I - Array */
725 if (a
->num_saved
>= _CUPS_MAXSAVE
)
728 a
->saved
[a
->num_saved
] = a
->current
;
736 * 'cupsArrayUserData()' - Return the user data for an array.
739 void * /* O - User data */
740 cupsArrayUserData(cups_array_t
*a
) /* I - Array */
750 * 'cups_array_add()' - Insert or append an element to the array...
753 static int /* O - 1 on success, 0 on failure */
754 cups_array_add(cups_array_t
*a
, /* I - Array */
755 void *e
, /* I - Element to add */
756 int insert
) /* I - 1 = insert, 0 = append */
758 int i
, /* Looping var */
759 current
, /* Current element */
760 diff
; /* Comparison with current element */
763 DEBUG_printf(("cups_array_add(a=%p, e=%p, insert=%d)\n", a
, e
, insert
));
766 * Verify we have room for the new element...
769 if (a
->num_elements
>= a
->alloc_elements
)
772 * Allocate additional elements; start with 16 elements, then
773 * double the size until 1024 elements, then add 1024 elements
777 void **temp
; /* New array elements */
778 int count
; /* New allocation count */
781 if (a
->alloc_elements
== 0)
784 temp
= malloc(count
* sizeof(void *));
788 if (a
->alloc_elements
< 1024)
789 count
= a
->alloc_elements
* 2;
791 count
= a
->alloc_elements
+ 1024;
793 temp
= realloc(a
->elements
, count
* sizeof(void *));
796 DEBUG_printf(("cups_array_add: count=%d\n", count
));
800 DEBUG_puts("cupsAddAdd: allocation failed, returning 0");
804 a
->alloc_elements
= count
;
809 * Find the insertion point for the new element; if there is no
810 * compare function or elements, just add it to the beginning or end...
813 if (!a
->num_elements
|| !a
->compare
)
816 * No elements or comparison function, insert/append as needed...
820 current
= 0; /* Insert at beginning */
822 current
= a
->num_elements
; /* Append to the end */
827 * Do a binary search for the insertion point...
830 current
= cups_array_find(a
, e
, a
->insert
, &diff
);
835 * Insert after the current element...
843 * Compared equal, make sure we add to the begining or end of
844 * the current run of equal elements...
852 * Insert at beginning of run...
855 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
862 * Append at end of run...
869 while (current
< a
->num_elements
&&
870 !(*(a
->compare
))(e
, a
->elements
[current
], a
->data
));
876 * Insert or append the element...
879 if (current
< a
->num_elements
)
882 * Shift other elements to the right...
885 memmove(a
->elements
+ current
+ 1, a
->elements
+ current
,
886 (a
->num_elements
- current
) * sizeof(void *));
888 if (a
->current
>= current
)
891 for (i
= 0; i
< a
->num_saved
; i
++)
892 if (a
->saved
[i
] >= current
)
895 DEBUG_printf(("cups_array_add: insert element at index %d...\n", current
));
899 printf("cups_array_add: append element at %d...\n", current
);
902 a
->elements
[current
] = e
;
907 for (current
= 0; current
< a
->num_elements
; current
++)
908 printf("cups_array_add: a->elements[%d]=%p\n", current
, a
->elements
[current
]);
911 DEBUG_puts("cups_array_add: returning 1");
918 * 'cups_array_find()' - Find an element in the array...
921 static int /* O - Index of match */
922 cups_array_find(cups_array_t
*a
, /* I - Array */
923 void *e
, /* I - Element */
924 int prev
, /* I - Previous index */
925 int *rdiff
) /* O - Difference of match */
927 int left
, /* Left side of search */
928 right
, /* Right side of search */
929 current
, /* Current element */
930 diff
; /* Comparison with current element */
933 DEBUG_printf(("cups_array_find(a=%p, e=%p, prev=%d, rdiff=%p)\n", a
, e
, prev
,
939 * Do a binary search for the element...
942 DEBUG_puts("cups_array_find: binary search");
944 if (prev
>= 0 && prev
< a
->num_elements
)
947 * Start search on either side of previous...
950 if ((diff
= (*(a
->compare
))(e
, a
->elements
[prev
], a
->data
)) == 0 ||
951 (diff
< 0 && prev
== 0) ||
952 (diff
> 0 && prev
== (a
->num_elements
- 1)))
955 * Exact or edge match, return it!
958 DEBUG_printf(("cups_array_find: Returning %d, diff=%d\n", prev
, diff
));
967 * Start with previous on right side...
976 * Start wih previous on left side...
980 right
= a
->num_elements
- 1;
986 * Start search in the middle...
990 right
= a
->num_elements
- 1;
995 current
= (left
+ right
) / 2;
996 diff
= (*(a
->compare
))(e
, a
->elements
[current
], a
->data
);
998 DEBUG_printf(("cups_array_find: left=%d, right=%d, current=%d, diff=%d\n",
999 left
, right
, current
, diff
));
1008 while ((right
- left
) > 1);
1013 * Check the last 1 or 2 elements...
1016 if ((diff
= (*(a
->compare
))(e
, a
->elements
[left
], a
->data
)) <= 0)
1020 diff
= (*(a
->compare
))(e
, a
->elements
[right
], a
->data
);
1028 * Do a linear pointer search...
1031 DEBUG_puts("cups_array_find: linear search");
1035 for (current
= 0; current
< a
->num_elements
; current
++)
1036 if (a
->elements
[current
] == e
)
1044 * Return the closest element and the difference...
1047 DEBUG_printf(("cups_array_find: Returning %d, diff=%d\n", current
, diff
));
1056 * End of "$Id: array.c 6649 2007-07-11 21:46:42Z mike $".