[bit-vector] refactor methods (#5302)

This commit refactors BitVector to use one method for setting or
clearing masks.
This commit is contained in:
Simon Lin
2020-07-28 09:09:52 -07:00
committed by GitHub
parent 89b657cc60
commit d46c03b588
3 changed files with 17 additions and 43 deletions
+11 -22
View File
@@ -59,7 +59,7 @@ namespace ot {
* @tparam N Specifies the number of bits. * @tparam N Specifies the number of bits.
* *
*/ */
template <uint16_t N> class BitVector : public Equatable<BitVector<N>> template <uint16_t N> class BitVector : public Equatable<BitVector<N>>, public Clearable<BitVector<N>>
{ {
public: public:
/** /**
@@ -81,34 +81,23 @@ public:
* This method sets the mask of a given index. * This method sets the mask of a given index.
* *
* @param[in] aIndex The index. * @param[in] aIndex The index.
* @param[in] aValue TRUE to set the mask, or FALSE to clear the mask.
* *
*/ */
void Set(uint16_t aIndex) void Set(uint16_t aIndex, bool aValue)
{ {
OT_ASSERT(aIndex < N); OT_ASSERT(aIndex < N);
mMask[aIndex / 8] |= 0x80 >> (aIndex % 8); if (aValue)
{
mMask[aIndex / 8] |= 0x80 >> (aIndex % 8);
}
else
{
mMask[aIndex / 8] &= ~(0x80 >> (aIndex % 8));
};
} }
/**
* This method clears the mask of a given index.
*
* @param[in] aIndex The index.
*
*/
void Clear(uint16_t aIndex)
{
OT_ASSERT(aIndex < N);
mMask[aIndex / 8] &= ~(0x80 >> (aIndex % 8));
}
/**
* This method clears all indexes.
*
*/
void ClearAll(void) { memset(mMask, 0, sizeof(mMask)); }
/** /**
* This method returns if any mask is set. * This method returns if any mask is set.
* *
+2 -2
View File
@@ -642,12 +642,12 @@ bool Message::GetChildMask(uint16_t aChildIndex) const
void Message::ClearChildMask(uint16_t aChildIndex) void Message::ClearChildMask(uint16_t aChildIndex)
{ {
GetMetadata().mChildMask.Clear(aChildIndex); GetMetadata().mChildMask.Set(aChildIndex, false);
} }
void Message::SetChildMask(uint16_t aChildIndex) void Message::SetChildMask(uint16_t aChildIndex)
{ {
GetMetadata().mChildMask.Set(aChildIndex); GetMetadata().mChildMask.Set(aChildIndex, true);
} }
bool Message::IsChildPending(void) const bool Message::IsChildPending(void) const
+4 -19
View File
@@ -157,8 +157,8 @@ void Child::ClearIp6Addresses(void)
mMeshLocalIid.Clear(); mMeshLocalIid.Clear();
memset(mIp6Address, 0, sizeof(mIp6Address)); memset(mIp6Address, 0, sizeof(mIp6Address));
#if OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE #if OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE
mMlrToRegisterMask.ClearAll(); mMlrToRegisterMask.Clear();
mMlrRegisteredMask.ClearAll(); mMlrRegisteredMask.Clear();
#endif #endif
} }
@@ -318,23 +318,8 @@ void Child::SetAddressMlrState(const Ip6::Address &aAddress, MlrState aState)
addressIndex = static_cast<uint16_t>(&aAddress - mIp6Address); addressIndex = static_cast<uint16_t>(&aAddress - mIp6Address);
if (aState == kMlrStateToRegister) mMlrToRegisterMask.Set(addressIndex, aState == kMlrStateToRegister);
{ mMlrRegisteredMask.Set(addressIndex, aState == kMlrStateRegistered);
mMlrToRegisterMask.Set(addressIndex);
}
else
{
mMlrToRegisterMask.Clear(addressIndex);
}
if (aState == kMlrStateRegistered)
{
mMlrRegisteredMask.Set(addressIndex);
}
else
{
mMlrRegisteredMask.Clear(addressIndex);
}
} }
#endif // OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE #endif // OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE