TooN
vbase.hh
1 // -*- c++ -*-
2 
3 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@cam.ac.uk)
5 
6 //All rights reserved.
7 //
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
10 //are met:
11 //1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
18 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
21 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 //POSSIBILITY OF SUCH DAMAGE.
28 
29 namespace TooN {
30 
31 namespace Internal{
32 template<int Size, class Precision, int Stride, class Mem> struct GenericVBase;
33 
35 //
36 // Slice holding class
37 //
38 struct Default{};
39 
40 template<int Stride, class Ptr=Default, class CPtr=Default, class Ref=Default, class CRef=Default>
41 struct SliceVBase {
42 
43  // this class is really just a typedef
44  template<int Size, typename Precision>
45  struct VLayout
46  : public GenericVBase<Size, Precision, Stride, VectorSlice<Size, Precision, Ptr, CPtr, Ref, CRef> > {
47  typedef typename VectorSlice<Size, Precision, Ptr, CPtr, Ref, CRef>::PointerType PointerType;
48 
49  VLayout(PointerType d, int length, int stride)
51  }
52 
53  template<class Op>
54  VLayout(const Operator<Op>& op)
56  };
57 
58 };
59 
60 template<int Stride>
62 
63  // this class is really just a typedef
64  template<int Size, typename Precision>
65  struct VLayout
66  : public GenericVBase<Size, Precision, Stride, VectorSlice<Size, Precision> > {
67 
68  typedef typename VectorSlice<Size, Precision>::PointerType PointerType;
69 
70  VLayout(PointerType d, int length, int stride)
72  }
73 
74  template<class Op>
75  VLayout(const Operator<Op>& op)
77  };
78 
79 };
80 
82 //
83 // Classes for Vectors owning memory
84 //
85 
86 struct VBase {
87 
88  // this class is really just a typedef
89  template<int Size, class Precision>
90  struct VLayout
91  : public GenericVBase<Size, Precision, 1, VectorAlloc<Size, Precision> > {
92 
93  VLayout(){}
94 
95  VLayout(VLayout&&) = default;
96  VLayout(const VLayout&) = default;
97 
98  VLayout(std::initializer_list<Precision> i)
100  {}
101 
102  template<typename Precision2, int Size2>
103  VLayout(const Precision2(&i)[Size2])
105  {}
106 
107  VLayout(int s)
109  {}
110 
111  template<class Op>
112  VLayout(const Operator<Op>& op)
114  };
115 };
116 
118 //
119 // Generic implementation
120 //
121 
122 template<int Size, typename Precision, int Stride, typename Mem> struct GenericVBase: public Mem, public StrideHolder<Stride>
123 {
124  int stride() const{
126  }
127 
128  //Optional constuctors
129  GenericVBase(){}
130 
131  GenericVBase(GenericVBase&&) = default;
132  GenericVBase(const GenericVBase&) = default;
133 
134  GenericVBase(int s)
135  :Mem(s)
136  {}
137 
138  template<typename Precision2, int Size2>
139  GenericVBase(const Precision2(&i)[Size2])
140  :Mem(i)
141  {}
142 
143  GenericVBase(std::initializer_list<Precision> i)
144  :Mem(i)
145  {}
146 
147  typedef typename Mem::PointerType PointerType;
148  typedef typename Mem::ConstPointerType ConstPointerType;
149  typedef typename Mem::ReferenceType ReferenceType;
150  typedef typename Mem::ConstReferenceType ConstReferenceType;
151 
152  GenericVBase(PointerType d, int length, int stride)
153  :Mem(d, length),StrideHolder<Stride>(stride){
154  }
155 
156  template<class Op>
157  GenericVBase(const Operator<Op> & op) : Mem(op), StrideHolder<Stride>(op) {}
158 
159  using Mem::data;
160  using Mem::size;
161 
162  ReferenceType operator[](int i) {
163  Internal::check_index(size(), i);
164  return data()[i * stride()];
165  }
166 
167  ConstReferenceType operator[](int i) const {
168  Internal::check_index(size(), i);
169  return data()[i * stride()];
170  }
171 
174 
175 
176  //Completely generic Vector slice operations below:
177  template<int Start, int Length>
178  Vector<Length, Precision, SliceBase> slice(int start, int length){
179  Internal::CheckSlice<Size, Start, Length>::check(size(), start, length);
180  return Vector<Length, Precision, SliceBase>(data() + stride() * (Start==Dynamic?start:Start), Length==Dynamic?length:Length, stride(), Slicing());
181  }
182 
183  template<int Start, int Length>
184  const Vector<Length, const Precision, ConstSliceBase> slice(int start, int length) const{
185  Internal::CheckSlice<Size, Start, Length>::check(size(), start, length);
186  return Vector<Length, const Precision, ConstSliceBase>(data() + stride() * (Start==Dynamic?start:Start), Length==Dynamic?length:Length, stride(), Slicing());
187  }
188 
189 
190 
191  //Special case slice operations
192  template<int Start, int Length> Vector<Length, Precision, SliceBase> slice(){
194  return slice<Start, Length>(Start, Length);
195  }
196 
197  template<int Start, int Length> const Vector<Length, const Precision, ConstSliceBase> slice() const {
199  return slice<Start, Length>(Start, Length);
200  }
201 
202  Vector<Dynamic, Precision, SliceBase> slice(int start, int length){
203  return slice<Dynamic, Dynamic>(start, length);
204  }
205 
206  const Vector<Dynamic, const Precision, ConstSliceBase> slice(int start, int length) const{
207  return slice<Dynamic, Dynamic>(start, length);
208  }
209 
210  //Other slices below
212  return Matrix<1, Size, const Precision, Slice<1,Stride> >(data(), 1, size(), 1, stride(), Slicing());
213  }
214 
216  return Matrix<1, Size, Precision, Slice<1,Stride> >(data(), 1, size(), 1, stride(), Slicing());
217  }
218 
220  return Matrix<Size, 1, const Precision, Slice<Stride,1> >(data(), size(), 1, stride(), 1, Slicing());
221  }
222 
224  return Matrix<Size, 1, Precision, Slice<Stride,1> >(data(), size(), 1, stride(), 1, Slicing());
225  }
226 
228 
230  return Vector<Size, Precision, SliceBase>(data(), size(), stride(), Slicing());
231  }
232 
233  const Vector<Size, const Precision, ConstSliceBase> as_slice() const {
234  return Vector<Size, const Precision, ConstSliceBase>(data(), size(), stride(), Slicing());
235  }
236 
238  return DiagonalMatrix<Size, Precision, SliceBase> (data(), size(), stride(), Slicing());
239  }
240 
241  const DiagonalMatrix<Size,const Precision, ConstSliceBase> as_diagonal() const {
242  return DiagonalMatrix<Size, const Precision, ConstSliceBase> (data(), size(), stride(), Slicing());
243  }
244 
245 };
246 
247 }
248 
249 }
Definition: vbase.hh:32
Definition: slice_error.hh:56
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A vector.
Definition: vector.hh:126
A matrix.
Definition: matrix.hh:105
A diagonal matrix.
Definition: diagmatrix.h:108
Definition: operators.hh:119
Definition: vbase.hh:90
Definition: vbase.hh:38
Definition: vbase.hh:41
Definition: allocator.hh:670
Definition: vbase.hh:86
Definition: TooN.h:145