TrinityCore
Loading...
Searching...
No Matches
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 <array>
23#include <cstring> // std::memset
24
26{
27 inline constexpr std::size_t GetBlockIndex(std::size_t bit) { return bit / 32u; }
28 inline constexpr uint32 GetBlockFlag(std::size_t bit) { return 1u << (bit % 32u); }
29}
30
31template<uint32 Bits>
33{
34public:
35 static constexpr uint32 BlockCount = (Bits + 31) / 32;
36 static constexpr uint32 BlocksMaskCount = (BlockCount + 31) / 32;
37
38 constexpr UpdateMask() : _blocksMask(), _blocks()
39 {
40 }
41
42 constexpr UpdateMask(std::array<uint32, BlockCount> const& init)
43 {
44 _blocksMask.back() = 0; // only last value of blocksMask will not be written fully
45 for (uint32 block = 0; block < BlockCount; ++block)
46 {
47 if ((_blocks[block] = init[block]) != 0)
49 else
51 }
52 }
53
54 constexpr uint32 GetBlocksMask(uint32 index) const
55 {
56 return _blocksMask[index];
57 }
58
59 constexpr uint32 GetBlock(uint32 index) const
60 {
61 return _blocks[index];
62 }
63
64 constexpr bool operator[](uint32 index) const
65 {
67 }
68
69 constexpr bool IsAnySet() const
70 {
71 for (uint32 i = 0; i < BlocksMaskCount; ++i)
72 if (_blocksMask[i])
73 return true;
74
75 return false;
76 }
77
78 constexpr void Reset(uint32 index)
79 {
80 std::size_t blockIndex = UpdateMaskHelpers::GetBlockIndex(index);
81 if (!(_blocks[blockIndex] &= ~UpdateMaskHelpers::GetBlockFlag(index)))
82 _blocksMask[UpdateMaskHelpers::GetBlockIndex(blockIndex)] &= ~UpdateMaskHelpers::GetBlockFlag(blockIndex);
83 }
84
85 constexpr void ResetAll()
86 {
87 _blocksMask = { };
88 _blocks = { };
89 }
90
91 constexpr void Set(uint32 index)
92 {
93 std::size_t blockIndex = UpdateMaskHelpers::GetBlockIndex(index);
94 _blocks[blockIndex] |= UpdateMaskHelpers::GetBlockFlag(index);
96 }
97
98 constexpr void SetAll()
99 {
100 std::memset(_blocksMask.data(), 0xFF, _blocksMask.size() * sizeof(typename decltype(_blocksMask)::value_type));
101 if constexpr (BlockCount % 32)
102 {
103 constexpr uint32 unused = 32 - (BlockCount % 32);
104 _blocksMask.back() &= (0xFFFFFFFF >> unused);
105 }
106 std::memset(_blocks.data(), 0xFF, _blocks.size() * sizeof(typename decltype(_blocks)::value_type));
107 if constexpr (Bits % 32)
108 {
109 constexpr uint32 unused = 32 - (Bits % 32);
110 _blocks.back() &= (0xFFFFFFFF >> unused);
111 }
112 }
113
114 constexpr UpdateMask& operator&=(UpdateMask const& right)
115 {
116 for (uint32 i = 0; i < BlocksMaskCount; ++i)
117 _blocksMask[i] &= right._blocksMask[i];
118
119 for (uint32 i = 0; i < BlockCount; ++i)
120 if (!(_blocks[i] &= right._blocks[i]))
122
123 return *this;
124 }
125
126 constexpr UpdateMask& operator|=(UpdateMask const& right)
127 {
128 for (std::size_t i = 0; i < BlocksMaskCount; ++i)
129 _blocksMask[i] |= right._blocksMask[i];
130
131 for (std::size_t i = 0; i < BlockCount; ++i)
132 _blocks[i] |= right._blocks[i];
133
134 return *this;
135 }
136
137private:
138 std::array<uint32, BlocksMaskCount> _blocksMask;
139 std::array<uint32, BlockCount> _blocks;
140};
141
142template<uint32 Bits>
144{
145 UpdateMask<Bits> result = left;
146 result &= right;
147 return result;
148}
149
150template<uint32 Bits>
152{
153 UpdateMask<Bits> result = left;
154 result |= right;
155 return result;
156}
157
158#endif // UpdateMask_h__
uint32_t uint32
Definition Define.h:154
constexpr UpdateMask< Bits > operator|(UpdateMask< Bits > const &left, UpdateMask< Bits > const &right)
Definition UpdateMask.h:151
constexpr UpdateMask< Bits > operator&(UpdateMask< Bits > const &left, UpdateMask< Bits > const &right)
Definition UpdateMask.h:143
constexpr UpdateMask & operator|=(UpdateMask const &right)
Definition UpdateMask.h:126
static constexpr uint32 BlockCount
Definition UpdateMask.h:35
std::array< uint32, BlockCount > _blocks
Definition UpdateMask.h:139
static constexpr uint32 BlocksMaskCount
Definition UpdateMask.h:36
std::array< uint32, BlocksMaskCount > _blocksMask
Definition UpdateMask.h:138
constexpr UpdateMask & operator&=(UpdateMask const &right)
Definition UpdateMask.h:114
constexpr uint32 GetBlock(uint32 index) const
Definition UpdateMask.h:59
constexpr void Reset(uint32 index)
Definition UpdateMask.h:78
constexpr void ResetAll()
Definition UpdateMask.h:85
constexpr void Set(uint32 index)
Definition UpdateMask.h:91
constexpr bool operator[](uint32 index) const
Definition UpdateMask.h:64
constexpr UpdateMask(std::array< uint32, BlockCount > const &init)
Definition UpdateMask.h:42
constexpr void SetAll()
Definition UpdateMask.h:98
constexpr UpdateMask()
Definition UpdateMask.h:38
constexpr uint32 GetBlocksMask(uint32 index) const
Definition UpdateMask.h:54
constexpr bool IsAnySet() const
Definition UpdateMask.h:69
constexpr std::size_t GetBlockIndex(std::size_t bit)
Definition UpdateMask.h:27
constexpr uint32 GetBlockFlag(std::size_t bit)
Definition UpdateMask.h:28