PieceIterator.h
Go to the documentation of this file.
1 // cwchessboard -- A C++ chessboard tool set
2 //
3 //! @file PieceIterator.h This file contains the definition of class PieceIterator.
4 //
5 // Copyright (C) 2008, by
6 //
7 // Carlo Wood, Run on IRC <carlo@alinoe.com>
8 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
9 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 2 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef PIECEITERATOR_H
25 #define PIECEITERATOR_H
26 
27 #ifndef USE_PCH
28 #include <iterator>
29 #endif
30 
31 #include "Piece.h"
32 
33 namespace cwchess {
34 
35 class ChessPosition;
36 
37 /** @brief Non-mutable iterator over selective chess pieces in a chess position.
38 *
39  * This iterator iterates over bits in a given BitBoard, passed during creation,
40  * and returns the Piece at the given position when dereferenced.
41 * /
42 class PieceIterator : public std::iterator<std::bidirectional_iterator_tag, Piece> {
43  protected:
44  ChessPosition const* M_chess_position; //!< The underlaying chess position.
45  BitBoard M_pieces; //!< The pieces that the iterator will iterate over.
46  Index M_current_index; //!< The index to the current piece, or 64 if this iterator points one passed the end.
47 
48  public:
49  /** @name Constructors* /
50  //@{
51 
52  // Default Constructible.
53  /** @brief Construct the corresponding one-passed-the-end iterator.
54  *
55  * @sa ChessPosition::piece_end(void)
56  * /
57  PieceIterator(void) : M_chess_position(NULL), M_current_index(index_end) { }
58 
59  /** @brief Construct the corresponding one-before-the-beginning iterator.
60  * /
61  PieceIterator(int) : M_chess_position(NULL), M_current_index(index_pre_begin) { }
62 
63  // Assignable.
64  //! @brief Copy-constructor.
66  M_chess_position(iter.M_chess_position), M_pieces(iter.M_pieces), M_current_index(iter.M_current_index) { }
67 
68  /** @brief Construct a fully initialized PieceIterator.
69  *
70  * @param chess_position : The ChessPosition that we will retrieve the Pieces from. It is only used when the iterator is dereferenced.
71  * @param pieces : A BitBoard with bits set for each square that the iterator should visit.
72  *
73  * This iterator is initialized to point at the beginning (the least significant bit).
74  * A typical loop would look as follows:
75  *
76  * \code
77  * PieceIterator const piece_end;
78  * for (PieceIterator piece_iter(chess_position, bitboard); piece_iter != piece_end; ++piece_iter)
79  * {
80  * // Use piece_iter.index(), the square that the piece is standing on, or
81  * // access the Piece directly through piece_iter->.
82  * }
83  * \endcode
84  *
85  * which will run over all bits set in bitboard.
86  *
87  * @sa ChessPosition::piece_begin, ChessPosition::all
88  * /
89  PieceIterator(ChessPosition const* chess_position, BitBoard pieces) :
90  M_chess_position(chess_position), M_pieces(pieces), M_current_index(get_first_bitindex()) { }
91 
92  /** @brief Construct a fully initialized PieceIterator.
93  *
94  * @param chess_position : The ChessPosition that we will retrieve the Pieces from.
95  * @param pieces : The pieces, a BitBoard with bits set for each square that the iterator should visit.
96  *
97  * This iterator is initialized to point at the end rather than the beginning.
98  * A typical loop would look as follows:
99  *
100  * \code
101  * PieceIterator const piece_end(0);
102  * for (PieceIterator piece_iter(chess_position, bitboard, 0); piece_iter != piece_end; --piece_iter)
103  * {
104  * // Use piece_iter.index(), the square that the piece is standing on, or
105  * // access the Piece directly through piece_iter->.
106  * }
107  * \endcode
108  * /
109  PieceIterator(ChessPosition const* chess_position, BitBoard pieces, int) :
110  M_chess_position(chess_position), M_pieces(pieces), M_current_index(get_last_bitindex()) { }
111 
112  //@}
113 
114  /** @name Assignment operator* /
115  //@{
116 
117  // Assignable.
118  //! @brief Assign from another PieceIterator.
120  { M_chess_position = iter.M_chess_position; M_pieces = iter.M_pieces; M_current_index = iter.M_current_index; return* this; }
121 
122  //@}
123 
124  /** @name Comparison operators* /
125  //@{
126 
127  // Equality Comparable.
128  //! Return TRUE if the current index of this PieceIterator and \a iter are equal.
129  bool operator==(PieceIterator const& iter) const { return M_current_index == iter.M_current_index; }
130 
131  //! Return TRUE if the current index of this PieceIterator and \a iter differ.
132  bool operator!=(PieceIterator const& iter) const { return M_current_index != iter.M_current_index; }
133 
134  //@}
135 
136  /** @name Accessors* /
137  //@{
138 
139  // Dereferencable.
140  //! Return the Piece that stands on the current index.
141  Piece operator*() const;
142 
143  //! Return a pointer to the Piece standing on the current index.
144  Piece const* operator->(void) const;
145 
146  //! Return the current index.
147  Index const& index(void) const { return M_current_index; }
148 
149  //@}
150 
151  /** @name Increment and decrement operators* /
152  //@{
153 
154  // Bi-directional iterator.
155  PieceIterator& operator++() { M_current_index.next_bit_in(M_pieces()); return* this; }
156  PieceIterator operator++(int) { PieceIterator result(*this); operator++(); return result; }
157  PieceIterator& operator--() { M_current_index.prev_bit_in(M_pieces()); return* this; }
158  PieceIterator operator--(int) { PieceIterator result(*this); operator--(); return result; }
159 
160  //@}
161 
162  private:
163  Index get_first_bitindex(void) const { Index result(index_pre_begin); result.next_bit_in(M_pieces()); return result; }
164  Index get_last_bitindex(void) const { Index result(index_end); result.prev_bit_in(M_pieces()); return result; }
165 };
166 
167 } // namespace cwchess
168 
169 #endif // PIECEITERATOR_H
A namespace for all chess related objects that are not related to the GUI.
Definition: Array.h:39
void prev_bit_in(uint64_t mask)
Retreat Index to the previous bit that is set.
Definition: Index.h:432
Index M_current_index
The index to the current piece, or 64 if this iterator points one passed the end. ...
Definition: PieceIterator.h:46
void next_bit_in(uint64_t mask)
Advance the index to the next bit that is set in mask.
Definition: Index.h:360
PieceIterator(ChessPosition const* chess_position, BitBoard pieces)
Construct a fully initialized PieceIterator.
Definition: PieceIterator.h:89
PieceIterator(PieceIterator const& iter)
Copy-constructor.
Definition: PieceIterator.h:65
PieceIterator(ChessPosition const* chess_position, BitBoard pieces, int)
Construct a fully initialized PieceIterator.
A one-boolean-per-square chessboard.
Definition: BitBoard.h:266
The index of a chess square.
Definition: Index.h:211
bool operator!=(PieceIterator const& iter) const
Return TRUE if the current index of this PieceIterator and iter differ.
A particular piece on the board.
Definition: Piece.h:45
A chess position.
Definition: ChessPosition.h:50
Piece operator*() const
Return the Piece that stands on the current index.
PieceIterator(int)
Construct the corresponding one-before-the-beginning iterator.
Definition: PieceIterator.h:61
ChessPosition const * M_chess_position
The underlaying chess position.
Definition: PieceIterator.h:44
bool operator==(PieceIterator const& iter) const
Return TRUE if the current index of this PieceIterator and iter are equal.
BitBoard M_pieces
The pieces that the iterator will iterate over.
Definition: PieceIterator.h:45
IndexData const index_pre_begin
A constant representing& #39;one before the start&#39;.
Definition: Index.h:182
IndexData const index_end
A constant representing& #39;one past the end&#39;.
Definition: Index.h:186
Non-mutable iterator over selective chess pieces in a chess position.
Definition: PieceIterator.h:42
PieceIterator & operator=(PieceIterator const& iter)
Assign from another PieceIterator.
This file contains the declaration of class Piece.
Piece const * operator->(void) const
Return a pointer to the Piece standing on the current index.
PieceIterator(void)
Construct the corresponding one-passed-the-end iterator.
Definition: PieceIterator.h:57
Index const & index(void) const
Return the current index.

Copyright © 2006 - 2010 Carlo Wood.  All rights reserved.