1
/********************************************************************\
2
* gtable.c -- glib -- basic datatype for 2D array of values *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License as *
6
* published by the Free Software Foundation; either version 2 of *
7
* the License, or (at your option) any later version. *
9
* This program 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 *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License*
15
* along with this program; if not, contact: *
17
* Free Software Foundation Voice: +1-617-542-5942 *
18
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19
* Boston, MA 02110-1301, USA gnu@gnu.org *
21
\********************************************************************/
37
g_table_entry_constructor constructor;
38
g_table_entry_destroyer destroyer;
44
g_table_new (guint entry_size,
45
g_table_entry_constructor constructor,
46
g_table_entry_destroyer destroyer,
51
gtable = g_new(GTable, 1);
53
gtable->array = g_array_new(FALSE, FALSE, entry_size);
55
gtable->entry_size = entry_size;
60
gtable->constructor = constructor;
61
gtable->destroyer = destroyer;
63
gtable->user_data = user_data;
69
g_table_destroy (GTable *gtable)
74
g_table_resize (gtable, 0, 0);
76
g_array_free (gtable->array, TRUE);
84
g_table_index (GTable *gtable, int row, int col)
90
if ((row < 0) || (col < 0))
92
if (row >= gtable->rows)
94
if (col >= gtable->cols)
97
index = ((row * gtable->cols) + col) * gtable->entry_size;
99
return >able->array->data[index];
103
g_table_resize (GTable *gtable, int rows, int cols)
110
if ((rows < 0) || (cols < 0))
113
old_len = gtable->array->len;
114
new_len = rows * cols;
116
if (new_len == old_len)
119
/* If shrinking, destroy extra cells */
120
if ((new_len < old_len) && gtable->destroyer)
125
entry = >able->array->data[new_len * gtable->entry_size];
126
for (i = new_len; i < old_len; i++)
128
gtable->destroyer(entry, gtable->user_data);
129
entry += gtable->entry_size;
133
/* Change the size */
134
g_array_set_size(gtable->array, new_len);
136
/* If expanding, construct the new cells */
137
if ((new_len > old_len) && gtable->constructor)
142
entry = >able->array->data[old_len * gtable->entry_size];
143
for (i = old_len; i < new_len; i++)
145
gtable->constructor(entry, gtable->user_data);
146
entry += gtable->entry_size;
155
g_table_rows (GTable *gtable)
164
g_table_cols (GTable *gtable)