1/* $Id$Revision: */
2
3/*************************************************************************
4 * Copyright (c) 2011 AT&T Intellectual Property
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors: See CVS logs. Details at http://www.graphviz.org/
11 *************************************************************************/
12
13#include "general.h"
14#include "vector.h"
15
16
17/*---------------- base vector class ----------- */
18Vector Vector_new(int maxlen, size_t size_of_elem, void (*deallocator)(void *v)){
19 Vector v;
20 v = malloc(sizeof(struct vector_struct));
21 if (maxlen <= 0) maxlen = 1;
22 v->maxlen = maxlen;
23 v->len = 0;
24 v->size_of_elem = size_of_elem;
25 v->deallocator = deallocator;
26 v->v = malloc(size_of_elem*maxlen);
27 if (!v->v){
28 free(v);
29 return NULL;
30 }
31 return v;
32}
33
34Vector Vector_assign(Vector v, void *stuff, int i){
35 memcpy(((char*) v->v)+(v->size_of_elem)*i/sizeof(char), stuff, v->size_of_elem);
36 return v;
37}
38
39Vector Vector_reset(Vector v, void *stuff, int i){
40 if (i >= v->len) return NULL;
41 if (v->deallocator)(v->deallocator)((char*)v->v + (v->size_of_elem)*i/sizeof(char));
42 return Vector_assign(v, stuff, i);
43}
44
45
46Vector Vector_add(Vector v, void *stuff){
47 if (v->len + 1 >= v->maxlen){
48 v->maxlen = v->maxlen + MAX((int) .2*(v->maxlen), 10);
49 v->v = realloc(v->v, (v->maxlen)*(v->size_of_elem));
50 if (!(v->v)) return NULL;
51 }
52
53 return Vector_assign(v, stuff, (v->len)++);
54}
55
56void Vector_delete(Vector v){
57 int i;
58 if (!v) return;
59 for (i = 0; i < v->len; i++){
60 if (v->deallocator)(v->deallocator)((char*)v->v + (v->size_of_elem)*i/sizeof(char));
61 }
62 free(v->v);
63 v->v = NULL;
64 free(v);
65};
66
67void* Vector_get(Vector v, int i){
68 if (i >= v->len) return NULL;
69 return ((char*)v->v + i*(v->size_of_elem)/sizeof(char));
70}
71
72int Vector_get_length(Vector v){
73 return v->len;
74}
75
76
77
78/*---------------- integer vector --------------- */
79
80void intdealloactor(void *v){
81}
82
83Vector IntegerVector_new(int len){
84 return Vector_new(len, sizeof(int), intdealloactor);
85
86}
87Vector IntegerVector_add(Vector v, int i){
88 return Vector_add(v, (void*) &i);
89}
90
91void IntegerVector_delete(Vector v){
92 return Vector_delete(v);
93}
94
95int* IntegerVector_get(Vector v, int i){
96 int *p;
97 p = (int*) Vector_get(v, i);
98 if (!p) return NULL;
99 return (int*) p;
100}
101
102int IntegerVector_get_length(Vector v){
103 return Vector_get_length(v);
104}
105
106Vector IntegerVector_reset(Vector v, int content, int pos){
107 return Vector_reset(v, (void*) (&content), pos);
108}
109
110
111
112
113/*---------------- string vector --------------- */
114
115void nulldealloactor(void *v){
116 return;
117}
118void strdealloactor(void *v){
119 char **s;
120 s = (char**) v;
121 free(*s);
122}
123
124Vector StringVector_new(int len, int delete_element_strings){
125 /* delete_element_strings decides whether we need to delete each string in the vector or leave it to be cleaned by other handles */
126 if (!delete_element_strings){
127 return Vector_new(len, sizeof(char*), nulldealloactor);
128 } else {
129 return Vector_new(len, sizeof(char*), strdealloactor);
130 }
131
132}
133Vector StringVector_add(Vector v, char *s){
134 return Vector_add(v, (void*) &s);
135}
136
137void StringVector_delete(Vector v){
138 return Vector_delete(v);
139}
140
141char** StringVector_get(Vector v, int i){
142 char **p;
143 p = (char**) Vector_get(v, i);
144 if (!p) return NULL;
145 return p;
146}
147
148int StringVector_get_length(Vector v){
149 return Vector_get_length(v);
150}
151
152Vector StringVector_reset(Vector v, char *content, int pos){
153 return Vector_reset(v, (void*) (&content), pos);
154}
155
156void StringVector_fprint1(FILE *fp, StringVector v){
157 int i;
158 if (!v) return;
159 for (i = 0; i < StringVector_get_length(v); i++){
160 fprintf(fp,"%s\n", *(StringVector_get(v, i)));
161 }
162}
163
164void StringVector_fprint(FILE *fp, StringVector v){
165 int i;
166 if (!v) return;
167 for (i = 0; i < StringVector_get_length(v); i++){
168 fprintf(fp,"%d %s\n", i+1,*(StringVector_get(v, i)));
169 }
170}
171
172StringVector StringVector_part(StringVector v, int n, int *selected_list){
173 /* select a list of n elements from vector v and form a new vector */
174 StringVector u;
175 char *s, *s2;
176 int i;
177 u = StringVector_new(1, TRUE);
178 for (i = 0; i < n; i++){
179 s = *(StringVector_get(v, selected_list[i]));
180 s2 = MALLOC(sizeof(char)*(strlen(s)+1));
181 strcpy(s2, s);
182 StringVector_add(u, s2);
183 }
184 return u;
185}
186
187
188
189
190
191
192/*
193#include <stdio.h>
194int main(){
195 IntegerVector v;
196 StringVector vs;
197 int i, *j;
198 char *s;
199 char **sp;
200
201 for (;;){
202 v = IntegerVector_new(1);
203 for (i = 0; i < 10; i++){
204 IntegerVector_add(v, i);
205 }
206
207 for (i = 0; i < Vector_get_length(v); i++){
208 j = IntegerVector_get(v, i);
209 if (j) printf("element %d = %d\n",i,*j);
210 }
211 for (i = 0; i < 12; i++){
212 IntegerVector_reset(v, i+10, i);
213 }
214
215 for (i = 0; i < Vector_get_length(v); i++){
216 j = IntegerVector_get(v, i);
217 if (j) printf("element %d = %d\n",i,*j);
218 }
219
220 IntegerVector_delete(v);
221 }
222
223 for (;;){
224
225
226 v = StringVector_new(1, TRUE);
227 for (i = 0; i < 10; i++){
228 s = malloc(sizeof(char)*2);
229 s[0] = '1';
230 s[1] = '1'+i;
231 StringVector_add(v, s);
232 }
233
234 for (i = 0; i < Vector_get_length(v); i++){
235 sp = StringVector_get(v, i);
236 if (sp) printf("element %d = %s\n",i,*sp);
237 }
238 for (i = 0; i < 10; i++){
239 s = malloc(sizeof(char)*2);
240 s[0] = '1';
241 s[1] = '2'+i;
242 StringVector_reset(v, s, i);
243 }
244
245 for (i = 0; i < Vector_get_length(v); i++){
246 sp = StringVector_get(v, i);
247 if (sp) printf("element %d = %s\n",i,*sp);
248 }
249
250 StringVector_delete(v);
251
252 }
253}
254*/
255
256