TrinityCore
UpdateMask.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef UpdateMask_h__
19#define UpdateMask_h__
20
21#include "Define.h"
22#include <algorithm>
23
25{
26 inline constexpr std::size_t GetBlockIndex(std::size_t bit) { return bit / 32u; }
27 inline constexpr uint32 GetBlockFlag(std::size_t bit) { return 1u << (bit % 32u); }
28}
29
30template<uint32 Bits>
32{
33public:
34 static constexpr uint32 BlockCount = (Bits + 31) / 32;
35 static constexpr uint32 BlocksMaskCount = (BlockCount + 31) / 32;
36
38 {
39 std::fill(std::begin(_blocksMask), std::end(_blocksMask), 0);
40 std::fill(std::begin(_blocks), std::end(_blocks), 0);
41 }
42
43 UpdateMask(std::initializer_list<uint32> init)
44 {
45 InitFromBlocks(init.begin(), init.size());
46 }
47
49 {
50 return _blocksMask[index];
51 }
52
53 uint32 GetBlock(uint32 index) const
54 {
55 return _blocks[index];
56 }
57
58 bool operator[](uint32 index) const
59 {
60 return (_blocks[index / 32] & (1 << (index % 32))) != 0;
61 }
62
63 bool IsAnySet() const
64 {
65 return std::any_of(std::begin(_blocksMask), std::end(_blocksMask), [](uint32 blockMask)
66 {
67 return blockMask != 0;
68 });
69 }
70
71 void Reset(uint32 index)
72 {
73 std::size_t blockIndex = UpdateMaskHelpers::GetBlockIndex(index);
74 if (!(_blocks[blockIndex] &= ~UpdateMaskHelpers::GetBlockFlag(index)))
76 }
77
78 void ResetAll()
79 {
80 std::fill(std::begin(_blocksMask), std::end(_blocksMask), 0);
81 std::fill(std::begin(_blocks), std::end(_blocks), 0);
82 }
83
84 void Set(uint32 index)
85 {
86 std::size_t blockIndex = UpdateMaskHelpers::GetBlockIndex(index);
87 _blocks[blockIndex] |= UpdateMaskHelpers::GetBlockFlag(index);
89 }
90
91 void SetAll()
92 {
93 std::fill(std::begin(_blocksMask), std::end(_blocksMask), 0xFFFFFFFF);
94 if (BlocksMaskCount % 32)
95 {
96 constexpr uint32 unused = 32 - (BlocksMaskCount % 32);
97 _blocksMask[BlocksMaskCount - 1] &= (0xFFFFFFFF >> unused);
98 }
99 std::fill(std::begin(_blocks), std::end(_blocks), 0xFFFFFFFF);
100 if (BlockCount % 32)
101 {
102 constexpr uint32 unused = 32 - (BlockCount % 32);
103 _blocks[BlockCount - 1] &= (0xFFFFFFFF >> unused);
104 }
105 }
106
108 {
109 for (uint32 i = 0; i < BlocksMaskCount; ++i)
110 _blocksMask[i] &= right._blocksMask[i];
111
112 for (uint32 i = 0; i < BlockCount; ++i)
113 if (!(_blocks[i] &= right._blocks[i]))
115
116 return *this;
117 }
118
120 {
121 for (std::size_t i = 0; i < BlocksMaskCount; ++i)
122 _blocksMask[i] |= right._blocksMask[i];
123
124 for (std::size_t i = 0; i < BlockCount; ++i)
125 _blocks[i] |= right._blocks[i];
126
127 return *this;
128 }
129
130private:
131 void InitFromBlocks(uint32 const* input, uint32 size)
132 {
133 std::fill(std::begin(_blocksMask), std::end(_blocksMask), 0);
134
135 uint32 block = 0;
136 for (; block < size; ++block)
137 if ((_blocks[block] = input[block]) != 0)
139
140 for (; block < BlockCount; ++block)
141 _blocks[block] = 0;
142 }
143
146};
147
148template<uint32 Bits>
150{
151 UpdateMask<Bits> result = left;
152 result &= right;
153 return result;
154}
155
156template<uint32 Bits>
158{
159 UpdateMask<Bits> result = left;
160 result |= right;
161 return result;
162}
163
164#endif // UpdateMask_h__
uint32_t uint32
Definition: Define.h:142
UpdateMask< Bits > operator|(UpdateMask< Bits > const &left, UpdateMask< Bits > const &right)
Definition: UpdateMask.h:157
UpdateMask< Bits > operator&(UpdateMask< Bits > const &left, UpdateMask< Bits > const &right)
Definition: UpdateMask.h:149
uint32 GetBlock(uint32 index) const
Definition: UpdateMask.h:53
uint32 _blocksMask[BlocksMaskCount]
Definition: UpdateMask.h:144
bool operator[](uint32 index) const
Definition: UpdateMask.h:58
static constexpr uint32 BlockCount
Definition: UpdateMask.h:34
uint32 GetBlocksMask(uint32 index) const
Definition: UpdateMask.h:48
void Reset(uint32 index)
Definition: UpdateMask.h:71
static constexpr uint32 BlocksMaskCount
Definition: UpdateMask.h:35
void InitFromBlocks(uint32 const *input, uint32 size)
Definition: UpdateMask.h:131
void Set(uint32 index)
Definition: UpdateMask.h:84
bool IsAnySet() const
Definition: UpdateMask.h:63
void ResetAll()
Definition: UpdateMask.h:78
UpdateMask & operator|=(UpdateMask const &right)
Definition: UpdateMask.h:119
UpdateMask & operator&=(UpdateMask const &right)
Definition: UpdateMask.h:107
void SetAll()
Definition: UpdateMask.h:91
UpdateMask(std::initializer_list< uint32 > init)
Definition: UpdateMask.h:43
uint32 _blocks[BlockCount]
Definition: UpdateMask.h:145
constexpr std::size_t size()
Definition: UpdateField.h:796
constexpr std::size_t GetBlockIndex(std::size_t bit)
Definition: UpdateMask.h:26
constexpr uint32 GetBlockFlag(std::size_t bit)
Definition: UpdateMask.h:27