]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/api-array.shtml
Import CUPS v2.0.1
[thirdparty/cups.git] / cups / api-array.shtml
CommitLineData
ef416fc2 1<!--
75bd9771 2 "$Id: api-array.shtml 7616 2008-05-28 00:34:13Z mike $"
ef416fc2 3
eac3a0a0 4 Array API introduction for CUPS.
ef416fc2 5
eac3a0a0 6 Copyright 2007-2011 by Apple Inc.
bc44d920 7 Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 8
9 These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14-->
15
5a738aea 16<h2 class='title'><a name='OVERVIEW'>Overview</a></h2>
ef416fc2 17
5a738aea
MS
18<p>The CUPS array API provides a high-performance generic array container.
19The contents of the array container can be sorted and the container itself is
20designed for optimal speed and memory usage under a wide variety of conditions.
21Sorted arrays use a binary search algorithm from the last found or inserted
22element to quickly find matching elements in the array. Arrays created with the
23optional hash function can often find elements with a single lookup. The
24<a href='#cups_array_t'><code>cups_array_t</code></a> type is used when
25referring to a CUPS array.</p>
ef416fc2 26
27<p>The CUPS scheduler (<tt>cupsd</tt>) and many of the CUPS API
28functions use the array API to efficiently manage large lists of
29data.</p>
30
5a738aea 31<h3><a name='MANAGING_ARRAYS'>Managing Arrays</a></h3>
ef416fc2 32
5a738aea 33<p>Arrays are created using either the
10d09e33
MS
34<a href='#cupsArrayNew'><code>cupsArrayNew</code></a>,
35<a href='#cupsArrayNew2'><code>cupsArrayNew2</code></a>, or
36<a href='#cupsArrayNew2'><code>cupsArrayNew3</code></a> functions. The
5a738aea
MS
37first function creates a new array with the specified callback function
38and user data pointer:</p>
ef416fc2 39
5a738aea
MS
40<pre class='example'>
41#include &lt;cups/array.h&gt;
ef416fc2 42
5a738aea
MS
43static int compare_func(void *first, void *second, void *user_data);
44
45void *user_data;
46<a href='#cups_array_t'>cups_array_t</a> *array = <a href='#cupsArrayNew'>cupsArrayNew</a>(compare_func, user_data);
47</pre>
48
49<p>The comparison function (type
50<a href="#cups_arrayfunc_t"><code>cups_arrayfunc_t</code></a>) is called
51whenever an element is added to the array and can be <code>NULL</code> to
52create an unsorted array. The function returns -1 if the first element should
53come before the second, 0 if the first and second elements should have the same
54ordering, and 1 if the first element should come after the second.</p>
55
56<p>The "user_data" pointer is passed to your comparison function. Pass
57<code>NULL</code> if you do not need to associate the elements in your array
58with additional information.</p>
59
60<p>The <a href='#cupsArrayNew2'><code>cupsArrayNew2</code></a> function adds
61two more arguments to support hashed lookups, which can potentially provide
62instantaneous ("O(1)") lookups in your array:</p>
63
64<pre class='example'>
65#include &lt;cups/array.h&gt;
66
67#define HASH_SIZE 512 /* Size of hash table */
68
69static int compare_func(void *first, void *second, void *user_data);
70static int hash_func(void *element, void *user_data);
71
72void *user_data;
10d09e33 73<a href='#cups_array_t'>cups_array_t</a> *hash_array = <a href='#cupsArrayNew2'>cupsArrayNew2</a>(compare_func, user_data, hash_func, HASH_SIZE);
5a738aea
MS
74</pre>
75
76<p>The hash function (type
79e1d494 77<a href="#cups_ahash_func_t"><code>cups_ahash_func_t</code></a>) should return a
5a738aea
MS
78number from 0 to (hash_size-1) that (hopefully) uniquely identifies the
79element and is called whenever you look up an element in the array with
80<a href='#cupsArrayFind'><code>cupsArrayFind</code></a>. The hash size is
81only limited by available memory, but generally should not be larger than
8216384 to realize any performance improvement.</p>
83
10d09e33
MS
84<p>The <a href='#cupsArrayNew3'><code>cupsArrayNew3</code></a> function adds
85copy and free callbacks to support basic memory management of elements:</p>
86
87<pre class='example'>
88#include &lt;cups/array.h&gt;
89
90#define HASH_SIZE 512 /* Size of hash table */
91
92static int compare_func(void *first, void *second, void *user_data);
93static void *copy_func(void *element, void *user_data);
94static void free_func(void *element, void *user_data);
95static int hash_func(void *element, void *user_data);
96
97void *user_data;
98<a href='#cups_array_t'>cups_array_t</a> *array = <a href='#cupsArrayNew3'>cupsArrayNew3</a>(compare_func, user_data, NULL, 0, copy_func, free_func);
99
100<a href='#cups_array_t'>cups_array_t</a> *hash_array = <a href='#cupsArrayNew3'>cupsArrayNew3</a>(compare_func, user_data, hash_func, HASH_SIZE, copy_func, free_func);
101</pre>
102
5a738aea
MS
103<p>Once you have created the array, you add elements using the
104<a href='#cupsArrayAdd'><code>cupsArrayAdd</code></a>
105<a href='#cupsArrayInsert'><code>cupsArrayInsert</code></a> functions.
106The first function adds an element to the array, adding the new element
107after any elements that have the same order, while the second inserts the
108element before others with the same order. For unsorted arrays,
79e1d494 109<a href='#cupsArrayAdd'><code>cupsArrayAdd</code></a> appends the element to
5a738aea
MS
110the end of the array while
111<a href='#cupsArrayInsert'><code>cupsArrayInsert</code></a> inserts the
112element at the beginning of the array. For example, the following code
113creates a sorted array of character strings:</p>
114
115<pre class='example'>
116#include &lt;cups/array.h&gt;
117
118/* Use strcmp() to compare strings - it will ignore the user_data pointer */
119<a href='#cups_array_t'>cups_array_t</a> *array = <a href='#cupsArrayNew'>cupsArrayNew</a>((<a href='#cups_array_func_t'>cups_array_func_t</a>)strcmp, NULL);
120
121/* Add four strings to the array */
122<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "One Fish");
123<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Two Fish");
124<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Red Fish");
125<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Blue Fish");
ef416fc2 126</pre>
127
5a738aea
MS
128<p>Elements are removed using the
129<a href='#cupsArrayRemove'><code>cupsArrayRemove</code></a> function, for
130example:</p>
ef416fc2 131
5a738aea
MS
132<pre class='example'>
133#include &lt;cups/array.h&gt;
134
135/* Use strcmp() to compare strings - it will ignore the user_data pointer */
136<a href='#cups_array_t'>cups_array_t</a> *array = <a href='#cupsArrayNew'>cupsArrayNew</a>((<a href='#cups_array_func_t'>cups_array_func_t</a>)strcmp, NULL);
137
138/* Add four strings to the array */
139<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "One Fish");
140<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Two Fish");
141<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Red Fish");
142<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Blue Fish");
143
144/* Remove "Red Fish" */
145<a href='#cupsArrayRemove'>cupsArrayRemove</a>(array, "Red Fish");
146</pre>
147
148<p>Finally, you free the memory used by the array using the
149<a href='#cupsArrayDelete'><code>cupsArrayDelete</code></a> function. All
150of the memory for the array and hash table (if any) is freed, however <em>CUPS
10d09e33 151does not free the elements unless you provide copy and free functions</em>.</p>
5a738aea
MS
152
153<h3><a name='FINDING_AND_ENUMERATING'>Finding and Enumerating Elements</a></h3>
154
155<p>CUPS provides several functions to find and enumerate elements in an
156array. Each one sets or updates a "current index" into the array, such that
157future lookups will start where the last one left off:</p>
158
159<dl>
160 <dt><a href='#cupsArrayFind'><code>cupsArrayFind</code></a></dt>
79e1d494 161 <dd>Returns the first matching element.</dd>
5a738aea
MS
162 <dt><a href='#cupsArrayFirst'><code>cupsArrayFirst</code></a></dt>
163 <dd>Returns the first element in the array.</dd>
164 <dt><a href='#cupsArrayIndex'><code>cupsArrayIndex</code></a></dt>
79e1d494 165 <dd>Returns the Nth element in the array, starting at 0.</dd>
5a738aea
MS
166 <dt><a href='#cupsArrayLast'><code>cupsArrayLast</code></a></dt>
167 <dd>Returns the last element in the array.</dd>
168 <dt><a href='#cupsArrayNext'><code>cupsArrayNext</code></a></dt>
169 <dd>Returns the next element in the array.</dd>
170 <dt><a href='#cupsArrayPrev'><code>cupsArrayPrev</code></a></dt>
171 <dd>Returns the previous element in the array.</dd>
172</dl>
173
174<p>Each of these functions returns <code>NULL</code> when there is no
175corresponding element. For example, a simple <code>for</code> loop using the
176<a href='#cupsArrayFirst'><code>cupsArrayFirst</code></a> and
177<a href='#cupsArrayNext'><code>cupsArrayNext</code></a> functions will
178enumerate all of the strings in our previous example:</p>
179
180<pre class='example'>
181#include &lt;cups/array.h&gt;
182
183/* Use strcmp() to compare strings - it will ignore the user_data pointer */
184<a href='#cups_array_t'>cups_array_t</a> *array = <a href='#cupsArrayNew'>cupsArrayNew</a>((<a href='#cups_array_func_t'>cups_array_func_t</a>)strcmp, NULL);
185
186/* Add four strings to the array */
187<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "One Fish");
188<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Two Fish");
189<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Red Fish");
190<a href='#cupsArrayAdd'>cupsArrayAdd</a>(array, "Blue Fish");
191
192/* Show all of the strings in the array */
193char *s;
194for (s = (char *)<a href='#cupsArrayFirst'>cupsArrayFirst</a>(array); s != NULL; s = (char *)<a href='#cupsArrayNext'>cupsArrayNext</a>(array))
195 puts(s);
196</pre>