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
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
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
00100
00101
00102
00103
00104
00105
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
00117
00118 SortAdapter<Compare> sa(comp);
00119 std::stable_sort(data.begin(), data.end(), sa);
00120
00121
00122
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
00132
00133
00134
00135
00136
00137
00138
00139
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
00163
00164
00165
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
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
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
00400 FileFilter<FileData>& r = (FileFilter<FileData>&)(right);
00401
00402
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 }
00411
00412 #endif // GPSTK_FILEFILTER_HPP