configure.ac: rename BV_LDFLAGS to BV_LIB_LDFLAGS
[barvinok.git] / zsolve / vectorarray.c
blob72b023497494b54f61e7df44303314b954c41a7d
1 /*
2 4ti2 -- A software package for algebraic, geometric and combinatorial
3 problems on linear spaces.
5 Copyright (C) 2006 4ti2 team.
6 Main author(s): Matthias Walter.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "vectorarray.h"
25 #include <assert.h>
26 #include <stdlib.h>
28 // //
30 VectorArray createVectorArray(int var)
32 VectorArray array = (VectorArray)malloc(sizeof(vectorarray_t));
34 if (array==NULL)
36 fprintf(stderr, "Fatal Error (%s/%d): Could not allocate memory for VectorArray!\n", __FILE__, __LINE__);
37 exit(1);
40 array->Variables = var;
41 array->Memory = 0;
42 array->Size = 0;
43 array->Data = NULL;
44 array->Properties = createVariableProperties(var);
46 return array;
49 // //
51 void deleteVectorArray(VectorArray array)
53 int i;
55 if (array)
57 for (i=0; i<array->Size; i++)
58 deleteVector(array->Data[i]);
60 deleteVariableProperties(array->Properties);
62 free(array->Data);
63 free(array);
67 // //
69 void fprintVectorArray(FILE *stream, VectorArray array, bool header)
71 int i;
73 assert(stream);
74 assert(array);
76 if (header)
78 fprintf(stream, "%d %d\n\n", array->Size, array->Variables);
79 fprintVariableProperties(stream, array->Properties, array->Variables);
80 fputs("\n", stream);
83 for (i=0; i<array->Size; i++)
85 fprintVector(stream, array->Data[i], array->Variables);
86 fputs("\n", stream);
90 // //
92 inline void printVectorArray(VectorArray array, bool header)
94 assert(array);
96 fprintVectorArray(stdout, array, header);
99 // //
101 void appendToVectorArray(VectorArray array, Vector vector)
103 assert(array);
104 assert(vector);
106 array->Size++;
107 if (array->Size>array->Memory)
109 array->Memory = 2*(array->Memory+1);
110 array->Data = (Vector *)realloc(array->Data, array->Memory * sizeof(Vector));
112 array->Data[array->Size-1] = vector;
115 // //
117 void swapVectorArrayRows(VectorArray array, int a, int b)
119 Vector vector;
121 assert(array);
122 assert(a>=0 && a<array->Size);
123 assert(b>=0 && b<array->Size);
125 vector = array->Data[a];
126 array->Data[a] = array->Data[b];
127 array->Data[b] = vector;
130 // //
132 void swapVectorArrayColumns(VectorArray array, int a, int b)
134 int i;
136 assert(array);
137 assert(a>=0 && a<array->Variables);
138 assert(b>=0 && b<array->Variables);
140 for (i=0; i<array->Size; i++)
141 swapVector(array->Data[i], a, b);
142 swapVariableProperties(array->Properties, a, b);
145 // //
147 void appendVectorArrayNegatives(VectorArray array)
149 int i,j;
150 Vector vec;
152 assert(array);
154 vec = createVector(array->Variables);
155 for (i=array->Size-1; i>=0; i--)
157 for (j=0; j<array->Variables; j++)
158 vec[j] = -array->Data[i][j];
159 appendToVectorArray(array, copyVector(vec, array->Variables));
161 deleteVector(vec);
164 // //
166 void sortVectorArrayColumns(VectorArray array, ColumnCompare cmp)
168 int i,j;
169 int bestvalue, bestindex;
171 assert(array);
172 assert(cmp);
174 for (i=0; i<array->Variables-1; i++)
176 bestindex = i;
177 bestvalue = array->Properties[i].Column;
178 for (j=i+1; j<array->Variables; j++)
180 if (cmp(array->Properties[j].Column, bestvalue) < 0)
182 bestindex = j;
183 bestvalue = array->Properties[j].Column;
186 swapVectorArrayColumns(array, i, bestindex);
190 // //
192 VectorArray readVectorArray(FILE *stream, bool header)
194 VectorArray array;
195 Vector vector;
196 int size, vars;
198 if (fscanf(stream, "%d %d", &size, &vars)!=2)
199 return NULL;
201 array = createVectorArray(vars);
203 if (header)
204 readVariableProperties(stream, array->Properties, vars);
206 while (size--)
208 vector = readVector(stream, vars);
209 if (vector)
210 appendToVectorArray(array, vector);
211 else
213 deleteVectorArray(array);
214 return NULL;
218 return array;