File: ReadSubcones.cpp

package info (click to toggle)
latte-int 1.7.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,260 kB
  • sloc: cpp: 32,231; sh: 4,413; makefile: 811; perl: 300
file content (257 lines) | stat: -rw-r--r-- 7,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* ReadSubcones.cpp -- Read/write a simple file format describing a collection of subcones
	       
   Copyright 2007 Matthias Koeppe

   This file is part of LattE.
   
   LattE is free software; you can redistribute it and/or modify it
   under the terms of the version 2 of the GNU General Public License
   as published by the Free Software Foundation.

   LattE is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LattE; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include <cassert>
#include <vector>
#include <iomanip>
#include "ReadSubcones.h"

using namespace std;

static void check_stream(const istream &f, const char *fileName, const char *proc)
{
  if (!f.good()) {
    cerr << "Read error on input file " << fileName << " in " << proc << "." << endl;
    exit(1);
  }
};

static vector<listVector *>
ray_array(listCone *cone)
{
  int num_rays = lengthListVector(cone->rays);
  vector<listVector *> rays(num_rays);
  int j;
  listVector *ray;
  for (j = 0, ray = cone->rays; ray!=NULL; j++, ray = ray->rest)
    rays[j] = ray;
  return rays;
}

static listCone *cone_from_ray_indicator(const vector<listVector *> &ray_array,
					 const vector<bool> &ray_indicator,
					 listCone *master_cone)
{
  listCone *result = createListCone();
  assert(ray_array.size() == ray_indicator.size());
  unsigned int i;
  for (i = 0; i<ray_array.size(); i++) {
    if (ray_indicator[i]) {
      result->rays = new listVector(ray_array[i]->first, result->rays,
				    /* index hint: */ i);
    }
  }
  result->vertex = new Vertex(*master_cone->vertex);
  return result;
}

void
ReadSubcones(listCone *master_cone, int numOfVars,
	     ifstream &f, const char *fileName,
	     ConeConsumer &consumer)
{
  int numOfSubcones, numOfRays;
  f >> numOfSubcones >> numOfRays;
  check_stream(f, fileName, "ReadSubcones");
  if (numOfRays != lengthListVector(master_cone->rays)) {
    cerr << "Wrong subcones file dimensions:"
	 << "Master cone has " << lengthListVector(master_cone->rays) << " rays, "
	 << "subcones file specified " << numOfRays << " rays."
	 << endl;
    exit(1);
  }
  int i;
  vector<listVector *> rays = ray_array(master_cone);
  vector<bool> ray_indicator(numOfRays);
  consumer.SetNumCones(numOfSubcones);
  for (i = 0; i<numOfSubcones; i++) {
    int j;
    for (j = 0; j<numOfRays; j++) {
      int x;
      f >> x;
      if (x != 0 && x != 1) {
	cerr << "Subcone file contains bad numbers, only 0 and 1 are defined." << endl;
	exit(1);
      }
      ray_indicator[j] = x;
    }
    check_stream(f, fileName, "ReadSubcones");
    listCone *cone = cone_from_ray_indicator(rays, ray_indicator,
					     master_cone);
    cone->index_hint = i;
    consumer.ConsumeCone(cone);
  }
}

void
ReadSubcones(listCone *master_cone, int numOfVars,
	     const string &fileName,
	     ConeConsumer &consumer)
{
  ifstream file(fileName.c_str());
  ReadSubcones(master_cone, numOfVars, file, fileName.c_str(), consumer);
}


SubconeReadingConeProducer::SubconeReadingConeProducer
(listCone *a_master_cone, const string &a_filename, int a_size_estimate)
  : master_cone(a_master_cone),
    filename(a_filename),
    size_estimate(a_size_estimate)
{
}

void SubconeReadingConeProducer::Produce(ConeConsumer &consumer)
{
  if (size_estimate)
    consumer.SetNumCones(size_estimate);
  ReadSubcones(master_cone, master_cone->rays->first.length(),
	       filename, consumer);
}


IncrementalVectorFileWriter::IncrementalVectorFileWriter(const std::string &filename, int a_dimension)
  : num_vectors(0), stream(filename.c_str()), dimension(a_dimension)
{
  // We fill in the correct number of lines later.
  if (!stream.good()) {
    cerr << "Cannot write to file " << filename << endl;
    exit(1);
  }
  stream << setw(16) << left << -1 << setw(0) << right
	 << " " << dimension << endl;
}

IncrementalVectorFileWriter::~IncrementalVectorFileWriter()
{
  UpdateNumVectors();
}

void
IncrementalVectorFileWriter::WriteVector(const vec_ZZ &v)
{
  int index;
  assert(dimension == v.length());
  for (index = 0; index<dimension; index++) {
    stream << v[index] << " ";
  }
  stream << endl;
  num_vectors++;
  if (!stream.good()) {
    cerr << "Error writing to vector file" << endl;
    exit(1);
  }
}

void
IncrementalVectorFileWriter::WriteVector(const std::vector<bool> &v)
{
  int index;
  assert(dimension == v.size());
  for (index = 0; index<dimension; index++) {
    stream << v[index] << " ";
  }
  stream << endl;
  num_vectors++;
  if (!stream.good()) {
    cerr << "Error writing to vector file" << endl;
    exit(1);
  }
}

void
IncrementalVectorFileWriter::WriteVector(const std::vector<int> &v)
{
  int index;
  assert(dimension == v.size());
  for (index = 0; index<dimension; index++) {
    stream << v[index] << " ";
  }
  stream << endl;
  num_vectors++;
  if (!stream.good()) {
    cerr << "Error writing to vector file" << endl;
    exit(1);
  }
}

void
IncrementalVectorFileWriter::UpdateNumVectors()
{
  stream.seekp(0, ios::beg);
  stream << setw(16) << left << num_vectors;
  stream.seekp(0, ios::end);
  stream.flush();
  if (!stream.good()) {
    cerr << "Error writing to vector file" << endl;
    exit(1);
  }
}


SubconePrintingConeConsumer::SubconePrintingConeConsumer(const listCone *master_cone, 
							 const std::string & filename)
  : cone_count(0), master_rays(lengthListVector(master_cone->rays))
{
  listVector *ray;
  int index;
  for (ray = master_cone->rays, index = 0; ray!=NULL; ray=ray->rest, index++) {
    std::map<vector<mpz_class>, int>::value_type p(convert_vec_ZZ_to_mpz_vector(ray->first), 
						   index);
    index_map.insert(p);
    master_rays[index] = ray->first;
  }
  file_writer = new IncrementalVectorFileWriter(filename, index);
}

SubconePrintingConeConsumer::~SubconePrintingConeConsumer()
{
  delete file_writer;
}

int SubconePrintingConeConsumer::ConsumeCone(listCone *cone)
{
  cone_count++;
  int num_master_rays = index_map.size();
  vector<bool> ray_indicator(num_master_rays);
  listVector *ray;
  for (ray = cone->rays; ray!=NULL; ray=ray->rest) {
    if (ray->index_hint >= 0
	&& ray->index_hint < master_rays.size()
	&& ray->first == master_rays[ray->index_hint])
      ray_indicator[ray->index_hint] = true;
    else {
      // index_hint not available or wrong
      vector<mpz_class> v = convert_vec_ZZ_to_mpz_vector(ray->first);
      std::map<vector<mpz_class>, int>::iterator i
	= index_map.find(v);
      if (i == index_map.end()) {
	cerr << "Cone has a ray that does not belong to the master cone; cannot print it as a subcone."
	     << endl;
	exit(1);
      }
      int index = (*i).second;
      ray_indicator[index] = true;
    }
  }
  file_writer->WriteVector(ray_indicator);
  freeCone(cone);
  return 1;
}