From 0281f306e1ef2478310934be634c8d3568bcb88e Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 24 Dec 2017 15:09:21 +0100 Subject: [PATCH] express: add PointerToArray.count() --- panda/src/express/pointerToArray.I | 26 ++++++++++++++++++++++++++ panda/src/express/pointerToArray.h | 8 ++++++++ panda/src/linmath/lmatrix4_src.I | 16 ++++++++++++++++ panda/src/linmath/lmatrix4_src.h | 3 +++ panda/src/linmath/lvecBase4_src.I | 19 +++++++++++++++++++ panda/src/linmath/lvecBase4_src.h | 3 +++ 6 files changed, 75 insertions(+) diff --git a/panda/src/express/pointerToArray.I b/panda/src/express/pointerToArray.I index 7bf8b89d4e..c3a10f3737 100644 --- a/panda/src/express/pointerToArray.I +++ b/panda/src/express/pointerToArray.I @@ -581,6 +581,19 @@ node_unref() const { return ((To *)(this->_void_ptr))->node_unref(); } +/** + * Counts the frequency at which the given element occurs in the vector. + */ +template +INLINE size_t PointerToArray:: +count(const Element &value) const { + if ((this->_void_ptr) != nullptr) { + return std::count(begin(), end(), value); + } else { + return 0; + } +} + /** * */ @@ -1006,6 +1019,19 @@ node_unref() const { return ((To *)(this->_void_ptr))->node_unref(); } +/** + * Counts the frequency at which the given element occurs in the vector. + */ +template +INLINE size_t ConstPointerToArray:: +count(const Element &value) const { + if ((this->_void_ptr) != nullptr) { + return std::count(begin(), end(), value); + } else { + return 0; + } +} + /** * */ diff --git a/panda/src/express/pointerToArray.h b/panda/src/express/pointerToArray.h index e3dcc2f110..762a6cb8ce 100644 --- a/panda/src/express/pointerToArray.h +++ b/panda/src/express/pointerToArray.h @@ -112,6 +112,8 @@ PUBLISHED: INLINE int get_ref_count() const; INLINE int get_node_ref_count() const; + INLINE size_t count(const Element &) const; + #ifdef HAVE_PYTHON EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags)); EXTENSION(void __releasebuffer__(PyObject *self, Py_buffer *view) const); @@ -214,6 +216,8 @@ public: INLINE void node_ref() const; INLINE bool node_unref() const; + INLINE size_t count(const Element &) const; + // Reassignment is by pointer, not memberwise as with a vector. INLINE PointerToArray & operator = (ReferenceCountedVector *ptr); @@ -266,6 +270,8 @@ PUBLISHED: INLINE int get_ref_count() const; INLINE int get_node_ref_count() const; + INLINE size_t count(const Element &) const; + #ifdef HAVE_PYTHON EXTENSION(int __getbuffer__(PyObject *self, Py_buffer *view, int flags) const); EXTENSION(void __releasebuffer__(PyObject *self, Py_buffer *view) const); @@ -341,6 +347,8 @@ PUBLISHED: INLINE void node_ref() const; INLINE bool node_unref() const; + INLINE size_t count(const Element &) const; + // Reassignment is by pointer, not memberwise as with a vector. INLINE ConstPointerToArray & operator = (ReferenceCountedVector *ptr); diff --git a/panda/src/linmath/lmatrix4_src.I b/panda/src/linmath/lmatrix4_src.I index 32250339c3..d5c16d5ce7 100644 --- a/panda/src/linmath/lmatrix4_src.I +++ b/panda/src/linmath/lmatrix4_src.I @@ -1706,3 +1706,19 @@ INLINE_LINMATH int FLOATNAME(UnalignedLMatrix4):: get_num_components() const { return 16; } + +/** + * + */ +INLINE_LINMATH bool FLOATNAME(UnalignedLMatrix4):: +operator == (const FLOATNAME(UnalignedLMatrix4) &other) const { + return memcmp(get_data(), other.get_data(), sizeof(FLOATTYPE) * 16) == 0; +} + +/** + * + */ +INLINE_LINMATH bool FLOATNAME(UnalignedLMatrix4):: +operator != (const FLOATNAME(UnalignedLMatrix4) &other) const { + return !operator == (other); +} diff --git a/panda/src/linmath/lmatrix4_src.h b/panda/src/linmath/lmatrix4_src.h index 0b9f1557e5..de2d2fbf78 100644 --- a/panda/src/linmath/lmatrix4_src.h +++ b/panda/src/linmath/lmatrix4_src.h @@ -345,6 +345,9 @@ PUBLISHED: INLINE_LINMATH const FLOATTYPE *get_data() const; INLINE_LINMATH int get_num_components() const; + INLINE_LINMATH bool operator == (const FLOATNAME(UnalignedLMatrix4) &other) const; + INLINE_LINMATH bool operator != (const FLOATNAME(UnalignedLMatrix4) &other) const; + public: typedef UNALIGNED_LINMATH_MATRIX(FLOATTYPE, 4, 4) UMatrix4; UMatrix4 _m; diff --git a/panda/src/linmath/lvecBase4_src.I b/panda/src/linmath/lvecBase4_src.I index 464cd6fa12..bdf7e917b6 100644 --- a/panda/src/linmath/lvecBase4_src.I +++ b/panda/src/linmath/lvecBase4_src.I @@ -971,3 +971,22 @@ INLINE_LINMATH const FLOATTYPE *FLOATNAME(UnalignedLVecBase4):: get_data() const { return &_v(0); } + +/** + * + */ +INLINE_LINMATH bool FLOATNAME(UnalignedLVecBase4):: +operator == (const FLOATNAME(UnalignedLVecBase4) &other) const { + return (_v(0) == other._v(0) && + _v(1) == other._v(1) && + _v(2) == other._v(2) && + _v(3) == other._v(3)); +} + +/** + * + */ +INLINE_LINMATH bool FLOATNAME(UnalignedLVecBase4):: +operator != (const FLOATNAME(UnalignedLVecBase4) &other) const { + return !operator == (other); +} diff --git a/panda/src/linmath/lvecBase4_src.h b/panda/src/linmath/lvecBase4_src.h index fdd81dad4b..d04201d988 100644 --- a/panda/src/linmath/lvecBase4_src.h +++ b/panda/src/linmath/lvecBase4_src.h @@ -243,6 +243,9 @@ PUBLISHED: INLINE_LINMATH const FLOATTYPE *get_data() const; CONSTEXPR static int get_num_components() { return 4; } + INLINE_LINMATH bool operator == (const FLOATNAME(UnalignedLVecBase4) &other) const; + INLINE_LINMATH bool operator != (const FLOATNAME(UnalignedLVecBase4) &other) const; + public: typedef FLOATTYPE numeric_type; typedef UNALIGNED_LINMATH_MATRIX(FLOATTYPE, 1, 4) UVector4;