PLearn 0.1
Public Types | Public Member Functions | Protected Attributes
PLearn::MRUFileList Class Reference

#include <MRUFileList.h>

Collaboration diagram for PLearn::MRUFileList:
Collaboration graph
[legend]

List of all members.

Public Types

typedef list< pair< string,
ofstream * > > 
list_typ

Public Member Functions

 MRUFileList (int _max_opened_files=1000, ios_base::openmode _mode=ios_base::out|ios_base::app)
 ~MRUFileList ()
ofstream * getFile (string fname)
string errorMsg ()
void clearError ()
float misses ()

Protected Attributes

int max_opened_files
string error
list_typ mru_list
int tot_miss
int tot_access
ios_base::openmode mode

Detailed Description

Definition at line 59 of file MRUFileList.h.


Member Typedef Documentation

typedef list<pair<string,ofstream*> > PLearn::MRUFileList::list_typ

Definition at line 63 of file MRUFileList.h.


Constructor & Destructor Documentation

PLearn::MRUFileList::MRUFileList ( int  _max_opened_files = 1000,
ios_base::openmode  _mode = ios_base::out | ios_base::app 
)

Definition at line 42 of file MRUFileList.cc.

    :max_opened_files(_max_opened_files),tot_miss(0),tot_access(0),mode(_mode)
{}
PLearn::MRUFileList::~MRUFileList ( )

Definition at line 90 of file MRUFileList.cc.

References mru_list.

{
    for(list_typ::iterator it = mru_list.begin(); it != mru_list.end(); ++it)
    {
        (it->second)->close();
        delete it->second;
    }
}

Member Function Documentation

void PLearn::MRUFileList::clearError ( ) [inline]

Definition at line 71 of file MRUFileList.h.

{error="";}
string PLearn::MRUFileList::errorMsg ( ) [inline]

Definition at line 70 of file MRUFileList.h.

{return error;}
ofstream * PLearn::MRUFileList::getFile ( string  fname)

Definition at line 46 of file MRUFileList.cc.

References error, max_opened_files, mode, mru_list, tot_access, and tot_miss.

{
    tot_access++;
    list_typ::iterator it = mru_list.end();
    ofstream * ofile;
  
    // search for the wanted file in the MRU list
    for(list_typ::iterator it2 = mru_list.begin(); it2 != mru_list.end(); ++it2)
        if(it2->first == fname)
        {
            it=it2;
            break;
        }

    // the file is not currently opened. 
    if(it == mru_list.end())
    {
        tot_miss++;
        // If we have already the max of opened files, we close the least recently used
        if((signed)mru_list.size() >= max_opened_files)
        {
            (mru_list.back().second)->close();
            delete (mru_list.back().second);
            mru_list.pop_back();
        }
        ofile = new ofstream(fname.c_str(),mode);
        if(ofile->bad())
        {
            error="Could not open/create file "+fname+".";
            return NULL;
        }
        mru_list.push_front(make_pair(fname,ofile));
        return ofile;
    }
  
    // else, we move the file to the top of the list and return the associated ofstream ptr
  
    ofile=it->second;
    mru_list.erase(it);
    mru_list.push_front(make_pair(fname,ofile));
    return ofile;
}
float PLearn::MRUFileList::misses ( ) [inline]

Definition at line 74 of file MRUFileList.h.

{return 100.0f * (float)tot_miss/(float)tot_access;}

Member Data Documentation

string PLearn::MRUFileList::error [protected]

Definition at line 78 of file MRUFileList.h.

Referenced by getFile().

Definition at line 77 of file MRUFileList.h.

Referenced by getFile().

ios_base::openmode PLearn::MRUFileList::mode [protected]

Definition at line 81 of file MRUFileList.h.

Referenced by getFile().

Definition at line 79 of file MRUFileList.h.

Referenced by getFile(), and ~MRUFileList().

Definition at line 80 of file MRUFileList.h.

Referenced by getFile().

Definition at line 80 of file MRUFileList.h.

Referenced by getFile().


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