File: lb-vector.C

package info (click to toggle)
linbox 1.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,940 kB
  • sloc: cpp: 108,392; lisp: 5,469; makefile: 1,345; sh: 1,244; csh: 131; python: 74; perl: 2
file content (378 lines) | stat: -rw-r--r-- 10,390 bytes parent folder | download | duplicates (4)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* lb-vector.C
 * Copyright (C) 2005 Pascal Giorgi
 *
 * Written by Pascal Giorgi <pgiorgi@uwaterloo.ca>
 *
 * ========LICENCE========
 * This file is part of the library LinBox.
 *
  * LinBox is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

#ifndef __LINBOX_lb_vector_C
#define __LINBOX_lb_vector_C

#include "linbox/linbox-config.h"

#include <lb-vector.h>
#include <lb-vector-function.h>

#include <lb-domain-collection.h>


/*\**************************
 * Allocate global variable *
 ****************************/

// global hash table for allocated vector
VectorTable vector_hashtable;

// global variable for the factory
Vector_Factory linbox_vector;

// global variable for current vector type
const char* current_vector  = default_vector;



/**********************************************************
 * function to add an abstract vector in linbox hashtable *
 **********************************************************/
const VectorKey& addVector(VectorAbstract * v){

	std::pair<VectorTable::const_iterator, bool> status;
	status = vector_hashtable.insert(std::pair<VectorKey, VectorAbstract*> (VectorKey(v), v));
	if (status.second)
		return status.first->first;
	else
		throw lb_runtime_error("LinBox ERROR: vector creation failed \n");// throw an exception
}


/*********************************************************
 * API to contruct a n dimensional vector  over a domain *
 *********************************************************/
const VectorKey& createVector(const DomainKey &k, size_t n, const char *name){
	const char *type=name;
	if (type == NULL)
		type= current_vector;

	VectorAbstract *v = linbox_vector.create(type, k, n);
	return addVector(v);
}



/******************************************
 * API to contruct a vector from a stream *
 ******************************************/
const VectorKey& createVector(const DomainKey &k, std::istream &in, const char *name){
	const char *type=name;
	if (type == NULL)
		type= current_vector;

	VectorAbstract *v = linbox_vector.create(type, k, in);
	return addVector(v);
}


/**********************************
 * API to copy an existing vector *
 **********************************/
const VectorKey& copyVector(const VectorKey &k){

	VectorTable::iterator it = vector_hashtable.find(k);
	if (it == vector_hashtable.end())
		throw lb_runtime_error("LinBox ERROR: vector does not exist (copying impossible)\n");

	VectorAbstract *v = it->second->clone();
	return addVector(v);
}


/******************************************
 * API to get the dimensions of  a vector *
 ******************************************/
class VectorDimensionFunctor{
public:
	template<class Vector>
	void operator()(size_t &dim, Vector *V) const{
		dim = V->size();
	}
};

size_t getVectorDimension(const VectorKey &key){
	size_t dim;
	VectorDimensionFunctor Fct;
	VectorFunction::call(dim, key, Fct);
	return dim;
}

/*****************************************
 * API to set a vector with random value *
 *****************************************/
template<class VElement, class DElement>
class RandomVector{
public:
	template<class Vector, class Domain>
	void operator()(Vector *V, Domain *D) {
		throw lb_runtime_error("LinBox ERROR: incompatible type (vector writing impossible)\n");
	}
};


template<class Element>
class RandomVector<Element, Element>{
public:
	template<class Vector, class Domain>
	void operator()(Vector *V, Domain *D) {
		typename Domain::RandIter G(*D);
		for (size_t i=0; i< V->size();++i)
			G.random((*V)[i]);
	}
};

template<class Vector>
class VectorAtRandomFunctorSpec{
protected:
	Vector *vect;
public:
	VectorAtRandomFunctorSpec(Vector *V) : vect(V) {}

	template<class Domain>
	void operator()(void *, Domain *D) const {
		RandomVector<typename Vector::value_type, typename Domain::Element>()(vect, D);
	}
};

class VectorAtRandomFunctor{
protected:
	const VectorKey key;
public:
	VectorAtRandomFunctor(const VectorKey &k) : key(k) {}

	template<class Vector>
	void operator()(void*, Vector *V) const {
		VectorTable::iterator it = vector_hashtable.find(key);
		if ( it == vector_hashtable.end())
			throw lb_runtime_error("LinBox ERROR: invalid vector (set random value impossible)");

		VectorAtRandomFunctorSpec<Vector> Fct(V);
		DomainFunction::call(it->second->getDomainKey(), Fct);
	}
};


void setVectorAtRandom(const VectorKey &k){
	VectorAtRandomFunctor Fct(k);
	VectorFunction::call(k, Fct);
}



/********************************************
 * API to rebind a vector over a new domain *
 ********************************************/
void rebindVector(const VectorKey &Vkey, const DomainKey &Dkey){
	VectorTable::iterator it = vector_hashtable.find(Vkey);
	if ( it == vector_hashtable.end())
		throw lb_runtime_error("LinBox ERROR: invalid vector (rebinding to another domain impossible)");
	it->second->rebind(Dkey);
}


/*****************************************
 * API to write a vector over an ostream *
 *****************************************/
template<class Element, class VElement>
class PrintVector{
public:
	template< class Domain, class Poly, class Out>
	void operator()(Domain *D, Poly *poly, Out &os){
		throw lb_runtime_error("LinBox ERROR: incompatible type (vector writing impossible)\n");
	}
};

template<class Element>
class PrintVector<Element, Element> {
public:
	template< class Domain, class Vector, class Out>
	void operator()(Domain *D, Vector *vect, Out &os){
		os<<" [ ";
		for (size_t i=0; i<vect->size()-1; ++i){
			D->write(os, (*vect)[i])<<" , ";
		}
		D->write(os, (*vect)[vect->size()-1]);
		os<<" ]\n";
	}
};

template<class Vector>
class WriteVectorSpecFunctor{
protected:
	std::ostream     &os;
	Vector         *vect;
public:
	WriteVectorSpecFunctor(std::ostream &o, Vector *V) : os(o), vect(V){}

	template<class Domain>
	void operator()(void *&, Domain *D) const {
		PrintVector<typename Domain::Element, typename Vector::value_type> ()(D, vect, os);
	}
};

class WriteVectorFunctor{
protected:
	std::ostream     &os;
	const VectorKey &key;
public:
	WriteVectorFunctor(std::ostream &o, const VectorKey &k) : os(o), key(k) {}

	template<class Vector>
	void operator() (void*, Vector *V) const {

		VectorTable::iterator it = vector_hashtable.find(key);
		if ( it == vector_hashtable.end())
			throw lb_runtime_error("LinBox ERROR: invalid vector (writing impossible)");

		WriteVectorSpecFunctor<Vector> Fct(os,V);
		DomainFunction::call(it->second->getDomainKey(), Fct);
	}
};


void writeVector (const VectorKey &key, std::ostream &os){
	WriteVectorFunctor Fct(os, key);
	VectorFunction::call(key, Fct);
}





/*******************************************
 * API to modify the current vector type *
 *******************************************/
void setVector(const char* t){
	current_vector= t;
}

/*********************************
 * API to write info on a vector *
 *********************************/
void writeVectorInfo(const VectorKey &key, std::ostream& os){
	VectorTable::iterator it = vector_hashtable.find(key);
	if ( it == vector_hashtable.end())
		throw lb_runtime_error("LinBox ERROR: invalid vector (writing vector info impossible)");
	os<<it->second->info();
}




/**********************************
 * API to serialize a  vector *
 **********************************/
template <class PElement, class DElement, class Category>
class ToSerialVector {
public:
	template<class Domain, class Vector>
	inline void operator()(SerialVector &s, Domain *D, Vector *poly){
		throw lb_runtime_error("LinBox ERROR: incompatible type (vector writing impossible)\n");
	}
};

template<class Element, class Category>
class ToSerialVector<Element, Element, Category>{
public:
	template<class Domain, class Vector>
	inline void operator()(SerialVector &s, Domain *D, Vector *poly){
		s.type = "integer";
		s.list.resize(poly->size());
		typename Vector::const_iterator    it_P= poly->begin();
		std::vector<LinBox::integer>::iterator it_s= s.list.begin();
		for (; it_P != poly->end(); ++it_P, ++it_s)
			D->convert(*it_s, *it_P);
	}
};

template<class Element>
class ToSerialVector<Element, Element, LinBox::RingCategories::RationalTag>{
public:
	template<class Domain, class Vector>
	inline void operator()(SerialVector &s, Domain *D, Vector *poly){
		s.type = "rational";
		s.list.resize(2*poly->size());

		typename Vector::const_iterator    it_P= poly->begin();
		std::vector<LinBox::integer>::iterator it_s= s.list.begin();
		for (; it_P != poly->end(); ++it_P, ++it_s){
			D->get_num(*it_s, *it_P);
			++it_s;
			D->get_den(*it_s, *it_P);
		}
	}
};


template<class Vector>
class SerializeVectorSpecFunctor{
protected:
	Vector     *poly;
public:
	SerializeVectorSpecFunctor(Vector *P) :  poly(P){}

	template<class Domain>
	void operator()(SerialVector &s, Domain *D) const {
		ToSerialVector<typename Vector::value_type, typename Domain::Element, typename LinBox::FieldTraits<Domain>::categoryTag>()(s, D, poly);
	}
};


class SerializeVectorFunctor{
protected:
	const VectorKey &key;
public:
	SerializeVectorFunctor(const VectorKey &k) :  key(k) {}

	template<class Vector>
	void operator() (SerialVector& s, Vector *P) const {

		VectorTable::iterator it = vector_hashtable.find(key);
		if ( it == vector_hashtable.end())
			throw lb_runtime_error("LinBox ERROR: invalid vector (serializing impossible)");

		SerializeVectorSpecFunctor<Vector> Fct(P);
		DomainFunction::call(s, it->second->getDomainKey(), Fct);
	}
};


void  SerializeVector (SerialVector &s, const VectorKey &key) {
	SerializeVectorFunctor Fct(key);
	VectorFunction::call(s, key, Fct);
}


#endif

// Local Variables:
// mode: C++
// tab-width: 4
// indent-tabs-mode: nil
// c-basic-offset: 4
// End:
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s