TMB Documentation  v1.9.11
concat.hpp
Go to the documentation of this file.
1 
5 namespace tmbutils {
6 
7 template<class T>
8 struct concat_traits {
9  typedef T value_type;
10  T* ptr(T &x) { return &x; }
11  size_t size(T &x) { return 1; }
12 };
13 template<class VT>
14 struct concat_traits_vector {
15  typedef typename VT::value_type value_type;
16  typedef value_type T;
17  T* ptr(VT &x) { return x.data(); }
18  size_t size(VT &x) { return x.size(); }
19 };
20 template<class T>
21 struct concat_traits<vector<T> > : concat_traits_vector<vector<T> > {};
22 template<class T>
23 struct concat_traits<matrix<T> > : concat_traits_vector<matrix<T> > {};
24 template<class T>
25 struct concat_traits<array<T> > : concat_traits_vector<array<T> > {};
26 
28 template<class S=void, class...Ts>
29 struct Concat {
30  S& cur;
31  Concat<Ts...> next;
32  typedef typename concat_traits<S>::value_type value_type;
33  void assign_from(const value_type* x) {
34  value_type* dest = concat_traits<S>().ptr(cur);
35  size_t n = concat_traits<S>().size(cur);
36  for (size_t i=0; i<n; i++) {dest[i] = *x; x++;}
37  next.assign_from(x);
38  }
39  void assign_to(value_type* x) {
40  value_type* orig = concat_traits<S>().ptr(cur);
41  size_t n = concat_traits<S>().size(cur);
42  for (size_t i=0; i<n; i++) {*x = orig[i]; x++;}
43  next.assign_to(x);
44  }
45  size_t size() {return concat_traits<S>().size(cur) + next.size(); }
46  operator vector<value_type>() {
47  vector<value_type> ans(size());
48  assign_to(ans.data());
49  return ans;
50  }
51  Concat& operator=(const vector<value_type> &x) {
52  assign_from(x.data());
53  return *this;
54  }
55 };
56 template<>
57 struct Concat<void> {
58  template<class T>
59  void assign_from(T x) { }
60  template<class T>
61  void assign_to(T x) { }
62  size_t size() { return 0; }
63 };
94 template<class...Ts>
95 Concat<Ts...> concat(Ts&... args) {
96  return Concat<Ts...>({args...});
97 }
98 
99 } // End namespace
Concat< Ts... > concat(Ts &... args)
Serialized representation of objects of different types.
Definition: concat.hpp:95
Vector class used by TMB.
Definition: tmbutils.hpp:18
Serialized representation of objects of different types.
Definition: concat.hpp:29
Utility functions for TMB (automatically included)
Definition: concat.hpp:5
License: GPL v2