/usr/include/linbox/util/matrix-stream.inl is in liblinbox-dev 1.1.6~rc0-4.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #include "linbox/util/formats/matrix-stream-readers.h"
namespace LinBox {
template<class Field>
bool MatrixStream<Field>::readWhiteSpace()
{
char c;
while(in.get(c)) {
if (isspace(c))
switch(c) {
case '\n': ++lineNumber; break;
case '\r': if( in.peek() != '\n' ) ++lineNumber; break;
}
else {
in.putback(c);
return true;
}
}
return false; // because eof or read error.
}
template<class Field>
void MatrixStreamReader<Field>::saveTriple(size_t m, size_t n, const Element& v ) {
static std::pair<std::pair<size_t,size_t>,Element> temp;
temp.first.first = m;
temp.first.second = n;
temp.second = v;
savedTriples.push(temp);
}
template<class Field>
MatrixStreamError MatrixStreamReader<Field>::init
(const char* firstLine, std::istream* i, MatrixStream<Field>* m )
{
if( !i || !m || !firstLine ) throw "Bad istream or MatrixStream";
sin = i;
ms = m;
return initImpl(firstLine);
}
template<class Field>
MatrixStreamError MatrixStreamReader<Field>::nextTriple
(size_t& m, size_t& n, Element& v)
{
if( savedTriples.size() == 0 ) {
if( atEnd ) {
if( lastError <= GOOD ) lastError = END_OF_MATRIX;
return lastError;
}
if( lastError > GOOD ) return lastError;
lastError = nextTripleImpl(m,n,v);
return lastError;
}
m = savedTriples.front().first.first;
n = savedTriples.front().first.second;
v = savedTriples.front().second;
savedTriples.pop();
return GOOD;
}
template<class Field>
MatrixStreamError MatrixStreamReader<Field>::getArray
(std::vector<Element> &array)
{
MatrixStreamError mse = GOOD;
size_t c = 0,loc,i,j;
Element v;
while( true ) {
mse = nextTriple(i,j,v);
if( mse > GOOD ) break;
if( i > 0 ) {
mse = getColumns(c);
if( mse > GOOD ) break;
}
loc = i*c+j;
if( loc >= array.size() )
array.resize(c ? (i+1)*c : loc+1);
array[loc] = v;
}
if( mse > END_OF_MATRIX ) return mse;
mse = getRows(j);
if( mse > END_OF_MATRIX ) return mse;
if( array.size() < j*c ) array.resize(j*c);
return GOOD;
}
template<class Field>
MatrixStreamError MatrixStreamReader<Field>::saveNext() {
if( lastError > GOOD ) return lastError;
if( atEnd ) {
lastError = END_OF_MATRIX;
return lastError;
}
size_t m, n;
Element v;
lastError = nextTripleImpl(m,n,v);
if( lastError <= GOOD ) saveTriple(m,n,v);
return lastError;
}
template<class Field>
MatrixStreamError MatrixStreamReader<Field>::getRows(size_t& m) {
MatrixStreamError toRet = GOOD;
while( !knowM ) {
if( atEnd ) return END_OF_MATRIX;
toRet = saveNext();
if( toRet > GOOD ) return toRet;
}
m = _m;
return toRet;
}
template<class Field>
MatrixStreamError MatrixStreamReader<Field>::getColumns(size_t& n) {
MatrixStreamError toRet = GOOD;
while( !knowN ) {
if( atEnd ) return END_OF_MATRIX;
toRet = saveNext();
if( toRet > GOOD ) return toRet;
}
n = _n;
return toRet;
}
template<class Field>
void MatrixStream<Field>::init() {
lineNumber = 1;
//Skip comments
readWhiteSpace();
while( !in.eof() && in.peek() == '#' ) {
char c;
while( in.get(c) ) {
if( c == '\n' ) break;
if( c == '\r' ) {
if( in.peek() == '\n' )
in.get();
break;
}
}
++lineNumber;
readWhiteSpace();
}
//Get first line
firstLine = new char[FIRST_LINE_LIMIT];
in.getline(firstLine,FIRST_LINE_LIMIT);
firstLine[in.gcount()] = '\0';
//Initialize readers
currentError = NO_FORMAT;
__MATRIX_STREAM_READERDEFS
delete[] firstLine;
if( !reader ) return;
else if( currentError > GOOD )
errorLineNumber = lineNumber;
}
template<class Field>
void MatrixStream<Field>::addReader( MatrixStreamReader<Field>* r ) {
if( currentError == GOOD ) {
delete r;
return;
}
MatrixStreamError mse = r->init( firstLine, &in, this );
if( mse < currentError ) {
if( reader ) delete reader;
reader = r;
currentError = mse;
}
}
template<class Field>
MatrixStream<Field>::MatrixStream(const Field& fld, std::istream& i )
:reader(NULL),in(i),readAnythingYet(false),f(fld)
{
init();
if( currentError > GOOD ) throw currentError;
}
template<class Field>
bool MatrixStream<Field>::nextTriple(size_t& m, size_t& n, Element& v) {
if( currentError > GOOD ) return false;
do {
currentError = reader->nextTriple(m,n,v);
} while( f.isZero(v) && currentError == GOOD );
if( currentError != GOOD ) {
errorLineNumber = lineNumber;
return false;
}
readAnythingYet = true;
return true;
}
template<class Field>
bool MatrixStream<Field>::getArray(std::vector<Element> &array) {
if( currentError > GOOD || readAnythingYet ) return false;
currentError = reader->getArray(array);
if( currentError != GOOD ) {
errorLineNumber = lineNumber;
return false;
}
readAnythingYet = true;
return true;
}
template<class Field>
bool MatrixStream<Field>::getRows(size_t& m) {
MatrixStreamError mse = reader->getRows(m);
if( currentError > GOOD )
return (mse == GOOD);
else if( mse > GOOD ) {
currentError = mse;
errorLineNumber = lineNumber;
return false;
}
else return true;
}
template<class Field>
bool MatrixStream<Field>::getColumns(size_t& n) {
MatrixStreamError mse = reader->getColumns(n);
if( currentError > GOOD )
return (mse == GOOD);
else if( mse > GOOD ) {
currentError = mse;
errorLineNumber = lineNumber;
return false;
}
else return true;
}
template<class Field>
bool MatrixStream<Field>::getDimensions( size_t& m, size_t& n ) {
return( getRows(m) && getColumns(n) );
}
template<class Field>
MatrixStreamError MatrixStream<Field>::reportError
( const char* func, int line ) const
{
std::cerr << std::endl
<< "ERROR (" << func << ":" << line << "): "
<< "Problem reading matrix:" << std::endl;
switch( getError() ) {
case END_OF_MATRIX:
std::cerr << "There is no more data in the matrix file.";
break;
case END_OF_FILE:
std::cerr << "An EOF was encountered unexpectedly in reading the data.";
break;
case BAD_FORMAT:
std::cerr << "There is a formatting error in the matrix.";
break;
case NO_FORMAT:
std::cerr << "The matrix format is not recognized or supported.";
break;
case GOOD: break;
default: break;
}
std::cerr << std::endl << "At line number: " << lineNumber << std::endl
<< "Matrix format is " << getFormat() << std::endl;
return currentError;
}
template<class Field>
int MatrixStream<Field>::getLineNumber() const {
if( currentError > GOOD ) return errorLineNumber;
else return lineNumber;
}
} // end of namespace LinBox
|