Library of Assembled Shared Sources
vector.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::num::Vector
46 * @brief a dynamic sized n-dimensional vector with vector expression templates
47 * @author Bram de Greve [BdG]
48 */
49
50#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_H
51#define LASS_GUARDIAN_OF_INCLUSION_NUM_VECTOR_H
52
53#include "num_common.h"
54#include "num_traits.h"
55#include "../meta/bool.h"
56#include "../meta/wrap.h"
57#include "../util/call_traits.h"
59
60namespace lass
61{
62namespace num
63{
64
65template
66<
67 typename T,
68 typename S = impl::VStorage<T>
69>
70class Vector
71{
72public:
73
74 typedef Vector<T, S> TSelf;
75 typedef S TStorage;
76
77 typedef typename util::CallTraits<T>::TValue TValue;
78 typedef typename util::CallTraits<T>::TParam TParam;
79 typedef typename util::CallTraits<T>::TReference TReference;
80 typedef typename util::CallTraits<T>::TConstReference TConstReference;
81 typedef num::NumTraits<T> TNumTraits;
82 typedef size_t TSize;
83
84 template <typename U> struct Rebind
85 {
86 typedef Vector<U, S> Type;
87 };
88
90 explicit Vector(TSize iDimension, TParam iInitialValue = TNumTraits::zero);
91 explicit Vector(const TStorage& iStorage);
92 template <typename VectorType> explicit Vector(const VectorType& iVector);
93 template <typename T2, typename S2> Vector(const Vector<T2, S2>& iOther);
94
95 template <typename T2, typename S2> Vector<T, S>& operator=(const Vector<T2, S2>& iOther);
96
97 TSize size() const;
98
99 const TValue operator[](TSize iIndex) const;
100 TReference operator[](TSize iIndex);
101 const TValue at(TSize iIndex) const;
102 TReference at(TSize iIndex);
103
104 const Vector<T, S>& operator+() const;
105 const Vector<T, impl::VNeg<T, S> > operator-() const;
106 template <typename T2, typename S2> Vector<T, S>& operator+=(const Vector<T2, S2>& iB);
107 template <typename T2, typename S2> Vector<T, S>& operator-=(const Vector<T2, S2>& iB);
108 template <typename T2, typename S2> Vector<T, S>& operator*=(const Vector<T2, S2>& iB);
109 template <typename T2, typename S2> Vector<T, S>& operator/=(const Vector<T2, S2>& iB);
110 template <typename T2> Vector<T, S>& operator+=(const T2& iB);
111 template <typename T2> Vector<T, S>& operator-=(const T2& iB);
112 template <typename T2> Vector<T, S>& operator*=(const T2& iB);
113 template <typename T2> Vector<T, S>& operator/=(const T2& iB);
114
115 bool isEmpty() const;
116 bool isZero() const;
117 const TValue sum() const;
118 const TValue min() const;
119 const TValue max() const;
120 const TValue squaredNorm() const;
121 const TValue norm() const;
122 const Vector<T, impl::VMul<T, S, impl::VScalar<T> > > normal() const;
123 const Vector<T, impl::VRec<T, S> > reciprocal() const;
124
125 template <typename S2> const Vector<T, impl::VMul<T, S, impl::VScalar<T> > >
126 project(const Vector<T, S2>& iB) const;
127 template <typename S2> const Vector<T, impl::VSub<T, S2, impl::VMul<T, S, impl::VScalar<T> > > >
128 reject(const Vector<T, S2>& iB) const;
129
130 const Vector<T, impl::VFun<T, S> > transform(T (*iOperator)(T));
131
132 void normalize();
133
134 const TStorage& storage() const;
135 TStorage& storage();
136 void swap(Vector<T, S>& iOther);
137
138private:
139
140 template <typename T2, typename S2> friend class Vector;
141
142 template <typename IntegralType> void init(IntegralType iDimension, meta::Wrap<meta::True>);
143 template <typename VectorType> void init(const VectorType& iVector, meta::Wrap<meta::False>);
144
145 TStorage storage_;
146};
147
148template <typename T, typename S1, typename S2>
149bool operator==(const Vector<T>& iA, const Vector<T>& iB);
150template <typename T, typename S1, typename S2>
151inline bool operator!=(const Vector<T>& iA, const Vector<T>& iB);
152
153template <typename T, typename S1, typename S2>
154const T dot(const Vector<T, S1>& iA, const Vector<T, S2>& iB);
155
156template <typename T, typename S1, typename S2>
157const Vector<T, impl::VAdd<T, S1, S2> > operator+(const Vector<T, S1>& iA, const Vector<T, S2>& iB);
158template <typename T, typename S1, typename S2>
159const Vector<T, impl::VSub<T, S1, S2> > operator-(const Vector<T, S1>& iA, const Vector<T, S2>& iB);
160template <typename T, typename S1, typename S2>
161const Vector<T, impl::VMul<T, S1, S2> > operator*(const Vector<T, S1>& iA, const Vector<T, S2>& iB);
162template <typename T, typename S1, typename S2>
163const Vector<T, impl::VDiv<T, S1, S2> > operator/(const Vector<T, S1>& iA, const Vector<T, S2>& iB);
164
165template <typename T, typename S>
166const Vector<T, impl::VAdd<T, impl::VScalar<T>, S> > operator+(const T& iA, const Vector<T, S>& iB);
167template <typename T, typename S>
168const Vector<T, impl::VSub<T, impl::VScalar<T>, S> > operator-(const T& iA, const Vector<T, S>& iB);
169template <typename T, typename S>
170const Vector<T, impl::VMul<T, impl::VScalar<T>, S> > operator*(const T& iA, const Vector<T, S>& iB);
171template <typename T, typename S>
172const Vector<T, impl::VDiv<T, impl::VScalar<T>, S> > operator/(const T& iA, const Vector<T, S>& iB);
173
174template <typename T, typename S>
175const Vector<T, impl::VAdd<T, S, impl::VScalar<T> > > operator+(const Vector<T, S>& iA, const T& iB);
176template <typename T, typename S>
177const Vector<T, impl::VSub<T, S, impl::VScalar<T> > > operator-(const Vector<T, S>& iA, const T& iB);
178template <typename T, typename S>
179const Vector<T, impl::VMul<T, S, impl::VScalar<T> > > operator*(const Vector<T, S>& iA, const T& iB);
180template <typename T, typename S>
181const Vector<T, impl::VDiv<T, S, impl::VScalar<T> > > operator/(const Vector<T, S>& iA, const T& iB);
182
183
184template <typename T, typename S, typename Char, typename Traits>
185std::basic_ostream<Char, Traits>&
186operator<<(std::basic_ostream<Char, Traits>& iS, const Vector<T, S>& iA);
187
188template <typename T, typename S, typename Char, typename Traits>
189std::basic_istream<Char, Traits>&
190operator>>(std::basic_istream<Char, Traits>& iS, Vector<T>& iB);
191
192}
193
194}
195
196#include "vector.inl"
197
198#ifdef LASS_GUARDIAN_OF_INCLUSION_NUM_MATRIX_H
199#include "matrix_vector.h"
200#endif
201
202#endif
203
204// EOF
a dynamic sized n-dimensional vector with vector expression templates
Definition vector.h:71
bool isZero() const
Return true if all the components are (exactly!) zero.
Definition vector.inl:516
Vector< T, S > & operator-=(const T2 &iB)
subtract iB from all components
Definition vector.inl:441
const TValue sum() const
Return sum of all components of vector.
Definition vector.inl:540
const TValue operator[](TSize iIndex) const
return the iIndex'th component value.
Definition vector.inl:207
Vector(TSize iDimension, TParam iInitialValue=TNumTraits::zero)
Construct a vector of dimension iDimension.
Definition vector.inl:93
const TValue squaredNorm() const
Return squared norm of vector.
Definition vector.inl:609
const Vector< T, impl::VMul< T, S, impl::VScalar< T > > > project(const Vector< T, S2 > &iB) const
Project vector on this one.
Definition vector.inl:684
const Vector< T, impl::VMul< T, S, impl::VScalar< T > > > normal() const
return a unit vector with same direction/sense as this vector.
Definition vector.inl:650
const Vector< T, impl::VNeg< T, S > > operator-() const
return a vector with all components negated (-v)[i] == -(v[i]).
Definition vector.inl:295
void swap(Vector< T, S > &iOther)
swap storage of two vectors
Definition vector.inl:755
Vector(const VectorType &iVector)
contruct by any particular type supporting [] and size().
Definition vector.inl:127
const TValue min() const
Return minimum of all components of vector.
Definition vector.inl:560
Vector< T, S > & operator*=(const Vector< T2, S2 > &iB)
multiply storage/expression vector with this (this should be a storage vector).
Definition vector.inl:371
const TValue norm() const
Return norm of vector.
Definition vector.inl:630
Vector< T, S > & operator=(const Vector< T2, S2 > &iOther)
assign storage/expression vector to this (this should be a storage vector).
Definition vector.inl:170
Vector< T, S > & operator/=(const Vector< T2, S2 > &iB)
divide this by storage/expression vector (this should be a storage vector).
Definition vector.inl:398
const Vector< T, impl::VRec< T, S > > reciprocal() const
return a vector with each component being the reciprocal value of this vector.
Definition vector.inl:666
bool isEmpty() const
return true if vector contains no dataa at all
Definition vector.inl:503
Vector< T, S > & operator*=(const T2 &iB)
multiply all components with iB.
Definition vector.inl:462
const Vector< T, impl::VSub< T, S2, impl::VMul< T, S, impl::VScalar< T > > > > reject(const Vector< T, S2 > &iB) const
Project vector on this one.
Definition vector.inl:704
TSize size() const
return dimension of vector.
Definition vector.inl:191
Vector(const TStorage &iStorage)
construct vector from storage type
Definition vector.inl:109
TReference at(TSize iIndex)
access the iIndex'th component value and wrap index if necessary.
Definition vector.inl:260
Vector< T, S > & operator/=(const T2 &iB)
divide all components by iB.
Definition vector.inl:483
Vector< T, S > & operator+=(const T2 &iB)
add iB to all components
Definition vector.inl:420
const Vector< T, S > & operator+() const
A weird way to get back the same object.
Definition vector.inl:277
Vector(const Vector< T2, S2 > &iOther)
construct storage/expression vector to this (this should be a storage vector).
Definition vector.inl:146
Vector< T, S > & operator+=(const Vector< T2, S2 > &iB)
add storage/expression vector to this (this should be a storage vector).
Definition vector.inl:317
const TValue at(TSize iIndex) const
return the iIndex'th component value and wrap index if necessary.
Definition vector.inl:242
Vector< T, S > & operator-=(const Vector< T2, S2 > &iB)
subtract storage/expression vector from this (this should be a storage vector).
Definition vector.inl:344
const TValue max() const
Return maximum of all components of vector.
Definition vector.inl:584
Vector()
constructs an empty vector
Definition vector.inl:73
void normalize()
Normalize vector.
Definition vector.inl:719
TReference operator[](TSize iIndex)
access the iIndex'th component value.
Definition vector.inl:226
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53