AnotherFileFilterTest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: AnotherFileFilterTest.cpp 1895 2009-05-12 19:34:29Z afarris $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 //  Copyright 2009, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 #include "FileFilter.hpp"
00026 #include "FFData.hpp"
00027 #include "FFStream.hpp"
00028 
00034 using namespace gpstk;
00035 using namespace std;
00036 
00037 // an FFData with just an int
00038 class TestFFData : public FFData
00039 {
00040 public:
00041    TestFFData(int i = 0) : val(i) {}
00042 
00043    virtual ~TestFFData() {}
00044 
00045    void reallyPutRecord(FFStream& s) const 
00046       throw(FFStreamError, gpstk::StringUtils::StringException)
00047       {}
00048 
00049 
00050    void reallyGetRecord(FFStream& s) 
00051       throw(FFStreamError, gpstk::StringUtils::StringException)
00052       {}
00053 
00054 
00055    virtual void dump(std::ostream& s) const {s << val;}
00056 
00057    int val;
00058 };
00059 
00060 
00061 // an operator < for TestFFData
00062 struct TestOperatorLessThan : 
00063    public binary_function<TestFFData, TestFFData, bool>
00064 {
00065 public:
00066    bool operator() (TestFFData l, TestFFData r) const
00067       {
00068          return (l.val < r.val);
00069       }
00070 };
00071 
00072 // an operator == for TestFFData
00073 struct TestOperatorEquals : 
00074    binary_function<TestFFData, TestFFData, bool>
00075 {
00076 public:
00077    bool operator() (TestFFData l, TestFFData r) const
00078       {
00079          return (l.val == r.val);
00080       }
00081 };
00082 
00083 // a filter for a range of values for TestFFData
00084 struct TestRangeFilter : 
00085    public unary_function<TestFFData, bool>
00086 {
00087 public:
00088    TestRangeFilter(const int b, const int e)
00089          : begin(b), end(e)
00090       {}
00091    
00092    bool operator() (TestFFData l) const
00093       {
00094          if ( (l.val < begin) ||
00095               (l.val > end) )
00096             return true;
00097          return false;
00098       }
00099    
00100 private:
00101    int begin, end;
00102    
00103 };
00104 
00105 
00106 // a removing filter for a single value
00107 struct TestValueFilter : 
00108    public unary_function<TestFFData, bool>
00109 {
00110 public:
00111    TestValueFilter(const int val)
00112          : value(val)
00113       {}
00114 
00115    bool operator() (TestFFData l) const
00116       {
00117          if (value == l.val)
00118             return true;
00119          return false;
00120       }
00121 
00122 private:
00123    int value;
00124 };
00125 
00126 
00127 
00128 main (int argc, char *argv[])
00129 {
00130    FileFilter<TestFFData> ff;
00131 
00132       // add data to the filter
00133    ff.addData(TestFFData(1));
00134    ff.addData(TestFFData(2));
00135    ff.addData(TestFFData(2));
00136    ff.addData(TestFFData(2));
00137    ff.addData(TestFFData(4));
00138    ff.addData(TestFFData(4));
00139    ff.addData(TestFFData(5));
00140    ff.addData(TestFFData(3));
00141    ff.addData(TestFFData(3));
00142    ff.addData(TestFFData(1));
00143 
00144       // do various operations on the data
00145 
00146    list<TestFFData>::iterator itr;
00147 
00148    cout << "unsorted" << endl;
00149    for(itr = ff.begin(); itr != ff.end(); itr++)
00150       (*itr).dump(cout << ' '); cout << endl;
00151 
00152    cout << "sorted" << endl;
00153    ff.sort(TestOperatorLessThan());
00154    for(itr = ff.begin(); itr != ff.end(); itr++)
00155       (*itr).dump(cout << ' '); cout << endl;
00156 
00157    cout << "filter out values > 3" << endl;
00158    ff.filter(TestRangeFilter(1,3));
00159    for(itr = ff.begin(); itr != ff.end(); itr++)
00160       (*itr).dump(cout << ' '); cout << endl;
00161 
00162    cout << "filter out 2" << endl;
00163    ff.filter(TestValueFilter(2));
00164    for(itr = ff.begin(); itr != ff.end(); itr++)
00165       (*itr).dump(cout << ' '); cout << endl;
00166 
00167    cout << "unique only" << endl;
00168    ff.unique(TestOperatorEquals());
00169    for(itr = ff.begin(); itr != ff.end(); itr++)
00170       (*itr).dump(cout << ' '); cout << endl;
00171 }

Generated on Thu Feb 9 03:30:54 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1