From aa7e9a7effd6c830d52cb0ca043348ee6ffa6224 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 30 Sep 2020 14:16:42 +0800 Subject: [PATCH] add bvec.h --- inc/hgl/math/bvec.h | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 inc/hgl/math/bvec.h diff --git a/inc/hgl/math/bvec.h b/inc/hgl/math/bvec.h new file mode 100644 index 0000000..26ae70f --- /dev/null +++ b/inc/hgl/math/bvec.h @@ -0,0 +1,91 @@ +#ifndef HGL_MATH_BVEC_INCLUDE +#define HGL_MATH_BVEC_INCLUDE + +#include +namespace hgl +{ + /** + * bool型矢量 + */ + template class bool_vec + { + public: + + bool data[N]; + + public: + + bool_vec() {hgl_zero(data);} + bool_vec(const bool_vec &b) {hgl_cpy(*this,b);} + bool_vec(const bool *ba) {hgl_cpy(data,ba,N);} + virtual ~bool_vec()=default; + + bool &operator[](const int index) + { + return data[index]; + } + + const bool_vec &operator = (const bool_vec &b) + { + hgl_cpy(this->data,b.data,N); + return *this; + } + + bool all()const + { + bool result = false; + + for (int i = 0; i < N; ++i) + result &= data[i]; + + return result; + + } + + bool any()const + { + bool result = false; + + for (int i = 0; i < N; ++i) + result |= data[i]; + + return result; + } + + const bool_vec not()const + { + bool_vec result; + + for (int i = 0; i < N; ++i) + result.data[i]=!data[i]; + + return result; + } + };//template class bool_vec + + #define BVEC_BEGIN(N) class bvec##N:public bool_vec \ + { \ + public: \ + bvec##N(){} \ + bvec##N(const bvec##N &b){hgl_cpy(this->data,b.data,N);} \ + bvec##N(const bool *b){hgl_cpy(this->data,b,N);} \ + ~bvec##N()=default; + + #define BVEC_END }; + + BVEC_BEGIN(2) + bvec2(const bool &x,const bool &y){data[0]=x;data[1]=y;} + BVEC_END + + BVEC_BEGIN(3) + bvec3(const bool &x,const bool &y,const bool &z){data[0]=x;data[1]=y;data[2]=z;} + BVEC_END + + BVEC_BEGIN(4) + bvec4(const bool &x,const bool &y,const bool &z,const bool &w){data[0]=x;data[1]=y;data[2]=z;data[3]=w;} + BVEC_END + + #undef BVEC_BEGIN + #undef BVEC_END +}//namespace hgl +#endif//HGL_MATH_BVEC_INCLUDE