Advertisement
Guest User

Untitled

a guest
Oct 20th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.35 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////
  2. /// \brief Unroll the expression given.
  3. /////////////////////////////////////////////////////////////////////
  4. template ExprUnroll(alias Code, alias Size, alias Op="") {
  5.     enum t = replace(Code, "%", "%1$d");
  6.     enum ExprUnroll = iota(Size).map!(i => format(t, i)).join(Op);
  7. }
  8.  
  9.  
  10. /////////////////////////////////////////////////////////////////////
  11. /// \brief Define a mathematic vector.
  12. ///
  13. /// \author Wolftein <wolftein@ghrum.org>
  14. /////////////////////////////////////////////////////////////////////
  15. public struct Vector(T, size_t Dimension) {
  16.    
  17.     /// \brief The array that contain the components of the vector.
  18.     private T component[Dimension];
  19.    
  20.     /// \brief The lenght of the vector.
  21.     private immutable size_t size = Dimension;
  22.  
  23.     /////////////////////////////////////////////////////////////////////
  24.     /// \brief
  25.     ///
  26.     /// \param[in] vector
  27.     ///
  28.     /// \return
  29.     /////////////////////////////////////////////////////////////////////
  30.     public Vector!(T, size) opAdd(auto ref Vector!(T, size) vector)
  31.     in {
  32.         assert(vector !is null, "Vector cannot be null");
  33.         assert(vector.size() != size(), "Vector differ size");
  34.     }
  35.     body {
  36.         mixin( ExprUnroll!("component[%] += vector.component[%]", Dimension));
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement