aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-08-19 10:52:33 +0200
committerDavid Robillard <d@drobilla.net>2018-09-29 14:48:05 +0200
commitc99e14d12be7797984cca0614de28ecda86f4af4 (patch)
tree813dbf8a072f93d65fa56ef39f1d9edb710cc990
parent8eef3b64474ab7b9c5642254dc53c11e7530134e (diff)
downloadchilbert-c99e14d12be7797984cca0614de28ecda86f4af4.tar.gz
chilbert-c99e14d12be7797984cca0614de28ecda86f4af4.tar.bz2
chilbert-c99e14d12be7797984cca0614de28ecda86f4af4.zip
Factor out const bitvec operators
-rw-r--r--chilbert/BigBitVec.hpp45
-rw-r--r--chilbert/FixBitVec.hpp42
-rw-r--r--chilbert/Operations.hpp1
-rw-r--r--chilbert/Operators.hpp83
4 files changed, 84 insertions, 87 deletions
diff --git a/chilbert/BigBitVec.hpp b/chilbert/BigBitVec.hpp
index 8d073b5..5b5f408 100644
--- a/chilbert/BigBitVec.hpp
+++ b/chilbert/BigBitVec.hpp
@@ -214,14 +214,6 @@ public:
return *this;
}
- CBigBitVec operator&(const CBigBitVec& vec) const
- {
- CBigBitVec t(*this);
- t &= vec;
-
- return t;
- }
-
CBigBitVec& operator|=(const CBigBitVec& vec)
{
for (size_t i = 0; i < std::min(rackCount(), vec.rackCount()); ++i) {
@@ -231,14 +223,6 @@ public:
return *this;
}
- CBigBitVec operator|(const CBigBitVec& vec) const
- {
- CBigBitVec t(*this);
- t |= vec;
-
- return t;
- }
-
CBigBitVec& operator^=(const CBigBitVec& vec)
{
for (size_t i = 0; i < std::min(rackCount(), vec.rackCount()); ++i) {
@@ -248,21 +232,6 @@ public:
return *this;
}
- CBigBitVec operator^(const CBigBitVec& vec) const
- {
- CBigBitVec t(*this);
- t ^= vec;
-
- return t;
- }
-
- CBigBitVec operator~() const
- {
- CBigBitVec t(*this);
- t.flip();
- return t;
- }
-
CBigBitVec& operator<<=(const size_t bits)
{
if (bits == 0) {
@@ -298,13 +267,6 @@ public:
return *this;
}
- CBigBitVec operator<<(const size_t bits) const
- {
- CBigBitVec t(*this);
- t <<= bits;
- return t;
- }
-
CBigBitVec& operator>>=(const size_t bits)
{
if (bits == 0) {
@@ -341,13 +303,6 @@ public:
return *this;
}
- CBigBitVec operator>>(const size_t bits) const
- {
- CBigBitVec t(*this);
- t >>= bits;
- return t;
- }
-
/// Right-rotate by `bits` positions
CBigBitVec& rotr(const size_t bits)
{
diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp
index f782723..0eab0e9 100644
--- a/chilbert/FixBitVec.hpp
+++ b/chilbert/FixBitVec.hpp
@@ -183,26 +183,12 @@ public:
return *this;
}
- CFixBitVec operator&(const CFixBitVec& vec) const
- {
- CFixBitVec t(*this);
- t &= vec;
- return t;
- }
-
CFixBitVec& operator|=(const CFixBitVec& vec)
{
m_rack |= vec.m_rack;
return *this;
}
- CFixBitVec operator|(const CFixBitVec& vec) const
- {
- CFixBitVec t(*this);
- t |= vec;
- return t;
- }
-
CFixBitVec& operator^=(const CFixBitVec& vec)
{
m_rack ^= vec.m_rack;
@@ -215,20 +201,6 @@ public:
return *this;
}
- CFixBitVec operator^(const CFixBitVec& vec) const
- {
- CFixBitVec t(*this);
- t ^= vec;
- return t;
- }
-
- CFixBitVec operator~() const
- {
- CFixBitVec t(*this);
- t.flip();
- return t;
- }
-
CFixBitVec& operator<<=(const size_t bits)
{
assert(bits < size());
@@ -236,13 +208,6 @@ public:
return *this;
}
- CFixBitVec operator<<(const size_t bits) const
- {
- CFixBitVec t(*this);
- t <<= bits;
- return t;
- }
-
CFixBitVec& operator>>=(const size_t bits)
{
assert(bits < size());
@@ -250,13 +215,6 @@ public:
return *this;
}
- CFixBitVec operator>>(const size_t bits) const
- {
- CFixBitVec t(*this);
- t >>= bits;
- return t;
- }
-
/// Right-rotate by `bits` positions
CFixBitVec& rotr(const size_t bits)
{
diff --git a/chilbert/Operations.hpp b/chilbert/Operations.hpp
index 29641fb..37a4b03 100644
--- a/chilbert/Operations.hpp
+++ b/chilbert/Operations.hpp
@@ -19,6 +19,7 @@
#ifndef CHILBERT_OPERATIONS_HPP
#define CHILBERT_OPERATIONS_HPP
+#include "chilbert/Operators.hpp"
#include "chilbert/Traits.hpp"
#include <cassert>
diff --git a/chilbert/Operators.hpp b/chilbert/Operators.hpp
new file mode 100644
index 0000000..9ae022a
--- /dev/null
+++ b/chilbert/Operators.hpp
@@ -0,0 +1,83 @@
+/*
+ Copyright (C) 2018 David Robillard <d@drobilla.net>
+ Copyright (C) 2006-2007 Chris Hamilton <chamilton@cs.dal.ca>
+
+ This program is free software: you can redistribute it and/or modify it under
+ the terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 2 of the License, or (at your option) any later
+ version.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#ifndef CHILBERT_OPERATORS_HPP
+#define CHILBERT_OPERATORS_HPP
+
+#include "chilbert/Traits.hpp"
+
+#include <type_traits>
+
+namespace chilbert {
+
+template <class T>
+std::enable_if_t<is_bitvec_v<T>, T> operator&(const T& lhs, const T& rhs)
+{
+ T r{lhs};
+ r &= rhs;
+ return r;
+}
+
+template <class T>
+std::enable_if_t<is_bitvec_v<T>, T>
+operator|(const T& lhs, const T& rhs)
+{
+ T r{lhs};
+ r |= rhs;
+ return r;
+}
+
+template <class T>
+std::enable_if_t<is_bitvec_v<T>, T>
+operator^(const T& lhs, const T& rhs)
+{
+ T r{lhs};
+ r ^= rhs;
+ return r;
+}
+
+template <class T>
+std::enable_if_t<is_bitvec_v<T>, T>
+operator~(const T& vec)
+{
+ T r{vec};
+ r.flip();
+ return r;
+}
+
+template <class T>
+std::enable_if_t<is_bitvec_v<T>, T>
+operator<<(const T& vec, const size_t bits)
+{
+ T r{vec};
+ r <<= bits;
+ return r;
+}
+
+template <class T>
+std::enable_if_t<is_bitvec_v<T>, T>
+operator>>(const T& vec, const size_t bits)
+{
+ T r{vec};
+ r >>= bits;
+ return r;
+}
+
+} // namespace chilbert
+
+#endif