]> git.tdb.fi Git - libs/math.git/blobdiff - source/linal/vector.h
Clearer API for slicing and dicing vectors, and also matrices
[libs/math.git] / source / linal / vector.h
index 9ef46fde412f22804535065d6dd59c10b11963e2..6dfe6ca0167fb3e1d57e65217003e3d700f6a3b3 100644 (file)
@@ -81,10 +81,9 @@ public:
        Vector(T, T, T, T);
        template<typename U>
        Vector(const Vector<U, N> &);
-       template<typename U>
-       Vector(const Vector<U, N-1> &, U);
-       template<typename U>
-       explicit Vector(const Vector<U, N+1> &);
+
+       template<unsigned M>
+       Vector<T, M> slice(unsigned) const;
 
        Vector &operator*=(T);
        Vector &operator/=(T);
@@ -145,20 +144,44 @@ inline Vector<T, N>::Vector(const Vector<U, N> &v)
 }
 
 template<typename T, unsigned N>
-template<typename U>
-inline Vector<T, N>::Vector(const Vector<U, N-1> &v, U s)
+inline Vector<T, N+1> compose(const Vector<T, N> &v, T s)
 {
-       for(unsigned i=0; i<N-1; ++i)
-               (*this)[i] = v[i];
-       (*this)[N-1] = s;
+       Vector<T, N+1> r;
+       for(unsigned i=0; i<N; ++i)
+               r[i] = v[i];
+       r[N] = s;
+       return r;
 }
 
 template<typename T, unsigned N>
-template<typename U>
-inline Vector<T, N>::Vector(const Vector<U, N+1> &v)
+inline Vector<T, N+1> compose(T s, const Vector<T, N> &v)
 {
+       Vector<T, N+1> r;
        for(unsigned i=0; i<N; ++i)
-               (*this)[i] = v[i];
+               r[i] = v[i];
+       r[N] = s;
+       return r;
+}
+
+template<typename T, unsigned N, unsigned M>
+inline Vector<T, N+M> compose(const Vector<T, N> &v1, const Vector<T, M> &v2)
+{
+       Vector<T, N+M> r;
+       for(unsigned i=0; i<N; ++i)
+               r[i] = v1[i];
+       for(unsigned i=0; i<M; ++i)
+               r[N+i] = v2[i];
+       return r;
+}
+
+template<typename T, unsigned N>
+template<unsigned M>
+inline Vector<T, M> Vector<T, N>::slice(unsigned j) const
+{
+       Vector<T, M> r;
+       for(unsigned i=0; i<M; ++i)
+               r[i] = (*this)[j+i];
+       return r;
 }
 
 template<typename T, unsigned N>