File: array.c

package info (click to toggle)
ippsample 0.0~git20220607.72f89b3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 36,580 kB
  • sloc: ansic: 108,192; sh: 3,417; makefile: 1,163
file content (1192 lines) | stat: -rw-r--r-- 27,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
/*
 * Sorted array routines for CUPS.
 *
 * Copyright © 2021-2022 by OpenPrinting.
 * Copyright © 2007-2014 by Apple Inc.
 * Copyright © 1997-2007 by Easy Software Products.
 *
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more
 * information.
 */

/*
 * Include necessary headers...
 */

#include <cups/cups.h>
#include "string-private.h"
#include "debug-internal.h"


/*
 * Limits...
 */

#define _CUPS_MAXSAVE	32		/**** Maximum number of saves ****/


/*
 * Types and structures...
 */

struct _cups_array_s			/**** CUPS array structure ****/
{
 /*
  * The current implementation uses an insertion sort into an array of
  * sorted pointers.  We leave the array type private/opaque so that we
  * can change the underlying implementation without affecting the users
  * of this API.
  */

  size_t		num_elements,	/* Number of array elements */
			alloc_elements,	/* Allocated array elements */
			current,	/* Current element */
			insert,		/* Last inserted element */
			num_saved,	/* Number of saved elements */
			saved[_CUPS_MAXSAVE];
					/* Saved elements */
  void			**elements;	/* Array elements */
  cups_array_cb_t	compare;	/* Element comparison function */
  bool			unique;		/* Are all elements unique? */
  void			*data;		/* User data passed to compare */
  cups_ahash_cb_t	hashfunc;	/* Hash function */
  size_t		hashsize,	/* Size of hash */
			*hash;		/* Hash array */
  cups_acopy_cb_t	copyfunc;	/* Copy function */
  cups_afree_cb_t	freefunc;	/* Free function */
};


/*
 * Local functions...
 */

static bool	cups_array_add(cups_array_t *a, void *e, bool insert);
static size_t	cups_array_find(cups_array_t *a, void *e, size_t prev, int *rdiff);


/*
 * 'cupsArrayAdd()' - Add an element to an array.
 *
 * This function adds an element to an array.  When adding an element to a
 * sorted array, non-unique elements are appended at the end of the run of
 * identical elements.  For unsorted arrays, the element is appended to the end
 * of the array.
 */

bool					/* O - `true` on success, `false` on failure */
cupsArrayAdd(cups_array_t *a,		/* I - Array */
             void         *e)		/* I - Element */
{
  DEBUG_printf(("2cupsArrayAdd(a=%p, e=%p)", (void *)a, e));

 /*
  * Range check input...
  */

  if (!a || !e)
  {
    DEBUG_puts("3cupsArrayAdd: returning false");
    return (false);
  }

 /*
  * Append the element...
  */

  return (cups_array_add(a, e, false));
}


/*
 * 'cupsArrayAddStrings()' - Add zero or more delimited strings to an array.
 *
 * This function adds zero or more delimited strings to an array created using
 * the @link cupsArrayNewStrings@ function. Duplicate strings are *not* added.
 * If the string pointer "s" is `NULL` or the empty string, no strings are
 * added to the array.  If "delim" is the space character, then all whitespace
 * is recognized as a delimiter.
 */

bool					/* O - `true` on success, `false` on failure */
cupsArrayAddStrings(cups_array_t *a,	/* I - Array */
                    const char   *s,	/* I - Delimited strings or `NULL` */
                    char         delim)	/* I - Delimiter character */
{
  char		*buffer,		/* Copy of string */
		*start,			/* Start of string */
		*end;			/* End of string */
  bool		status = true;		/* Status of add */


  DEBUG_printf(("cupsArrayAddStrings(a=%p, s=\"%s\", delim='%c')", (void *)a, s, delim));

  if (!a || !s || !*s)
  {
    DEBUG_puts("1cupsArrayAddStrings: Returning 0");
    return (false);
  }

  if (delim == ' ')
  {
   /*
    * Skip leading whitespace...
    */

    DEBUG_puts("1cupsArrayAddStrings: Skipping leading whitespace.");

    while (*s && isspace(*s & 255))
      s ++;

    DEBUG_printf(("1cupsArrayAddStrings: Remaining string \"%s\".", s));
  }

  if (!strchr(s, delim) && (delim != ' ' || (!strchr(s, '\t') && !strchr(s, '\n'))))
  {
   /*
    * String doesn't contain a delimiter, so add it as a single value...
    */

    DEBUG_puts("1cupsArrayAddStrings: No delimiter seen, adding a single "
               "value.");

    if (!cupsArrayFind(a, (void *)s))
      status = cupsArrayAdd(a, (void *)s);
  }
  else if ((buffer = strdup(s)) == NULL)
  {
    DEBUG_puts("1cupsArrayAddStrings: Unable to duplicate string.");
    status = false;
  }
  else
  {
    for (start = end = buffer; *end; start = end)
    {
     /*
      * Find the end of the current delimited string and see if we need to add
      * it...
      */

      if (delim == ' ')
      {
        while (*end && !isspace(*end & 255))
          end ++;
        while (*end && isspace(*end & 255))
          *end++ = '\0';
      }
      else if ((end = strchr(start, delim)) != NULL)
        *end++ = '\0';
      else
        end = start + strlen(start);

      DEBUG_printf(("1cupsArrayAddStrings: Adding \"%s\", end=\"%s\"", start, end));

      if (!cupsArrayFind(a, start))
        status &= cupsArrayAdd(a, start);
    }

    free(buffer);
  }

  DEBUG_printf(("1cupsArrayAddStrings: Returning %s.", status ? "true" : "false"));

  return (status);
}


/*
 * 'cupsArrayClear()' - Clear an array.
 *
 * This function is equivalent to removing all elements in the array, so the
 * free callback (if any) is called for each element that is removed.
 */

void
cupsArrayClear(cups_array_t *a)		/* I - Array */
{
 /*
  * Range check input...
  */

  if (!a)
    return;

 /*
  * Free the existing elements as needed..
  */

  if (a->freefunc)
  {
    size_t	i;			/* Looping var */
    void	**e;			/* Current element */

    for (i = a->num_elements, e = a->elements; i > 0; i --, e ++)
      (a->freefunc)(*e, a->data);
  }

 /*
  * Set the number of elements to 0; we don't actually free the memory
  * here - that is done in cupsArrayDelete()...
  */

  a->num_elements = 0;
  a->current      = SIZE_MAX;
  a->insert       = SIZE_MAX;
  a->unique       = true;
  a->num_saved    = 0;
}


/*
 * 'cupsArrayDelete()' - Free all memory used by an array.
 *
 * This function frees all memory used by an array.  The free callback (if any)
 * is called for each element in the array.
 */

void
cupsArrayDelete(cups_array_t *a)	/* I - Array */
{
 /*
  * Range check input...
  */

  if (!a)
    return;

 /*
  * Clear the array...
  */

  cupsArrayClear(a);

 /*
  * Free the other buffers...
  */

  free(a->elements);
  free(a->hash);
  free(a);
}


/*
 * 'cupsArrayDup()' - Duplicate an array.
 */

cups_array_t *				/* O - Duplicate array */
cupsArrayDup(cups_array_t *a)		/* I - Array */
{
  cups_array_t	*da;			/* Duplicate array */


 /*
  * Range check input...
  */

  if (!a)
    return (NULL);

 /*
  * Allocate memory for the array...
  */

  da = calloc(1, sizeof(cups_array_t));
  if (!da)
    return (NULL);

  da->compare   = a->compare;
  da->copyfunc  = a->copyfunc;
  da->freefunc  = a->freefunc;
  da->data      = a->data;
  da->current   = a->current;
  da->insert    = a->insert;
  da->unique    = a->unique;
  da->num_saved = a->num_saved;

  memcpy(da->saved, a->saved, sizeof(a->saved));

  if (a->num_elements)
  {
   /*
    * Allocate memory for the elements...
    */

    da->elements = malloc((size_t)a->num_elements * sizeof(void *));
    if (!da->elements)
    {
      free(da);
      return (NULL);
    }

   /*
    * Copy the element pointers...
    */

    if (a->copyfunc)
    {
     /*
      * Use the copy function to make a copy of each element...
      */

      size_t	i;			/* Looping var */

      for (i = 0; i < a->num_elements; i ++)
	da->elements[i] = (a->copyfunc)(a->elements[i], a->data);
    }
    else
    {
     /*
      * Just copy raw pointers...
      */

      memcpy(da->elements, a->elements, (size_t)a->num_elements * sizeof(void *));
    }

    da->num_elements   = a->num_elements;
    da->alloc_elements = a->num_elements;
  }

 /*
  * Return the new array...
  */

  return (da);
}


/*
 * 'cupsArrayFind()' - Find an element in an array.
 */

void *					/* O - Element found or @code NULL@ */
cupsArrayFind(cups_array_t *a,		/* I - Array */
              void         *e)		/* I - Element */
{
  size_t	current,		/* Current element */
		hash;			/* Hash index */
  int		diff;			/* Difference */


 /*
  * Range check input...
  */

  if (!a || !a->num_elements || !e)
    return (NULL);

 /*
  * Look for a match...
  */

  if (a->hash)
  {
    if ((hash = (*(a->hashfunc))(e, a->data)) >= a->hashsize)
    {
      current = a->current;
      hash    = SIZE_MAX;
    }
    else if ((current = a->hash[hash]) >= a->num_elements)
      current = a->current;
  }
  else
  {
    current = a->current;
    hash    = SIZE_MAX;
  }

  current = cups_array_find(a, e, current, &diff);
  if (!diff)
  {
   /*
    * Found a match!  If the array does not contain unique values, find
    * the first element that is the same...
    */

    if (!a->unique && a->compare)
    {
     /*
      * The array is not unique, find the first match...
      */

      while (current > 0 && !(*(a->compare))(e, a->elements[current - 1], a->data))
        current --;
    }

    a->current = current;

    if (hash < a->hashsize)
      a->hash[hash] = current;

    return (a->elements[current]);
  }
  else
  {
   /*
    * No match...
    */

    a->current = SIZE_MAX;

    return (NULL);
  }
}


/*
 * 'cupsArrayGetCount()' - Get the number of elements in an array.
 */

size_t					/* O - Number of elements */
cupsArrayGetCount(cups_array_t *a)	/* I - Array */
{
  return (a ? a->num_elements : 0);
}


/*
 * 'cupsArrayGetCurrent()' - Return the current element in an array.
 *
 * This function returns the current element in an array.  The current element
 * is undefined until you call @link cupsArrayFind@, @link cupsArrayGetElement@,
 * @link cupsArrayGetFirst@, or @link cupsArrayGetLast@.
 */

void *					/* O - Element */
cupsArrayGetCurrent(cups_array_t *a)	/* I - Array */
{
 /*
  * Range check input...
  */

  if (!a)
    return (NULL);

 /*
  * Return the current element...
  */

  if (a->current < a->num_elements)
    return (a->elements[a->current]);
  else
    return (NULL);
}


/*
 * 'cupsArrayGetFirst()' - Get the first element in an array.
 */

void *					/* O - First element or `NULL` if the array is empty */
cupsArrayGetFirst(cups_array_t *a)	/* I - Array */
{
  return (cupsArrayGetElement(a, 0));
}


/*
 * 'cupsArrayGetIndex()' - Get the index of the current element.
 *
 * This function returns the index of the current element or `SIZE_MAX` if
 * there is no current element.  The current element is undefined until you call
 * @link cupsArrayFind@, @link cupsArrayGetElement@, @link cupsArrayGetFirst@,
 * or @link cupsArrayGetLast@.
 */

size_t					/* O - Index of the current element, starting at 0 */
cupsArrayGetIndex(cups_array_t *a)	/* I - Array */
{
  return (a ? a->current : SIZE_MAX);
}


/*
 * 'cupsArrayGetInsert()' - Get the index of the last added or inserted element.
 *
 * This function returns the index of the last added or inserted element or
 * `SIZE_MAX` if no elements have been added or inserted.
 */

size_t					/* O - Index of the last added or inserted element, starting at 0 */
cupsArrayGetInsert(cups_array_t *a)	/* I - Array */
{
  return (a ? a->insert : SIZE_MAX);
}


/*
 * 'cupsArrayGetElement()' - Get the N-th element in the array.
 */

void *					/* O - N-th element or `NULL` */
cupsArrayGetElement(cups_array_t *a,	/* I - Array */
                    size_t       n)	/* I - Index into array, starting at 0 */
{
  if (!a || n >= a->num_elements)
    return (NULL);

  a->current = n;

  return (a->elements[n]);
}


/*
 * 'cupsArrayGetLast()' - Get the last element in the array.
 */

void *					/* O - Last element or`NULL` if the array is empty */
cupsArrayGetLast(cups_array_t *a)	/* I - Array */
{
 /*
  * Range check input...
  */

  if (!a || a->num_elements == 0)
    return (NULL);

 /*
  * Return the last element...
  */

  return (cupsArrayGetElement(a, a->num_elements - 1));
}


/*
 * 'cupsArrayGetNext()' - Get the next element in an array.
 *
 * This function returns the next element in an array.  The next element is
 * undefined until you call @link cupsArrayFind@, @link cupsArrayGetElement@,
 * @link cupsArrayGetFirst@, or @link cupsArrayGetLast@ to set the current
 * element.
 */

void *					/* O - Next element or @code NULL@ */
cupsArrayGetNext(cups_array_t *a)	/* I - Array */
{
  if (!a || a->num_elements == 0)
    return (NULL);
  else if (a->current == SIZE_MAX)
    return (cupsArrayGetElement(a, 0));
  else
    return (cupsArrayGetElement(a, a->current + 1));
}


/*
 * 'cupsArrayGetPrev()' - Get the previous element in an array.
 *
 * This function returns the previous element in an array.  The previous element
 * is undefined until you call @link cupsArrayFind@, @link cupsArrayGetElement@,
 * @link cupsArrayGetFirst@, or @link cupsArrayGetLast@ to set the current
 * element.
 */

void *					/* O - Previous element or @code NULL@ */
cupsArrayGetPrev(cups_array_t *a)	/* I - Array */
{
  if (!a || a->num_elements == 0 || a->current == 0 || a->current == SIZE_MAX)
    return (NULL);
  else
    return (cupsArrayGetElement(a, a->current - 1));
}


/*
 * 'cupsArrayGetUserData()' - Return the user data for an array.
 */

void *					/* O - User data */
cupsArrayGetUserData(cups_array_t *a)	/* I - Array */
{
  return (a ? a->data : NULL);
}


/*
 * 'cupsArrayInsert()' - Insert an element in an array.
 *
 * This function inserts an element in an array.  When inserting an element
 * in a sorted array, non-unique elements are inserted at the beginning of the
 * run of identical elements.  For unsorted arrays, the element is inserted at
 * the beginning of the array.
 */

bool					/* O - `true` on success, `false` on failure */
cupsArrayInsert(cups_array_t *a,	/* I - Array */
		void         *e)	/* I - Element */
{
  DEBUG_printf(("2cupsArrayInsert(a=%p, e=%p)", (void *)a, e));

 /*
  * Range check input...
  */

  if (!a || !e)
  {
    DEBUG_puts("3cupsArrayInsert: returning false");
    return (false);
  }

 /*
  * Insert the element...
  */

  return (cups_array_add(a, e, true));
}


/*
 * 'cupsArrayNew()' - Create a new array with callback functions.
 *
 * This function creates a new array with optional callback functions.  The
 * comparison callback function ("f") is used to create a sorted array.  The
 * function receives pointers to two elements and the user data pointer ("d").
 * The user data pointer argument can safely be omitted when not required so
 * functions like `strcmp` can be used for sorted string arrays.
 *
 * ```
 * int                  // -1 if a < b, 0 if a == b, and 1 if a > b
 * compare_cb(void *a,  // First element
 *            void *b,  // Second element
 *            void *d)  // User data pointer
 * {
 *   ...
 * }
 * ```
 *
 * The hash callback function ("hf") is used to implement cached lookups with
 * the specified hash size ("hsize").  The function receives a pointer to an
 * element and the user data pointer ("d") and returns an unsigned integer
 * representing a hash into the array.  The hash value is of type `size_t` which
 * provides at least 32-bits of resolution.
 *
 * ```
 * size_t            // Hash value from 0 to (hashsize - 1)
 * hash_cb(void *e,  // Element
 *         void *d)  // User data pointer
 * {
 *   ...
 * }
 * ```
 *
 * The copy callback function ("cf") is used to automatically copy/retain
 * elements when added to the array or the array is copied with
 * @link cupsArrayDup@.  The function receives a pointer to the element and the
 * user data pointer ("d") and returns a new pointer that is stored in the array.
 *
 * ```
 * void *            // Pointer to copied/retained element or NULL
 * copy_cb(void *e,  // Element to copy/retain
 *         void *d)  // User data pointer
 * {
 *   ...
 * }
 * ```
 *
 * Finally, the free callback function ("cf") is used to automatically
 * free/release elements when removed or the array is deleted.  The function
 * receives a pointer to the element and the user data pointer ("d").
 *
 * ```
 * void
 * free_cb(void *e,  // Element to free/release
 *         void *d)  // User data pointer
 * {
 *   ...
 * }
 * ```
 */

cups_array_t *				/* O - Array */
cupsArrayNew(cups_array_cb_t  f,	/* I - Comparison callback function or `NULL` for an unsorted array */
             void             *d,	/* I - User data or `NULL` */
             cups_ahash_cb_t  hf,	/* I - Hash callback function or `NULL` for unhashed lookups */
	     size_t           hsize,	/* I - Hash size (>= `0`) */
	     cups_acopy_cb_t  cf,	/* I - Copy callback function or `NULL` for none */
	     cups_afree_cb_t  ff)	/* I - Free callback function or `NULL` for none */
{
  cups_array_t	*a;			/* Array  */


 /*
  * Allocate memory for the array...
  */

  if ((a = calloc(1, sizeof(cups_array_t))) == NULL)
    return (NULL);

  a->compare   = f;
  a->data      = d;
  a->current   = SIZE_MAX;
  a->insert    = SIZE_MAX;
  a->num_saved = 0;
  a->unique    = true;

  if (hsize > 0 && hf)
  {
    a->hashfunc  = hf;
    a->hashsize  = hsize;
    a->hash      = malloc((size_t)hsize * sizeof(size_t));

    if (!a->hash)
    {
      free(a);
      return (NULL);
    }

    memset(a->hash, -1, (size_t)hsize * sizeof(size_t));
  }

  a->copyfunc = cf;
  a->freefunc = ff;

  return (a);
}


/*
 * 'cupsArrayNewStrings()' - Create a new array of delimited strings.
 *
 * This function creates an array that holds zero or more strings.  The created
 * array automatically manages copies of the strings passed and sorts them in
 * ascending order using a case-sensitive comparison.  If the string pointer "s"
 * is `NULL` or the empty string, no strings are added to the newly created
 * array.
 *
 * Additional strings can be added using the @link cupsArrayAdd@ or
 * @link cupsArrayAddStrings@ functions.
 */

cups_array_t *				/* O - Array */
cupsArrayNewStrings(const char *s,	/* I - Delimited strings or `NULL` to create an empty array */
                    char       delim)	/* I - Delimiter character */
{
  cups_array_t	*a;			/* Array */


  if ((a = cupsArrayNew((cups_array_cb_t)strcmp, NULL, NULL, 0, (cups_acopy_cb_t)_cupsStrAlloc, (cups_afree_cb_t)_cupsStrFree)) != NULL)
    cupsArrayAddStrings(a, s, delim);

  return (a);
}


/*
 * 'cupsArrayRemove()' - Remove an element from an array.
 *
 * This function removes an element from an array.  If more than one element
 * matches "e", only the first matching element is removed.
 */

bool					/* O - `true` on success, `false` on failure */
cupsArrayRemove(cups_array_t *a,	/* I - Array */
                void         *e)	/* I - Element */
{
  size_t	i,			/* Looping var */
		current;		/* Current element */
  int		diff;			/* Difference */


 /*
  * Range check input...
  */

  if (!a || a->num_elements == 0 || !e)
    return (false);

 /*
  * See if the element is in the array...
  */

  current = cups_array_find(a, e, a->current, &diff);
  if (diff)
    return (false);

 /*
  * Yes, now remove it...
  */

  a->num_elements --;

  if (a->freefunc)
    (a->freefunc)(a->elements[current], a->data);

  if (current < a->num_elements)
    memmove(a->elements + current, a->elements + current + 1, (a->num_elements - current) * sizeof(void *));

  if (current <= a->current)
  {
    if (a->current)
      a->current --;
    else
      a->current = SIZE_MAX;
  }

  if (current < a->insert)
    a->insert --;
  else if (current == a->insert)
    a->insert = SIZE_MAX;

  for (i = 0; i < a->num_saved; i ++)
  {
    if (current <= a->saved[i])
      a->saved[i] --;
  }

  if (a->num_elements <= 1)
    a->unique = true;

  return (true);
}


/*
 * 'cupsArrayRestore()' - Reset the current element to the last @link cupsArraySave@.
 */

void *					/* O - New current element */
cupsArrayRestore(cups_array_t *a)	/* I - Array */
{
  if (!a || a->num_saved == 0)
    return (NULL);

  a->num_saved --;
  a->current = a->saved[a->num_saved];

  if (a->current < a->num_elements)
    return (a->elements[a->current]);
  else
    return (NULL);
}


/*
 * 'cupsArraySave()' - Mark the current element for a later @link cupsArrayRestore@.
 *
 * The current element is undefined until you call @link cupsArrayFind@,
 * @link cupsArrayGetElement@, @link cupsArrayGetFirst@, or
 * @link cupsArrayGetLast@ to set the current element.
 *
 * The save/restore stack is guaranteed to be at least 32 elements deep.
 */

bool					/* O - `true` on success, `false` on failure */
cupsArraySave(cups_array_t *a)		/* I - Array */
{
  if (!a || a->num_saved >= _CUPS_MAXSAVE)
    return (false);

  a->saved[a->num_saved] = a->current;
  a->num_saved ++;

  return (true);
}


/*
 * 'cups_array_add()' - Insert or append an element to the array.
 */

static bool				/* O - `true` on success, `false` on failure */
cups_array_add(cups_array_t *a,		/* I - Array */
               void         *e,		/* I - Element to add */
	       bool         insert)	/* I - `true` = insert, `false` = append */
{
  size_t	i,			/* Looping var */
		current;		/* Current element */
  int		diff;			/* Comparison with current element */


  DEBUG_printf(("7cups_array_add(a=%p, e=%p, insert=%d)", (void *)a, e, insert));

 /*
  * Verify we have room for the new element...
  */

  if (a->num_elements >= a->alloc_elements)
  {
   /*
    * Allocate additional elements; start with 16 elements, then double the
    * size until 1024 elements, then add 1024 elements thereafter...
    */

    void	**temp;			/* New array elements */
    size_t	count;			/* New allocation count */

    if (a->alloc_elements == 0)
      count = 16;
    else if (a->alloc_elements < 1024)
      count = a->alloc_elements * 2;
    else
      count = a->alloc_elements + 1024;

    DEBUG_printf(("9cups_array_add: count=" CUPS_LLFMT, CUPS_LLCAST count));

    if ((temp = realloc(a->elements, count * sizeof(void *))) == NULL)
    {
      DEBUG_puts("9cups_array_add: allocation failed, returning false");
      return (false);
    }

    a->alloc_elements = count;
    a->elements       = temp;
  }

 /*
  * Find the insertion point for the new element; if there is no
  * compare function or elements, just add it to the beginning or end...
  */

  if (!a->num_elements || !a->compare)
  {
   /*
    * No elements or comparison function, insert/append as needed...
    */

    if (insert)
      current = 0;			/* Insert at beginning */
    else
      current = a->num_elements;	/* Append to the end */
  }
  else
  {
   /*
    * Do a binary search for the insertion point...
    */

    current = cups_array_find(a, e, a->insert, &diff);

    if (diff > 0)
    {
     /*
      * Insert after the current element...
      */

      current ++;
    }
    else if (!diff)
    {
     /*
      * Compared equal, make sure we add to the begining or end of
      * the current run of equal elements...
      */

      a->unique = false;

      if (insert)
      {
       /*
        * Insert at beginning of run...
	*/

	while (current > 0 && !(*(a->compare))(e, a->elements[current - 1], a->data))
          current --;
      }
      else
      {
       /*
        * Append at end of run...
	*/

	do
	{
          current ++;
	}
	while (current < a->num_elements && !(*(a->compare))(e, a->elements[current], a->data));
      }
    }
  }

 /*
  * Insert or append the element...
  */

  if (current < a->num_elements)
  {
   /*
    * Shift other elements to the right...
    */

    memmove(a->elements + current + 1, a->elements + current, (a->num_elements - current) * sizeof(void *));

    if (a->current >= current)
      a->current ++;

    for (i = 0; i < a->num_saved; i ++)
    {
      if (a->saved[i] >= current)
	a->saved[i] ++;
    }

    DEBUG_printf(("9cups_array_add: insert element at index " CUPS_LLFMT, CUPS_LLCAST current));
  }
#ifdef DEBUG
  else
    DEBUG_printf(("9cups_array_add: append element at " CUPS_LLFMT, CUPS_LLCAST current));
#endif /* DEBUG */

  if (a->copyfunc)
  {
    if ((a->elements[current] = (a->copyfunc)(e, a->data)) == NULL)
    {
      DEBUG_puts("8cups_array_add: Copy function returned NULL, returning false");
      return (false);
    }
  }
  else
    a->elements[current] = e;

  a->num_elements ++;
  a->insert = current;

#ifdef DEBUG
  for (current = 0; current < a->num_elements; current ++)
    DEBUG_printf(("9cups_array_add: a->elements[" CUPS_LLFMT "]=%p", CUPS_LLCAST current, a->elements[current]));
#endif /* DEBUG */

  DEBUG_puts("9cups_array_add: returning true");

  return (true);
}


/*
 * 'cups_array_find()' - Find an element in the array.
 */

static size_t				/* O - Index of match */
cups_array_find(cups_array_t *a,	/* I - Array */
        	void         *e,	/* I - Element */
		size_t       prev,	/* I - Previous index */
		int          *rdiff)	/* O - Difference of match */
{
  size_t	left,			/* Left side of search */
		right,			/* Right side of search */
		current;		/* Current element */
  int		diff;			/* Comparison with current element */


  DEBUG_printf(("7cups_array_find(a=%p, e=%p, prev=%u, rdiff=%p)", (void *)a, e, (unsigned)prev, (void *)rdiff));

  if (a->compare)
  {
   /*
    * Do a binary search for the element...
    */

    DEBUG_puts("9cups_array_find: binary search");

    if (prev < a->num_elements)
    {
     /*
      * Start search on either side of previous...
      */

      if ((diff = (*(a->compare))(e, a->elements[prev], a->data)) == 0 ||
          (diff < 0 && prev == 0) ||
	  (diff > 0 && prev == (a->num_elements - 1)))
      {
       /*
        * Exact or edge match, return it!
	*/

        DEBUG_printf(("9cups_array_find: Returning %u, diff=%d", (unsigned)prev, diff));

	*rdiff = diff;

	return (prev);
      }
      else if (diff < 0)
      {
       /*
        * Start with previous on right side...
	*/

	left  = 0;
	right = prev;
      }
      else
      {
       /*
        * Start wih previous on left side...
	*/

        left  = prev;
	right = a->num_elements - 1;
      }
    }
    else
    {
     /*
      * Start search in the middle...
      */

      left  = 0;
      right = a->num_elements - 1;
    }

    do
    {
      current = (left + right) / 2;
      diff    = (*(a->compare))(e, a->elements[current], a->data);

      DEBUG_printf(("9cups_array_find: left=%u, right=%u, current=%u, diff=%d", (unsigned)left, (unsigned)right, (unsigned)current, diff));

      if (diff == 0)
	break;
      else if (diff < 0)
	right = current;
      else
	left = current;
    }
    while ((right - left) > 1);

    if (diff != 0)
    {
     /*
      * Check the last 1 or 2 elements...
      */

      if ((diff = (*(a->compare))(e, a->elements[left], a->data)) <= 0)
      {
        current = left;
      }
      else
      {
        diff    = (*(a->compare))(e, a->elements[right], a->data);
        current = right;
      }
    }
  }
  else
  {
   /*
    * Do a linear pointer search...
    */

    DEBUG_puts("9cups_array_find: linear search");

    diff = 1;

    for (current = 0; current < a->num_elements; current ++)
    {
      if (a->elements[current] == e)
      {
        diff = 0;
        break;
      }
    }
  }

 /*
  * Return the closest element and the difference...
  */

  DEBUG_printf(("8cups_array_find: Returning %u, diff=%d", (unsigned)current, diff));

  *rdiff = diff;

  return (current);
}