FLAME  devel
 All Classes Functions Variables Typedefs Enumerations Pages
util.h
1 #ifndef UTIL_H
2 #define UTIL_H
3 
4 #include <map>
5 #include <ostream>
6 #include <stdexcept>
7 
8 #include <boost/call_traits.hpp>
9 #include <boost/numeric/ublas/matrix.hpp>
10 
11 #ifdef __GNUC__
12 # define UNLIKELY(E) __builtin_expect(E, 0)
13 #else
14 # define UNLIKELY(E) (E)
15 #endif
16 
17 struct key_error : public std::runtime_error
18 {
19  key_error(const std::string& s) : std::runtime_error(s) {}
20 };
21 
23 template<typename M>
24 bool find(const M& map,
25  typename boost::call_traits<typename M::key_type>::param_type key,
26  typename M::mapped_type& result)
27 {
28  typename M::const_iterator it = map.find(key);
29  if(it==map.end()) return false;
30  result = it->second;
31  return true;
32 }
33 
37 template<typename I>
39 {
40  I orig;
41 public:
42  typedef typename I::iterator_category iterator_category;
43  typedef typename I::value_type::second_type value_type;
44  typedef std::ptrdiff_t difference_type;
45  typedef value_type* pointer;
46  typedef value_type& reference;
47 
49  explicit value_proxy_iterator(const I& o) :orig(o) {}
50  value_proxy_iterator(const value_proxy_iterator& o) :orig(o.orig) {}
51 
52  reference operator*() const { return orig->second; }
53  pointer operator->() const { return &orig->second; }
54  reference operator[](size_t i) const { return orig[i]->second; }
55 
56  bool operator==(const value_proxy_iterator& o) const { return orig==o.orig; }
57  bool operator!=(const value_proxy_iterator& o) const { return orig!=o.orig; }
58 
59  value_proxy_iterator& operator++() { ++orig; return *this; }
60  value_proxy_iterator operator++(int) { return value_proxy_iterator(orig++); }
61  value_proxy_iterator& operator--() { --orig; return *this; }
62  value_proxy_iterator operator--(int) { return value_proxy_iterator(orig--); }
63 
64  value_proxy_iterator& operator+=(size_t i) { orig+=i; return *this; }
65  value_proxy_iterator& operator-=(size_t i) { orig-=i; return *this; }
66  value_proxy_iterator operator+(size_t i) const { return value_proxy_iterator(orig+i); }
67  value_proxy_iterator operator-(size_t i) const { return value_proxy_iterator(orig-i); }
68 };
69 
72 struct numeric_table {
73  typedef boost::numeric::ublas::matrix<double,
74  boost::numeric::ublas::row_major
75  > value_t;
76 
77  typedef std::map<std::string, size_t> colnames_t;
78  colnames_t colnames;
79 
80  value_t table;
81 
82  void read(std::istream&);
83  void readvec(std::vector<double> vec, int numrow);
84 };
85 
86 class numeric_table_cache {
87  struct Pvt;
88  std::unique_ptr<Pvt> pvt;
89 public:
90  numeric_table_cache();
91  ~numeric_table_cache();
92 
93  typedef boost::shared_ptr<const numeric_table> table_pointer;
94 
95  table_pointer fetch(const std::string& path);
96 
97  void clear();
98 
99  static numeric_table_cache* get();
100 };
101 
102 struct SB {
103  std::ostringstream strm;
104  SB() {}
105  operator std::string() const { return strm.str(); }
106  template<typename T>
107  SB& operator<<(T i) { strm<<i; return *this; }
108 };
109 
111 template<unsigned MAX>
113  bool done;
114  const unsigned ndim;
115  size_t index[MAX], limit[MAX];
116  ndindex_iterate(unsigned nd, size_t *lim) :done(false), ndim(nd) {
117  std::fill(index, index+MAX, 0u);
118  std::copy(lim, lim+nd, limit);
119  }
120  bool next() {
121  if(!done) {
122  for(unsigned nd=0; nd<ndim; nd++) {
123  index[nd]++;
124  if(index[nd]<limit[nd]) {
125  return done;
126  } else {
127  index[nd] = 0;
128  }
129  }
130  done = true;
131  }
132  return done;
133  }
134 };
135 
136 #ifdef __GNUC__
137 #define FLAME_UNUSED __attribute__((unused))
138 #else
139 #define FLAME_UNUSED
140 #endif
141 
142 #if __cplusplus<201103L && !defined(static_assert)
143 #define STATIC_JOIN(x, y) STATIC_JOIN2(x, y)
144 #define STATIC_JOIN2(x, y) x ## y
145 #define static_assert(expr, msg) \
146  typedef int STATIC_JOIN(static_assert_failed_at_line_, __LINE__) \
147  [ (expr) ? 1 : -1 ] FLAME_UNUSED
148 #endif
149 
150 #endif // UTIL_H
Helper to step through the indicies of an Nd array.
Definition: util.h:112