HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
chessboard-types-squareset.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2024-2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__CHESSBOARD_TYPES_SQUARESET_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__CHESSBOARD_TYPES_SQUARESET_H_INCLUDED
19
20#include "chessboard-types.h"
21
22#include <atomic>
23#include <bit>
24#include <cassert>
25#include <cstdint>
26#include <type_traits>
27
28
30{
31
35{
36private:
39 std::uint64_t m_bitmask { };
40
41public:
43 constexpr inline SquareSet() = default;
44
48 constexpr inline SquareSet(std::same_as<Square> auto... squares) noexcept :
49 m_bitmask { }
50 {
51 std::uint64_t bitmask { };
52 for (auto sq : std::initializer_list<Square>{ squares... })
53 {
54 const auto sqRaw { static_cast<SquareUnderlyingType>(sq) };
55
56 assert(sqRaw < 64U);
57 [[assume(sqRaw < 64U)]];
58
59 bitmask |= UINT64_C(1) << sqRaw;
60 }
61
62 m_bitmask = bitmask;
63 }
64
66 constexpr ~SquareSet() = default;
67
69 constexpr inline SquareSet(const SquareSet &) = default;
70
72 constexpr inline SquareSet(SquareSet &&) = default;
73
75 constexpr inline SquareSet &operator = (const SquareSet &) = default;
76
78 constexpr inline SquareSet &operator = (SquareSet &&) = default;
79
81 explicit constexpr inline SquareSet(std::uint64_t mask) noexcept :
82 m_bitmask { mask }
83 {
84 }
85
87 explicit constexpr inline operator std::uint64_t() const noexcept
88 {
89 return m_bitmask;
90 }
91
95 constexpr inline std::uint_fast8_t popcount() const noexcept
96 {
97 return std::popcount(m_bitmask);
98 }
99
105 constexpr inline Square firstSquare() const noexcept
106 {
107 return Square(std::countr_zero(m_bitmask));
108 }
109
115 constexpr inline Square lastSquare() const noexcept
116 {
117 SquareUnderlyingType tmp = 63U - std::countl_zero(m_bitmask);
118 return tmp <= 63U ? Square(tmp) : Square::NONE;
119 }
120
124 constexpr inline SquareSet removeFirstSquare() const noexcept
125 {
126 return SquareSet { m_bitmask & (m_bitmask - 1U) };
127 }
128
139 constexpr inline SquareSet operator & (SquareSet other) const noexcept
140 {
141 return SquareSet { m_bitmask & other.m_bitmask };
142 }
143
154 constexpr inline SquareSet operator | (SquareSet other) const noexcept
155 {
156 return SquareSet { m_bitmask | other.m_bitmask };
157 }
158
174 constexpr inline SquareSet operator ^ (SquareSet other) const noexcept
175 {
176 return SquareSet { m_bitmask ^ other.m_bitmask };
177 }
178
183 constexpr inline SquareSet &operator &= (SquareSet other) noexcept
184 {
185 m_bitmask &= other.m_bitmask;
186 return (*this);
187 }
188
193 constexpr inline SquareSet &operator |= (SquareSet other) noexcept
194 {
195 m_bitmask |= other.m_bitmask;
196 return (*this);
197 }
198
203 constexpr inline SquareSet &operator ^= (SquareSet other) noexcept
204 {
205 m_bitmask ^= other.m_bitmask;
206 return (*this);
207 }
208
218 constexpr inline SquareSet operator ~ () const noexcept
219 {
220 return SquareSet { ~m_bitmask };
221 }
222
242 constexpr inline SquareSet operator << (std::uint_fast8_t shift) const noexcept
243 {
244 return SquareSet { m_bitmask << shift };
245 }
246
266 constexpr inline SquareSet operator >> (std::uint_fast8_t shift) const noexcept
267 {
268 return SquareSet { m_bitmask >> shift };
269 }
270
287 constexpr inline SquareSet rotl(std::int_fast8_t shift) const noexcept
288 {
289 return SquareSet { std::rotl(m_bitmask, shift) };
290 }
291
308 constexpr inline SquareSet rotr(std::int_fast8_t shift) const noexcept
309 {
310 return SquareSet { std::rotr(m_bitmask, shift) };
311 }
312
317 constexpr inline SquareSet operator <<= (std::uint_fast8_t shift) noexcept
318 {
319 m_bitmask <<= shift;
320 return (*this);
321 }
322
327 constexpr inline SquareSet operator >>= (std::uint_fast8_t shift) noexcept
328 {
329 m_bitmask >>= shift;
330 return (*this);
331 }
332
336 constexpr inline SquareSet flipVert() const noexcept
337 {
338 return SquareSet { std::byteswap(m_bitmask) };
339 }
340
346 constexpr inline bool operator == (SquareSet other) const noexcept
347 {
348 return m_bitmask == other.m_bitmask;
349 }
350
356 constexpr inline bool operator != (SquareSet other) const noexcept
357 {
358 return m_bitmask != other.m_bitmask;
359 }
360
397 SquareSet parallelExtract(SquareSet extractMask) noexcept;
398
447 SquareSet parallelDeposit(SquareSet extractMask) noexcept;
448
452 static constexpr inline SquareSet none() noexcept
453 {
454 return SquareSet { };
455 }
456
460 static constexpr inline SquareSet all() noexcept
461 {
462 return SquareSet { 0xFF'FF'FF'FF'FF'FF'FF'FFU };
463 }
464
471 static constexpr inline SquareSet column(RowColumn col) noexcept
472 {
473 assert(col <= 7U);
474 [[assume(col <= 7U)]];
475
476 return SquareSet { std::uint64_t { 0x01'01'01'01'01'01'01'01U } << col };
477 }
478
485 static constexpr inline SquareSet row(RowColumn row) noexcept
486 {
487 assert(row <= 7U);
488 [[assume(row <= 7U)]];
489
490 return SquareSet { std::uint64_t { 0x00'00'00'00'00'00'00'FFU } << (row * 8U) };
491 }
492
497 static constexpr inline SquareSet square(Square sq) noexcept
498 {
499 assert(static_cast<SquareUnderlyingType>(sq) <= 63U);
500 [[assume(static_cast<SquareUnderlyingType>(sq) <= 63U)]];
501
502 return SquareSet { std::uint64_t { 1U } << static_cast<SquareUnderlyingType>(sq) };
503 }
504
509 static constexpr inline SquareSet squareOrNone(Square sq) noexcept
510 {
511 const std::uint64_t bitToShift { sq < Square::NONE };
512 return SquareSet { std::rotl(bitToShift, static_cast<SquareUnderlyingType>(sq)) };
513 }
514
520 static constexpr inline SquareSet square(RowColumn col, RowColumn row) noexcept
521 {
522 assert(col <= 7U);
523 assert(row <= 7U);
524
525 const auto shift { col + (row * 8U) };
526 [[assume(shift <= 63U)]];
527
528 return SquareSet { std::uint64_t { 1U } << shift };
529 }
530
532 constexpr inline bool isMember(Square sq) const noexcept
533 {
534 assert(static_cast<std::uint8_t>(sq) <= 63U);
535 [[assume(static_cast<std::uint8_t>(sq) <= 63U)]];
536
537 return (m_bitmask & (std::uint64_t { 1U } << static_cast<SquareUnderlyingType>(sq))) != 0U;
538 }
539
541 constexpr inline SquareSet allIfNone() const noexcept
542 {
544 }
545
547 constexpr inline SquareSet allIfAny() const noexcept
548 {
550 }
551};
552
553}
554
582#define SQUARESET_ENUMERATE_INTERNAL(sq, squareSet, tmpMask, ...) \
583 do \
584 { \
585 SquareSet tmpMask { squareSet }; \
586 \
587 while (true) \
588 { \
589 const Square sq { tmpMask.firstSquare() }; \
590 \
591 if (sq > Square::H8) \
592 break; \
593 \
594 tmpMask &= ~SquareSet::square(sq); \
595 \
596 __VA_ARGS__; \
597 } \
598 } \
599 while (false)
600
623#define SQUARESET_ENUMERATE(sq, squareSet, ...) \
624 SQUARESET_ENUMERATE_INTERNAL(sq, squareSet, squareset_enumerate_internal_mask, __VA_ARGS__)
625
626#endif
Set of squares. Implemented using a bit-mask.
Definition chessboard-types-squareset.h:35
static constexpr SquareSet column(RowColumn col) noexcept
Returns a set of squares in column number col.
Definition chessboard-types-squareset.h:471
constexpr SquareSet operator&(SquareSet other) const noexcept
Returns an intersection with another square set.
Definition chessboard-types-squareset.h:139
constexpr SquareSet(std::uint64_t mask) noexcept
Cast operator.
Definition chessboard-types-squareset.h:81
constexpr SquareSet rotr(std::int_fast8_t shift) const noexcept
Rotates right element-wise (circular shift)
Definition chessboard-types-squareset.h:308
constexpr SquareSet()=default
Default constructor (empty set)
SquareSet parallelExtract(SquareSet extractMask) noexcept
Extracts squares from *this using extraction mask. The extracted squares are mapped as follows:
SquareSet parallelDeposit(SquareSet extractMask) noexcept
Maps squares from *this using extraction mask. Squares are mapped as follows: mapped as follows:
constexpr SquareSet allIfAny() const noexcept
Conditional.
Definition chessboard-types-squareset.h:547
constexpr SquareSet & operator|=(SquareSet other) noexcept
Assignment operator (union). Shorthand for x = x | other.
Definition chessboard-types-squareset.h:193
constexpr SquareSet allIfNone() const noexcept
Conditional.
Definition chessboard-types-squareset.h:541
constexpr Square firstSquare() const noexcept
Returns the first (lowest-value) square in the set or Square::NONE if the set is empty.
Definition chessboard-types-squareset.h:105
static constexpr SquareSet squareOrNone(Square sq) noexcept
Returns a set of 0 or 1 squares.
Definition chessboard-types-squareset.h:509
constexpr SquareSet(std::same_as< Square > auto... squares) noexcept
Constructor from a list of Squares.
Definition chessboard-types-squareset.h:48
constexpr SquareSet(SquareSet &&)=default
Default move constructor.
static constexpr SquareSet all() noexcept
Returns a set of all squares.
Definition chessboard-types-squareset.h:460
constexpr SquareSet rotl(std::int_fast8_t shift) const noexcept
Rotates left element-wise (circular shift)
Definition chessboard-types-squareset.h:287
constexpr std::uint_fast8_t popcount() const noexcept
Returns the number of squares in the set.
Definition chessboard-types-squareset.h:95
constexpr SquareSet & operator=(const SquareSet &)=default
Default copy assignment.
static constexpr SquareSet square(Square sq) noexcept
Returns a set of single square.
Definition chessboard-types-squareset.h:497
std::uint64_t m_bitmask
The set of squares. Bit N set in the mask represents square N included in the set....
Definition chessboard-types-squareset.h:39
static constexpr SquareSet row(RowColumn row) noexcept
Returns a set of squares in row number row.
Definition chessboard-types-squareset.h:485
static constexpr SquareSet square(RowColumn col, RowColumn row) noexcept
Returns a set of single square specified by column and row numbers.
Definition chessboard-types-squareset.h:520
constexpr SquareSet operator<<=(std::uint_fast8_t shift) noexcept
Assignment operator (left shift). Shorthand for x = x << shift.
Definition chessboard-types-squareset.h:317
constexpr SquareSet removeFirstSquare() const noexcept
Returns a square set with the first square (if any) removed.
Definition chessboard-types-squareset.h:124
constexpr SquareSet operator<<(std::uint_fast8_t shift) const noexcept
Shifts left element-wise.
Definition chessboard-types-squareset.h:242
constexpr Square lastSquare() const noexcept
Returns the last (highest-value) square in the set or Square::NONE if the set is empty.
Definition chessboard-types-squareset.h:115
constexpr SquareSet & operator^=(SquareSet other) noexcept
Assignment operator (union). Shorthand for x = x ^ other.
Definition chessboard-types-squareset.h:203
constexpr bool operator!=(SquareSet other) const noexcept
Comparison operator (non-equality). Returns true if *this does not equal to other.
Definition chessboard-types-squareset.h:356
constexpr SquareSet operator>>(std::uint_fast8_t shift) const noexcept
Shifts right element-wise.
Definition chessboard-types-squareset.h:266
constexpr SquareSet operator>>=(std::uint_fast8_t shift) noexcept
Assignment operator (right shift). Shorthand for x = x >> shift.
Definition chessboard-types-squareset.h:327
constexpr SquareSet operator~() const noexcept
Returns the complement of *this.
Definition chessboard-types-squareset.h:218
constexpr SquareSet operator|(SquareSet other) const noexcept
Returns an union with another square set.
Definition chessboard-types-squareset.h:154
constexpr SquareSet(const SquareSet &)=default
Default copy constructor.
constexpr bool operator==(SquareSet other) const noexcept
Comparison operator (equality). Returns true if *this equals to other.
Definition chessboard-types-squareset.h:346
constexpr ~SquareSet()=default
Default destructor.
constexpr SquareSet & operator&=(SquareSet other) noexcept
Assignment operator (union). Shorthand for x = x & other.
Definition chessboard-types-squareset.h:183
constexpr SquareSet flipVert() const noexcept
Flips the squares vertically (upside down)
Definition chessboard-types-squareset.h:336
static constexpr SquareSet none() noexcept
Returns an empty set.
Definition chessboard-types-squareset.h:452
constexpr bool isMember(Square sq) const noexcept
Checks whether a valid square is in the set.
Definition chessboard-types-squareset.h:532
constexpr SquareSet operator^(SquareSet other) const noexcept
Returns an exclusive or with another square set. Formally, when square s is included in exactly one o...
Definition chessboard-types-squareset.h:174
std::uint_fast8_t SquareUnderlyingType
Underlying type of Square
Definition chessboard-types.h:38
Square
Named square.
Definition chessboard-types.h:122
RowColumnUnderlyingType RowColumn
Row/column coordinate type. Valid range: [0, 7] signifying ranks 1–8 (as rows) or files A–H (as colum...
Definition chessboard-types.h:92
Definition chessboard-types-squareset.h:30