Bridge++  Version 1.5.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sorter.h
Go to the documentation of this file.
1 
14 #ifndef SORTER_INCLUDED
15 #define SORTER_INCLUDED
16 
17 #include <cmath>
18 #include <algorithm>
19 #include <cassert>
20 #include <vector>
21 #include <string>
22 
24 // sort utility class.
25 //
26 // usage:
27 // Sorter sorter(sort_type);
28 // sort_type =
29 // abs_ascending : absolute ascending order |v_0| <= |v_1| <= ...
30 // abs_descending : absolute descending order |v_0| >= |v_1| >= ...
31 // ascending : arithmetic ascending order v_0 <= v_1 <= ...
32 // descending : arithmetic descending order v_0 >= v_1 >= ...
33 // sorter.sort(vec); to sort vec : vector<double>. destructive.
34 // sorter.sort_index(vec); returns index of vec in sorted order.
35 // sorter.comp(a, b); compare two values a, b with the sort condition.
36 //
37 
38 class Sorter
39 {
40  public:
41 
42  Sorter(const std::string& type);
43 
44  virtual ~Sorter();
45 
47  void sort(std::vector<double>& v);
48 
50  void sort(std::vector<double>& v, const size_t nelem);
51 
53  std::vector<int> sort_index(std::vector<double>& v);
54 
56  std::vector<int> sort_index(std::vector<double>& v, const size_t nelem);
57 
59  bool comp(const double lhs, const double rhs);
60 
61  private:
62 
63  // sort order
64  struct by_abs_ascend;
65  struct by_abs_descend;
66  struct by_ascend;
67  struct by_descend;
68 
69  struct by_order;
70  struct proxy;
71 
72  typedef std::pair<int, double> pair_t;
73 
75 
76  // non-copyable
77  Sorter(const Sorter&);
78  Sorter& operator=(const Sorter&);
79 };
80 #endif /* SORTER_INCLUDED */
Definition: sorter.h:38
void sort(std::vector< double > &v)
sort an array of values; v is sorted on exit.
Definition: sorter.cpp:117
std::pair< int, double > pair_t
Definition: sorter.h:70
ascending order
Definition: sorter.cpp:54
absolute descending order
Definition: sorter.cpp:43
proxy object to pass to stl sort algorithm
Definition: sorter.cpp:77
std::vector< int > sort_index(std::vector< double > &v)
sort an array and return list of index; v is sorted on exit.
Definition: sorter.cpp:137
by_order * m_order
Definition: sorter.h:74
virtual ~Sorter()
destructor
Definition: sorter.cpp:109
Sorter(const std::string &type)
constructor with sort ordering as a string arg
Definition: sorter.cpp:98
absolute ascending order
Definition: sorter.cpp:34
Sorter & operator=(const Sorter &)
base class for sort ordering
Definition: sorter.cpp:18
descending order
Definition: sorter.cpp:66
bool comp(const double lhs, const double rhs)
call sort condition.
Definition: sorter.cpp:189