• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 #ifndef __G_ARRAY_H__
26 #define __G_ARRAY_H__
27 
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
31 
32 #include <glib/gtypes.h>
33 
34 G_BEGIN_DECLS
35 
36 typedef struct _GBytes          GBytes;
37 typedef struct _GArray		GArray;
38 typedef struct _GByteArray	GByteArray;
39 typedef struct _GPtrArray	GPtrArray;
40 
41 struct _GArray
42 {
43   gchar *data;
44   guint len;
45 };
46 
47 struct _GByteArray
48 {
49   guint8 *data;
50   guint	  len;
51 };
52 
53 struct _GPtrArray
54 {
55   gpointer *pdata;
56   guint	    len;
57 };
58 
59 /* Resizable arrays. remove fills any cleared spot and shortens the
60  * array, while preserving the order. remove_fast will distort the
61  * order by moving the last element to the position of the removed.
62  */
63 
64 #define g_array_append_val(a,v)	  g_array_append_vals (a, &(v), 1)
65 #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &(v), 1)
66 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &(v), 1)
67 #define g_array_index(a,t,i)      (((t*) (void *) (a)->data) [(i)])
68 
69 GLIB_AVAILABLE_IN_ALL
70 GArray* g_array_new               (gboolean          zero_terminated,
71 				   gboolean          clear_,
72 				   guint             element_size);
73 GLIB_AVAILABLE_IN_2_64
74 gpointer g_array_steal            (GArray           *array,
75                                    gsize            *len);
76 GLIB_AVAILABLE_IN_ALL
77 GArray* g_array_sized_new         (gboolean          zero_terminated,
78 				   gboolean          clear_,
79 				   guint             element_size,
80 				   guint             reserved_size);
81 GLIB_AVAILABLE_IN_2_62
82 GArray* g_array_copy              (GArray           *array);
83 GLIB_AVAILABLE_IN_ALL
84 gchar*  g_array_free              (GArray           *array,
85 				   gboolean          free_segment);
86 GLIB_AVAILABLE_IN_ALL
87 GArray *g_array_ref               (GArray           *array);
88 GLIB_AVAILABLE_IN_ALL
89 void    g_array_unref             (GArray           *array);
90 GLIB_AVAILABLE_IN_ALL
91 guint   g_array_get_element_size  (GArray           *array);
92 GLIB_AVAILABLE_IN_ALL
93 GArray* g_array_append_vals       (GArray           *array,
94 				   gconstpointer     data,
95 				   guint             len);
96 GLIB_AVAILABLE_IN_ALL
97 GArray* g_array_prepend_vals      (GArray           *array,
98 				   gconstpointer     data,
99 				   guint             len);
100 GLIB_AVAILABLE_IN_ALL
101 GArray* g_array_insert_vals       (GArray           *array,
102 				   guint             index_,
103 				   gconstpointer     data,
104 				   guint             len);
105 GLIB_AVAILABLE_IN_ALL
106 GArray* g_array_set_size          (GArray           *array,
107 				   guint             length);
108 GLIB_AVAILABLE_IN_ALL
109 GArray* g_array_remove_index      (GArray           *array,
110 				   guint             index_);
111 GLIB_AVAILABLE_IN_ALL
112 GArray* g_array_remove_index_fast (GArray           *array,
113 				   guint             index_);
114 GLIB_AVAILABLE_IN_ALL
115 GArray* g_array_remove_range      (GArray           *array,
116 				   guint             index_,
117 				   guint             length);
118 GLIB_AVAILABLE_IN_ALL
119 void    g_array_sort              (GArray           *array,
120 				   GCompareFunc      compare_func);
121 GLIB_AVAILABLE_IN_ALL
122 void    g_array_sort_with_data    (GArray           *array,
123 				   GCompareDataFunc  compare_func,
124 				   gpointer          user_data);
125 GLIB_AVAILABLE_IN_2_62
126 gboolean g_array_binary_search    (GArray           *array,
127                                    gconstpointer     target,
128                                    GCompareFunc      compare_func,
129                                    guint            *out_match_index);
130 GLIB_AVAILABLE_IN_ALL
131 void    g_array_set_clear_func    (GArray           *array,
132                                    GDestroyNotify    clear_func);
133 
134 /* Resizable pointer array.  This interface is much less complicated
135  * than the above.  Add appends a pointer.  Remove fills any cleared
136  * spot and shortens the array. remove_fast will again distort order.
137  */
138 #define    g_ptr_array_index(array,index_) ((array)->pdata)[index_]
139 GLIB_AVAILABLE_IN_ALL
140 GPtrArray* g_ptr_array_new                (void);
141 GLIB_AVAILABLE_IN_ALL
142 GPtrArray* g_ptr_array_new_with_free_func (GDestroyNotify    element_free_func);
143 GLIB_AVAILABLE_IN_2_64
144 gpointer*   g_ptr_array_steal              (GPtrArray        *array,
145                                             gsize            *len);
146 GLIB_AVAILABLE_IN_2_62
147 GPtrArray *g_ptr_array_copy               (GPtrArray        *array,
148                                            GCopyFunc         func,
149                                            gpointer          user_data);
150 GLIB_AVAILABLE_IN_ALL
151 GPtrArray* g_ptr_array_sized_new          (guint             reserved_size);
152 GLIB_AVAILABLE_IN_ALL
153 GPtrArray* g_ptr_array_new_full           (guint             reserved_size,
154 					   GDestroyNotify    element_free_func);
155 GLIB_AVAILABLE_IN_ALL
156 gpointer*  g_ptr_array_free               (GPtrArray        *array,
157 					   gboolean          free_seg);
158 GLIB_AVAILABLE_IN_ALL
159 GPtrArray* g_ptr_array_ref                (GPtrArray        *array);
160 GLIB_AVAILABLE_IN_ALL
161 void       g_ptr_array_unref              (GPtrArray        *array);
162 GLIB_AVAILABLE_IN_ALL
163 void       g_ptr_array_set_free_func      (GPtrArray        *array,
164                                            GDestroyNotify    element_free_func);
165 GLIB_AVAILABLE_IN_ALL
166 void       g_ptr_array_set_size           (GPtrArray        *array,
167 					   gint              length);
168 GLIB_AVAILABLE_IN_ALL
169 gpointer   g_ptr_array_remove_index       (GPtrArray        *array,
170 					   guint             index_);
171 GLIB_AVAILABLE_IN_ALL
172 gpointer   g_ptr_array_remove_index_fast  (GPtrArray        *array,
173 					   guint             index_);
174 GLIB_AVAILABLE_IN_2_58
175 gpointer   g_ptr_array_steal_index        (GPtrArray        *array,
176                                            guint             index_);
177 GLIB_AVAILABLE_IN_2_58
178 gpointer   g_ptr_array_steal_index_fast   (GPtrArray        *array,
179                                            guint             index_);
180 GLIB_AVAILABLE_IN_ALL
181 gboolean   g_ptr_array_remove             (GPtrArray        *array,
182 					   gpointer          data);
183 GLIB_AVAILABLE_IN_ALL
184 gboolean   g_ptr_array_remove_fast        (GPtrArray        *array,
185 					   gpointer          data);
186 GLIB_AVAILABLE_IN_ALL
187 GPtrArray *g_ptr_array_remove_range       (GPtrArray        *array,
188 					   guint             index_,
189 					   guint             length);
190 GLIB_AVAILABLE_IN_ALL
191 void       g_ptr_array_add                (GPtrArray        *array,
192 					   gpointer          data);
193 GLIB_AVAILABLE_IN_2_62
194 void g_ptr_array_extend                   (GPtrArray        *array_to_extend,
195                                            GPtrArray        *array,
196                                            GCopyFunc         func,
197                                            gpointer          user_data);
198 GLIB_AVAILABLE_IN_2_62
199 void g_ptr_array_extend_and_steal         (GPtrArray        *array_to_extend,
200                                            GPtrArray        *array);
201 GLIB_AVAILABLE_IN_2_40
202 void       g_ptr_array_insert             (GPtrArray        *array,
203                                            gint              index_,
204                                            gpointer          data);
205 GLIB_AVAILABLE_IN_ALL
206 void       g_ptr_array_sort               (GPtrArray        *array,
207 					   GCompareFunc      compare_func);
208 GLIB_AVAILABLE_IN_ALL
209 void       g_ptr_array_sort_with_data     (GPtrArray        *array,
210 					   GCompareDataFunc  compare_func,
211 					   gpointer          user_data);
212 GLIB_AVAILABLE_IN_ALL
213 void       g_ptr_array_foreach            (GPtrArray        *array,
214 					   GFunc             func,
215 					   gpointer          user_data);
216 GLIB_AVAILABLE_IN_2_54
217 gboolean   g_ptr_array_find               (GPtrArray        *haystack,
218                                            gconstpointer     needle,
219                                            guint            *index_);
220 GLIB_AVAILABLE_IN_2_54
221 gboolean   g_ptr_array_find_with_equal_func (GPtrArray     *haystack,
222                                              gconstpointer  needle,
223                                              GEqualFunc     equal_func,
224                                              guint         *index_);
225 
226 
227 /* Byte arrays, an array of guint8.  Implemented as a GArray,
228  * but type-safe.
229  */
230 
231 GLIB_AVAILABLE_IN_ALL
232 GByteArray* g_byte_array_new               (void);
233 GLIB_AVAILABLE_IN_ALL
234 GByteArray* g_byte_array_new_take          (guint8           *data,
235                                             gsize             len);
236 GLIB_AVAILABLE_IN_2_64
237 guint8*     g_byte_array_steal             (GByteArray       *array,
238                                             gsize            *len);
239 GLIB_AVAILABLE_IN_ALL
240 GByteArray* g_byte_array_sized_new         (guint             reserved_size);
241 GLIB_AVAILABLE_IN_ALL
242 guint8*     g_byte_array_free              (GByteArray       *array,
243 					    gboolean          free_segment);
244 GLIB_AVAILABLE_IN_ALL
245 GBytes*     g_byte_array_free_to_bytes     (GByteArray       *array);
246 GLIB_AVAILABLE_IN_ALL
247 GByteArray *g_byte_array_ref               (GByteArray       *array);
248 GLIB_AVAILABLE_IN_ALL
249 void        g_byte_array_unref             (GByteArray       *array);
250 GLIB_AVAILABLE_IN_ALL
251 GByteArray* g_byte_array_append            (GByteArray       *array,
252 					    const guint8     *data,
253 					    guint             len);
254 GLIB_AVAILABLE_IN_ALL
255 GByteArray* g_byte_array_prepend           (GByteArray       *array,
256 					    const guint8     *data,
257 					    guint             len);
258 GLIB_AVAILABLE_IN_ALL
259 GByteArray* g_byte_array_set_size          (GByteArray       *array,
260 					    guint             length);
261 GLIB_AVAILABLE_IN_ALL
262 GByteArray* g_byte_array_remove_index      (GByteArray       *array,
263 					    guint             index_);
264 GLIB_AVAILABLE_IN_ALL
265 GByteArray* g_byte_array_remove_index_fast (GByteArray       *array,
266 					    guint             index_);
267 GLIB_AVAILABLE_IN_ALL
268 GByteArray* g_byte_array_remove_range      (GByteArray       *array,
269 					    guint             index_,
270 					    guint             length);
271 GLIB_AVAILABLE_IN_ALL
272 void        g_byte_array_sort              (GByteArray       *array,
273 					    GCompareFunc      compare_func);
274 GLIB_AVAILABLE_IN_ALL
275 void        g_byte_array_sort_with_data    (GByteArray       *array,
276 					    GCompareDataFunc  compare_func,
277 					    gpointer          user_data);
278 
279 G_END_DECLS
280 
281 #endif /* __G_ARRAY_H__ */
282