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-2008 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.
108 int /* O - 1 on success, 0 on failure */
109 cupsArrayAdd(cups_array_t
*a
, /* I - Array */
110 void *e
) /* I - Element */
112 DEBUG_printf(("cupsArrayAdd(a=%p, e=%p)\n", a
, e
));
115 * Range check input...
120 DEBUG_puts("cupsArrayAdd: returning 0");
125 * Append the element...
128 return (cups_array_add(a
, e
, 0));
133 * '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.
167 int /* O - Number of elements */
168 cupsArrayCount(cups_array_t
*a
) /* I - Array */
171 * Range check input...
178 * Return the number of elements...
181 return (a
->num_elements
);
186 * 'cupsArrayCurrent()' - Return the current element in the array.
191 void * /* O - Element */
192 cupsArrayCurrent(cups_array_t
*a
) /* I - Array */
195 * Range check input...
202 * Return the current element...
205 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
206 return (a
->elements
[a
->current
]);
213 * 'cupsArrayDelete()' - Free all memory used by the array.
219 cupsArrayDelete(cups_array_t
*a
) /* I - Array */
222 * Range check input...
229 * Free the array of element pointers - the caller is responsible
230 * for freeing the elements themselves...
233 if (a
->alloc_elements
)
244 * 'cupsArrayDup()' - Duplicate the array.
249 cups_array_t
* /* O - Duplicate array */
250 cupsArrayDup(cups_array_t
*a
) /* I - Array */
252 cups_array_t
*da
; /* Duplicate array */
256 * Range check input...
263 * Allocate memory for the array...
266 da
= calloc(1, sizeof(cups_array_t
));
270 da
->compare
= a
->compare
;
272 da
->current
= a
->current
;
273 da
->insert
= a
->insert
;
274 da
->unique
= a
->unique
;
275 da
->num_saved
= a
->num_saved
;
277 memcpy(da
->saved
, a
->saved
, sizeof(a
->saved
));
282 * Allocate memory for the elements...
285 da
->elements
= malloc(a
->num_elements
* sizeof(void *));
293 * Copy the element pointers...
296 memcpy(da
->elements
, a
->elements
, a
->num_elements
* sizeof(void *));
297 da
->num_elements
= a
->num_elements
;
298 da
->alloc_elements
= a
->num_elements
;
302 * Return the new array...
310 * 'cupsArrayFind()' - Find an element in the array.
315 void * /* O - Element found or @code NULL@ */
316 cupsArrayFind(cups_array_t
*a
, /* I - Array */
317 void *e
) /* I - Element */
319 int current
, /* Current element */
320 diff
, /* Difference */
321 hash
; /* Hash index */
325 * Range check input...
332 * See if we have any elements...
335 if (!a
->num_elements
)
339 * Yes, look for a match...
344 hash
= (*(a
->hashfunc
))(e
, a
->data
);
346 if (hash
< 0 || hash
>= a
->hashsize
)
348 current
= a
->current
;
353 current
= a
->hash
[hash
];
355 if (current
< 0 || current
>= a
->num_elements
)
356 current
= a
->current
;
361 current
= a
->current
;
365 current
= cups_array_find(a
, e
, current
, &diff
);
369 * Found a match! If the array does not contain unique values, find
370 * the first element that is the same...
373 if (!a
->unique
&& a
->compare
)
376 * The array is not unique, find the first match...
379 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
384 a
->current
= current
;
387 a
->hash
[hash
] = current
;
389 return (a
->elements
[current
]);
405 * 'cupsArrayFirst()' - Get the first element in the array.
410 void * /* O - First element or @code NULL@ */
411 cupsArrayFirst(cups_array_t
*a
) /* I - Array */
414 * Range check input...
421 * Return the first element...
426 return (cupsArrayCurrent(a
));
431 * 'cupsArrayGetIndex()' - Get the index of the current element.
436 int /* O - Index of the current element */
437 cupsArrayGetIndex(cups_array_t
*a
) /* I - Array */
447 * 'cupsArrayGetInsert()' - Get the index of the last inserted element.
452 int /* O - Index of the last inserted element */
453 cupsArrayGetInsert(cups_array_t
*a
) /* I - Array */
463 * 'cupsArrayIndex()' - Get the N-th element in the array.
468 void * /* O - N-th element or @code NULL@ */
469 cupsArrayIndex(cups_array_t
*a
, /* I - Array */
470 int n
) /* I - Index into array, starting at 0 */
477 return (cupsArrayCurrent(a
));
482 * 'cupsArrayInsert()' - Insert an element in the array.
484 * When inserting an element in a sorted array, non-unique elements are
485 * inserted at the beginning of the run. For unsorted arrays, the element
486 * is inserted at the beginning of the array.
491 int /* O - 0 on failure, 1 on success */
492 cupsArrayInsert(cups_array_t
*a
, /* I - Array */
493 void *e
) /* I - Element */
495 DEBUG_printf(("cupsArrayInsert(a=%p, e=%p)\n", a
, e
));
498 * Range check input...
503 DEBUG_puts("cupsArrayInsert: returning 0");
508 * Insert the element...
511 return (cups_array_add(a
, e
, 1));
516 * 'cupsArrayLast()' - Get the last element in the array.
521 void * /* O - Last element or @code NULL@ */
522 cupsArrayLast(cups_array_t
*a
) /* I - Array */
525 * Range check input...
532 * Return the last element...
535 a
->current
= a
->num_elements
- 1;
537 return (cupsArrayCurrent(a
));
542 * 'cupsArrayNew()' - Create a new array.
547 cups_array_t
* /* O - Array */
548 cupsArrayNew(cups_array_func_t f
, /* I - Comparison function */
549 void *d
) /* I - User data */
551 return (cupsArrayNew2(f
, d
, 0, 0));
556 * 'cupsArrayNew2()' - Create a new array with hash.
561 cups_array_t
* /* O - Array */
562 cupsArrayNew2(cups_array_func_t f
, /* I - Comparison function */
563 void *d
, /* I - User data */
564 cups_ahash_func_t h
, /* I - Hash function*/
565 int hsize
) /* I - Hash size */
567 cups_array_t
*a
; /* Array */
571 * Allocate memory for the array...
574 a
= calloc(1, sizeof(cups_array_t
));
589 a
->hash
= malloc(hsize
* sizeof(int));
597 memset(a
->hash
, -1, hsize
* sizeof(int));
605 * 'cupsArrayNext()' - Get the next element in the array.
610 void * /* O - Next element or @code NULL@ */
611 cupsArrayNext(cups_array_t
*a
) /* I - Array */
614 * Range check input...
621 * Return the next element...
624 if (a
->current
< a
->num_elements
)
627 return (cupsArrayCurrent(a
));
632 * 'cupsArrayPrev()' - Get the previous element in the array.
637 void * /* O - Previous element or @code NULL@ */
638 cupsArrayPrev(cups_array_t
*a
) /* I - Array */
641 * Range check input...
648 * Return the previous element...
654 return (cupsArrayCurrent(a
));
659 * 'cupsArrayRemove()' - Remove an element from the array.
664 int /* O - 1 on success, 0 on failure */
665 cupsArrayRemove(cups_array_t
*a
, /* I - Array */
666 void *e
) /* I - Element */
668 int i
, /* Looping var */
669 current
, /* Current element */
670 diff
; /* Difference */
674 * Range check input...
681 * See if the element is in the array...
684 if (!a
->num_elements
)
687 current
= cups_array_find(a
, e
, a
->current
, &diff
);
692 * Yes, now remove it...
697 if (current
< a
->num_elements
)
698 memmove(a
->elements
+ current
, a
->elements
+ current
+ 1,
699 (a
->num_elements
- current
) * sizeof(void *));
701 if (current
<= a
->current
)
704 if (current
< a
->insert
)
706 else if (current
== a
->insert
)
709 for (i
= 0; i
< a
->num_saved
; i
++)
710 if (current
<= a
->saved
[i
])
713 if (a
->num_elements
<= 1)
721 * 'cupsArrayRestore()' - Reset the current element to the last cupsArraySave.
726 void * /* O - New current element */
727 cupsArrayRestore(cups_array_t
*a
) /* I - Array */
732 if (a
->num_saved
<= 0)
736 a
->current
= a
->saved
[a
->num_saved
];
738 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
739 return (a
->elements
[a
->current
]);
746 * 'cupsArraySave()' - Mark the current element for a later cupsArrayRestore.
748 * The save/restore stack is guaranteed to be at least 32 elements deep.
753 int /* O - 1 on success, 0 on failure */
754 cupsArraySave(cups_array_t
*a
) /* I - Array */
759 if (a
->num_saved
>= _CUPS_MAXSAVE
)
762 a
->saved
[a
->num_saved
] = a
->current
;
770 * 'cupsArrayUserData()' - Return the user data for an array.
775 void * /* O - User data */
776 cupsArrayUserData(cups_array_t
*a
) /* I - Array */
786 * 'cups_array_add()' - Insert or append an element to the array...
791 static int /* O - 1 on success, 0 on failure */
792 cups_array_add(cups_array_t
*a
, /* I - Array */
793 void *e
, /* I - Element to add */
794 int insert
) /* I - 1 = insert, 0 = append */
796 int i
, /* Looping var */
797 current
, /* Current element */
798 diff
; /* Comparison with current element */
801 DEBUG_printf(("cups_array_add(a=%p, e=%p, insert=%d)\n", a
, e
, insert
));
804 * Verify we have room for the new element...
807 if (a
->num_elements
>= a
->alloc_elements
)
810 * Allocate additional elements; start with 16 elements, then
811 * double the size until 1024 elements, then add 1024 elements
815 void **temp
; /* New array elements */
816 int count
; /* New allocation count */
819 if (a
->alloc_elements
== 0)
822 temp
= malloc(count
* sizeof(void *));
826 if (a
->alloc_elements
< 1024)
827 count
= a
->alloc_elements
* 2;
829 count
= a
->alloc_elements
+ 1024;
831 temp
= realloc(a
->elements
, count
* sizeof(void *));
834 DEBUG_printf(("cups_array_add: count=%d\n", count
));
838 DEBUG_puts("cupsAddAdd: allocation failed, returning 0");
842 a
->alloc_elements
= count
;
847 * Find the insertion point for the new element; if there is no
848 * compare function or elements, just add it to the beginning or end...
851 if (!a
->num_elements
|| !a
->compare
)
854 * No elements or comparison function, insert/append as needed...
858 current
= 0; /* Insert at beginning */
860 current
= a
->num_elements
; /* Append to the end */
865 * Do a binary search for the insertion point...
868 current
= cups_array_find(a
, e
, a
->insert
, &diff
);
873 * Insert after the current element...
881 * Compared equal, make sure we add to the begining or end of
882 * the current run of equal elements...
890 * Insert at beginning of run...
893 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
900 * Append at end of run...
907 while (current
< a
->num_elements
&&
908 !(*(a
->compare
))(e
, a
->elements
[current
], a
->data
));
914 * Insert or append the element...
917 if (current
< a
->num_elements
)
920 * Shift other elements to the right...
923 memmove(a
->elements
+ current
+ 1, a
->elements
+ current
,
924 (a
->num_elements
- current
) * sizeof(void *));
926 if (a
->current
>= current
)
929 for (i
= 0; i
< a
->num_saved
; i
++)
930 if (a
->saved
[i
] >= current
)
933 DEBUG_printf(("cups_array_add: insert element at index %d...\n", current
));
937 printf("cups_array_add: append element at %d...\n", current
);
940 a
->elements
[current
] = e
;
945 for (current
= 0; current
< a
->num_elements
; current
++)
946 printf("cups_array_add: a->elements[%d]=%p\n", current
, a
->elements
[current
]);
949 DEBUG_puts("cups_array_add: returning 1");
956 * 'cups_array_find()' - Find an element in the array...
961 static int /* O - Index of match */
962 cups_array_find(cups_array_t
*a
, /* I - Array */
963 void *e
, /* I - Element */
964 int prev
, /* I - Previous index */
965 int *rdiff
) /* O - Difference of match */
967 int left
, /* Left side of search */
968 right
, /* Right side of search */
969 current
, /* Current element */
970 diff
; /* Comparison with current element */
973 DEBUG_printf(("cups_array_find(a=%p, e=%p, prev=%d, rdiff=%p)\n", a
, e
, prev
,
979 * Do a binary search for the element...
982 DEBUG_puts("cups_array_find: binary search");
984 if (prev
>= 0 && prev
< a
->num_elements
)
987 * Start search on either side of previous...
990 if ((diff
= (*(a
->compare
))(e
, a
->elements
[prev
], a
->data
)) == 0 ||
991 (diff
< 0 && prev
== 0) ||
992 (diff
> 0 && prev
== (a
->num_elements
- 1)))
995 * Exact or edge match, return it!
998 DEBUG_printf(("cups_array_find: Returning %d, diff=%d\n", prev
, diff
));
1007 * Start with previous on right side...
1016 * Start wih previous on left side...
1020 right
= a
->num_elements
- 1;
1026 * Start search in the middle...
1030 right
= a
->num_elements
- 1;
1035 current
= (left
+ right
) / 2;
1036 diff
= (*(a
->compare
))(e
, a
->elements
[current
], a
->data
);
1038 DEBUG_printf(("cups_array_find: left=%d, right=%d, current=%d, diff=%d\n",
1039 left
, right
, current
, diff
));
1048 while ((right
- left
) > 1);
1053 * Check the last 1 or 2 elements...
1056 if ((diff
= (*(a
->compare
))(e
, a
->elements
[left
], a
->data
)) <= 0)
1060 diff
= (*(a
->compare
))(e
, a
->elements
[right
], a
->data
);
1068 * Do a linear pointer search...
1071 DEBUG_puts("cups_array_find: linear search");
1075 for (current
= 0; current
< a
->num_elements
; current
++)
1076 if (a
->elements
[current
] == e
)
1084 * Return the closest element and the difference...
1087 DEBUG_printf(("cups_array_find: Returning %d, diff=%d\n", current
, diff
));
1096 * End of "$Id: array.c 6649 2007-07-11 21:46:42Z mike $".