os/ossrv/glib/glib/gbsearcharray.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* GBSearchArray - Binary Searchable Array implementation
sl@0
     2
 * Copyright (C) 2000-2003 Tim Janik
sl@0
     3
 *
sl@0
     4
 * This software is provided "as is"; redistribution and modification
sl@0
     5
 * is permitted, provided that the following disclaimer is retained.
sl@0
     6
 *
sl@0
     7
 * This software is distributed in the hope that it will be useful,
sl@0
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    10
 * In no event shall the authors or contributors be liable for any
sl@0
    11
 * direct, indirect, incidental, special, exemplary, or consequential
sl@0
    12
 * damages (including, but not limited to, procurement of substitute
sl@0
    13
 * goods or services; loss of use, data, or profits; or business
sl@0
    14
 * interruption) however caused and on any theory of liability, whether
sl@0
    15
 * in contract, strict liability, or tort (including negligence or
sl@0
    16
 * otherwise) arising in any way out of the use of this software, even
sl@0
    17
 * if advised of the possibility of such damage.
sl@0
    18
 */
sl@0
    19
#ifndef __G_BSEARCH_ARRAY_H__
sl@0
    20
#define __G_BSEARCH_ARRAY_H__
sl@0
    21
sl@0
    22
#include <glib.h>
sl@0
    23
#include <string.h>
sl@0
    24
sl@0
    25
sl@0
    26
G_BEGIN_DECLS   /* c++ guards */
sl@0
    27
sl@0
    28
/* this implementation is intended to be usable in third-party code
sl@0
    29
 * simply by pasting the contents of this file. as such, the
sl@0
    30
 * implementation needs to be self-contained within this file.
sl@0
    31
 */
sl@0
    32
sl@0
    33
/* convenience macro to avoid signed overflow for value comparisions */
sl@0
    34
#define G_BSEARCH_ARRAY_CMP(v1,v2) ((v1) > (v2) ? +1 : (v1) == (v2) ? 0 : -1)
sl@0
    35
sl@0
    36
sl@0
    37
/* --- typedefs --- */
sl@0
    38
typedef gint  (*GBSearchCompareFunc) (gconstpointer bsearch_node1, /* key */
sl@0
    39
                                      gconstpointer bsearch_node2);
sl@0
    40
typedef enum
sl@0
    41
{
sl@0
    42
  G_BSEARCH_ARRAY_ALIGN_POWER2  = 1 << 0, /* align memory to power2 sizes */
sl@0
    43
  G_BSEARCH_ARRAY_AUTO_SHRINK  = 1 << 1   /* shrink array upon removal */
sl@0
    44
} GBSearchArrayFlags;
sl@0
    45
sl@0
    46
sl@0
    47
/* --- structures --- */
sl@0
    48
typedef struct
sl@0
    49
{
sl@0
    50
  guint               sizeof_node;
sl@0
    51
  GBSearchCompareFunc cmp_nodes;
sl@0
    52
  guint               flags;
sl@0
    53
} GBSearchConfig;
sl@0
    54
typedef union
sl@0
    55
{
sl@0
    56
  guint    n_nodes;
sl@0
    57
  /*< private >*/
sl@0
    58
  gpointer alignment_dummy1;
sl@0
    59
  glong    alignment_dummy2;
sl@0
    60
  gdouble  alignment_dummy3;
sl@0
    61
} GBSearchArray;
sl@0
    62
sl@0
    63
sl@0
    64
/* --- public API --- */
sl@0
    65
static inline GBSearchArray*    g_bsearch_array_create    (const GBSearchConfig *bconfig);
sl@0
    66
static inline gpointer          g_bsearch_array_get_nth   (GBSearchArray        *barray,
sl@0
    67
                                                           const GBSearchConfig *bconfig,
sl@0
    68
                                                           guint                 nth);
sl@0
    69
static inline guint             g_bsearch_array_get_index (GBSearchArray        *barray,
sl@0
    70
                                                           const GBSearchConfig *bconfig,
sl@0
    71
                                                           gconstpointer         node_in_array);
sl@0
    72
static inline GBSearchArray*    g_bsearch_array_remove    (GBSearchArray        *barray,
sl@0
    73
                                                           const GBSearchConfig *bconfig,
sl@0
    74
                                                           guint                 index_);
sl@0
    75
/* provide uninitialized space at index for node insertion */
sl@0
    76
static inline GBSearchArray*    g_bsearch_array_grow      (GBSearchArray        *barray,
sl@0
    77
                                                           const GBSearchConfig *bconfig,
sl@0
    78
                                                           guint                 index);
sl@0
    79
/* insert key_node into array if it does not exist, otherwise do nothing */
sl@0
    80
static inline GBSearchArray*    g_bsearch_array_insert    (GBSearchArray        *barray,
sl@0
    81
                                                           const GBSearchConfig *bconfig,
sl@0
    82
                                                           gconstpointer         key_node);
sl@0
    83
/* insert key_node into array if it does not exist,
sl@0
    84
 * otherwise replace the existing node's contents with key_node
sl@0
    85
 */
sl@0
    86
static inline GBSearchArray*    g_bsearch_array_replace   (GBSearchArray        *barray,
sl@0
    87
                                                           const GBSearchConfig *bconfig,
sl@0
    88
                                                           gconstpointer         key_node);
sl@0
    89
static inline void              g_bsearch_array_free      (GBSearchArray        *barray,
sl@0
    90
                                                           const GBSearchConfig *bconfig);
sl@0
    91
#define g_bsearch_array_get_n_nodes(barray)     (((GBSearchArray*) (barray))->n_nodes)
sl@0
    92
sl@0
    93
/* g_bsearch_array_lookup():
sl@0
    94
 * return NULL or exact match node
sl@0
    95
 */
sl@0
    96
#define g_bsearch_array_lookup(barray, bconfig, key_node)       \
sl@0
    97
    g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 0)
sl@0
    98
sl@0
    99
/* g_bsearch_array_lookup_sibling():
sl@0
   100
 * return NULL for barray->n_nodes==0, otherwise return the
sl@0
   101
 * exact match node, or, if there's no such node, return the
sl@0
   102
 * node last visited, which is pretty close to an exact match
sl@0
   103
 * (will be one off into either direction).
sl@0
   104
 */
sl@0
   105
#define g_bsearch_array_lookup_sibling(barray, bconfig, key_node)       \
sl@0
   106
    g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 1)
sl@0
   107
sl@0
   108
/* g_bsearch_array_lookup_insertion():
sl@0
   109
 * return NULL for barray->n_nodes==0 or exact match, otherwise
sl@0
   110
 * return the node where key_node should be inserted (may be one
sl@0
   111
 * after end, i.e. g_bsearch_array_get_index(result) <= barray->n_nodes).
sl@0
   112
 */
sl@0
   113
#define g_bsearch_array_lookup_insertion(barray, bconfig, key_node)     \
sl@0
   114
    g_bsearch_array_lookup_fuzzy ((barray), (bconfig), (key_node), 2)
sl@0
   115
sl@0
   116
sl@0
   117
/* --- implementation --- */
sl@0
   118
/* helper macro to cut down realloc()s */
sl@0
   119
#ifdef  DISABLE_MEM_POOLS
sl@0
   120
#define G_BSEARCH_UPPER_POWER2(n)       (n)
sl@0
   121
#else   /* !DISABLE_MEM_POOLS */
sl@0
   122
#define G_BSEARCH_UPPER_POWER2(n)       ((n) ? 1 << g_bit_storage ((n) - 1) : 0)
sl@0
   123
#endif  /* !DISABLE_MEM_POOLS */
sl@0
   124
#define G_BSEARCH_ARRAY_NODES(barray)    (((guint8*) (barray)) + sizeof (GBSearchArray))
sl@0
   125
static inline GBSearchArray*
sl@0
   126
g_bsearch_array_create (const GBSearchConfig *bconfig)
sl@0
   127
{
sl@0
   128
  GBSearchArray *barray;
sl@0
   129
  guint size;
sl@0
   130
sl@0
   131
  g_return_val_if_fail (bconfig != NULL, NULL);
sl@0
   132
sl@0
   133
  size = sizeof (GBSearchArray) + bconfig->sizeof_node;
sl@0
   134
  if (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2)
sl@0
   135
    size = G_BSEARCH_UPPER_POWER2 (size);
sl@0
   136
  barray = (GBSearchArray *) g_realloc (NULL, size);
sl@0
   137
  memset (barray, 0, sizeof (GBSearchArray));
sl@0
   138
sl@0
   139
  return barray;
sl@0
   140
}
sl@0
   141
static inline gpointer
sl@0
   142
g_bsearch_array_lookup_fuzzy (GBSearchArray             *barray,
sl@0
   143
                              const GBSearchConfig      *bconfig,
sl@0
   144
                              gconstpointer              key_node,
sl@0
   145
                              const guint                sibling_or_after);
sl@0
   146
static inline gpointer
sl@0
   147
g_bsearch_array_lookup_fuzzy (GBSearchArray        *barray,
sl@0
   148
                              const GBSearchConfig *bconfig,
sl@0
   149
                              gconstpointer         key_node,
sl@0
   150
                              const guint           sibling_or_after)
sl@0
   151
{
sl@0
   152
  GBSearchCompareFunc cmp_nodes = bconfig->cmp_nodes;
sl@0
   153
  guint8 *check = NULL, *nodes = G_BSEARCH_ARRAY_NODES (barray);
sl@0
   154
  guint n_nodes = barray->n_nodes, offs = 0;
sl@0
   155
  guint sizeof_node = bconfig->sizeof_node;
sl@0
   156
  gint cmp = 0;
sl@0
   157
sl@0
   158
  while (offs < n_nodes)
sl@0
   159
    {
sl@0
   160
      guint i = (offs + n_nodes) >> 1;
sl@0
   161
sl@0
   162
      check = nodes + i * sizeof_node;
sl@0
   163
      cmp = cmp_nodes (key_node, check);
sl@0
   164
      if (cmp == 0)
sl@0
   165
        return sibling_or_after > 1 ? NULL : check;
sl@0
   166
      else if (cmp < 0)
sl@0
   167
        n_nodes = i;
sl@0
   168
      else /* (cmp > 0) */
sl@0
   169
        offs = i + 1;
sl@0
   170
    }
sl@0
   171
sl@0
   172
  /* check is last mismatch, cmp > 0 indicates greater key */
sl@0
   173
  return G_LIKELY (!sibling_or_after) ? NULL : (sibling_or_after > 1 && cmp > 0) ? check + sizeof_node : check;
sl@0
   174
}
sl@0
   175
static inline gpointer
sl@0
   176
g_bsearch_array_get_nth (GBSearchArray        *barray,
sl@0
   177
                         const GBSearchConfig *bconfig,
sl@0
   178
                         guint                 nth)
sl@0
   179
{
sl@0
   180
  return (G_LIKELY (nth < barray->n_nodes) ?
sl@0
   181
          G_BSEARCH_ARRAY_NODES (barray) + nth * bconfig->sizeof_node :
sl@0
   182
          NULL);
sl@0
   183
}
sl@0
   184
static inline guint
sl@0
   185
g_bsearch_array_get_index (GBSearchArray        *barray,
sl@0
   186
                           const GBSearchConfig *bconfig,
sl@0
   187
                           gconstpointer         node_in_array)
sl@0
   188
{
sl@0
   189
  guint distance = ((guint8*) node_in_array) - G_BSEARCH_ARRAY_NODES (barray);
sl@0
   190
sl@0
   191
  g_return_val_if_fail (node_in_array != NULL, barray->n_nodes);
sl@0
   192
sl@0
   193
  distance /= bconfig->sizeof_node;
sl@0
   194
sl@0
   195
  return MIN (distance, barray->n_nodes + 1); /* may return one after end */
sl@0
   196
}
sl@0
   197
static inline GBSearchArray*
sl@0
   198
g_bsearch_array_grow (GBSearchArray        *barray,
sl@0
   199
                      const GBSearchConfig *bconfig,
sl@0
   200
                      guint                 index_)
sl@0
   201
{
sl@0
   202
  guint old_size = barray->n_nodes * bconfig->sizeof_node;
sl@0
   203
  guint new_size = old_size + bconfig->sizeof_node;
sl@0
   204
  guint8 *node;
sl@0
   205
sl@0
   206
  g_return_val_if_fail (index_ <= barray->n_nodes, NULL);
sl@0
   207
sl@0
   208
  if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2))
sl@0
   209
    {
sl@0
   210
      new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size);
sl@0
   211
      old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size);
sl@0
   212
      if (old_size != new_size)
sl@0
   213
        barray = (GBSearchArray *) g_realloc (barray, new_size);
sl@0
   214
    }
sl@0
   215
  else
sl@0
   216
    barray = (GBSearchArray *) g_realloc (barray, sizeof (GBSearchArray) + new_size);
sl@0
   217
  node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node;
sl@0
   218
  g_memmove (node + bconfig->sizeof_node, node, (barray->n_nodes - index_) * bconfig->sizeof_node);
sl@0
   219
  barray->n_nodes += 1;
sl@0
   220
  return barray;
sl@0
   221
}
sl@0
   222
static inline GBSearchArray*
sl@0
   223
g_bsearch_array_insert (GBSearchArray        *barray,
sl@0
   224
                        const GBSearchConfig *bconfig,
sl@0
   225
                        gconstpointer         key_node)
sl@0
   226
{
sl@0
   227
  guint8 *node;
sl@0
   228
sl@0
   229
  if (G_UNLIKELY (!barray->n_nodes))
sl@0
   230
    {
sl@0
   231
      barray = g_bsearch_array_grow (barray, bconfig, 0);
sl@0
   232
      node = G_BSEARCH_ARRAY_NODES (barray);
sl@0
   233
    }
sl@0
   234
  else
sl@0
   235
    {
sl@0
   236
      node = (guint8 *) g_bsearch_array_lookup_insertion (barray, bconfig, key_node);
sl@0
   237
      if (G_LIKELY (node))
sl@0
   238
        {
sl@0
   239
          guint index_ = g_bsearch_array_get_index (barray, bconfig, node);
sl@0
   240
sl@0
   241
          /* grow and insert */
sl@0
   242
          barray = g_bsearch_array_grow (barray, bconfig, index_);
sl@0
   243
          node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node;
sl@0
   244
        }
sl@0
   245
      else /* no insertion needed, node already there */
sl@0
   246
        return barray;
sl@0
   247
    }
sl@0
   248
  memcpy (node, key_node, bconfig->sizeof_node);
sl@0
   249
  return barray;
sl@0
   250
}
sl@0
   251
static inline GBSearchArray*
sl@0
   252
g_bsearch_array_replace (GBSearchArray        *barray,
sl@0
   253
                         const GBSearchConfig *bconfig,
sl@0
   254
                         gconstpointer         key_node)
sl@0
   255
{
sl@0
   256
  guint8 *node = (guint8 *) g_bsearch_array_lookup (barray, bconfig, key_node);
sl@0
   257
  if (G_LIKELY (node))  /* expected path */
sl@0
   258
    memcpy (node, key_node, bconfig->sizeof_node);
sl@0
   259
  else                  /* revert to insertion */
sl@0
   260
    barray = g_bsearch_array_insert (barray, bconfig, key_node);
sl@0
   261
  return barray;
sl@0
   262
}
sl@0
   263
static inline GBSearchArray*
sl@0
   264
g_bsearch_array_remove (GBSearchArray        *barray,
sl@0
   265
                        const GBSearchConfig *bconfig,
sl@0
   266
                        guint                 index_)
sl@0
   267
{
sl@0
   268
  guint8 *node;
sl@0
   269
sl@0
   270
  g_return_val_if_fail (index_ < barray->n_nodes, NULL);
sl@0
   271
sl@0
   272
  barray->n_nodes -= 1;
sl@0
   273
  node = G_BSEARCH_ARRAY_NODES (barray) + index_ * bconfig->sizeof_node;
sl@0
   274
  g_memmove (node, node + bconfig->sizeof_node, (barray->n_nodes - index_) * bconfig->sizeof_node);
sl@0
   275
  if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_AUTO_SHRINK))
sl@0
   276
    {
sl@0
   277
      guint new_size = barray->n_nodes * bconfig->sizeof_node;
sl@0
   278
      guint old_size = new_size + bconfig->sizeof_node;
sl@0
   279
sl@0
   280
      if (G_UNLIKELY (bconfig->flags & G_BSEARCH_ARRAY_ALIGN_POWER2))
sl@0
   281
        {
sl@0
   282
          new_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + new_size);
sl@0
   283
          old_size = G_BSEARCH_UPPER_POWER2 (sizeof (GBSearchArray) + old_size);
sl@0
   284
          if (old_size != new_size)
sl@0
   285
            barray = (GBSearchArray *) g_realloc (barray, new_size);
sl@0
   286
        }
sl@0
   287
      else
sl@0
   288
        barray = (GBSearchArray *) g_realloc (barray, sizeof (GBSearchArray) + new_size);
sl@0
   289
    }
sl@0
   290
  return barray;
sl@0
   291
}
sl@0
   292
static inline void
sl@0
   293
g_bsearch_array_free (GBSearchArray        *barray,
sl@0
   294
                      const GBSearchConfig *bconfig)
sl@0
   295
{
sl@0
   296
  g_return_if_fail (barray != NULL);
sl@0
   297
sl@0
   298
  g_free (barray);
sl@0
   299
}
sl@0
   300
sl@0
   301
G_END_DECLS     /* c++ guards */
sl@0
   302
sl@0
   303
#endif  /* !__G_BSEARCH_ARRAY_H__ */