2 * "$Id: array.c 4921 2006-01-12 21:26:26Z 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 * cupsArrayLast() - Get the last element in the array.
38 * cupsArrayNew() - Create a new array.
39 * cupsArrayNext() - Get the next element in the array.
40 * cupsArrayPrev() - Get the previous element in the array.
41 * cupsArrayRemove() - Remove an element from the array.
42 * cupsArrayRestore() - Reset the current element to the last cupsArraySave.
43 * cupsArraySave() - Mark the current element for a later cupsArrayRestore.
44 * cups_find() - Find an element in the array...
48 * Include necessary headers...
60 #define _CUPS_MAXSAVE 32 /**** Maximum number of saves ****/
64 * Types and structures...
67 struct _cups_array_s
/**** CUPS array structure ****/
70 * The current implementation uses an insertion sort into an array of
71 * sorted pointers. We leave the array type private/opaque so that we
72 * can change the underlying implementation without affecting the users
76 int num_elements
, /* Number of array elements */
77 alloc_elements
, /* Allocated array elements */
78 current
, /* Current element */
79 insert
, /* Last inserted element */
80 num_saved
, /* Number of saved elements */
83 void **elements
; /* Array elements */
84 cups_array_func_t compare
; /* Element comparison function */
85 void *data
; /* User data passed to compare */
93 static int cups_find(cups_array_t
*a
, void *e
, int prev
, int *rdiff
);
97 * 'cupsArrayAdd()' - Add an element to the array.
100 int /* O - 1 on success, 0 on failure */
101 cupsArrayAdd(cups_array_t
*a
, /* I - Array */
102 void *e
) /* I - Element */
104 int i
, /* Looping var */
105 current
, /* Current element */
106 diff
; /* Comparison with current element */
109 DEBUG_printf(("cupsArrayAdd(a=%p, e=%p)\n", a
, e
));
112 * Range check input...
117 DEBUG_puts("cupsArrayAdd: returning 0");
122 * Verify we have room for the new element...
125 if (a
->num_elements
>= a
->alloc_elements
)
128 * Allocate additional elements; start with 16 elements, then
129 * double the size until 1024 elements, then add 1024 elements
133 void **temp
; /* New array elements */
134 int count
; /* New allocation count */
137 if (a
->alloc_elements
== 0)
140 temp
= malloc(count
* sizeof(void *));
144 if (a
->alloc_elements
< 1024)
145 count
= a
->alloc_elements
* 2;
147 count
= a
->alloc_elements
+ 1024;
149 temp
= realloc(a
->elements
, count
* sizeof(void *));
152 DEBUG_printf(("cupsArrayAdd: count=%d\n", count
));
156 DEBUG_puts("cupsAddAdd: allocation failed, returning 0");
160 a
->alloc_elements
= count
;
165 * Find the insertion point for the new element; if there is no
166 * compare function or elements, just add it to the end...
169 if (!a
->num_elements
|| !a
->compare
)
172 * Append to the end...
175 current
= a
->num_elements
;
180 * Do a binary search for the insertion point...
183 current
= cups_find(a
, e
, a
->insert
, &diff
);
190 * Insert or append the element...
193 if (current
< a
->num_elements
)
196 * Shift other elements to the right...
199 memmove(a
->elements
+ current
+ 1, a
->elements
+ current
,
200 (a
->num_elements
- current
) * sizeof(void *));
202 if (a
->current
>= current
)
205 for (i
= 0; i
< a
->num_saved
; i
++)
206 if (a
->saved
[i
] >= current
)
209 DEBUG_printf(("cupsArrayAdd: insert element at index %d...\n", current
));
213 printf("cupsArrayAdd: append element at %d...\n", current
);
216 a
->elements
[current
] = e
;
221 for (current
= 0; current
< a
->num_elements
; current
++)
222 printf("cupsArrayAdd: a->elements[%d]=%p\n", current
, a
->elements
[current
]);
225 DEBUG_puts("cupsArrayAdd: returning 1");
232 * 'cupsArrayClear()' - Clear the array.
236 cupsArrayClear(cups_array_t
*a
) /* I - Array */
239 * Range check input...
246 * Set the number of elements to 0; we don't actually free the memory
247 * here - that is done in cupsArrayDelete()...
258 * 'cupsArrayCount()' - Get the number of elements in the array.
261 int /* O - Number of elements */
262 cupsArrayCount(cups_array_t
*a
) /* I - Array */
265 * Range check input...
272 * Return the number of elements...
275 return (a
->num_elements
);
280 * 'cupsArrayCurrent()' - Return the current element in the array.
283 void * /* O - Element */
284 cupsArrayCurrent(cups_array_t
*a
) /* I - Array */
287 * Range check input...
294 * Return the current element...
297 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
298 return (a
->elements
[a
->current
]);
305 * 'cupsArrayDelete()' - Free all memory used by the array.
309 cupsArrayDelete(cups_array_t
*a
) /* I - Array */
312 * Range check input...
319 * Free the array of element pointers - the caller is responsible
320 * for freeing the elements themselves...
323 if (a
->alloc_elements
)
331 * 'cupsArrayDup()' - Duplicate the array.
334 cups_array_t
* /* O - Duplicate array */
335 cupsArrayDup(cups_array_t
*a
) /* I - Array */
337 cups_array_t
*da
; /* Duplicate array */
341 * Range check input...
348 * Allocate memory for the array...
351 da
= calloc(1, sizeof(cups_array_t
));
355 da
->compare
= a
->compare
;
357 da
->current
= a
->current
;
358 da
->insert
= a
->insert
;
359 da
->num_saved
= a
->num_saved
;
361 memcpy(da
->saved
, a
->saved
, sizeof(a
->saved
));
366 * Allocate memory for the elements...
369 da
->elements
= malloc(a
->num_elements
* sizeof(void *));
377 * Copy the element pointers...
380 memcpy(da
->elements
, a
->elements
, a
->num_elements
* sizeof(void *));
381 da
->num_elements
= a
->num_elements
;
382 da
->alloc_elements
= a
->num_elements
;
386 * Return the new array...
394 * 'cupsArrayFind()' - Find an element in the array.
397 void * /* O - Element found or NULL */
398 cupsArrayFind(cups_array_t
*a
, /* I - Array */
399 void *e
) /* I - Element */
401 int current
, /* Current element */
402 diff
; /* Difference */
406 * Range check input...
413 * See if we have any elements...
416 if (!a
->num_elements
)
420 * Yes, look for a match...
423 current
= cups_find(a
, e
, a
->current
, &diff
);
430 a
->current
= current
;
432 return (a
->elements
[current
]);
448 * 'cupsArrayFirst()' - Get the first element in the array.
451 void * /* O - First element or NULL */
452 cupsArrayFirst(cups_array_t
*a
) /* I - Array */
455 * Range check input...
462 * Return the first element...
467 return (cupsArrayCurrent(a
));
472 * 'cupsArrayIndex()' - Get the N-th element in the array.
475 void * /* O - N-th element or NULL */
476 cupsArrayIndex(cups_array_t
*a
, /* I - Array */
477 int n
) /* I - Index into array, starting at 0 */
484 return (cupsArrayCurrent(a
));
489 * 'cupsArrayLast()' - Get the last element in the array.
492 void * /* O - Last element or NULL */
493 cupsArrayLast(cups_array_t
*a
) /* I - Array */
496 * Range check input...
503 * Return the last element...
506 a
->current
= a
->num_elements
- 1;
508 return (cupsArrayCurrent(a
));
513 * 'cupsArrayNew()' - Create a new array.
516 cups_array_t
* /* O - Array */
517 cupsArrayNew(cups_array_func_t f
, /* I - Comparison function */
518 void *d
) /* I - User data */
520 cups_array_t
*a
; /* Array */
524 * Allocate memory for the array...
527 a
= calloc(1, sizeof(cups_array_t
));
542 * 'cupsArrayNext()' - Get the next element in the array.
545 void * /* O - Next element or NULL */
546 cupsArrayNext(cups_array_t
*a
) /* I - Array */
549 * Range check input...
556 * Return the next element...
559 if (a
->current
< a
->num_elements
)
562 return (cupsArrayCurrent(a
));
567 * 'cupsArrayPrev()' - Get the previous element in the array.
570 void * /* O - Previous element or NULL */
571 cupsArrayPrev(cups_array_t
*a
) /* I - Array */
574 * Range check input...
581 * Return the previous element...
587 return (cupsArrayCurrent(a
));
592 * 'cupsArrayRemove()' - Remove an element from the array.
595 int /* O - 1 on success, 0 on failure */
596 cupsArrayRemove(cups_array_t
*a
, /* I - Array */
597 void *e
) /* I - Element */
599 int i
, /* Looping var */
600 current
, /* Current element */
601 diff
; /* Difference */
605 * Range check input...
612 * See if the element is in the array...
615 if (!a
->num_elements
)
618 current
= cups_find(a
, e
, a
->current
, &diff
);
623 * Yes, now remove it...
628 if (current
< a
->num_elements
)
629 memmove(a
->elements
+ current
, a
->elements
+ current
+ 1,
630 (a
->num_elements
- current
) * sizeof(void *));
632 if (current
<= a
->current
)
635 if (current
<= a
->insert
)
638 for (i
= 0; i
< a
->num_saved
; i
++)
639 if (current
<= a
->saved
[i
])
647 * 'cupsArrayRestore()' - Reset the current element to the last cupsArraySave.
650 void * /* O - New current element */
651 cupsArrayRestore(cups_array_t
*a
) /* I - Array */
656 if (a
->num_saved
<= 0)
660 a
->current
= a
->saved
[a
->num_saved
];
662 if (a
->current
>= 0 && a
->current
< a
->num_elements
)
663 return (a
->elements
[a
->current
]);
670 * 'cupsArraySave()' - Mark the current element for a later cupsArrayRestore.
672 * The save/restore stack is guaranteed to be at least 32 elements deep.
675 int /* O - 1 on success, 0 on failure */
676 cupsArraySave(cups_array_t
*a
) /* I - Array */
681 if (a
->num_saved
>= _CUPS_MAXSAVE
)
684 a
->saved
[a
->num_saved
] = a
->current
;
692 * 'cups_find()' - Find an element in the array...
695 static int /* O - Index of match */
696 cups_find(cups_array_t
*a
, /* I - Array */
697 void *e
, /* I - Element */
698 int prev
, /* I - Previous index */
699 int *rdiff
) /* O - Difference of match */
701 int left
, /* Left side of search */
702 right
, /* Right side of search */
703 current
, /* Current element */
704 diff
; /* Comparison with current element */
707 DEBUG_printf(("cups_find(a=%p, e=%p, prev=%d, rdiff=%p)\n", a
, e
, prev
,
713 * Do a binary search for the element...
716 DEBUG_puts("cups_find: binary search");
718 if (prev
>= 0 && prev
< a
->num_elements
)
721 * Start search on either side of previous...
724 if ((diff
= (*(a
->compare
))(e
, a
->elements
[prev
], a
->data
)) == 0 ||
725 (diff
< 0 && prev
== 0) ||
726 (diff
> 0 && prev
== (a
->num_elements
- 1)))
729 * Exact or edge match, return it!
732 DEBUG_printf(("cups_find: Returning %d, diff=%d\n", prev
, diff
));
741 * Start with previous on right side...
750 * Start wih previous on left side...
754 right
= a
->num_elements
- 1;
760 * Start search in the middle...
764 right
= a
->num_elements
- 1;
769 current
= (left
+ right
) / 2;
770 diff
= (*(a
->compare
))(e
, a
->elements
[current
], a
->data
);
772 DEBUG_printf(("cups_find: left=%d, right=%d, current=%d, diff=%d\n",
773 left
, right
, current
, diff
));
782 while ((right
- left
) > 1);
787 * Check the last 1 or 2 elements...
790 if ((diff
= (*(a
->compare
))(e
, a
->elements
[left
], a
->data
)) <= 0)
794 diff
= (*(a
->compare
))(e
, a
->elements
[right
], a
->data
);
802 * Do a linear pointer search...
805 DEBUG_puts("cups_find: linear search");
809 for (current
= 0; current
< a
->num_elements
; current
++)
810 if (a
->elements
[current
] == e
)
815 * Return the closest element and the difference...
818 DEBUG_printf(("cups_find: Returning %d, diff=%d\n", current
, diff
));
827 * End of "$Id: array.c 4921 2006-01-12 21:26:26Z mike $".