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