Anp::RunFinder Class Reference

#include <RunFinder.h>

List of all members.

Public Member Functions

 RunFinder ()
 ~RunFinder ()
int AddDir (const std::string path, const std::string key="")
int Add (const std::string path, const std::string ifile, const std::string key="")
int AddIndex (const std::string &ifile, const std::string &key="")
void AddKey (const std::string &key)
int GetNFiles () const
const std::string GetFile (int index) const
const std::string GetPath (int index) const
double GetFileSize (int index=-1) const
void Compress ()

Private Member Functions

bool FindMatch (const std::string &filename) const
const Finder::File GetFile (std::string path, const std::string &entry) const

Private Attributes

std::vector< std::string > fKeys
std::vector< Anp::Finder::FilefFiles

Detailed Description

Definition at line 48 of file RunFinder.h.


Constructor & Destructor Documentation

Anp::RunFinder::RunFinder (  ) 

Definition at line 81 of file RunFinder.cxx.

00082 {
00083 }

Anp::RunFinder::~RunFinder (  ) 

Definition at line 86 of file RunFinder.cxx.

00087 {
00088 }


Member Function Documentation

int Anp::RunFinder::Add ( const std::string  path,
const std::string  ifile,
const std::string  key = "" 
)
int Anp::RunFinder::AddDir ( const std::string  path,
const std::string  key = "" 
)
int Anp::RunFinder::AddIndex ( const std::string &  ifile,
const std::string &  key = "" 
)

Definition at line 238 of file RunFinder.cxx.

References fFiles, find(), FindMatch(), Anp::Finder::File::name, and Anp::Finder::read_file().

00239 {  
00240    const std::vector<std::string> ivec = Anp::Finder::read_file(ifile);
00241    if(ivec.empty())
00242    {
00243       std::cerr << "RunFinder::AddIndex() - failed to read index file" << std::endl;
00244       return 0;
00245    }
00246 
00247    unsigned int npass = 0, nmiss = 0;
00248    for(std::vector<std::string>::const_iterator sit = ivec.begin(); sit != ivec.end(); ++sit)
00249    {               
00250       std::string filename = *sit;
00251 
00252       const Finder::File newfile("", filename);
00253 
00254       if(newfile.name.empty())
00255       {
00256          std::cerr << "RunFinder::Add() - invalid file: " << *sit << std::endl;
00257          continue;
00258       }
00259       
00260       if(!FindMatch(newfile.name))
00261       {
00262          continue;
00263       }
00264 
00265       if(!key.empty() && newfile.name.find(key, 0) == std::string::npos)
00266       {
00267          continue;
00268       }
00269 
00270       if(std::find(fFiles.begin(), fFiles.end(), newfile.name) == fFiles.end())
00271       {  
00272          fFiles.push_back(newfile);
00273          ++npass;
00274       }
00275       else
00276       {
00277          ++nmiss;
00278          std::cerr << "RunFinder::AddFileName(): duplicate file " << filename << std::endl;
00279       }
00280    }
00281    
00282    if(nmiss > 0)
00283    {
00284       std::cerr << "RunFinder: skipped " << nmiss << " non existing files" << std::endl;
00285    }
00286 
00287    std::sort(fFiles.begin(), fFiles.end());   
00288 
00289    return npass;
00290 }

void Anp::RunFinder::AddKey ( const std::string &  key  )  [inline]

Definition at line 87 of file RunFinder.h.

References fKeys.

00087 { fKeys.push_back(key); } 

void Anp::RunFinder::Compress (  ) 

Definition at line 293 of file RunFinder.cxx.

References count, fFiles, find(), fKeys, max, and min.

00294 {
00295    //
00296    // Check for duplicate file names, even if stored in different directories
00297    //
00298 
00299    vector<unsigned int> svec;
00300 
00301    for(std::vector<std::string>::const_iterator kit = fKeys.begin(); kit != fKeys.end(); ++kit)
00302    {
00303       unsigned int count = 0;
00304       for(std::vector<Anp::Finder::File>::const_iterator fit = fFiles.begin(); fit != fFiles.end(); ++fit)
00305       {
00306          if((fit -> name).find(*kit, 0) != std::string::npos)
00307          {
00308             ++count;
00309          }
00310       }
00311       
00312       if(std::find(svec.begin(), svec.end(), count) == svec.end())
00313       {
00314          svec.push_back(count);
00315       }
00316    }
00317 
00318    if(svec.empty())
00319    {
00320       std::cerr << "Anp::RunFinder::Compress() - no files match any keys" << std::endl;
00321       return;
00322    }
00323    else if(svec.size() == 1)
00324    {
00325       return;
00326    }
00327 
00328    std::sort(svec.begin(), svec.end());
00329 
00330    const unsigned int max = svec.back();
00331    const unsigned int min = svec.front();
00332 
00333    std::sort(svec.begin(), svec.end());
00334 
00335    std::cout << "Anp::RunFinder::Compress()" << std::endl
00336              << "   Minimum number of matches per key: " << min << std::endl
00337              << "   Maximum number of matches per key: " << max << std::endl
00338              << "   Will forces number of matches pre key to be " << max << std::endl;
00339    
00340    unsigned int nremoved = 0;
00341    for(std::vector<std::string>::const_iterator kit = fKeys.begin(); kit != fKeys.end(); ++kit)
00342    {
00343       unsigned int count = 0;
00344       for(std::vector<Anp::Finder::File>::const_iterator fit = fFiles.begin(); fit != fFiles.end(); ++fit)
00345       {
00346          if((fit -> name).find(*kit, 0) != std::string::npos)
00347          {
00348             ++count;
00349          }
00350       }
00351       
00352       if(count == max)
00353       {
00354          continue;
00355       }
00356 
00357       ++nremoved;
00358 
00359       std::vector<Anp::Finder::File>::iterator fit = fFiles.begin();
00360       while(fit != fFiles.end())
00361       {
00362          if((fit -> name).find(*kit, 0) != std::string::npos)
00363          {
00364             std::cout << "Removed " << fit -> name << std::endl;
00365             fit = fFiles.erase(fit);
00366          }
00367          else
00368          {
00369             ++fit;
00370          }
00371       }
00372    }
00373  
00374    std::sort(fFiles.begin(), fFiles.end());
00375   
00376    cout << "Anp::RunFinder::Compress() - removed " << nremoved << " keys" << endl;
00377 }

bool Anp::RunFinder::FindMatch ( const std::string &  filename  )  const [private]

Definition at line 380 of file RunFinder.cxx.

References fKeys, and it.

Referenced by AddIndex().

00381 {
00382    for(std::vector<std::string>::const_iterator it = fKeys.begin(); it != fKeys.end(); ++it)
00383    {
00384       if(filename.find(*it, 0) == std::string::npos)
00385       {
00386          return false;
00387       }
00388    }
00389 
00390    return true;
00391 }

const Anp::Finder::File Anp::RunFinder::GetFile ( std::string  path,
const std::string &  entry 
) const [private]

Definition at line 394 of file RunFinder.cxx.

00395 {
00396    if(path.empty() || entry.empty())
00397    {
00398       std::cerr << "RunFinder::GetFile() - empty path or filename" << std::endl;
00399       return Anp::Finder::File(path, entry);
00400    }
00401 
00402    if(path.find_last_of("/") == path.size() - 1)
00403    {
00404       path.erase(path.size() - 1, std::string::npos);
00405    }
00406 
00407    const std::string::size_type lpos = entry.find_last_of("/", std::string::npos);
00408    const std::string::size_type fpos = entry.find_first_of("/", 0);
00409 
00410    // entry does not contain "/"
00411    if(lpos == std::string::npos)
00412    {
00413       return Anp::Finder::File(path, entry);
00414    }
00415    
00416    // check that "/" is not first or last
00417    if(fpos == 0 || lpos == entry.size() - 1)
00418    {
00419       std::cerr << "RunFinder::GetFile() - file format error!" << std::endl;
00420       return Anp::Finder::File(path, entry);
00421    }   
00422 
00423    // must never happen unless there is error in this code
00424    if(fpos > lpos)
00425    {
00426       std::cerr << "RunFinder::GetFile() - logic error!" << std::endl;
00427       return Anp::Finder::File(path, entry);
00428    }
00429    
00430    path = path + "/" + entry.substr(0, lpos);
00431 
00432    return Anp::Finder::File(path, entry.substr(lpos + 1, std::string::npos));
00433 }

const std::string Anp::RunFinder::GetFile ( int  index  )  const

Definition at line 436 of file RunFinder.cxx.

References fFiles.

Referenced by Anp::ReadTree< T >::Find().

00437 {
00438    if(index >= int(fFiles.size()) || index < 0)
00439    {
00440       cerr << "RunFinder::GetFile - index " << index << " is out of range" << endl;
00441       return std::string();
00442    }
00443    
00444    return fFiles[index].name;
00445 }

double Anp::RunFinder::GetFileSize ( int  index = -1  )  const

Definition at line 460 of file RunFinder.cxx.

References fFiles, Anp::Finder::file_size(), GetPath(), it, and size.

Referenced by Anp::ReadTree< T >::Find().

00461 {
00462    if(index < 0)
00463    {
00464       double size = 0.0;
00465       for(std::vector<Anp::Finder::File>::const_iterator it = fFiles.begin(); it != fFiles.end(); ++it)
00466       {
00467          size += Anp::Finder::file_size(it -> fullpath());
00468       }
00469 
00470       return size;
00471    }
00472       
00473    return Anp::Finder::file_size(GetPath(index));
00474 }

int Anp::RunFinder::GetNFiles (  )  const [inline]

Definition at line 88 of file RunFinder.h.

References fFiles.

Referenced by Anp::ReadTree< T >::Find().

00088 { return fFiles.size(); }

const std::string Anp::RunFinder::GetPath ( int  index  )  const

Definition at line 448 of file RunFinder.cxx.

References fFiles.

Referenced by Anp::ReadTree< T >::Find(), and GetFileSize().

00449 {
00450    if(index >= int(fFiles.size()) || index < 0)
00451    {
00452       cerr << "RunFinder::GetPath - index " << index << " is out of range" << endl;
00453       return std::string();
00454    }
00455    
00456    return fFiles[index].fullpath();
00457 }


Member Data Documentation

std::vector<Anp::Finder::File> Anp::RunFinder::fFiles [private]

Definition at line 81 of file RunFinder.h.

Referenced by AddIndex(), Compress(), GetFile(), GetFileSize(), GetNFiles(), and GetPath().

std::vector<std::string> Anp::RunFinder::fKeys [private]

Definition at line 80 of file RunFinder.h.

Referenced by AddKey(), Compress(), and FindMatch().


The documentation for this class was generated from the following files:

Generated on 16 Apr 2018 for loon by  doxygen 1.6.1