bitSet: add logical_and()/_or()

Basic logical operations for tests
where a temporary can be avoided.
This commit is contained in:
Michael Davidsaver
2016-03-02 18:11:33 -05:00
parent 28b5dd0163
commit a3c57a5077
3 changed files with 47 additions and 4 deletions

View File

@@ -196,6 +196,20 @@ namespace epics { namespace pvData {
return words.size() * BITS_PER_WORD;
}
bool BitSet::logical_and(const BitSet& set) const
{
size_t nwords = std::min(words.size(), set.words.size());
for(size_t i=0; i<nwords; i++) {
if(words[i] & set.words[i])
return true;
}
return false;
}
bool BitSet::logical_or(const BitSet& set) const
{
return !words.empty() || !set.words.empty();
}
BitSet& BitSet::operator&=(const BitSet& set) {
// Check for self-assignment!
if (this == &set) return *this;

View File

@@ -167,8 +167,13 @@ namespace epics { namespace pvData {
*/
uint32 size() const;
//! Returns true if any bit is set in both *this and other
bool logical_and(const BitSet& other) const;
//! Returns true if any bit is set in both *this or other
bool logical_or(const BitSet& other) const;
/**
* Performs a logical <b>AND</b> of this target bit set with the
* Performs a bitwise <b>AND</b> of this target bit set with the
* argument bit set. This bit set is modified so that each bit in it
* has the value @c true if and only if it both initially
* had the value @c true and the corresponding bit in the
@@ -179,7 +184,7 @@ namespace epics { namespace pvData {
BitSet& operator&=(const BitSet& set);
/**
* Performs a logical <b>OR</b> of this bit set with the bit set
* Performs a bitwise <b>OR</b> of this bit set with the bit set
* argument. This bit set is modified so that a bit in it has the
* value @c true if and only if it either already had the
* value @c true or the corresponding bit in the bit set
@@ -190,7 +195,7 @@ namespace epics { namespace pvData {
BitSet& operator|=(const BitSet& set);
/**
* Performs a logical <b>XOR</b> of this bit set with the bit set
* Performs a bitwise <b>XOR</b> of this bit set with the bit set
* argument. This bit set is modified so that a bit in it has the
* value @c true if and only if one of the following
* statements holds:

View File

@@ -164,6 +164,29 @@ static void testOperators()
testOk(toString(b3) == "{1}", "%s == {1}", toString(b3).c_str());
}
static void testLogical()
{
BitSet A, B;
testOk1(!A.logical_and(B));
testOk1(!A.logical_or(B));
A.set(41);
testOk1(!A.logical_and(B));
testOk1(A.logical_or(B));
A.set(42);
testOk1(!A.logical_and(B));
testOk1(A.logical_or(B));
B.set(41);
testOk1(A.logical_and(B));
testOk1(A.logical_or(B));
}
static void tofrostring(const BitSet& in, const char *expect, size_t elen, int byteOrder)
{
{
@@ -271,9 +294,10 @@ static void testSerialize()
MAIN(testBitSet)
{
testPlan(79);
testPlan(87);
testGetSetClearFlip();
testOperators();
testLogical();
testSerialize();
return testDone();
}