X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fcml%2Fvector%2Ffixed.h;fp=src%2Fcml%2Fvector%2Ffixed.h;h=6ec9d2076bd7116faa50447a1f8c7a99a292a25a;hp=0000000000000000000000000000000000000000;hb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c;hpb=85783316365181491a3e3c0c63659972477cebba diff --git a/src/cml/vector/fixed.h b/src/cml/vector/fixed.h new file mode 100644 index 0000000..6ec9d20 --- /dev/null +++ b/src/cml/vector/fixed.h @@ -0,0 +1,191 @@ +/* -*- C++ -*- ------------------------------------------------------------ + +Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/ + +The Configurable Math Library (CML) is distributed under the terms of the +Boost Software License, v1.0 (see cml/LICENSE for details). + + *-----------------------------------------------------------------------*/ +/** @file + * @brief Specialization for fixed-size, fixed-memory vectors. + */ + +#ifndef fixed_vector_h +#define fixed_vector_h + +#include +#include +#include +#include +#include +#include + +namespace cml { + +/** Fixed-size, fixed-memory vector. */ +template +class vector< Element, fixed > +: public fixed_1D +{ + public: + + /* Shorthand for the generator: */ + typedef fixed<> storage_type; + typedef fixed generator_type; + + /* Shorthand for the array type: */ + typedef fixed_1D array_type; + + /* Shorthand for the type of this vector: */ + typedef vector vector_type; + + /* The vector coordinate type: */ + typedef Element coordinate_type; + + /* For integration into the expression template code: */ + typedef vector_type expr_type; + + /* For integration into the expression template code: */ + typedef vector_type temporary_type; + typedef vector< Element, fixed > subvector_type; + + /* Standard: */ + typedef typename array_type::value_type value_type; + typedef typename array_type::reference reference; + typedef typename array_type::const_reference const_reference; + + /* For integration into the expression templates code: */ + typedef vector_type& expr_reference; + typedef const vector_type& expr_const_reference; + + /* For matching by storage type: */ + typedef typename array_type::memory_tag memory_tag; + + /* For matching by size type: */ + typedef typename array_type::size_tag size_tag; + + /* For matching by result-type: */ + typedef cml::et::vector_result_tag result_tag; + + /* For matching by assignability: */ + typedef cml::et::assignable_tag assignable_tag; + + + public: + + /** Static constant containing the vector's space dimension. */ + enum { dimension = Size }; + + + public: + + /** Return square of the length. */ + value_type length_squared() const { + return cml::dot(*this,*this); + } + + /** Return the length. */ + value_type length() const { + return std::sqrt(length_squared()); + } + + /** Normalize the vector. */ + vector_type& normalize() { + return (*this /= length()); + } + + /** Set this vector to [0]. */ + vector_type& zero() { + typedef cml::et::OpAssign OpT; + cml::et::UnrollAssignment(*this,Element(0)); + return *this; + } + + /** Set this vector to a cardinal vector. */ + vector_type& cardinal(size_t i) { + zero(); + (*this)[i] = Element(1); + return *this; + } + + /** Pairwise minimum of this vector with another. */ + template + void minimize(const vector& v) { + /* XXX This should probably use ScalarPromote: */ + for (size_t i = 0; i < this->size(); ++i) { + (*this)[i] = std::min((*this)[i],v[i]); + } + } + + /** Pairwise maximum of this vector with another. */ + template + void maximize(const vector& v) { + /* XXX This should probably use ScalarPromote: */ + for (size_t i = 0; i < this->size(); ++i) { + (*this)[i] = std::max((*this)[i],v[i]); + } + } + + /** Fill vector with random elements. */ + void random(value_type min, value_type max) { + for (size_t i = 0; i < this->size(); ++i) { + (*this)[i] = cml::random_real(min,max); + } + } + + /** Return a subvector by removing element i. + * + * @internal This is horribly inefficient... + */ + subvector_type subvector(size_t i) const { + subvector_type s; + for(size_t m = 0, n = 0; m < this->size(); ++ m) + if(m != i) s[n++] = (*this)[m]; + return s; + }; + + + public: + + vector() : array_type() {} + + + public: + + /* Define common class operators: */ + + CML_CONSTRUCT_VEC_2(/**/) + CML_CONSTRUCT_VEC_3(/**/) + CML_CONSTRUCT_VEC_4(/**/) + + CML_CONSTRUCT_FROM_SUBVEC(/**/) + + CML_VEC_COPY_FROM_FIXED_ARRAY(array_type::array_size,/**/) + CML_VEC_COPY_FROM_VECTYPE(: array_type()) + CML_VEC_COPY_FROM_VEC + CML_VEC_COPY_FROM_VECXPR + + CML_ASSIGN_VEC_2 + CML_ASSIGN_VEC_3 + CML_ASSIGN_VEC_4 + + CML_VEC_ASSIGN_FROM_VECTYPE + + CML_VEC_ASSIGN_FROM_VEC(=, cml::et::OpAssign) + CML_VEC_ASSIGN_FROM_VEC(+=, cml::et::OpAddAssign) + CML_VEC_ASSIGN_FROM_VEC(-=, cml::et::OpSubAssign) + + CML_VEC_ASSIGN_FROM_VECXPR(=, cml::et::OpAssign) + CML_VEC_ASSIGN_FROM_VECXPR(+=, cml::et::OpAddAssign) + CML_VEC_ASSIGN_FROM_VECXPR(-=, cml::et::OpSubAssign) + + CML_VEC_ASSIGN_FROM_SCALAR(*=, cml::et::OpMulAssign) + CML_VEC_ASSIGN_FROM_SCALAR(/=, cml::et::OpDivAssign) +}; + +} // namespace cml + +#endif + +// ------------------------------------------------------------------------- +// vim:ft=cpp