2
* $Id: idtable.h,v 1.5 2003/07/31 01:32:34 cbiere Exp $
4
* Copyright (c) 2001-2003, Richard Eckart
6
*----------------------------------------------------------------------
7
* This file is part of gtk-gnutella.
9
* gtk-gnutella is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* gtk-gnutella is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with gtk-gnutella; if not, write to the Free Software
22
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
*----------------------------------------------------------------------
30
* The idtable provides a automatically growing table which can resolve
31
* ids to values very fast. The ids are issues by the table and internally
32
* refer to an array row in the table. The table starts with an initial size
33
* and if full is extended by a definable number of rows. Initial size and
34
* extend size are internally rounded up to a multiple of 32. There is no
35
* limitation to the value and is can be queried whether a given id is in
37
* You can also request special id/value combinations, but you need to keep
38
* in mind that the ids are row numbers. The table is then automatically
39
* grown to contain the requested id, but you can't shrink it later, because
40
* that would mean that the row numbers change and the ids already issued
41
* would become invalid.
42
* If the application needs to shrink a table, I suggest creating a new
43
* table and request the needed number of ids from that. Of course all
44
* ids currently in use by the application must be updated. Once that is
45
* done, flush and destroy the old table.
50
#define idtable_ids(tbl) (tbl->ids)
51
#define idtable_size(tbl) (tbl->size)
53
typedef struct idtable {
54
guint32 size; /* numbers of slots available */
55
guint32 esize; /* number of slots to add if table is full */
56
guint32 ids; /* numbers of slots currently used */
57
guint32 last_id; /* last issued id */
58
guint32 *used_ids; /* binary array of used ids */
59
gpointer *data; /* actual table array */
62
idtable_t *idtable_new(guint32 isize, guint32 esize);
64
void idtable_destroy(idtable_t *table);
66
guint32 idtable_new_id(idtable_t *tbl, gpointer value);
67
void idtable_new_id_value(idtable_t *tbl, guint32 id, gpointer value);
69
void idtable_free_id(idtable_t *tbl, guint32 id);
71
G_INLINE_FUNC gboolean idtable_is_id_used(idtable_t *tbl, guint32 id);
73
void idtable_set_value(idtable_t *tbl, guint32 id, gpointer value);
74
gpointer idtable_get_value(idtable_t *tbl, guint32 id);
76
#endif /* _idtable_h_ */