From c99e14d12be7797984cca0614de28ecda86f4af4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 19 Aug 2018 10:52:33 +0200 Subject: Factor out const bitvec operators --- chilbert/BigBitVec.hpp | 45 --------------------------- chilbert/FixBitVec.hpp | 42 ------------------------- chilbert/Operations.hpp | 1 + chilbert/Operators.hpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 87 deletions(-) create mode 100644 chilbert/Operators.hpp (limited to 'chilbert') 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 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 + Copyright (C) 2006-2007 Chris Hamilton + + 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 . +*/ + +#ifndef CHILBERT_OPERATORS_HPP +#define CHILBERT_OPERATORS_HPP + +#include "chilbert/Traits.hpp" + +#include + +namespace chilbert { + +template +std::enable_if_t, T> operator&(const T& lhs, const T& rhs) +{ + T r{lhs}; + r &= rhs; + return r; +} + +template +std::enable_if_t, T> +operator|(const T& lhs, const T& rhs) +{ + T r{lhs}; + r |= rhs; + return r; +} + +template +std::enable_if_t, T> +operator^(const T& lhs, const T& rhs) +{ + T r{lhs}; + r ^= rhs; + return r; +} + +template +std::enable_if_t, T> +operator~(const T& vec) +{ + T r{vec}; + r.flip(); + return r; +} + +template +std::enable_if_t, T> +operator<<(const T& vec, const size_t bits) +{ + T r{vec}; + r <<= bits; + return r; +} + +template +std::enable_if_t, T> +operator>>(const T& vec, const size_t bits) +{ + T r{vec}; + r >>= bits; + return r; +} + +} // namespace chilbert + +#endif -- cgit v1.2.1