CUBA
utilities.hh
1 
8 #ifndef UTILS_UTILITIES_HH_
9 #define UTILS_UTILITIES_HH_
10 
11 #include <map>
12 
13 #include "heads.hh"
14 
15 namespace ruba {
16 
17 enum class alignment {
18  LEFTJUST, RIGHTJUST, CENTERED
19 };
20 
21 class algs {
22 public:
23  template<typename T>
24  static int compare(const vector<T>& v1, const vector<T>& v2);
25 
26  template<class T>
27  static string widthify(const T& x, const ushort& width = 0,
28  const alignment& c = alignment::CENTERED, const char& fill = ' ');
29 
30 private:
31 
32 };
33 
46 template<typename T>
47 int algs::compare(const vector<T>& v1, const vector<T>& v2) {
48  if (v1.size() != v2.size()) {
49  throw cuba_runtime_error("algs::compare: sizes are not equal!");
50  }
51 
52 // auto tv1(v1);
53 // auto tv2(v2);
54 // if (is_symmetry) {
55 // std::sort(tv1.begin(), tv1.end());
56 // std::sort(tv2.begin(), tv2.end());
57 // }
58 
59  auto iv1 = v1.cbegin();
60  auto iv2 = v2.cbegin();
61  while (iv1 != v1.cend()) {
62  if (*iv1 < *iv2)
63  return -1;
64  if (*iv1 > *iv2)
65  return 1;
66  ++iv1, ++iv2;
67  }
68  return 0;
69 }
70 
71 template<class T>
72 string algs::widthify(const T& x, const ushort& width, const alignment& c,
73  const char& fill) {
74  std::ostringstream os;
75  os << x;
76  string s = os.str();
77 
78  ushort n = s.size();
79  if (n >= width)
80  return s;
81  ushort addlength = width - n;
82  string result;
83  switch (c) {
84  case alignment::LEFTJUST:
85  result = s + string(addlength, fill);
86  break;
87  case alignment::RIGHTJUST:
88  result = string(addlength, fill) + s;
89  break;
90  case alignment::CENTERED:
91  result = (
92  addlength % 2 == 0 ?
93  string(addlength / 2, fill) + s
94  + string(addlength / 2, fill) :
95  string((addlength - 1) / 2, fill) + s
96  + string((addlength + 1) / 2, fill));
97  break;
98  }
99  return result;
100 }
101 } /* namespace ruba */
102 
103 #endif /* UTILS_UTILITIES_HH_ */
customized runtime error class
Definition: excep.hh:24
Definition: utilities.hh:21
Definition: cpda.cc:10
static int compare(const vector< T > &v1, const vector< T > &v2)
Definition: utilities.hh:47