FileFilter.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: FileFilter.hpp 2535 2011-03-25 15:58:06Z ccutlip $"
00002 
00003 
00004 
00010 #ifndef GPSTK_FILEFILTER_HPP
00011 #define GPSTK_FILEFILTER_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 //============================================================================
00036 //
00037 //This software developed by Applied Research Laboratories at the University of
00038 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00039 //Department of Defense. The U.S. Government retains all rights to use,
00040 //duplicate, distribute, disclose, or release this software. 
00041 //
00042 //Pursuant to DoD Directive 523024 
00043 //
00044 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00045 //                           release, distribution is unlimited.
00046 //
00047 //=============================================================================
00048 
00049 
00050 
00051 
00052 
00053 
00054 #include <functional>
00055 #include <algorithm>
00056 #include <iterator>
00057 
00058 #include "FFData.hpp"
00059 #include "FileSpec.hpp"
00060 
00061 namespace gpstk
00062 {
00065 
00078    template<class FileData>
00079    class FileFilter
00080    {
00081    public:
00083       FileFilter(void);
00084 
00086       virtual ~FileFilter();
00087 
00089       FileFilter& addData(const FileData& ffd);
00090 
00092       FileFilter& addData(const std::list<FileData>& datavec);
00093 
00096       template <class Compare>
00097       FileFilter& sort(Compare comp)
00098      {
00099          // FIX: this someday...
00100          // this is a total hack until Solaris gets their act together and
00101          // gets list::sort() working again
00102          // all the code below can be replaced (someday) by this one line:
00103          //      dataVec.sort(comp);
00104 
00105          // make a vector of pointer to list iterator objects...
00106       std::vector<lItrType> data(dataVec.size());
00107       lItrType itr = dataVec.begin();
00108       typename std::vector<lItrType>::size_type i = 0;
00109       while (itr != dataVec.end())
00110       {
00111          data[i] = itr;
00112          i++;
00113          itr++;
00114       }
00115       
00116          // use SortAdapter to use comp with the pointer vector
00117          // then sort the vector
00118       SortAdapter<Compare> sa(comp);
00119       std::stable_sort(data.begin(), data.end(), sa);
00120       
00121          // make a new list of the data in the right order, then copy that
00122          // over dataVec.
00123       lType fdlist;
00124       for (i = 0; i < data.size(); i++)
00125       {
00126          fdlist.push_back(*data[i]);
00127       }
00128       dataVec = fdlist;
00129       
00130 /*
00131          // move the items into the correct order with splice.
00132          // splice does nothing if (itr == data[i]) || (itr == ++data[i])
00133          // so the data is inserted backwards to avoid this...
00134       i = data.size();
00135       while (i != 0)
00136       {
00137          itr = dataVec.begin();
00138          --i;
00139          dataVec.splice(itr, dataVec, data[i]);
00140       }
00141 */            
00142       return *this;
00143    }
00144 
00145 
00147       FileFilter& merge(const FileFilter& right);
00148 
00153       template <class Compare>
00154       FileFilter& merge(const FileFilter& right, Compare bp)
00155          { merge(right); sort(bp); return *this; }
00156 
00159       template <class BinaryPredicate>
00160       FileFilter& unique(BinaryPredicate bp)
00161    {
00162          //  FIX: unique is broken or doesnt like my syntax
00163          //  so i wrote my own version of it
00164 //      list<FileData>::iterator itr = 
00165 //         unique(dataVec.begin(), dataVec.end(), bp);
00166       filtered = 0;
00167       
00168       typename std::list<FileData>::iterator first = dataVec.begin();
00169       typename std::list<FileData>::iterator second= dataVec.begin();
00170       second++;
00171       
00172          // keep only the first of many unique values
00173       while (second != dataVec.end())
00174       {
00175          if ( bp(*first, *second))
00176          {
00177             second = dataVec.erase(second);
00178             filtered++;
00179          }
00180          else
00181          {
00182             first++;
00183             second++;
00184          }
00185       }
00186       
00187       return *this;
00188    }
00189 
00193       template <class Predicate>
00194       FileFilter& filter(Predicate up)
00195    {
00196          // delete all values for which up() is true
00197       filtered = 0;
00198       
00199       typename std::list<FileData>::iterator itr = dataVec.begin();
00200       
00201       while (itr != dataVec.end())
00202       {
00203          if (up(*itr))
00204          {
00205             itr = dataVec.erase(itr);
00206             filtered++;
00207          }
00208          else
00209             itr++;
00210       }
00211       
00212       return *this;
00213    }
00214 
00219       template <class Operation>
00220       FileFilter& touch(Operation& op)
00221    {
00222       filtered = 0;
00223       
00224       typename std::list<FileData>::iterator itr = dataVec.begin();
00225       
00226       while (itr != dataVec.end())
00227       {
00228          if (op(*itr))
00229             filtered++;
00230          itr++;
00231       }
00232       
00233       return *this;
00234    }
00235 
00237       template <class Operation>
00238       FileFilter& touch(const Operation& op)
00239          { Operation o(op); return touch(o); }
00240 
00247       template <class BinaryPredicate>
00248       std::pair< std::list<FileData>, std::list<FileData> > 
00249       diff(const FileFilter<FileData>& r, BinaryPredicate p) const
00250    {
00251       std::pair< std::list<FileData>, std::list<FileData> > toReturn;
00252       
00253       std::set_difference(dataVec.begin(), dataVec.end(),
00254                           r.dataVec.begin(), r.dataVec.end(),
00255                           std::inserter(toReturn.first, 
00256                                         toReturn.first.begin()),
00257                           p);
00258       
00259       std::set_difference(r.dataVec.begin(), r.dataVec.end(),
00260                           dataVec.begin(), dataVec.end(),
00261                           std::inserter(toReturn.second, 
00262                                         toReturn.second.begin()),
00263                           p);
00264       
00265       return toReturn;
00266    }
00267 
00269       template <class Predicate>
00270       std::list<FileData> findAll(Predicate p) const
00271    {
00272       std::list<FileData> toReturn;
00273       typename std::list<FileData>::const_iterator itr = dataVec.begin();
00274       
00275       while (itr != dataVec.end())
00276       {
00277          if (p(*itr))
00278             toReturn.push_back((*itr));
00279          itr++;
00280       }
00281       
00282       return toReturn;
00283       
00284    }
00285 
00288       int getFiltered() const {return filtered;}
00289 
00291       std::list<FileData>& getData(void) {return dataVec;}
00292 
00294       std::list<FileData> getData(void) const {return dataVec;}
00295 
00297       typename std::list<FileData>::size_type getDataCount(void) const 
00298          { return dataVec.size(); }
00299 
00300       typename std::list<FileData>::const_iterator begin() const
00301          { return dataVec.begin(); }
00302 
00303       typename std::list<FileData>::const_iterator end() const
00304          { return dataVec.end(); }
00305 
00306       typename std::list<FileData>::iterator begin() 
00307          { return dataVec.begin(); }
00308 
00309       typename std::list<FileData>::iterator end() 
00310          { return dataVec.end(); }
00311 
00312       bool empty() const
00313          { return dataVec.empty(); }
00314 
00315       void clear()
00316          { dataVec.clear(); }
00317 
00318       typename std::list<FileData>::size_type size()
00319          { return dataVec.size(); }
00320 
00321       FileData& front()
00322          { return dataVec.front(); }
00323 
00324       const FileData& front() const
00325          { return dataVec.front(); }
00326 
00327       FileData& back()
00328          { return dataVec.back(); }
00329 
00330       const FileData& back() const
00331          { return dataVec.back(); }
00332 
00333    protected:
00335       typedef std::list<FileData> lType;
00336       lType dataVec;
00337       typedef typename std::list<FileData>::iterator lItrType;
00338 
00343       template<class Compare>
00344       class SortAdapter  : 
00345          public std::binary_function<lItrType, lItrType, bool>
00346       {
00347       public:
00348          SortAdapter(Compare& c)
00349                : comp(c)
00350             {}
00351 
00352          bool operator()(const lItrType l,
00353                          const lItrType r) const
00354             {
00355                return comp(*l, *r);
00356             }
00357       private:
00358          Compare comp;
00359       };
00360 
00362       int filtered;
00363    };
00364 
00366 
00367       template<class FileData>
00368    FileFilter<FileData> :: FileFilter(void)
00369          : filtered(0)
00370    {}
00371 
00372    template<class FileData>
00373    FileFilter<FileData> :: ~FileFilter()
00374    {
00375    }
00376 
00377    template<class FileData>
00378    FileFilter<FileData>& FileFilter<FileData> :: 
00379    addData(const FileData& ffd)
00380    {
00381       dataVec.push_back(ffd);
00382       return *this;
00383    }
00384 
00385    template <class FileData>
00386    FileFilter<FileData>& FileFilter<FileData> :: 
00387    addData(const std::list<FileData>& datavec)
00388    {
00389       std::copy(datavec.begin(), datavec.end(),
00390                 std::inserter(dataVec, dataVec.begin()));
00391       return *this;
00392    }
00393    
00394    template <class FileData>
00395    FileFilter<FileData>&
00396    FileFilter<FileData> ::
00397    merge(const FileFilter<FileData>& right)
00398    {
00399          // cast out const to use the non-const version of getData()
00400       FileFilter<FileData>& r = (FileFilter<FileData>&)(right);
00401       
00402          // copy rightData into *this
00403       std::list<FileData>& rightData = r.getData();
00404       std::copy(rightData.begin(), rightData.end(),
00405                 std::inserter(dataVec, dataVec.begin()));
00406       
00407       return *this;
00408    }
00409 
00410 } // namespace gpstk
00411 
00412 #endif // GPSTK_FILEFILTER_HPP

Generated on Wed Feb 8 03:30:59 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1