// // Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // io/types.hfa -- // // Author : Dimitry Kobets // Created On : // Last Modified By : // Last Modified On : // Update Count : // #pragma once #include #include "vec.hfa" forall (T) { struct vec2 { T x, y; }; } forall (T) { static inline { void ?{}(vec2(T)& v, T x, T y) { v.[x, y] = [x, y]; } forall(| zero_assign(T)) void ?{}(vec2(T)& vec, zero_t) with (vec) { x = y = 0; } void ?{}(vec2(T)& vec, T val) with (vec) { x = y = val; } void ?{}(vec2(T)& vec, vec2(T) other) with (vec) { [x,y] = other.[x,y]; } void ?=?(vec2(T)& vec, vec2(T) other) with (vec) { [x,y] = other.[x,y]; } forall(| zero_assign(T)) void ?=?(vec2(T)& vec, zero_t) with (vec) { x = y = 0; } // Primitive mathematical operations // - forall(| subtract(T)) { vec2(T) ?-?(vec2(T) u, vec2(T) v) { return [u.x - v.x, u.y - v.y]; } vec2(T)& ?-=?(vec2(T)& u, vec2(T) v) { u = u - v; return u; } } forall(| negate(T)) vec2(T) -?(vec2(T) v) with (v) { return [-x, -y]; } forall(| { T --?(T&); }) { vec2(T)& --?(vec2(T)& v) { --v.x; --v.y; return v; } vec2(T) ?--(vec2(T)& v) { vec2(T) copy = v; --v; return copy; } } // + forall(| add(T)) { vec2(T) ?+?(vec2(T) u, vec2(T) v) { return [u.x + v.x, u.y + v.y]; } vec2(T)& ?+=?(vec2(T)& u, vec2(T) v) { u = u + v; return u; } } forall(| { T ++?(T&); }) { vec2(T)& ++?(vec2(T)& v) { ++v.x; ++v.y; return v; } vec2(T) ?++(vec2(T)& v) { vec2(T) copy = v; ++v; return copy; } } // * forall(| multiply(T)) { vec2(T) ?*?(vec2(T) v, T scalar) with (v) { return [x * scalar, y * scalar]; } vec2(T) ?*?(T scalar, vec2(T) v) { return v * scalar; } vec2(T) ?*?(vec2(T) u, vec2(T) v) { return [u.x * v.x, u.y * v.y]; } vec2(T)& ?*=?(vec2(T)& v, T scalar) { v = v * scalar; return v; } vec2(T) ?*=?(vec2(T)& u, vec2(T) v) { u = u * v; return u; } } // / forall(| divide(T)) { vec2(T) ?/?(vec2(T) v, T scalar) with (v) { return [x / scalar, y / scalar]; } vec2(T) ?/?(vec2(T) u, vec2(T) v) { return [u.x / v.x, u.y / v.y]; } vec2(T)& ?/=?(vec2(T)& v, T scalar) { v = v / scalar; return v; } vec2(T) ?/=?(vec2(T)& u, vec2(T) v) { u = u / v; return u; } } // % forall(| { T ?%?(T,T); }) { vec2(T) ?%?(vec2(T) v, T scalar) with (v) { return [x % scalar, y % scalar]; } vec2(T)& ?%=?(vec2(T)& u, T scalar) { u = u % scalar; return u; } vec2(T) ?%?(vec2(T) u, vec2(T) v) { return [u.x % v.x, u.y % v.y]; } vec2(T)& ?%=?(vec2(T)& u, vec2(T) v) { u = u % v; return u; } } // & forall(| { T ?&?(T,T); }) { vec2(T) ?&?(vec2(T) v, T scalar) with (v) { return [x & scalar, y & scalar]; } vec2(T)& ?&=?(vec2(T)& u, T scalar) { u = u & scalar; return u; } vec2(T) ?&?(vec2(T) u, vec2(T) v) { return [u.x & v.x, u.y & v.y]; } vec2(T)& ?&=?(vec2(T)& u, vec2(T) v) { u = u & v; return u; } } // | forall(| { T ?|?(T,T); }) { vec2(T) ?|?(vec2(T) v, T scalar) with (v) { return [x | scalar, y | scalar]; } vec2(T)& ?|=?(vec2(T)& u, T scalar) { u = u | scalar; return u; } vec2(T) ?|?(vec2(T) u, vec2(T) v) { return [u.x | v.x, u.y | v.y]; } vec2(T)& ?|=?(vec2(T)& u, vec2(T) v) { u = u | v; return u; } } // ^ forall(| { T ?^?(T,T); }) { vec2(T) ?^?(vec2(T) v, T scalar) with (v) { return [x ^ scalar, y ^ scalar]; } vec2(T)& ?^=?(vec2(T)& u, T scalar) { u = u ^ scalar; return u; } vec2(T) ?^?(vec2(T) u, vec2(T) v) { return [u.x ^ v.x, u.y ^ v.y]; } vec2(T)& ?^=?(vec2(T)& u, vec2(T) v) { u = u ^ v; return u; } } // << forall(| { T ?<> forall(| { T ?>>?(T,T); }) { vec2(T) ?>>?(vec2(T) v, T scalar) with (v) { return [x >> scalar, y >> scalar]; } vec2(T)& ?>>=?(vec2(T)& u, T scalar) { u = u >> scalar; return u; } vec2(T) ?>>?(vec2(T) u, vec2(T) v) { return [u.x >> v.x, u.y >> v.y]; } vec2(T)& ?>>=?(vec2(T)& u, vec2(T) v) { u = u >> v; return u; } } // ~ forall(| { T ~?(T); }) vec2(T) ~?(vec2(T) v) with (v) { return [~v.x, ~v.y]; } // relational forall(| equality(T)) { bool ?==?(vec2(T) u, vec2(T) v) with (u) { return x == v.x && y == v.y; } bool ?!=?(vec2(T) u, vec2(T) v) { return !(u == v); } } // Geometric functions forall(| add(T) | multiply(T)) T dot(vec2(T) u, vec2(T) v) { return u.x * v.x + u.y * v.y; } } // static inline } forall(ostype &, T | writeable(T, ostype)) { ostype & ?|?(ostype & os, vec2(T) v) with (v) { return os | '<' | x | ',' | y | '>'; } void ?|?(ostype & os, vec2(T) v ) with (v) { (ostype &)(os | v); ends(os); } }