* src/carray.h
[claws.git] / src / carray.h
1
2 /*
3  * libEtPan! -- a mail stuff library
4  *
5  * carray - Implements simple dynamic pointer arrays
6  *
7  * Copyright (c) 1999-2000, GaĆ«l Roualland <gael.roualland@iname.com>
8  * interface changes - 2002 - DINH Viet Hoa
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the libEtPan! project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #ifndef CARRAY_H
37 #define CARRAY_H
38
39 #include <glib.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45
46 typedef GPtrArray carray;
47
48 /* Creates a new array of pointers, with initsize preallocated cells */
49 static inline carray *carray_new(guint initsize)
50 {
51         carray *res = g_ptr_array_new();
52         gint n = (gint) initsize;
53         g_assert(n >= 0);
54         g_ptr_array_set_size(res, n);
55         return res;
56 }
57
58 static inline gint carray_add(carray *array, void *data, guint *index)
59 {
60         g_ptr_array_add(array, data);
61         *index = array->len - 1;
62         return 0;
63 }
64
65 static inline gint carray_set_size(carray *array, guint new_size)
66 {
67         gint n = (gint) new_size;
68         g_assert(n >= 0);
69         g_ptr_array_set_size(array, n);
70         return 0;
71 }
72
73 static inline gint carray_delete(carray *array, guint indx)
74 {
75         gint n = (gint) indx;
76         if (n < 0) return -1;
77         if (n >= array->len) return -1;
78         g_ptr_array_remove_index_fast(array, n);
79         return 0;
80 }
81
82 static inline gint carray_delete_slow(carray *array, guint indx)
83 {
84         gint n = (gint) indx;
85         if (n < 0) return -1;
86         return g_ptr_array_remove_index(array, n) ? 0 : -1;
87 }
88
89 static inline gint carray_delete_fast(carray *array, guint indx)
90 {
91         gint n = (gint) indx;
92         if (n < 0) return -1;
93         g_ptr_array_index(array, n) = NULL;
94         return 0;
95 }
96
97 static inline gint carray_free(carray *array)
98 {
99         g_ptr_array_free(array, TRUE);
100 }
101
102 static inline gpointer carray_get(carray *array, guint index)
103 {
104         gint n = (gint) index;
105         if (n < 0) return NULL;
106         return g_ptr_array_index(array, n);
107 }
108
109 static inline carray_set(carray *array, guint index, gpointer value)
110 {
111         gint n = (gint) index;
112         g_assert(n >= 0);
113         g_ptr_array_index(array, n) = value;
114 }
115
116 static guint carray_count(carray *array)
117 {
118         /* NOTE: We're hosed when len < 0, but that won't occur */
119         return (guint) array->len;
120 }
121
122 #ifdef __cplusplus
123 }
124 #endif
125
126 #endif