Milo  0.1
Milo:EquationManipulationProgram
util.h
Go to the documentation of this file.
1 #ifndef __UTIL_H
2 #define __UTIL_H
3 
4 /* Copyright (C) 2017 - James Terman
5  *
6  * milo is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * milo is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #include <string>
27 #include <vector>
28 #include <array>
29 #include <complex>
30 #include <algorithm>
31 #include <unordered_map>
32 #include <boost/functional/hash.hpp>
33 
40 bool isZero(double x);
41 
46 bool isZero(std::complex<double> z);
47 
52 bool isInteger(const std::string& n);
53 
58 bool isInteger(double value);
59 
64 inline std::string to_hexstring(unsigned long x)
65 {
66  std::string hex;
67  while (x != 0) {
68  int n = x % 16; x /= 16;
69  char d = (n < 10) ? '0' + n : 'A' + n - 10;
70  hex.insert(0, 1, d);
71  }
72  return hex;
73 }
74 
83 template <class T>
84 inline auto find(const std::vector<T>& v, const T& val) -> decltype( v.cbegin() )
85 {
86  return std::find(v.cbegin(), v.cend(), val);
87 }
88 
94 template <class T>
95 inline void freeVector(std::vector<T*>& v)
96 {
97  for ( auto p : v ) { delete p; }
98  v.clear();
99 }
100 
106 template <class T>
107 inline void mergeVectors(std::vector<T>& a, std::vector<T>& b)
108 {
109  a.reserve(a.size() + b.size());
110  for ( auto e : b ) { a.insert(a.end(), e); }
111  b.clear();
112 }
113 
120 template <class T>
121 inline void eraseElement(std::vector<T>& v, int index)
122 {
123  v.erase((index < 0 ? v.end() - 1 : v.begin()) + index);
124 }
125 
133 template <class T>
134 inline void insertElement(std::vector<T>& v, int index, const T& e)
135 {
136  v.insert((index < 0 ? v.end() : v.begin()) + index, e);
137 }
138 
146 template <class T>
147 auto AddAfter(std::vector<T>& v, decltype(v.begin()) it, const T& elem)->decltype(v.begin())
148 {
149  if (v.empty()) {
150  v.push_back(elem);
151  return v.begin();
152  }
153  if (it == v.end()) {
154  return v.insert(v.begin(), elem);
155  }
156  ++it;
157  return v.insert(it, elem);
158 }
159 
166 template <typename K, typename V>
167 const K searchMapValue(const std::unordered_map<K, V>&kv_map, V value)
168 {
169  for ( auto& kv : kv_map ) {
170  if ( kv.second == value ) {
171  return kv.first;
172  }
173  }
174  return 0;
175 }
177 
183 template <class T>
185 {
186 public:
193  Rectangle() : m_rect{0, 0, 0, 0} {}
194 
202  Rectangle(T x, T y, T x0, T y0) : m_rect{x, y, x0, y0} {}
204 
214  void set(T x, T y, T x0, T y0) { m_rect = {x, y, x0, y0}; }
215 
221  void setOrigin(T x0, T y0) { m_rect[X0] = x0; m_rect[Y0] = y0; }
222 
228  void setSize(T x, T y) { m_rect[WIDTH] = x; m_rect[HEIGHT] = y; }
230 
237  int& x0() { return m_rect[X0]; }
238 
243  int& y0() { return m_rect[Y0]; }
244 
249  int& width() { return m_rect[WIDTH]; }
250 
255  int& height() { return m_rect[HEIGHT]; }
256 
261  int x0() const { return m_rect[X0]; }
262 
267  int y0() const { return m_rect[Y0]; }
268 
273  int width() const { return m_rect[WIDTH]; }
274 
279  int height() const { return m_rect[HEIGHT]; }
281 
288  std::string toString()
289  {
290  std::string s = "width: " + std::to_string(width()) + ", height: " + std::to_string(height()) +
291  ", x0: " + std::to_string(x0()) + ", y0: " + std::to_string(y0());
292  return s;
293  }
294 
299  T area() { return width()*height(); }
300 
307  bool inside(T x, T y) const {
308  return ( x >= x0() && x < (x0() + width()) &&
309  y >= y0() && y < (y0() + height()) );
310  }
311 
317  bool inside(Rectangle r) const {
318  return ( x0() >= r.x0() && (x0() + width()) <= (r.x0() + r.width()) &&
319  y0() >= r.y0() && (y0() + height()) <= (r.y0() + r.height()) );
320  }
321 
326  bool intersect(const Rectangle& r) const {
327  bool noOverlap = (x0() - r.x0()) > r.width() ||
328  (r.x0() - x0()) > width() ||
329  (y0() - r.y0()) > r.height() ||
330  (r.y0() - y0()) > height();
331  return !noOverlap;
332  }
333 
337  void merge(const Rectangle& r) {
338  T x0 = min(x0(), r.x0());
339  T y0 = min(y0(), r.y0());
340  T x1 = max(x0() + width(), r.x0() + r.width());
341  T y1 = max(y0() + height(), r.y0() + r.height());
342  set(x1 - x0, y1 - y0, x0, y0);
343  }
344 
348  static Rectangle merge(const Rectangle& r1, const Rectangle& r2) {
349  Rectangle r{r1};
350  r.merge(r2);
351  return r;
352  }
354 
355 private:
356  enum { WIDTH, HEIGHT, X0, Y0, SIZE };
357  std::array<T, SIZE> m_rect;
358 };
359 
363 using StringMap = std::unordered_map<std::string, std::string>;
364 
365 
369 template <class T> inline std::size_t hash_calculate(std::initializer_list<T> l)
370 {
371  std::size_t seed = 0;
372  for (T n : l) {
373  boost::hash_combine<T>(seed, n);
374  }
375  return seed;
376 }
377 
381 namespace Log
382 {
386  void msg(const std::string& m);
387 }
388 
389 #define LOG_TRACE_MSG(m) Log::msg(string("") + __FILE__ + ": " + to_string(__LINE__) + ": " + (m))
390 #define LOG_TRACE_FILE "/tmp/milo.log"
391 #define LOG_TRACE_CLEAR() remove(LOG_TRACE_FILE)
392 
393 #define QUOTE(arg) #arg
394 #define STR(macro) QUOTE(macro)
395 
396 #ifndef INSTALL_PATH
397 #define INSTALL_PATH /usr/local/milo
398 #endif
399 
400 #define INSTALL_PATH_STR STR(INSTALL_PATH)
401 
402 #endif // __UTIL_H
void eraseElement(std::vector< T > &v, int index)
Definition: util.h:121
void setSize(T x, T y)
Definition: util.h:228
int & x0()
Definition: util.h:237
T area()
Definition: util.h:299
void merge(const Rectangle &r)
Definition: util.h:337
const K searchMapValue(const std::unordered_map< K, V > &kv_map, V value)
Definition: util.h:167
void msg(const std::string &m)
int & y0()
Definition: util.h:243
int x0() const
Definition: util.h:261
Definition: milo.cpp:667
bool intersect(const Rectangle &r) const
Definition: util.h:326
int y0() const
Definition: util.h:267
Definition: util.h:184
std::unordered_map< std::string, std::string > StringMap
hash map for string => string
Definition: util.h:363
bool inside(T x, T y) const
Definition: util.h:307
std::string toString()
Definition: util.h:288
Rectangle()
Definition: util.h:193
bool isZero(double x)
Definition: milo.cpp:86
int & height()
Definition: util.h:255
std::array< T, SIZE > m_rect
Storage of rectangle parameters.
Definition: util.h:357
void setOrigin(T x0, T y0)
Definition: util.h:221
std::size_t hash_calculate(std::initializer_list< T > l)
Definition: util.h:369
void freeVector(std::vector< T * > &v)
Definition: util.h:95
auto find(const std::vector< T > &v, const T &val) -> decltype(v.cbegin())
Definition: util.h:84
void mergeVectors(std::vector< T > &a, std::vector< T > &b)
Definition: util.h:107
int height() const
Definition: util.h:279
static Rectangle merge(const Rectangle &r1, const Rectangle &r2)
Definition: util.h:348
Rectangle(T x, T y, T x0, T y0)
Definition: util.h:202
bool isInteger(const std::string &n)
int & width()
Definition: util.h:249
std::string to_hexstring(unsigned long x)
Definition: util.h:64
auto AddAfter(std::vector< T > &v, decltype(v.begin()) it, const T &elem) -> decltype(v.begin())
Definition: util.h:147
int width() const
Definition: util.h:273
void insertElement(std::vector< T > &v, int index, const T &e)
Definition: util.h:134
bool inside(Rectangle r) const
Definition: util.h:317