1682a76064e868d2e006d86b93c6127576059dcd
[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))
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_ARRAY_ALIGN_POWER2  = 1 << 0,
39   G_BSEARCH_ARRAY_DEFER_SHRINK  = 1 << 1
40 } GBSearchArrayFlags;
41
42
43 /* --- structures --- */
44 struct _GBSearchArray
45 {
46   GBSearchCompareFunc cmp_nodes;
47   guint16             flags;
48   guint16             sizeof_node;
49   guint               n_nodes;
50   gpointer            nodes;
51 };
52
53
54 /* --- prototypes --- */
55 GBSearchArray*  g_bsearch_array_new             (guint16             sizeof_node,
56                                                  GBSearchCompareFunc node_cmp_func,
57                                                  GBSearchArrayFlags  flags);
58 void            g_bsearch_array_destroy         (GBSearchArray  *barray);
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 /* initialization of static arrays */
78 #define G_STATIC_BSEARCH_ARRAY_INIT(sizeof_node, cmp_nodes, flags) \
79   { (cmp_nodes), (flags), (sizeof_node), 0, NULL }
80
81
82 /* --- implementation details --- */
83 #if defined (G_CAN_INLINE) || defined (__G_BSEARCHARRAY_C__)
84 G_INLINE_FUNC gpointer
85 g_bsearch_array_lookup (GBSearchArray *barray,
86                         gconstpointer  key_node)
87 {
88   if (barray->n_nodes > 0)
89     {
90       GBSearchCompareFunc cmp_nodes = barray->cmp_nodes;
91       gint sizeof_node = barray->sizeof_node;
92       guint n_nodes = barray->n_nodes;
93       guint8 *nodes = (guint8 *) barray->nodes;
94       
95       nodes -= sizeof_node;
96       do
97         {
98           guint8 *check;
99           guint i;
100           register gint cmp;
101           
102           i = (n_nodes + 1) >> 1;
103           check = nodes + i * sizeof_node;
104           cmp = cmp_nodes (key_node, check);
105           if (cmp == 0)
106             return check;
107           else if (cmp > 0)
108             {
109               n_nodes -= i;
110               nodes = check;
111             }
112           else /* if (cmp < 0) */
113             n_nodes = i - 1;
114         }
115       while (n_nodes);
116     }
117   
118   return NULL;
119 }
120 G_INLINE_FUNC gpointer
121 g_bsearch_array_get_nth (GBSearchArray *barray,
122                          guint          n)
123 {
124   if (n < barray->n_nodes)
125     {
126       guint8 *nodes = (guint8*) barray->nodes;
127
128       return nodes + n * barray->sizeof_node;
129     }
130   else
131     return NULL;
132 }
133 G_INLINE_FUNC
134 guint
135 g_bsearch_array_get_index (GBSearchArray *barray,
136                            gpointer       node_in_array)
137 {
138   guint distance = ((guint8*) node_in_array) - ((guint8*) barray->nodes);
139
140   distance /= barray->sizeof_node;
141
142   return MIN (distance, barray->n_nodes);
143 }
144 #endif  /* G_CAN_INLINE || __G_BSEARCHARRAY_C__ */
145
146 G_END_DECLS
147
148 #endif /* __G_BSEARCH_ARRAY_H__ */