Skip to content

Commit

Permalink
* Make MatrixReader::detectFormat and dependencies static
Browse files Browse the repository at this point in the history
* Make MatrixReader::detectFormat public
* Implement guess_format in support.h to guess format of input-matrix from file
* Have equal guess field over which to operate based on input-formats
  • Loading branch information
hovinen committed Jun 22, 2011
1 parent 7e45439 commit 06a912a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
11 changes: 1 addition & 10 deletions linbox/matrix/io-png.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace LinBox
{

template <class Ring>
bool MatrixReader<Ring>::isPNG (std::istream &is) const
bool MatrixReader<Ring>::isPNG (std::istream &is)
{
png_byte pngsig[_png_sig_size];

Expand All @@ -36,15 +36,6 @@ bool MatrixReader<Ring>::isPNG (std::istream &is) const
return png_sig_cmp (pngsig, 0, _png_sig_size) == 0;
}

template <class Ring>
bool MatrixReader<Ring>::isPNG (char *buf, std::streamsize n) const
{
if (n < (std::streamsize) _png_sig_size)
return false;

return png_sig_cmp ((png_byte *) buf, 0, _png_sig_size) == 0;
}

template <class Ring>
void MatrixReader<Ring>::PNGReadData (png_structp png_ptr, png_bytep data, png_size_t length)
{
Expand Down
20 changes: 9 additions & 11 deletions linbox/matrix/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class MatrixReader {
template <class Matrix>
std::istream &read (std::istream &is, Matrix &A, FileFormatTag format = FORMAT_DETECT) const;

private:
FileFormatTag detectFormat (std::istream &is) const;
static FileFormatTag detectFormat (std::istream &is);

private:
template <class Matrix>
std::istream &readTurner (std::istream &is, Matrix &A) const;

Expand All @@ -77,19 +77,17 @@ class MatrixReader {
template <class Matrix>
std::istream &readPretty (std::istream &is, Matrix &A) const;

bool isDumas (char *buf, std::streamsize n) const;
bool isTurner (char *buf, std::streamsize n) const;
bool isMaple (char *buf, std::streamsize n) const;
bool isMatlab (char *buf, std::streamsize n) const;
bool isSage (char *buf, std::streamsize n) const;
bool isPretty (char *buf, std::streamsize n) const;
static bool isDumas (char *buf, std::streamsize n);
static bool isTurner (char *buf, std::streamsize n);
static bool isMaple (char *buf, std::streamsize n);
static bool isMatlab (char *buf, std::streamsize n);
static bool isSage (char *buf, std::streamsize n);
static bool isPretty (char *buf, std::streamsize n);

#ifdef __LINBOX_HAVE_LIBPNG
static const unsigned _png_sig_size = 8;

bool isPNG (std::istream &is) const;

bool isPNG (char *buf, std::streamsize n) const;
static bool isPNG (std::istream &is);

static void PNGReadData (png_structp pngPtr, png_bytep data, png_size_t length);

Expand Down
14 changes: 7 additions & 7 deletions linbox/matrix/io.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::istream &MatrixReader<Ring>::read (std::istream &is, Matrix &A, FileFormatT
}

template <class Ring>
bool MatrixReader<Ring>::isDumas (char *buf, std::streamsize n) const
bool MatrixReader<Ring>::isDumas (char *buf, std::streamsize n)
{
regex_t re;

Expand All @@ -86,7 +86,7 @@ bool MatrixReader<Ring>::isDumas (char *buf, std::streamsize n) const
}

template <class Ring>
bool MatrixReader<Ring>::isTurner (char *buf, std::streamsize n) const
bool MatrixReader<Ring>::isTurner (char *buf, std::streamsize n)
{
regex_t re;

Expand All @@ -97,7 +97,7 @@ bool MatrixReader<Ring>::isTurner (char *buf, std::streamsize n) const
}

template <class Ring>
bool MatrixReader<Ring>::isMaple (char *buf, std::streamsize n) const
bool MatrixReader<Ring>::isMaple (char *buf, std::streamsize n)
{
regex_t re;

Expand All @@ -108,7 +108,7 @@ bool MatrixReader<Ring>::isMaple (char *buf, std::streamsize n) const
}

template <class Ring>
bool MatrixReader<Ring>::isMatlab (char *buf, std::streamsize n) const
bool MatrixReader<Ring>::isMatlab (char *buf, std::streamsize n)
{
regex_t re;

Expand All @@ -119,7 +119,7 @@ bool MatrixReader<Ring>::isMatlab (char *buf, std::streamsize n) const
}

template <class Ring>
bool MatrixReader<Ring>::isSage (char *buf, std::streamsize n) const
bool MatrixReader<Ring>::isSage (char *buf, std::streamsize n)
{
regex_t re;

Expand All @@ -130,7 +130,7 @@ bool MatrixReader<Ring>::isSage (char *buf, std::streamsize n) const
}

template <class Ring>
bool MatrixReader<Ring>::isPretty (char *buf, std::streamsize n) const
bool MatrixReader<Ring>::isPretty (char *buf, std::streamsize n)
{
regex_t re;

Expand All @@ -141,7 +141,7 @@ bool MatrixReader<Ring>::isPretty (char *buf, std::streamsize n) const
}

template <class Ring>
FileFormatTag MatrixReader<Ring>::detectFormat (std::istream &is) const
FileFormatTag MatrixReader<Ring>::detectFormat (std::istream &is)
{
FileFormatTag format = FORMAT_UNKNOWN;

Expand Down
22 changes: 18 additions & 4 deletions util/equal.C
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "linbox/ring/modular.h"
#include "linbox/blas/level1.h"
#include "linbox/blas/level3.h"
#include "linbox/matrix/dense.h"

#include "support.h"

Expand Down Expand Up @@ -102,7 +103,7 @@ int check_equal (const Ring &R, const char *input1, FileFormatTag input1_format,

int main (int argc, char **argv)
{
static const char *ringString = "modular";
static const char *ringString = "guess";
static integer p = 65521;
static bool floatingPoint = false;
static const char *input1FileFormat = "guess";
Expand All @@ -111,7 +112,7 @@ int main (int argc, char **argv)
static char *input2 = NULL;

static Argument args[] = {
{ 'k', "-k", "Ring over which to compute ('gf2', 'modular')", TYPE_STRING, &ringString },
{ 'k', "-k", "Ring over which to compute ('guess', 'gf2', 'modular')", TYPE_STRING, &ringString },
{ 'p', "-p", "Modulus of ring, when ring is 'modular'", TYPE_INT, &p },
{ 'f', "-f", "Compute using floating point, when ring is 'modular'", TYPE_NONE, &floatingPoint },
{ '1', "-1", "File format of first input-matrix ('guess', 'dumas', 'turner', 'maple', 'matlab', 'sage', 'png')", TYPE_STRING, &input1FileFormat },
Expand All @@ -131,18 +132,31 @@ int main (int argc, char **argv)
FileFormatTag input1_format = get_format_tag (input1FileFormat);
FileFormatTag input2_format = get_format_tag (input2FileFormat);

if (input1_format == FORMAT_UNKNOWN || input1_format == FORMAT_PRETTY) {
if (input1_format == FORMAT_UNKNOWN) {
std::cerr << "Invalid first input-file-format (use 'guess', 'dumas', 'turner', 'maple', 'matlab', 'sage', or 'png')" << std::endl;
return -1;
}

if (input2_format == FORMAT_UNKNOWN || input2_format == FORMAT_PRETTY) {
if (input2_format == FORMAT_UNKNOWN) {
std::cerr << "Invalid second input-file-format (use 'guess', 'dumas', 'turner', 'maple', 'matlab', 'sage', or 'png')" << std::endl;
return -1;
}

if (input1_format == FORMAT_DETECT)
input1_format = guess_format (input1);

if (input2_format == FORMAT_DETECT)
input2_format = guess_format (input2);

RingType ring_type = get_ring_type (ringString);

if (ring_type == RING_GUESS) {
if (input1_format == FORMAT_PNG || input2_format == FORMAT_PNG)
ring_type = RING_GF2;
else
ring_type = RING_MODULAR;
}

if (ring_type == RING_GF2)
return check_equal (GF2 (), input1, input1_format, input2, input2_format);
else if (ring_type == RING_MODULAR) {
Expand Down
10 changes: 10 additions & 0 deletions util/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ MatrixType get_matrix_type<LinBox::GF2> (const char *str)
return MATRIX_UNKNOWN;
}

LinBox::FileFormatTag guess_format (char *filename)
{
std::ifstream file (filename);

if (!file.good ())
return LinBox::FORMAT_UNKNOWN;

return LinBox::MatrixReader<LinBox::GF2>::detectFormat (file);
}

#endif // __SUPPORT_C

/* template <class Ring> */
Expand Down

0 comments on commit 06a912a

Please sign in to comment.