• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes definitions for a bit-set.
32  */
33 
34 #ifndef BIT_SET_HPP_
35 #define BIT_SET_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include "common/clearable.hpp"
40 #include "common/equatable.hpp"
41 #include "common/numeric_limits.hpp"
42 
43 namespace ot {
44 
45 /**
46  * @addtogroup core-bit-set
47  *
48  * @brief
49  *   This module includes definitions for bit-set.
50  *
51  * @{
52  */
53 
54 /**
55  * Represents a bit-set.
56  *
57  * @tparam kNumBits  Specifies the number of bits.
58  */
59 template <uint16_t kNumBits> class BitSet : public Equatable<BitSet<kNumBits>>, public Clearable<BitSet<kNumBits>>
60 {
61 public:
62     /**
63      * Indicates whether a given bit index is contained in the set.
64      *
65      * The caller MUST ensure that @p aIndex is smaller than `kNumBits`. Otherwise, the behavior of this method is
66      * undefined.
67      *
68      * @param[in] aIndex  The bit index to check
69      *
70      * @retval TRUE   If the bit index @p aIndex is contained in the set.
71      * @retval FALSE  If the bit index @p aIndex is not contained in the set.
72      */
Has(uint16_t aIndex) const73     bool Has(uint16_t aIndex) const { return (mMask[aIndex / 8] & BitMaskFor(aIndex)) != 0; }
74 
75     /**
76      * Adds the given bit index to the set.
77      *
78      * The caller MUST ensure that @p aIndex is smaller than `kNumBits`. Otherwise, the behavior of this method is
79      * undefined.
80      *
81      * @param[in] aIndex  The bit index to add.
82      */
Add(uint16_t aIndex)83     void Add(uint16_t aIndex) { mMask[aIndex / 8] |= BitMaskFor(aIndex); }
84 
85     /**
86      * Removes the given bit index from the set.
87      *
88      * The caller MUST ensure that @p aIndex is smaller than `kNumBits`. Otherwise, the behavior of this method is
89      * undefined.
90      *
91      * @param[in] aIndex  The bit index to remove.
92      */
Remove(uint16_t aIndex)93     void Remove(uint16_t aIndex) { mMask[aIndex / 8] &= ~BitMaskFor(aIndex); }
94 
95     /**
96      * Updates the set by either adding or removing the given bit index.
97      *
98      * The caller MUST ensure that @p aIndex is smaller than `kNumBits`. Otherwise, the behavior of this method is
99      * undefined.
100      *
101      * @param[in] aIndex  The bit index.
102      * @param[in] aToAdd  Boolean indicating whether to add (when set to TRUE) or to remove (when set to FALSE).
103      */
Update(uint16_t aIndex,bool aToAdd)104     void Update(uint16_t aIndex, bool aToAdd) { aToAdd ? Add(aIndex) : Remove(aIndex); }
105 
106     /**
107      * Indicates whether or not the set is empty.
108      *
109      * @retval TRUE   If the set is empty.
110      * @retval FALSE  If the set is not empty.
111      */
IsEmpty(void) const112     bool IsEmpty(void) const
113     {
114         bool isEmpty = true;
115 
116         for (uint8_t byte : mMask)
117         {
118             if (byte != 0)
119             {
120                 isEmpty = false;
121                 break;
122             }
123         }
124 
125         return isEmpty;
126     }
127 
128 private:
BitMaskFor(uint16_t aIndex)129     static uint8_t BitMaskFor(uint16_t aIndex) { return (0x80 >> (aIndex & 7)); }
130 
131     uint8_t mMask[BytesForBitSize(kNumBits)];
132 };
133 
134 /**
135  * @}
136  */
137 
138 } // namespace ot
139 
140 #endif // BIT_SET_HPP_
141