tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
gen.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_GEN_H
21 #define TENSOR_GEN_H
22 
23 #include <tensor/numbers.h>
24 #include <tensor/vector.h>
25 
28 namespace tensor {
29 
41  template<typename elt_t> class ListGenerator {};
42 
49  extern const ListGenerator<index> igen;
55  extern const ListGenerator<double> rgen;
61  extern const ListGenerator<cdouble> cgen;
67  extern const ListGenerator<bool> bgen;
68 
75  extern class FlexiListGenerator {} xgen;
76 
77  template<typename elt_t, size_t n>
78  class StaticVector {
79  public:
80  StaticVector(const StaticVector<elt_t,n-1> &other, elt_t x) :
81  inner(other), extra(x)
82  {};
83  operator Vector<elt_t>() const {
84  Vector<elt_t> output(n);
85  push(output.begin());
86  return output;
87  }
88  void push(elt_t *v) const {
89  inner.push(v);
90  v[n-1] = extra;
91  }
92  index size() const {
93  return n;
94  }
95  protected:
96  StaticVector<elt_t,n-1> inner;
97  elt_t extra;
98  };
99 
100  template<typename elt_t>
101  class StaticVector<elt_t,(size_t)1> {
102  public:
103  StaticVector(elt_t x) :
104  extra(x)
105  {};
106  operator Vector<elt_t>() const {
107  Vector<elt_t> output(1);
108  push(output.begin());
109  return output;
110  }
111  void push(elt_t *v) const {
112  v[0] = extra;
113  }
114  index size() const {
115  return 1;
116  }
117  private:
118  elt_t extra;
119  };
120 
121  template<typename t1, typename t2>
122  StaticVector<t1,1> operator<<(const ListGenerator<t1> &g, const t2 x) {
123  return StaticVector<t1,1>(x);
124  }
125 
126  template<typename t1>
127  StaticVector<t1,1> operator<<(const FlexiListGenerator &g, const t1 x) {
128  return StaticVector<t1,1>(x);
129  }
130 
131  template<typename t1, typename t2, size_t n>
132  StaticVector<t1,n+1> operator<<(const StaticVector<t1,n> &g, const t2 x) {
133  return StaticVector<t1,n+1>(g,x);
134  }
135 
136  template<typename t1> StaticVector<t1,1> gen(t1 r) {
137  return StaticVector<t1,1>(r);
138  }
139 
140  template<typename elt_t, size_t n>
141  bool operator==(const tensor::StaticVector<elt_t,n> &v1,
142  const tensor::Vector<elt_t> &v2)
143  {
144  tensor::Vector<elt_t> v0(v1);
145  if (v0.size() != v2.size()) return false;
146  return std::equal(v0.begin_const(), v0.end_const(), v2.begin_const());
147  }
148 
149  template<typename elt_t, size_t n>
150  bool operator==(const tensor::Vector<elt_t> &v2,
151  const tensor::StaticVector<elt_t,n> &v1)
152  {
153  tensor::Vector<elt_t> v0(v1);
154  if (v0.size() != v2.size()) return false;
155  return std::equal(v0.begin_const(), v0.end_const(), v2.begin_const());
156  }
157 
158 } // namespace tensor
161 #endif // TENSOR_GEN_H
Compile-time generator of vectors, tensors and indices.
Definition: gen.h:41
Placeholder for statically creating a vector of elements, whose type is determined by the first eleme...
Definition: gen.h:75