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 };
84 
85 class numeric_table_cache {
86  struct Pvt;
87  std::auto_ptr<Pvt> pvt;
88 public:
89  numeric_table_cache();
90  ~numeric_table_cache();
91 
92  typedef boost::shared_ptr<const numeric_table> table_pointer;
93 
94  table_pointer fetch(const std::string& path);
95 
96  void clear();
97 
98  static numeric_table_cache* get();
99 };
100 
101 struct SB {
102  std::ostringstream strm;
103  SB() {}
104  operator std::string() const { return strm.str(); }
105  template<typename T>
106  SB& operator<<(T i) { strm<<i; return *this; }
107 };
108 
110 template<unsigned MAX>
112  bool done;
113  const unsigned ndim;
114  size_t index[MAX], limit[MAX];
115  ndindex_iterate(unsigned nd, size_t *lim) :done(false), ndim(nd) {
116  std::fill(index, index+MAX, 0u);
117  std::copy(lim, lim+nd, limit);
118  }
119  bool next() {
120  if(!done) {
121  for(unsigned nd=0; nd<ndim; nd++) {
122  index[nd]++;
123  if(index[nd]<limit[nd]) {
124  return done;
125  } else {
126  index[nd] = 0;
127  }
128  }
129  done = true;
130  }
131  return done;
132  }
133 };
134 
135 #ifdef __GNUC__
136 #define FLAME_UNUSED __attribute__((unused))
137 #else
138 #define FLAME_UNUSED
139 #endif
140 
141 #if __cplusplus<201103L && !defined(static_assert)
142 #define STATIC_JOIN(x, y) STATIC_JOIN2(x, y)
143 #define STATIC_JOIN2(x, y) x ## y
144 #define static_assert(expr, msg) \
145  typedef int STATIC_JOIN(static_assert_failed_at_line_, __LINE__) \
146  [ (expr) ? 1 : -1 ] FLAME_UNUSED
147 #endif
148 
149 #endif // UTIL_H
STL namespace.
Helper to step through the indicies of an Nd array.
Definition: util.h:111