Tizen 2.1 base
[platform/upstream/glib2.0.git] / glib / garray.h
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 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_ARRAY_H__
32 #define __G_ARRAY_H__
33
34 #include <glib/gtypes.h>
35
36 G_BEGIN_DECLS
37
38 typedef struct _GBytes          GBytes;
39 typedef struct _GArray          GArray;
40 typedef struct _GByteArray      GByteArray;
41 typedef struct _GPtrArray       GPtrArray;
42
43 struct _GArray
44 {
45   gchar *data;
46   guint len;
47 };
48
49 struct _GByteArray
50 {
51   guint8 *data;
52   guint   len;
53 };
54
55 struct _GPtrArray
56 {
57   gpointer *pdata;
58   guint     len;
59 };
60
61 /* Resizable arrays. remove fills any cleared spot and shortens the
62  * array, while preserving the order. remove_fast will distort the
63  * order by moving the last element to the position of the removed.
64  */
65
66 #define g_array_append_val(a,v)   g_array_append_vals (a, &(v), 1)
67 #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &(v), 1)
68 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &(v), 1)
69 #define g_array_index(a,t,i)      (((t*) (void *) (a)->data) [(i)])
70
71 GArray* g_array_new               (gboolean          zero_terminated,
72                                    gboolean          clear_,
73                                    guint             element_size);
74 GArray* g_array_sized_new         (gboolean          zero_terminated,
75                                    gboolean          clear_,
76                                    guint             element_size,
77                                    guint             reserved_size);
78 gchar*  g_array_free              (GArray           *array,
79                                    gboolean          free_segment);
80 GArray *g_array_ref               (GArray           *array);
81 void    g_array_unref             (GArray           *array);
82 guint   g_array_get_element_size  (GArray           *array);
83 GArray* g_array_append_vals       (GArray           *array,
84                                    gconstpointer     data,
85                                    guint             len);
86 GArray* g_array_prepend_vals      (GArray           *array,
87                                    gconstpointer     data,
88                                    guint             len);
89 GArray* g_array_insert_vals       (GArray           *array,
90                                    guint             index_,
91                                    gconstpointer     data,
92                                    guint             len);
93 GArray* g_array_set_size          (GArray           *array,
94                                    guint             length);
95 GArray* g_array_remove_index      (GArray           *array,
96                                    guint             index_);
97 GArray* g_array_remove_index_fast (GArray           *array,
98                                    guint             index_);
99 GArray* g_array_remove_range      (GArray           *array,
100                                    guint             index_,
101                                    guint             length);
102 void    g_array_sort              (GArray           *array,
103                                    GCompareFunc      compare_func);
104 void    g_array_sort_with_data    (GArray           *array,
105                                    GCompareDataFunc  compare_func,
106                                    gpointer          user_data);
107 void    g_array_set_clear_func    (GArray           *array,
108                                    GDestroyNotify    clear_func);
109
110 /* Resizable pointer array.  This interface is much less complicated
111  * than the above.  Add appends a pointer.  Remove fills any cleared 
112  * spot and shortens the array. remove_fast will again distort order.  
113  */
114 #define    g_ptr_array_index(array,index_) ((array)->pdata)[index_]
115 GPtrArray* g_ptr_array_new                (void);
116 GPtrArray* g_ptr_array_new_with_free_func (GDestroyNotify    element_free_func);
117 GPtrArray* g_ptr_array_sized_new          (guint             reserved_size);
118 GPtrArray* g_ptr_array_new_full           (guint             reserved_size,
119                                            GDestroyNotify    element_free_func);
120 gpointer*  g_ptr_array_free               (GPtrArray        *array,
121                                            gboolean          free_seg);
122 GPtrArray* g_ptr_array_ref                (GPtrArray        *array);
123 void       g_ptr_array_unref              (GPtrArray        *array);
124 void       g_ptr_array_set_free_func      (GPtrArray        *array,
125                                            GDestroyNotify    element_free_func);
126 void       g_ptr_array_set_size           (GPtrArray        *array,
127                                            gint              length);
128 gpointer   g_ptr_array_remove_index       (GPtrArray        *array,
129                                            guint             index_);
130 gpointer   g_ptr_array_remove_index_fast  (GPtrArray        *array,
131                                            guint             index_);
132 gboolean   g_ptr_array_remove             (GPtrArray        *array,
133                                            gpointer          data);
134 gboolean   g_ptr_array_remove_fast        (GPtrArray        *array,
135                                            gpointer          data);
136 void       g_ptr_array_remove_range       (GPtrArray        *array,
137                                            guint             index_,
138                                            guint             length);
139 void       g_ptr_array_add                (GPtrArray        *array,
140                                            gpointer          data);
141 void       g_ptr_array_sort               (GPtrArray        *array,
142                                            GCompareFunc      compare_func);
143 void       g_ptr_array_sort_with_data     (GPtrArray        *array,
144                                            GCompareDataFunc  compare_func,
145                                            gpointer          user_data);
146 void       g_ptr_array_foreach            (GPtrArray        *array,
147                                            GFunc             func,
148                                            gpointer          user_data);
149
150
151 /* Byte arrays, an array of guint8.  Implemented as a GArray,
152  * but type-safe.
153  */
154
155 GByteArray* g_byte_array_new               (void);
156 GByteArray* g_byte_array_new_take          (guint8           *data,
157                                             gsize             len);
158 GByteArray* g_byte_array_sized_new         (guint             reserved_size);
159 guint8*     g_byte_array_free              (GByteArray       *array,
160                                             gboolean          free_segment);
161 GBytes*     g_byte_array_free_to_bytes     (GByteArray       *array);
162 GByteArray *g_byte_array_ref               (GByteArray       *array);
163 void        g_byte_array_unref             (GByteArray       *array);
164 GByteArray* g_byte_array_append            (GByteArray       *array,
165                                             const guint8     *data,
166                                             guint             len);
167 GByteArray* g_byte_array_prepend           (GByteArray       *array,
168                                             const guint8     *data,
169                                             guint             len);
170 GByteArray* g_byte_array_set_size          (GByteArray       *array,
171                                             guint             length);
172 GByteArray* g_byte_array_remove_index      (GByteArray       *array,
173                                             guint             index_);
174 GByteArray* g_byte_array_remove_index_fast (GByteArray       *array,
175                                             guint             index_);
176 GByteArray* g_byte_array_remove_range      (GByteArray       *array,
177                                             guint             index_,
178                                             guint             length);
179 void        g_byte_array_sort              (GByteArray       *array,
180                                             GCompareFunc      compare_func);
181 void        g_byte_array_sort_with_data    (GByteArray       *array,
182                                             GCompareDataFunc  compare_func,
183                                             gpointer          user_data);
184
185 G_END_DECLS
186
187 #endif /* __G_ARRAY_H__ */