1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000 Tim Janik
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.
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.
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.
19 * gbsearcharray.h: binary searchable sorted array maintenance
21 #ifndef __G_BSEARCH_ARRAY_H__
22 #define __G_BSEARCH_ARRAY_H__
24 #include <gobject/gtype.h>
29 #endif /* __cplusplus */
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)
36 /* --- typedefs --- */
37 typedef struct _GBSearchArray GBSearchArray;
38 typedef gint (*GBSearchCompareFunc) (gconstpointer bsearch_node1,
39 gconstpointer bsearch_node2);
42 G_BSEARCH_ALIGN_POWER2 = 1 << 0,
43 G_BSEARCH_DEFER_SHRINK = 1 << 1
47 /* --- structures --- */
50 GBSearchCompareFunc cmp_func;
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);
67 gpointer g_bsearch_array_lookup (GBSearchArray *barray,
68 gconstpointer key_node);
70 gpointer g_bsearch_array_get_nth (GBSearchArray *barray,
74 /* --- implementation details --- */
75 #if defined (G_CAN_INLINE) || defined (__G_BSEARCHARRAY_C__)
76 G_INLINE_FUNC gpointer
77 g_bsearch_array_lookup (GBSearchArray *barray,
78 gconstpointer key_node)
80 if (barray->n_nodes > 0)
82 GBSearchCompareFunc cmp_func = barray->cmp_func;
83 gint sizeof_node = barray->sizeof_node;
84 guint n_nodes = barray->n_nodes;
85 guint8 *nodes = barray->nodes;
94 i = (n_nodes + 1) >> 1;
95 check = nodes + i * sizeof_node;
96 cmp = cmp_func (key_node, check);
104 else /* if (cmp < 0) */
112 G_INLINE_FUNC gpointer
113 g_bsearch_array_get_nth (GBSearchArray *barray,
116 if (n < barray->n_nodes)
118 guint8 *nodes = barray->nodes;
120 return nodes + n * barray->sizeof_node;
125 #endif /* G_CAN_INLINE && __G_BSEARCHARRAY_C__ */
132 #endif /* __cplusplus */
134 #endif /* __G_BSEARCH_ARRAY_H__ */