1 /*
2 Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3 Organisation (CSIRO) Australia
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 - Neither the name of CSIRO Australia nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "config.h"
34
35 #include <stdlib.h>
36 #include "oggz_macros.h"
37 #include "oggz_vector.h"
38
39 typedef struct _OggzTable OggzTable;
40
41 struct _OggzTable {
42 OggzVector * keys;
43 OggzVector * data;
44 };
45
46 OggzTable *
oggz_table_new(void)47 oggz_table_new (void)
48 {
49 OggzTable * table;
50
51 table = oggz_malloc (sizeof (OggzTable));
52 if (table == NULL) return NULL;
53
54 table->keys = oggz_vector_new ();
55 table->data = oggz_vector_new ();
56
57 return table;
58 }
59
60 void
oggz_table_delete(OggzTable * table)61 oggz_table_delete (OggzTable * table)
62 {
63 if (table == NULL) return;
64
65 oggz_vector_delete (table->keys);
66 oggz_vector_delete (table->data);
67 oggz_free (table);
68 }
69
70 void *
oggz_table_lookup(OggzTable * table,long key)71 oggz_table_lookup (OggzTable * table, long key)
72 {
73 int i, size;
74
75 if (table == NULL) return NULL;
76
77 size = oggz_vector_size (table->keys);
78 for (i = 0; i < size; i++) {
79 if (oggz_vector_nth_l (table->keys, i) == key) {
80 return oggz_vector_nth_p (table->data, i);
81 }
82 }
83
84 return NULL;
85 }
86
87 void *
oggz_table_insert(OggzTable * table,long key,void * data)88 oggz_table_insert (OggzTable * table, long key, void * data)
89 {
90 void * old_data;
91
92 if ((old_data = oggz_table_lookup (table, key)) != NULL) {
93 if (oggz_vector_remove_l (table->keys, key) == NULL)
94 return NULL;
95
96 if (oggz_vector_remove_p (table->data, old_data) == NULL) {
97 /* XXX: This error condition can only happen if the previous
98 * removal succeeded, and this removal failed, ie. there was
99 * an error reallocing table->data->data downwards. */
100 return NULL;
101 }
102 }
103
104 if (oggz_vector_insert_l (table->keys, key) == -1)
105 return NULL;
106
107 if (oggz_vector_insert_p (table->data, data) == NULL) {
108 oggz_vector_remove_l (table->keys, key);
109 return NULL;
110 }
111
112 return data;
113 }
114
115 int
oggz_table_remove(OggzTable * table,long key)116 oggz_table_remove (OggzTable * table, long key)
117 {
118 void * old_data;
119
120 if ((old_data = oggz_table_lookup (table, key)) != NULL) {
121 if (oggz_vector_remove_l (table->keys, key) == NULL)
122 return -1;
123
124 if (oggz_vector_remove_p (table->data, old_data) == NULL) {
125 /* XXX: This error condition can only happen if the previous
126 * removal succeeded, and this removal failed, ie. there was
127 * an error reallocing table->data->data downwards. */
128 return -1;
129 }
130 }
131
132 return 0;
133 }
134
135 int
oggz_table_size(OggzTable * table)136 oggz_table_size (OggzTable * table)
137 {
138 if (table == NULL) return 0;
139 return oggz_vector_size (table->data);
140 }
141
142 void *
oggz_table_nth(OggzTable * table,int n,long * key)143 oggz_table_nth (OggzTable * table, int n, long * key)
144 {
145 if (table == NULL) return NULL;
146 if (key) *key = oggz_vector_nth_l (table->keys, n);
147 return oggz_vector_nth_p (table->data, n);
148 }
149