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