4 * Sorted array routines for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include <cups/cups.h>
23 #include "string-private.h"
24 #include "debug-private.h"
25 #include "array-private.h"
32 #define _CUPS_MAXSAVE 32 /**** Maximum number of saves ****/
36 * Types and structures...
39 struct _cups_array_s
/**** CUPS array structure ****/
42 * The current implementation uses an insertion sort into an array of
43 * sorted pointers. We leave the array type private/opaque so that we
44 * can change the underlying implementation without affecting the users
48 int num_elements
, /* Number of array elements */
49 alloc_elements
, /* Allocated array elements */
50 current
, /* Current element */
51 insert
, /* Last inserted element */
52 unique
, /* Are all elements unique? */
53 num_saved
, /* Number of saved elements */
56 void **elements
; /* Array elements */
57 cups_array_func_t compare
; /* Element comparison function */
58 void *data
; /* User data passed to compare */
59 cups_ahash_func_t hashfunc
; /* Hash function */
60 int hashsize
, /* Size of hash */
61 *hash
; /* Hash array */
62 cups_acopy_func_t copyfunc
; /* Copy function */
63 cups_afree_func_t freefunc
; /* Free function */
71 static int cups_array_add(cups_array_t
*a
, void *e
, int insert
);
72 static int cups_array_find(cups_array_t
*a
, void *e
, int prev
, int *rdiff
);
76 * 'cupsArrayAdd()' - Add an element to the array.
78 * When adding an element to a sorted array, non-unique elements are
79 * appended at the end of the run of identical elements. For unsorted arrays,
80 * the element is appended to the end of the array.
82 * @since CUPS 1.2/OS X 10.5@
85 int /* O - 1 on success, 0 on failure */
86 cupsArrayAdd(cups_array_t
*a
, /* I - Array */
87 void *e
) /* I - Element */
89 DEBUG_printf(("2cupsArrayAdd(a=%p, e=%p)", (void *)a
, e
));
92 * Range check input...
97 DEBUG_puts("3cupsArrayAdd: returning 0");
102 * Append the element...
105 return (cups_array_add(a
, e
, 0));
110 * '_cupsArrayAddStrings()' - Add zero or more delimited strings to an array.
112 * Note: The array MUST be created using the @link _cupsArrayNewStrings@
113 * function. Duplicate strings are NOT added. If the string pointer "s" is NULL
114 * or the empty string, no strings are added to the array.
117 int /* O - 1 on success, 0 on failure */
118 _cupsArrayAddStrings(cups_array_t
*a
, /* I - Array */
119 const char *s
, /* I - Delimited strings or NULL */
120 char delim
)/* I - Delimiter character */
122 char *buffer
, /* Copy of string */
123 *start
, /* Start of string */
124 *end
; /* End of string */
125 int status
= 1; /* Status of add */
128 DEBUG_printf(("_cupsArrayAddStrings(a=%p, s=\"%s\", delim='%c')", (void *)a
, s
, delim
));
132 DEBUG_puts("1_cupsArrayAddStrings: Returning 0");
139 * Skip leading whitespace...
142 DEBUG_puts("1_cupsArrayAddStrings: Skipping leading whitespace.");
144 while (*s
&& isspace(*s
& 255))
147 DEBUG_printf(("1_cupsArrayAddStrings: Remaining string \"%s\".", s
));
150 if (!strchr(s
, delim
) &&
151 (delim
!= ' ' || (!strchr(s
, '\t') && !strchr(s
, '\n'))))
154 * String doesn't contain a delimiter, so add it as a single value...
157 DEBUG_puts("1_cupsArrayAddStrings: No delimiter seen, adding a single "
160 if (!cupsArrayFind(a
, (void *)s
))
161 status
= cupsArrayAdd(a
, (void *)s
);
163 else if ((buffer
= strdup(s
)) == NULL
)
165 DEBUG_puts("1_cupsArrayAddStrings: Unable to duplicate string.");
170 for (start
= end
= buffer
; *end
; start
= end
)
173 * Find the end of the current delimited string and see if we need to add
179 while (*end
&& !isspace(*end
& 255))
181 while (*end
&& isspace(*end
& 255))
184 else if ((end
= strchr(start
, delim
)) != NULL
)
187 end
= start
+ strlen(start
);
189 DEBUG_printf(("1_cupsArrayAddStrings: Adding \"%s\", end=\"%s\"", start
,
192 if (!cupsArrayFind(a
, start
))
193 status
&= cupsArrayAdd(a
, start
);
199 DEBUG_printf(("1_cupsArrayAddStrings: Returning %d.", status
));
206 * 'cupsArrayClear()' - Clear the array.
208 * This function is equivalent to removing all elements in the array.
209 * The caller is responsible for freeing the memory used by the
210 * elements themselves.
212 * @since CUPS 1.2/OS X 10.5@
216 cupsArrayClear(cups_array_t
*a
) /* I - Array */
219 * Range check input...
226 * Free the existing elements as needed..
231 int i
; /* Looping var */
232 void **e
; /* Current element */
234 for (i
= a
->num_elements
, e
= a
->elements
; i
> 0; i
--, e
++)
235 (a
->freefunc
)(*e
, a
->data
);
239 * Set the number of elements to 0; we don't actually free the memory
240 * here - that is done in cupsArrayDelete()...
252 * 'cupsArrayCount()' - Get the number of elements in the array.
254 * @since CUPS 1.2/OS X 10.5@
257 int /* O - Number of elements */
258 cupsArrayCount(cups_array_t
*a
) /* I - Array */
261 * Range check input...
268 * Return the number of elements...
271 return (a
->num_elements
);
276 * 'cupsArrayCurrent()' - Return the current element in the array.
278 * The current element is undefined until you call @link cupsArrayFind@,
279 * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@.
281 * @since CUPS 1.2/OS X 10.5@
284 void * /* O - Element */
285 cupsArrayCurrent(cups_array_t
*a
) /* I - Array */
288 * Range check input...
295 * Return the current element...
298 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
299 return (a
->elements
[a
->current
]);
306 * 'cupsArrayDelete()' - Free all memory used by the array.
308 * The caller is responsible for freeing the memory used by the
309 * elements themselves.
311 * @since CUPS 1.2/OS X 10.5@
315 cupsArrayDelete(cups_array_t
*a
) /* I - Array */
318 * Range check input...
325 * Free the elements if we have a free function (otherwise the caller is
326 * responsible for doing the dirty work...)
331 int i
; /* Looping var */
332 void **e
; /* Current element */
334 for (i
= a
->num_elements
, e
= a
->elements
; i
> 0; i
--, e
++)
335 (a
->freefunc
)(*e
, a
->data
);
339 * Free the array of element pointers...
342 if (a
->alloc_elements
)
353 * 'cupsArrayDup()' - Duplicate the array.
355 * @since CUPS 1.2/OS X 10.5@
358 cups_array_t
* /* O - Duplicate array */
359 cupsArrayDup(cups_array_t
*a
) /* I - Array */
361 cups_array_t
*da
; /* Duplicate array */
365 * Range check input...
372 * Allocate memory for the array...
375 da
= calloc(1, sizeof(cups_array_t
));
379 da
->compare
= a
->compare
;
381 da
->current
= a
->current
;
382 da
->insert
= a
->insert
;
383 da
->unique
= a
->unique
;
384 da
->num_saved
= a
->num_saved
;
386 memcpy(da
->saved
, a
->saved
, sizeof(a
->saved
));
391 * Allocate memory for the elements...
394 da
->elements
= malloc((size_t)a
->num_elements
* sizeof(void *));
402 * Copy the element pointers...
408 * Use the copy function to make a copy of each element...
411 int i
; /* Looping var */
413 for (i
= 0; i
< a
->num_elements
; i
++)
414 da
->elements
[i
] = (a
->copyfunc
)(a
->elements
[i
], a
->data
);
419 * Just copy raw pointers...
422 memcpy(da
->elements
, a
->elements
, (size_t)a
->num_elements
* sizeof(void *));
425 da
->num_elements
= a
->num_elements
;
426 da
->alloc_elements
= a
->num_elements
;
430 * Return the new array...
438 * 'cupsArrayFind()' - Find an element in the array.
440 * @since CUPS 1.2/OS X 10.5@
443 void * /* O - Element found or @code NULL@ */
444 cupsArrayFind(cups_array_t
*a
, /* I - Array */
445 void *e
) /* I - Element */
447 int current
, /* Current element */
448 diff
, /* Difference */
449 hash
; /* Hash index */
453 * Range check input...
460 * See if we have any elements...
463 if (!a
->num_elements
)
467 * Yes, look for a match...
472 hash
= (*(a
->hashfunc
))(e
, a
->data
);
474 if (hash
< 0 || hash
>= a
->hashsize
)
476 current
= a
->current
;
481 current
= a
->hash
[hash
];
483 if (current
< 0 || current
>= a
->num_elements
)
484 current
= a
->current
;
489 current
= a
->current
;
493 current
= cups_array_find(a
, e
, current
, &diff
);
497 * Found a match! If the array does not contain unique values, find
498 * the first element that is the same...
501 if (!a
->unique
&& a
->compare
)
504 * The array is not unique, find the first match...
507 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
512 a
->current
= current
;
515 a
->hash
[hash
] = current
;
517 return (a
->elements
[current
]);
533 * 'cupsArrayFirst()' - Get the first element in the array.
535 * @since CUPS 1.2/OS X 10.5@
538 void * /* O - First element or @code NULL@ if the array is empty */
539 cupsArrayFirst(cups_array_t
*a
) /* I - Array */
542 * Range check input...
549 * Return the first element...
554 return (cupsArrayCurrent(a
));
559 * 'cupsArrayGetIndex()' - Get the index of the current element.
561 * The current element is undefined until you call @link cupsArrayFind@,
562 * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@.
564 * @since CUPS 1.3/OS X 10.5@
567 int /* O - Index of the current element, starting at 0 */
568 cupsArrayGetIndex(cups_array_t
*a
) /* I - Array */
578 * 'cupsArrayGetInsert()' - Get the index of the last inserted element.
580 * @since CUPS 1.3/OS X 10.5@
583 int /* O - Index of the last inserted element, starting at 0 */
584 cupsArrayGetInsert(cups_array_t
*a
) /* I - Array */
594 * 'cupsArrayIndex()' - Get the N-th element in the array.
596 * @since CUPS 1.2/OS X 10.5@
599 void * /* O - N-th element or @code NULL@ */
600 cupsArrayIndex(cups_array_t
*a
, /* I - Array */
601 int n
) /* I - Index into array, starting at 0 */
608 return (cupsArrayCurrent(a
));
613 * 'cupsArrayInsert()' - Insert an element in the array.
615 * When inserting an element in a sorted array, non-unique elements are
616 * inserted at the beginning of the run of identical elements. For unsorted
617 * arrays, the element is inserted at the beginning of the array.
619 * @since CUPS 1.2/OS X 10.5@
622 int /* O - 0 on failure, 1 on success */
623 cupsArrayInsert(cups_array_t
*a
, /* I - Array */
624 void *e
) /* I - Element */
626 DEBUG_printf(("2cupsArrayInsert(a=%p, e=%p)", (void *)a
, e
));
629 * Range check input...
634 DEBUG_puts("3cupsArrayInsert: returning 0");
639 * Insert the element...
642 return (cups_array_add(a
, e
, 1));
647 * 'cupsArrayLast()' - Get the last element in the array.
649 * @since CUPS 1.2/OS X 10.5@
652 void * /* O - Last element or @code NULL@ if the array is empty */
653 cupsArrayLast(cups_array_t
*a
) /* I - Array */
656 * Range check input...
663 * Return the last element...
666 a
->current
= a
->num_elements
- 1;
668 return (cupsArrayCurrent(a
));
673 * 'cupsArrayNew()' - Create a new array.
675 * The comparison function ("f") is used to create a sorted array. The function
676 * receives pointers to two elements and the user data pointer ("d") - the user
677 * data pointer argument can safely be omitted when not required so functions
678 * like @code strcmp@ can be used for sorted string arrays.
680 * @since CUPS 1.2/OS X 10.5@
683 cups_array_t
* /* O - Array */
684 cupsArrayNew(cups_array_func_t f
, /* I - Comparison function or @code NULL@ for an unsorted array */
685 void *d
) /* I - User data pointer or @code NULL@ */
687 return (cupsArrayNew3(f
, d
, 0, 0, 0, 0));
692 * 'cupsArrayNew2()' - Create a new array with hash.
694 * The comparison function ("f") is used to create a sorted array. The function
695 * receives pointers to two elements and the user data pointer ("d") - the user
696 * data pointer argument can safely be omitted when not required so functions
697 * like @code strcmp@ can be used for sorted string arrays.
699 * The hash function ("h") is used to implement cached lookups with the
700 * specified hash size ("hsize").
702 * @since CUPS 1.3/OS X 10.5@
705 cups_array_t
* /* O - Array */
706 cupsArrayNew2(cups_array_func_t f
, /* I - Comparison function or @code NULL@ for an unsorted array */
707 void *d
, /* I - User data or @code NULL@ */
708 cups_ahash_func_t h
, /* I - Hash function or @code NULL@ for unhashed lookups */
709 int hsize
) /* I - Hash size (>= 0) */
711 return (cupsArrayNew3(f
, d
, h
, hsize
, 0, 0));
716 * 'cupsArrayNew3()' - Create a new array with hash and/or free function.
718 * The comparison function ("f") is used to create a sorted array. The function
719 * receives pointers to two elements and the user data pointer ("d") - the user
720 * data pointer argument can safely be omitted when not required so functions
721 * like @code strcmp@ can be used for sorted string arrays.
723 * The hash function ("h") is used to implement cached lookups with the
724 * specified hash size ("hsize").
726 * The copy function ("cf") is used to automatically copy/retain elements when
727 * added or the array is copied.
729 * The free function ("cf") is used to automatically free/release elements when
730 * removed or the array is deleted.
732 * @since CUPS 1.5/OS X 10.7@
735 cups_array_t
* /* O - Array */
736 cupsArrayNew3(cups_array_func_t f
, /* I - Comparison function or @code NULL@ for an unsorted array */
737 void *d
, /* I - User data or @code NULL@ */
738 cups_ahash_func_t h
, /* I - Hash function or @code NULL@ for unhashed lookups */
739 int hsize
, /* I - Hash size (>= 0) */
740 cups_acopy_func_t cf
, /* I - Copy function */
741 cups_afree_func_t ff
) /* I - Free function */
743 cups_array_t
*a
; /* Array */
747 * Allocate memory for the array...
750 a
= calloc(1, sizeof(cups_array_t
));
765 a
->hash
= malloc((size_t)hsize
* sizeof(int));
773 memset(a
->hash
, -1, (size_t)hsize
* sizeof(int));
784 * '_cupsArrayNewStrings()' - Create a new array of comma-delimited strings.
786 * Note: The array automatically manages copies of the strings passed. If the
787 * string pointer "s" is NULL or the empty string, no strings are added to the
788 * newly created array.
791 cups_array_t
* /* O - Array */
792 _cupsArrayNewStrings(const char *s
, /* I - Delimited strings or NULL */
793 char delim
) /* I - Delimiter character */
795 cups_array_t
*a
; /* Array */
798 if ((a
= cupsArrayNew3((cups_array_func_t
)strcmp
, NULL
, NULL
, 0,
799 (cups_acopy_func_t
)_cupsStrAlloc
,
800 (cups_afree_func_t
)_cupsStrFree
)) != NULL
)
801 _cupsArrayAddStrings(a
, s
, delim
);
808 * 'cupsArrayNext()' - Get the next element in the array.
810 * This function is equivalent to "cupsArrayIndex(a, cupsArrayGetIndex(a) + 1)".
812 * The next element is undefined until you call @link cupsArrayFind@,
813 * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@
814 * to set the current element.
816 * @since CUPS 1.2/OS X 10.5@
819 void * /* O - Next element or @code NULL@ */
820 cupsArrayNext(cups_array_t
*a
) /* I - Array */
823 * Range check input...
830 * Return the next element...
833 if (a
->current
< a
->num_elements
)
836 return (cupsArrayCurrent(a
));
841 * 'cupsArrayPrev()' - Get the previous element in the array.
843 * This function is equivalent to "cupsArrayIndex(a, cupsArrayGetIndex(a) - 1)".
845 * The previous element is undefined until you call @link cupsArrayFind@,
846 * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@
847 * to set the current element.
849 * @since CUPS 1.2/OS X 10.5@
852 void * /* O - Previous element or @code NULL@ */
853 cupsArrayPrev(cups_array_t
*a
) /* I - Array */
856 * Range check input...
863 * Return the previous element...
869 return (cupsArrayCurrent(a
));
874 * 'cupsArrayRemove()' - Remove an element from the array.
876 * If more than one element matches "e", only the first matching element is
879 * The caller is responsible for freeing the memory used by the
882 * @since CUPS 1.2/OS X 10.5@
885 int /* O - 1 on success, 0 on failure */
886 cupsArrayRemove(cups_array_t
*a
, /* I - Array */
887 void *e
) /* I - Element */
889 ssize_t i
, /* Looping var */
890 current
; /* Current element */
891 int diff
; /* Difference */
895 * Range check input...
902 * See if the element is in the array...
905 if (!a
->num_elements
)
908 current
= cups_array_find(a
, e
, a
->current
, &diff
);
913 * Yes, now remove it...
919 (a
->freefunc
)(a
->elements
[current
], a
->data
);
921 if (current
< a
->num_elements
)
922 memmove(a
->elements
+ current
, a
->elements
+ current
+ 1,
923 (size_t)(a
->num_elements
- current
) * sizeof(void *));
925 if (current
<= a
->current
)
928 if (current
< a
->insert
)
930 else if (current
== a
->insert
)
933 for (i
= 0; i
< a
->num_saved
; i
++)
934 if (current
<= a
->saved
[i
])
937 if (a
->num_elements
<= 1)
945 * 'cupsArrayRestore()' - Reset the current element to the last @link cupsArraySave@.
947 * @since CUPS 1.2/OS X 10.5@
950 void * /* O - New current element */
951 cupsArrayRestore(cups_array_t
*a
) /* I - Array */
956 if (a
->num_saved
<= 0)
960 a
->current
= a
->saved
[a
->num_saved
];
962 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
963 return (a
->elements
[a
->current
]);
970 * 'cupsArraySave()' - Mark the current element for a later @link cupsArrayRestore@.
972 * The current element is undefined until you call @link cupsArrayFind@,
973 * @link cupsArrayFirst@, or @link cupsArrayIndex@, or @link cupsArrayLast@
974 * to set the current element.
976 * The save/restore stack is guaranteed to be at least 32 elements deep.
978 * @since CUPS 1.2/OS X 10.5@
981 int /* O - 1 on success, 0 on failure */
982 cupsArraySave(cups_array_t
*a
) /* I - Array */
987 if (a
->num_saved
>= _CUPS_MAXSAVE
)
990 a
->saved
[a
->num_saved
] = a
->current
;
998 * 'cupsArrayUserData()' - Return the user data for an array.
1000 * @since CUPS 1.2/OS X 10.5@
1003 void * /* O - User data */
1004 cupsArrayUserData(cups_array_t
*a
) /* I - Array */
1014 * 'cups_array_add()' - Insert or append an element to the array.
1016 * @since CUPS 1.2/OS X 10.5@
1019 static int /* O - 1 on success, 0 on failure */
1020 cups_array_add(cups_array_t
*a
, /* I - Array */
1021 void *e
, /* I - Element to add */
1022 int insert
) /* I - 1 = insert, 0 = append */
1024 int i
, /* Looping var */
1025 current
; /* Current element */
1026 int diff
; /* Comparison with current element */
1029 DEBUG_printf(("7cups_array_add(a=%p, e=%p, insert=%d)", (void *)a
, e
, insert
));
1032 * Verify we have room for the new element...
1035 if (a
->num_elements
>= a
->alloc_elements
)
1038 * Allocate additional elements; start with 16 elements, then
1039 * double the size until 1024 elements, then add 1024 elements
1043 void **temp
; /* New array elements */
1044 int count
; /* New allocation count */
1047 if (a
->alloc_elements
== 0)
1050 temp
= malloc((size_t)count
* sizeof(void *));
1054 if (a
->alloc_elements
< 1024)
1055 count
= a
->alloc_elements
* 2;
1057 count
= a
->alloc_elements
+ 1024;
1059 temp
= realloc(a
->elements
, (size_t)count
* sizeof(void *));
1062 DEBUG_printf(("9cups_array_add: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
1066 DEBUG_puts("9cups_array_add: allocation failed, returning 0");
1070 a
->alloc_elements
= count
;
1075 * Find the insertion point for the new element; if there is no
1076 * compare function or elements, just add it to the beginning or end...
1079 if (!a
->num_elements
|| !a
->compare
)
1082 * No elements or comparison function, insert/append as needed...
1086 current
= 0; /* Insert at beginning */
1088 current
= a
->num_elements
; /* Append to the end */
1093 * Do a binary search for the insertion point...
1096 current
= cups_array_find(a
, e
, a
->insert
, &diff
);
1101 * Insert after the current element...
1109 * Compared equal, make sure we add to the begining or end of
1110 * the current run of equal elements...
1118 * Insert at beginning of run...
1121 while (current
> 0 && !(*(a
->compare
))(e
, a
->elements
[current
- 1],
1128 * Append at end of run...
1135 while (current
< a
->num_elements
&&
1136 !(*(a
->compare
))(e
, a
->elements
[current
], a
->data
));
1142 * Insert or append the element...
1145 if (current
< a
->num_elements
)
1148 * Shift other elements to the right...
1151 memmove(a
->elements
+ current
+ 1, a
->elements
+ current
,
1152 (size_t)(a
->num_elements
- current
) * sizeof(void *));
1154 if (a
->current
>= current
)
1157 for (i
= 0; i
< a
->num_saved
; i
++)
1158 if (a
->saved
[i
] >= current
)
1161 DEBUG_printf(("9cups_array_add: insert element at index " CUPS_LLFMT
, CUPS_LLCAST current
));
1165 DEBUG_printf(("9cups_array_add: append element at " CUPS_LLFMT
, CUPS_LLCAST current
));
1170 if ((a
->elements
[current
] = (a
->copyfunc
)(e
, a
->data
)) == NULL
)
1172 DEBUG_puts("8cups_array_add: Copy function returned NULL, returning 0");
1177 a
->elements
[current
] = e
;
1180 a
->insert
= current
;
1183 for (current
= 0; current
< a
->num_elements
; current
++)
1184 DEBUG_printf(("9cups_array_add: a->elements[" CUPS_LLFMT
"]=%p", CUPS_LLCAST current
, a
->elements
[current
]));
1187 DEBUG_puts("9cups_array_add: returning 1");
1194 * 'cups_array_find()' - Find an element in the array.
1197 static int /* O - Index of match */
1198 cups_array_find(cups_array_t
*a
, /* I - Array */
1199 void *e
, /* I - Element */
1200 int prev
, /* I - Previous index */
1201 int *rdiff
) /* O - Difference of match */
1203 int left
, /* Left side of search */
1204 right
, /* Right side of search */
1205 current
, /* Current element */
1206 diff
; /* Comparison with current element */
1209 DEBUG_printf(("7cups_array_find(a=%p, e=%p, prev=%d, rdiff=%p)", (void *)a
, e
, prev
, (void *)rdiff
));
1214 * Do a binary search for the element...
1217 DEBUG_puts("9cups_array_find: binary search");
1219 if (prev
>= 0 && prev
< a
->num_elements
)
1222 * Start search on either side of previous...
1225 if ((diff
= (*(a
->compare
))(e
, a
->elements
[prev
], a
->data
)) == 0 ||
1226 (diff
< 0 && prev
== 0) ||
1227 (diff
> 0 && prev
== (a
->num_elements
- 1)))
1230 * Exact or edge match, return it!
1233 DEBUG_printf(("9cups_array_find: Returning %d, diff=%d", prev
, diff
));
1242 * Start with previous on right side...
1251 * Start wih previous on left side...
1255 right
= a
->num_elements
- 1;
1261 * Start search in the middle...
1265 right
= a
->num_elements
- 1;
1270 current
= (left
+ right
) / 2;
1271 diff
= (*(a
->compare
))(e
, a
->elements
[current
], a
->data
);
1273 DEBUG_printf(("9cups_array_find: left=%d, right=%d, current=%d, diff=%d",
1274 left
, right
, current
, diff
));
1283 while ((right
- left
) > 1);
1288 * Check the last 1 or 2 elements...
1291 if ((diff
= (*(a
->compare
))(e
, a
->elements
[left
], a
->data
)) <= 0)
1295 diff
= (*(a
->compare
))(e
, a
->elements
[right
], a
->data
);
1303 * Do a linear pointer search...
1306 DEBUG_puts("9cups_array_find: linear search");
1310 for (current
= 0; current
< a
->num_elements
; current
++)
1311 if (a
->elements
[current
] == e
)
1319 * Return the closest element and the difference...
1322 DEBUG_printf(("8cups_array_find: Returning %d, diff=%d", current
, diff
));