FileFilter.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 #ifndef GNSSTK_FILEFILTER_HPP
45 #define GNSSTK_FILEFILTER_HPP
46 
47 #include <functional>
48 #include <algorithm>
49 #include <iterator>
50 
51 #include "FFData.hpp"
52 #include "FileSpec.hpp"
53 
54 namespace gnsstk
55 {
61 
63 
75  template<class FileData>
76  class FileFilter
77  {
78  public:
80  FileFilter(void);
81 
83  virtual ~FileFilter();
84 
86  FileFilter& addData(const FileData& ffd);
87 
89  FileFilter& addData(const std::list<FileData>& datavec);
90 
93  template <class Compare>
94  FileFilter& sort(Compare comp)
95  {
96  // FIX: this someday...
97  // this is a total hack until Solaris gets their act together and
98  // gets list::sort() working again
99  // all the code below can be replaced (someday) by this one line:
100  // dataVec.sort(comp);
101 
102  // make a vector of pointer to list iterator objects...
103  std::vector<lItrType> data(dataVec.size());
104  lItrType itr = dataVec.begin();
105  typename std::vector<lItrType>::size_type i = 0;
106  while (itr != dataVec.end())
107  {
108  data[i] = itr;
109  i++;
110  itr++;
111  }
112 
113  // use SortAdapter to use comp with the pointer vector
114  // then sort the vector
115  SortAdapter<Compare> sa(comp);
116  std::stable_sort(data.begin(), data.end(), sa);
117 
118  // make a new list of the data in the right order, then copy that
119  // over dataVec.
120  lType fdlist;
121  for (i = 0; i < data.size(); i++)
122  {
123  fdlist.push_back(*data[i]);
124  }
125  dataVec = fdlist;
126 
127  /*
128  // move the items into the correct order with splice.
129  // splice does nothing if (itr == data[i]) || (itr == ++data[i])
130  // so the data is inserted backwards to avoid this...
131  i = data.size();
132  while (i != 0)
133  {
134  itr = dataVec.begin();
135  --i;
136  dataVec.splice(itr, dataVec, data[i]);
137  }
138  */
139 
140  return *this;
141  }
142 
143 
145  FileFilter& merge(const FileFilter& right);
146 
151  template <class Compare>
152  FileFilter& merge(const FileFilter& right, Compare bp)
153  { merge(right); sort(bp); return *this; }
154 
159  template <class BinaryPredicate>
160  FileFilter& unique(BinaryPredicate bp)
161  {
162  // FIX: unique is broken or doesnt like my syntax
163  // so i wrote my own version of it.
164  // list<FileData>::iterator itr =
165  // unique(dataVec.begin(), dataVec.end(), bp);
166  filtered = 0;
167 
168  typename std::list<FileData>::iterator first = dataVec.begin();
169  typename std::list<FileData>::iterator second= dataVec.begin();
170 
171  if (second != dataVec.end() )
172  second++;
173  else
174  return *this; // empty list
175 
176  // keep only the first of many unique values
177  while (second != dataVec.end())
178  {
179  if ( bp(*first, *second))
180  {
181  second = dataVec.erase(second);
182  filtered++;
183  }
184  else
185  {
186  first++;
187  second++;
188  }
189  }
190 
191  return *this;
192  }
193 
198  template <class Predicate>
199  FileFilter& filter(Predicate up)
200  {
201  // delete all values for which up() is true
202  filtered = 0;
203 
204  typename std::list<FileData>::iterator itr = dataVec.begin();
205 
206  while (itr != dataVec.end())
207  {
208  if (up(*itr))
209  {
210  itr = dataVec.erase(itr);
211  filtered++;
212  }
213  else
214  itr++;
215  }
216 
217  return *this;
218  }
219 
224  template <class Operation>
225  FileFilter& touch(Operation& op)
226  {
227  filtered = 0;
228 
229  typename std::list<FileData>::iterator itr = dataVec.begin();
230 
231  while (itr != dataVec.end())
232  {
233  if (op(*itr))
234  filtered++;
235  itr++;
236  }
237 
238  return *this;
239  }
240 
242  template <class Operation>
243  FileFilter& touch(const Operation& op)
244  {
245  Operation o(op);
246  return touch(o);
247  }
248 
255  template <class BinaryPredicate>
256  std::pair< std::list<FileData>, std::list<FileData> >
257  diff(const FileFilter<FileData>& r, BinaryPredicate p) const
258  {
259  std::pair< std::list<FileData>, std::list<FileData> > toReturn;
260 
261  std::set_difference(dataVec.begin(), dataVec.end(),
262  r.dataVec.begin(), r.dataVec.end(),
263  std::inserter(toReturn.first,
264  toReturn.first.begin()),
265  p);
266 
267  std::set_difference(r.dataVec.begin(), r.dataVec.end(),
268  dataVec.begin(), dataVec.end(),
269  std::inserter(toReturn.second,
270  toReturn.second.begin()),
271  p);
272 
273  return toReturn;
274  }
275 
277  template <class Predicate>
278  std::list<FileData> findAll(Predicate p) const
279  {
280  std::list<FileData> toReturn;
281  typename std::list<FileData>::const_iterator itr = dataVec.begin();
282 
283  while (itr != dataVec.end())
284  {
285  if (p(*itr))
286  toReturn.push_back((*itr));
287  itr++;
288  }
289 
290  return toReturn;
291  }
292 
295  int getFiltered() const
296  { return filtered; }
297 
299  std::list<FileData>& getData(void)
300  { return dataVec; }
301 
303  std::list<FileData> getData(void) const
304  { return dataVec; }
305 
307  typename std::list<FileData>::size_type getDataCount(void) const
308  { return dataVec.size(); }
309 
310  typename std::list<FileData>::const_iterator begin() const
311  { return dataVec.begin(); }
312 
313  typename std::list<FileData>::const_iterator end() const
314  { return dataVec.end(); }
315 
316  typename std::list<FileData>::iterator begin()
317  { return dataVec.begin(); }
318 
319  typename std::list<FileData>::iterator end()
320  { return dataVec.end(); }
321 
322  bool empty() const
323  { return dataVec.empty(); }
324 
325  void clear()
326  { dataVec.clear(); }
327 
328  typename std::list<FileData>::size_type size()
329  { return dataVec.size(); }
330 
331  FileData& front()
332  {
333  GNSSTK_ASSERT(!empty());
334  return dataVec.front();
335  }
336 
337  const FileData& front() const
338  {
339  GNSSTK_ASSERT(!empty());
340  return dataVec.front();
341  }
342 
343  FileData& back()
344  {
345  GNSSTK_ASSERT(!empty());
346  return dataVec.back();
347  }
348 
349  const FileData& back() const
350  {
351  GNSSTK_ASSERT(!empty());
352  return dataVec.back();
353  }
354 
355  protected:
357  typedef std::list<FileData> lType;
359  typedef typename std::list<FileData>::iterator lItrType;
360 
365  template<class Compare>
366  class SortAdapter :
367  public std::binary_function<lItrType, lItrType, bool>
368  {
369  public:
370  SortAdapter(Compare& c)
371  : comp(c)
372  {}
373 
374  bool operator()(const lItrType l,
375  const lItrType r) const
376  {
377  return comp(*l, *r);
378  }
379  private:
380  Compare comp;
381  };
382 
384  int filtered;
385  };
386 
388 
389  template<class FileData>
391  : filtered(0)
392  {}
393 
394  template<class FileData>
396  {
397  }
398 
399  template<class FileData>
401  addData(const FileData& ffd)
402  {
403  dataVec.push_back(ffd);
404  return *this;
405  }
406 
407  template <class FileData>
409  addData(const std::list<FileData>& datavec)
410  {
411  std::copy(datavec.begin(), datavec.end(),
412  std::inserter(dataVec, dataVec.begin()));
413  return *this;
414  }
415 
416  template <class FileData>
420  {
421  // cast out const to use the non-const version of getData()
423 
424  // copy rightData into *this
425  std::list<FileData>& rightData = r.getData();
426  std::copy(rightData.begin(), rightData.end(),
427  std::inserter(dataVec, dataVec.begin()));
428 
429  return *this;
430  }
431 
432 } // namespace gnsstk
433 
434 #endif // GNSSTK_FILEFILTER_HPP
gnsstk::FileFilter::front
FileData & front()
Definition: FileFilter.hpp:331
gnsstk::FileFilter::SortAdapter::operator()
bool operator()(const lItrType l, const lItrType r) const
Definition: FileFilter.hpp:374
gnsstk::FileFilter
Definition: FileFilter.hpp:76
gnsstk::FileFilter::getFiltered
int getFiltered() const
Definition: FileFilter.hpp:295
gnsstk::FileFilter::SortAdapter::SortAdapter
SortAdapter(Compare &c)
Definition: FileFilter.hpp:370
gnsstk::FileFilter::FileFilter
FileFilter(void)
Default constructor.
Definition: FileFilter.hpp:390
gnsstk::FileFilter::~FileFilter
virtual ~FileFilter()
Destructor.
Definition: FileFilter.hpp:395
gnsstk::FileFilter::touch
FileFilter & touch(const Operation &op)
a const operator touch for the classes that need it.
Definition: FileFilter.hpp:243
gnsstk::FileFilter::empty
bool empty() const
Definition: FileFilter.hpp:322
gnsstk::FileFilter::end
std::list< FileData >::iterator end()
Definition: FileFilter.hpp:319
gnsstk::FileFilter::findAll
std::list< FileData > findAll(Predicate p) const
Returns a list of data matching the given unary predicate.
Definition: FileFilter.hpp:278
gnsstk::FileFilter::SortAdapter
Definition: FileFilter.hpp:366
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::FileFilter::filtered
int filtered
A count of the last number of items filtered.
Definition: FileFilter.hpp:384
gnsstk::FileFilter::addData
FileFilter & addData(const FileData &ffd)
Adds the given data into the filter.
Definition: FileFilter.hpp:401
gnsstk::FileFilter::touch
FileFilter & touch(Operation &op)
Definition: FileFilter.hpp:225
example6.second
second
Definition: example6.py:69
gnsstk::FileFilter::begin
std::list< FileData >::const_iterator begin() const
Definition: FileFilter.hpp:310
FileSpec.hpp
gnsstk::FileFilter::back
FileData & back()
Definition: FileFilter.hpp:343
gnsstk::FileFilter::back
const FileData & back() const
Definition: FileFilter.hpp:349
gnsstk::FileFilter::unique
FileFilter & unique(BinaryPredicate bp)
Definition: FileFilter.hpp:160
gnsstk::FileFilter::clear
void clear()
Definition: FileFilter.hpp:325
gnsstk::FileFilter::merge
FileFilter & merge(const FileFilter &right)
Combines the data from the input filter to this object.
Definition: FileFilter.hpp:419
gnsstk::FileFilter::getDataCount
std::list< FileData >::size_type getDataCount(void) const
Returns the number of data items in the filter.
Definition: FileFilter.hpp:307
gnsstk::FileFilter::sort
FileFilter & sort(Compare comp)
Definition: FileFilter.hpp:94
gnsstk::FileFilter::lItrType
std::list< FileData >::iterator lItrType
Definition: FileFilter.hpp:359
gnsstk::FileFilter::size
std::list< FileData >::size_type size()
Definition: FileFilter.hpp:328
gnsstk::FileFilter::merge
FileFilter & merge(const FileFilter &right, Compare bp)
Definition: FileFilter.hpp:152
example3.data
data
Definition: example3.py:22
gnsstk::FileFilter::end
std::list< FileData >::const_iterator end() const
Definition: FileFilter.hpp:313
GNSSTK_ASSERT
#define GNSSTK_ASSERT(CONDITION)
Provide an "ASSERT" type macro.
Definition: Exception.hpp:373
gnsstk::FileFilter::begin
std::list< FileData >::iterator begin()
Definition: FileFilter.hpp:316
gnsstk::FileFilter::SortAdapter::comp
Compare comp
Definition: FileFilter.hpp:380
gnsstk::FileFilter::front
const FileData & front() const
Definition: FileFilter.hpp:337
gnsstk::FileFilter::lType
std::list< FileData > lType
List of file data to be filtered.
Definition: FileFilter.hpp:357
FFData.hpp
gnsstk::FileFilter::filter
FileFilter & filter(Predicate up)
Definition: FileFilter.hpp:199
gnsstk::FileFilter::getData
std::list< FileData > getData(void) const
Returns the contents of the data list, const.
Definition: FileFilter.hpp:303
gnsstk::FileFilter::getData
std::list< FileData > & getData(void)
Returns the contents of the data list.
Definition: FileFilter.hpp:299
gnsstk::FileFilter::diff
std::pair< std::list< FileData >, std::list< FileData > > diff(const FileFilter< FileData > &r, BinaryPredicate p) const
Definition: FileFilter.hpp:257
gnsstk::FileFilter::dataVec
lType dataVec
Definition: FileFilter.hpp:358


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:39