File: id-map.c

package info (click to toggle)
mrproject 0.5.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,136 kB
  • ctags: 4,114
  • sloc: ansic: 40,041; sh: 7,059; makefile: 1,107; yacc: 318; python: 111
file content (236 lines) | stat: -rw-r--r-- 5,045 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 CodeFactory AB
 * Copyright (C) 2001 Richard Hult <rhult@codefactory.se>
 * Copyright (C) 2001 Mikael Hallendal <micke@codefactory.se>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors: Richard Hult
 * 
 */

#include <gtk/gtk.h>
#include "util/type-utils.h"
#include "id-map.h"

struct _IdMapPriv {
	GHashTable *id_hash;
	gint        next_id;
};

static void id_map_init		(IdMap		 *map);
static void id_map_class_init	(IdMapClass	 *klass);
static void id_map_destroy      (GtkObject       *object);

GNOME_CLASS_BOILERPLATE (IdMap, id_map, GtkObject, gtk_object);

static void
id_map_class_init (IdMapClass *klass)
{
	GtkObjectClass *object_class;
	
	object_class = (GtkObjectClass*) klass;
	object_class->destroy = id_map_destroy;
	
	parent_class = gtk_type_class (GTK_TYPE_OBJECT);
}

static void
id_map_init (IdMap *map)
{
	IdMapPriv *priv;
	
	priv = g_new0 (IdMapPriv, 1);
	map->priv = priv;

	priv->id_hash = g_hash_table_new (g_int_hash, g_int_equal);
	priv->next_id = 0;
}

static void
destroy_id (gpointer   key, 
	    gpointer   value, 
	    gpointer   user_data)
{
	g_free (key);
}

static void
id_map_destroy (GtkObject *object)
{
	IdMap *id_map;

	id_map = ID_MAP (object);
	
	g_hash_table_foreach (id_map->priv->id_hash, destroy_id, NULL);
	g_hash_table_destroy (id_map->priv->id_hash);
	
	g_free (id_map->priv);
	id_map->priv = NULL;

	GNOME_CALL_PARENT_HANDLER (GTK_OBJECT_CLASS, destroy, (object));
}

IdMap *
id_map_new (GNOME_MrProject_Id first_id)
{
	IdMap *map;

	map = gtk_type_new (TYPE_ID_MAP);
	map->priv->next_id = first_id;

	return map;
}

GNOME_MrProject_Id
id_map_insert (IdMap *map, gpointer data)
{
	gint     id, *key;

	id = map->priv->next_id++;
	while (1) {
		if (!g_hash_table_lookup_extended (map->priv->id_hash,
						   &id,
						   NULL,
						   NULL)) {
			/* We found an unsed id. */
			key = g_new (gint, 1);
			*key = id; 
			g_hash_table_insert (map->priv->id_hash, key, data);
			return id;
		}
		id++;
	}

	return -1;
}

gboolean
id_map_insert_id (IdMap *map, GNOME_MrProject_Id id, gpointer data)
{
	gint     *key, id_int;

	id_int = (int) id;
	if (g_hash_table_lookup_extended (map->priv->id_hash,
					  &id_int,
					  NULL,
					  NULL)) {
		/* We already have an entry for this id. */
		return FALSE;
	}

	key = g_new (gint, 1);
	*key = id_int;
	g_hash_table_insert (map->priv->id_hash, key, data);

	return TRUE;
}

gboolean
id_map_remove (IdMap *map, GNOME_MrProject_Id id)
{
	gint     *key, id_int;

	id_int = (int) id;
	if (!g_hash_table_lookup_extended (map->priv->id_hash,
					  &id_int,
					  (gpointer *) &key,
					  NULL)) {
		/* We don't have this id stored. */
		return FALSE;
	}
	
	g_hash_table_remove (map->priv->id_hash, &id_int);
	g_free (key);

	/* Set next id to the one we removed so that we will try to use
	 * it next.
	 */
	if (id_int < map->priv->next_id) {
		map->priv->next_id = id_int;
	}
	
	return TRUE;
}

gpointer
id_map_lookup (IdMap *map, GNOME_MrProject_Id id)
{
	gint id_int;

	id_int = (int) id;
	return g_hash_table_lookup (map->priv->id_hash, &id_int);
}

void
id_map_foreach (IdMap *map, GHFunc func, gpointer user_data)
{
	g_hash_table_foreach (map->priv->id_hash, func, user_data);
}

gint
id_map_size (IdMap *map) 
{
        return g_hash_table_size (map->priv->id_hash);
}

static void
get_key (gpointer key, gpointer value, gpointer user_data) 
{
        GSList   **list;
        
        g_return_if_fail (key != NULL);
        g_return_if_fail (user_data != NULL);
        
        list = (GSList **) user_data;
        
        *list = g_slist_prepend (*list, key);
}

GSList *             
id_map_get_keys (IdMap *map)
{
        GSList       *list = NULL;

        g_hash_table_foreach (map->priv->id_hash, get_key, &list);
        
        return list;
}

static void
get_object (gpointer key, gpointer value, gpointer user_data) 
{
        GSList   **list;
        
        g_return_if_fail (value != NULL);
        g_return_if_fail (user_data != NULL);
        
        list = (GSList **) user_data;
        
        *list = g_slist_prepend (*list, value);
}

GSList *             
id_map_get_objects (IdMap *map)
{
        GSList       *list = NULL;

        g_hash_table_foreach (map->priv->id_hash, get_object, &list);
        
        return list;
}