Use G_BEGIN_DECLS and G_END_DECLS.
[platform/upstream/glib.git] / glib / gbsearcharray.h
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000-2001 Tim Janik
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
15  * Public 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  * gbsearcharray.h: binary searchable sorted array maintenance
20  */
21 #ifndef __G_BSEARCH_ARRAY_H__
22 #define __G_BSEARCH_ARRAY_H__
23
24 #include        <gobject/gtype.h>
25
26 G_BEGIN_DECLS
27
28 /* helper macro to avoid signed overflow for value comparisions */
29 #define G_BSEARCH_ARRAY_CMP(v1,v2) ((v1) < (v2) ? -1 : (v1) > (v2) ? 1 : 0)
30
31
32 /* --- typedefs --- */
33 typedef struct _GBSearchArray         GBSearchArray;
34 typedef gint  (*GBSearchCompareFunc) (gconstpointer bsearch_node1,
35                                       gconstpointer bsearch_node2);
36 typedef enum
37 {
38   G_BSEARCH_ALIGN_POWER2        = 1 << 0,
39   G_BSEARCH_DEFER_SHRINK        = 1 << 1
40 } GBSearchFlags;
41
42
43 /* --- structures --- */
44 struct _GBSearchArray
45 {
46   GBSearchCompareFunc cmp_func;
47   guint16             sizeof_node;
48   guint16             flags;
49   guint               n_nodes;
50   gpointer            nodes;
51 };
52
53
54 /* --- prototypes --- */
55 gpointer        g_bsearch_array_insert          (GBSearchArray  *barray,
56                                                  gconstpointer   key_node,
57                                                  gboolean        replace_existing);
58 void            g_bsearch_array_remove          (GBSearchArray  *barray,
59                                                  gconstpointer   key_node);
60 void            g_bsearch_array_remove_node     (GBSearchArray  *barray,
61                                                  gpointer        node_in_array);
62 G_INLINE_FUNC
63 gpointer        g_bsearch_array_lookup          (GBSearchArray  *barray,
64                                                  gconstpointer   key_node);
65 G_INLINE_FUNC
66 gpointer        g_bsearch_array_get_nth         (GBSearchArray  *barray,
67                                                  guint           n);
68 G_INLINE_FUNC
69 guint           g_bsearch_array_get_index       (GBSearchArray  *barray,
70                                                  gpointer        node_in_array);
71
72
73 /* --- implementation details --- */
74 #if defined (G_CAN_INLINE) || defined (__G_BSEARCHARRAY_C__)
75 G_INLINE_FUNC gpointer
76 g_bsearch_array_lookup (GBSearchArray *barray,
77                         gconstpointer  key_node)
78 {
79   if (barray->n_nodes > 0)
80     {
81       GBSearchCompareFunc cmp_func = barray->cmp_func;
82       gint sizeof_node = barray->sizeof_node;
83       guint n_nodes = barray->n_nodes;
84       guint8 *nodes = (guint8 *) barray->nodes;
85       
86       nodes -= sizeof_node;
87       do
88         {
89           guint8 *check;
90           guint i;
91           register gint cmp;
92           
93           i = (n_nodes + 1) >> 1;
94           check = nodes + i * sizeof_node;
95           cmp = cmp_func (key_node, check);
96           if (cmp == 0)
97             return check;
98           else if (cmp > 0)
99             {
100               n_nodes -= i;
101               nodes = check;
102             }
103           else /* if (cmp < 0) */
104             n_nodes = i - 1;
105         }
106       while (n_nodes);
107     }
108   
109   return NULL;
110 }
111 G_INLINE_FUNC gpointer
112 g_bsearch_array_get_nth (GBSearchArray *barray,
113                          guint          n)
114 {
115   if (n < barray->n_nodes)
116     {
117       guint8 *nodes = (guint8*) barray->nodes;
118
119       return nodes + n * barray->sizeof_node;
120     }
121   else
122     return NULL;
123 }
124 G_INLINE_FUNC
125 guint
126 g_bsearch_array_get_index (GBSearchArray *barray,
127                            gpointer       node_in_array)
128 {
129   guint distance = ((guint8*) node_in_array) - ((guint8*) barray->nodes);
130
131   distance /= barray->sizeof_node;
132
133   return MIN (distance, barray->n_nodes);
134 }
135 #endif  /* G_CAN_INLINE || __G_BSEARCHARRAY_C__ */
136
137 G_END_DECLS
138
139 #endif /* __G_BSEARCH_ARRAY_H__ */