PLearn 0.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::AsciiVMatrix Class Reference

#include <AsciiVMatrix.h>

Inheritance diagram for PLearn::AsciiVMatrix:
Inheritance graph
[legend]
Collaboration diagram for PLearn::AsciiVMatrix:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 AsciiVMatrix ()
 default constructor (for automatic deserialization)
 AsciiVMatrix (const string &fname, bool readwrite=true)
 AsciiVMatrix (const string &fname, int the_width, const TVec< string > &fieldnames=TVec< string >(), const string &the_comment="")
virtual void appendRow (Vec v)
 Append a row at the end of the file.
virtual void put (int i, int j, real value)
 These methods send an error message since, by choice, we accept only to append row at the end of the file.
virtual void putSubRow (int i, int j, Vec v)
 It is suggested that this method be implemented in subclasses of writable matrices to speed up accesses (default version repeatedly calls put(i,j,value) which may have a significant overhead)
virtual void putRow (int i, Vec v)
virtual void build ()
 Simply calls inherited::build() then build_().
virtual ~AsciiVMatrix ()
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual AsciiVMatrixdeepCopy (CopiesMap &copies) const

Static Public Member Functions

static string _classname_ ()
 Declares name and deepCopy methods.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

virtual void getNewRow (int i, const Vec &v) const
 This is the only method requiring implementation.

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares this class' options.

Protected Attributes

string filename
PStream file
vector< streampos > pos_rows
streampos vmatlength_pos
bool readwritemode
bool newfile
bool rewrite_length
int length_max

Private Types

typedef RowBufferedVMatrix inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 49 of file AsciiVMatrix.h.


Member Typedef Documentation

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 51 of file AsciiVMatrix.h.


Constructor & Destructor Documentation

PLearn::AsciiVMatrix::AsciiVMatrix ( )

default constructor (for automatic deserialization)

Definition at line 55 of file AsciiVMatrix.cc.

    :readwritemode(false), newfile(true),
     rewrite_length(true)
{}
PLearn::AsciiVMatrix::AsciiVMatrix ( const string &  fname,
bool  readwrite = true 
)

Definition at line 60 of file AsciiVMatrix.cc.

References build().

    :filename(fname), readwritemode(readwrite), newfile(false),
     rewrite_length(true)
{
    build();
}

Here is the call graph for this function:

PLearn::AsciiVMatrix::AsciiVMatrix ( const string &  fname,
int  the_width,
const TVec< string > &  fieldnames = TVec<string>(),
const string &  the_comment = "" 
)

Definition at line 67 of file AsciiVMatrix.cc.

References PLearn::VMatrix::build(), PLearn::VMatrix::declareField(), PLearn::endl(), file, filename, PLearn::isfile(), PLearn::TVec< T >::length(), length_max, PLearn::openFile(), PLERROR, PLearn::PStream::raw_ascii, PLearn::space_to_underscore(), and PLearn::VMatrix::width().

    :inherited(0,the_width), filename(fname),
     readwritemode(true), newfile(true), rewrite_length(true)
{
    inherited::build();

    if (isfile(filename))
        PLERROR("In AsciiVMatrix constructor: filename %s already exists",filename.c_str());
    file = openFile(filename, PStream::raw_ascii, "w");
    // TODO Is this really the same as old code ?
    // file->open(filename.c_str(), fstream::in | fstream::out | fstream::trunc);

    file << "#size: ";
    PLERROR("In AsciiVMatrix::AsciiVMatrix - Code is not yet PStram compatible");
    // vmatlength_pos = file.tellp(); // TODO See how to do this with PStreams.
    length_max = 9999999;  // = 10 000 000 - 1
    file << "0 " << width() << "      " << endl;


    if(the_fieldnames.length()>0)
    {
        if(the_fieldnames.length()!=the_width)
            PLERROR("In AsciiVMatrix constructor: number of given fieldnames (%d) differs from given width (%d)",
                    the_fieldnames.length(), the_width);
        file << "#: ";
        for(int k=0; k<the_width; k++)
        {
            string field_k = space_to_underscore(the_fieldnames[k]);
            declareField(k, field_k);
            file << field_k << ' ';
        }
        file << endl;
    }

    if(comment!="")
    {
        if(comment[0]!='#')
            PLERROR("In AsciiVMatrix constructor: comment must start with a #");
        file << comment;
        if(comment[comment.length()-1]!='\n')
            file << endl;
    }

}

Here is the call graph for this function:

PLearn::AsciiVMatrix::~AsciiVMatrix ( ) [virtual]

Definition at line 316 of file AsciiVMatrix.cc.

{}

Member Function Documentation

string PLearn::AsciiVMatrix::_classname_ ( ) [static]

Declares name and deepCopy methods.

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 53 of file AsciiVMatrix.cc.

OptionList & PLearn::AsciiVMatrix::_getOptionList_ ( ) [static]

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 53 of file AsciiVMatrix.cc.

RemoteMethodMap & PLearn::AsciiVMatrix::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 53 of file AsciiVMatrix.cc.

bool PLearn::AsciiVMatrix::_isa_ ( const Object o) [static]

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 53 of file AsciiVMatrix.cc.

Object * PLearn::AsciiVMatrix::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 53 of file AsciiVMatrix.cc.

StaticInitializer AsciiVMatrix::_static_initializer_ & PLearn::AsciiVMatrix::_static_initialize_ ( ) [static]

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 53 of file AsciiVMatrix.cc.

void PLearn::AsciiVMatrix::appendRow ( Vec  v) [virtual]

Append a row at the end of the file.

Reimplemented from PLearn::VMatrix.

Definition at line 273 of file AsciiVMatrix.cc.

References PLearn::endl(), file, i, PLearn::VMatrix::length(), PLearn::TVec< T >::length(), PLearn::VMatrix::length_, length_max, PLERROR, readwritemode, rewrite_length, and PLearn::VMatrix::width().

{
    if(v.length()!=width())
        PLERROR("In AsciiVMatrix::appendRow, length of Vec to append (%d) differs from width of matrix (%d)",v.length(), width());

    if (length() == length_max)
        PLERROR("AsciiVMatrix::appendRow aborted: the matrix has reach its maximum length.");

    if(!readwritemode)
        PLERROR("AsciiVMatrix::appendRow aborted: the vmat was opened in read only format.");

    // write the Vec at the end of the file
    // file.seekp(0,fstream::end); // TODO How to do this with PStreams ?
    // pos_rows.push_back(file.tellg()); // TODO How to do this with PStreams ?

    // file.precision(15); // TODO See what it means to remove it.
    for (int i=0; i<v.length(); i++)
        file << v[i] << ' ';
    file << endl;

    // update the length
    length_++;
    if (rewrite_length)
    {
        // file->seekp(vmatlength_pos); // TODO How to do this with PStreams ?
        file << length() << " " << width();
    }
}

Here is the call graph for this function:

void PLearn::AsciiVMatrix::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::VMatrix.

Definition at line 114 of file AsciiVMatrix.cc.

References PLearn::VMatrix::build(), and build_().

Referenced by AsciiVMatrix().

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::AsciiVMatrix::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::VMatrix.

Definition at line 120 of file AsciiVMatrix.cc.

References a, b, c, PLearn::countNonBlankLinesOfFile(), PLearn::fast_exact_is_equal(), file, filename, PLearn::PStream::getline(), PLearn::getNextNonBlankLine(), PLearn::isfile(), PLearn::VMatrix::length(), PLearn::VMatrix::length_, length_max, newfile, PLearn::openFile(), pl_log, PLERROR, PLWARNING, pos_rows, PLearn::pow(), PLearn::PStream::raw_ascii, PLearn::removeblanks(), PLearn::removenewline(), rewrite_length, PLearn::split(), PLearn::toint(), PLearn::VMatrix::updateMtime(), PLearn::VMatrix::width(), PLearn::VMatrix::width_, and PLearn::ws().

Referenced by build().

{
    updateMtime(filename);

    if(!newfile)  // open old file
    {
        if (!isfile(filename))
            PLERROR("In AsciiVMatrix constructor (with specified width), filename %s does not exists",filename.c_str());
        file = openFile(filename, PStream::raw_ascii, "w");
        // TODO Is this really the same as the old code ?
        // file->open(filename.c_str(), fstream::in | fstream::out);

        // read the matrix in old or new format
        int length = -1;
        int width = -1;
        bool could_be_old_amat = true;
        // file->seekg(0,fstream::beg); // TODO Will it work without this ?
        file >> ws;
        string line;
        while (file.peek()=='#')
        {
            PLERROR("In AsciiVMatrix::build_ - Code is not PStream-compatible yet");
            // streampos old_pos = file.tellg(); // TODO See how to do this with PStreams.
            file.getline(line);
            could_be_old_amat = false;
            size_t pos=line.find(":");
            if (pos!=string::npos)
            {
                string sub=line.substr(0,pos);
                if (sub=="#size") // we've found the dimension specification line
                {
                    string siz=removeblanks((line.substr(pos)).substr(1));
                    vector<string> dim = split(siz," ");
                    if (dim.size()!=2)  PLERROR("I need exactly 2 dimensions for matrix");
                    length = toint(dim[0]);
                    width = toint(dim[1]);

                    // we set vmatlength_pos
                    // streampos current_pos = file.tellg(); // TODO See how to do this with PStreams.
                    PLERROR("In AsciiVMatrix::build_ - Code is not PStream-compatible yet");
                    // file->seekg(old_pos); // TODO How to do this with PStreams ?
                    char c = file.get();
                    while (c != ':')
                    {
                        c = file.get();
                    }
                    c = file.get();
                    // vmatlength_pos = file.tellp(); // TODO See how to do this with PStreams.
                    //file.seekg(current_pos); // TODO How to do this with PStreams ?

                    // we set length_max
                    int width_ndigits = (int)pl_log(real(width)) + 1;
                    string remain = removenewline(line.substr(pos+1));
                    int length_ndigits = (int)remain.length() - width_ndigits - 1;
                    length_max = (int)pow(10.0,double(length_ndigits)) - 1;
                }
            }
            file >> ws;
        }

        if (length==-1)  // still looking for size info...
        {
            getNextNonBlankLine(file,line);
            int nfields1 = (int)split(line).size();
            getNextNonBlankLine(file,line);
            if (line=="") // only one line, no length nor width info
            {
                length=1;
                width=nfields1;
                rewrite_length = false;
                could_be_old_amat = false;
            }
            int nfields2 = (int)split(line).size();
            int guesslength = countNonBlankLinesOfFile(filename);
            real a, b;
            if (could_be_old_amat && nfields1==2) // could be an old .amat with first 2 numbers being length width
            {
                PLERROR("In AsciiVMatrix::build_ - Code is not PStream-compatible yet");
                // file.seekg(0,fstream::beg); // TODO How to do this with PStreams ?
                // file.clear(); // TODO Same question
                // vmatlength_pos = file.tellp(); // TODO See how to do this with PStreams.
                file >> a >> b;
                if (guesslength == int(a)+1              &&
                    fast_exact_is_equal(real(int(a)), a) &&
                    fast_exact_is_equal(real(int(b)), b) &&
                    a>0 && b>0 && int(b)==nfields2) // it's clearly an old .amat
                {
                    length = int(a);
                    width = int(b);

                    // file.seekg(vmatlength_pos); // TODO How to do this with PStreams ?
                    file.getline(line);
                    int width_ndigits = (int)pl_log(real(width)) + 1;
                    int max_length_ndigits = (int)line.length() - width_ndigits - 1;
                    length_max = (int)pow(10.0,double(max_length_ndigits)) - 1;
                }
            }

            if (length==-1) // still don't know size info...
            {
                if (nfields1==nfields2) // looks like a plain ascii file
                {
                    rewrite_length = false;
                    length=guesslength;
                    if (width!=-1 && width!=nfields1)
                    {
                        PLWARNING("In AsciiVMatrix:  Number of fieldnames and width mismatch in file %s.", filename.c_str());
                    }
                    width = nfields1;
                }
                else
                    PLERROR("In AsciiVMatrix: trying to load but couldn't determine file format automatically for %s",filename.c_str());
            }
        }

        length_ = length;
        width_ = width;

        // build the vector of position of the begining of the lines
        PLERROR("In AsciiVMatrix::build_ - Code is not PStream-compatible yet");
        // file.seekg(0,fstream::beg); // TODO How to do this with PStreams ?
        // file.clear();
        if (could_be_old_amat && rewrite_length)
        {
            file.getline(line);
        }
        pos_rows.clear();
        while (!file.eof())
        {
            file >> ws;
            // if (file.peek()!='#' && file.peek()!=EOF) pos_rows.push_back(file.tellg()); // TODO See how to do this with PStreams.
            file.getline(line);
        }
        // file.clear(); // TODO See how to do this with PStreams.
        if ((int)pos_rows.size() != length)
            PLERROR("In AsciiVMatrix: the matrix has not the rigth size");
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::AsciiVMatrix::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 53 of file AsciiVMatrix.cc.

void PLearn::AsciiVMatrix::declareOptions ( OptionList ol) [static, protected]

Declares this class' options.

Reimplemented from PLearn::VMatrix.

Definition at line 309 of file AsciiVMatrix.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::VMatrix::declareOptions(), filename, and readwritemode.

{
    declareOption(ol, "filename", &AsciiVMatrix::filename, OptionBase::buildoption, "Filename of the matrix");
    declareOption(ol, "readwritemode", &AsciiVMatrix::readwritemode, OptionBase::buildoption, "Is the file to be opened in read/write mode");
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::AsciiVMatrix::declaringFile ( ) [inline, static]

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 110 of file AsciiVMatrix.h.

AsciiVMatrix * PLearn::AsciiVMatrix::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 53 of file AsciiVMatrix.cc.

void PLearn::AsciiVMatrix::getNewRow ( int  i,
const Vec v 
) const [protected, virtual]

This is the only method requiring implementation.

Implements PLearn::RowBufferedVMatrix.

Definition at line 259 of file AsciiVMatrix.cc.

References file, j, PLearn::VMatrix::length(), PLearn::TVec< T >::length(), PLERROR, and PLearn::VMatrix::width().

{
#ifdef BOUNDCHECK
    if(i<0 || i>length())
        PLERROR("In AsciiVMatrix::getNewRow, bad row number %d",i);
    if(v.length() != width())
        PLERROR("In AsciiVMatrix::getNewRow, length of v (%d) does not match matrix width (%d)",v.length(),width());
#endif

    // file.seekg(pos_rows[i]); // TODO How to do this with PStreams ?
    for (int j=0; j<width(); j++)
        file >> v[j];
}

Here is the call graph for this function:

OptionList & PLearn::AsciiVMatrix::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 53 of file AsciiVMatrix.cc.

OptionMap & PLearn::AsciiVMatrix::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 53 of file AsciiVMatrix.cc.

RemoteMethodMap & PLearn::AsciiVMatrix::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 53 of file AsciiVMatrix.cc.

void PLearn::AsciiVMatrix::put ( int  i,
int  j,
real  value 
) [virtual]

These methods send an error message since, by choice, we accept only to append row at the end of the file.

Reimplemented from PLearn::VMatrix.

Definition at line 302 of file AsciiVMatrix.cc.

References PLERROR.

{ PLERROR("In AsciiVMatrix::put not permitted."); }
void PLearn::AsciiVMatrix::putRow ( int  i,
Vec  v 
) [virtual]

Reimplemented from PLearn::VMatrix.

Definition at line 306 of file AsciiVMatrix.cc.

References PLERROR.

{ PLERROR("In AsciiVMatrix::putRow not permitted."); }
void PLearn::AsciiVMatrix::putSubRow ( int  i,
int  j,
Vec  v 
) [virtual]

It is suggested that this method be implemented in subclasses of writable matrices to speed up accesses (default version repeatedly calls put(i,j,value) which may have a significant overhead)

Reimplemented from PLearn::VMatrix.

Definition at line 304 of file AsciiVMatrix.cc.

References PLERROR.

{ PLERROR("In AsciiVMatrix::putSubRow not permitted."); }

Member Data Documentation

Reimplemented from PLearn::RowBufferedVMatrix.

Definition at line 110 of file AsciiVMatrix.h.

PStream PLearn::AsciiVMatrix::file [mutable, protected]

Definition at line 55 of file AsciiVMatrix.h.

Referenced by appendRow(), AsciiVMatrix(), build_(), and getNewRow().

string PLearn::AsciiVMatrix::filename [protected]

Definition at line 54 of file AsciiVMatrix.h.

Referenced by AsciiVMatrix(), build_(), and declareOptions().

Definition at line 61 of file AsciiVMatrix.h.

Referenced by appendRow(), AsciiVMatrix(), and build_().

Definition at line 59 of file AsciiVMatrix.h.

Referenced by build_().

vector<streampos> PLearn::AsciiVMatrix::pos_rows [protected]

Definition at line 56 of file AsciiVMatrix.h.

Referenced by build_().

Definition at line 58 of file AsciiVMatrix.h.

Referenced by appendRow(), and declareOptions().

Definition at line 60 of file AsciiVMatrix.h.

Referenced by appendRow(), and build_().

streampos PLearn::AsciiVMatrix::vmatlength_pos [protected]

Definition at line 57 of file AsciiVMatrix.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines