Last active
December 29, 2015 06:49
-
-
Save shoooe/7632037 to your computer and use it in GitHub Desktop.
Bitboard boilerplate code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<std::size_t Cols, std::size_t Rows> | |
class bitboard { | |
private: | |
std::bitset<Cols * Rows> _bitset; | |
public: | |
using reference = std::bitboard<Cols, Rows><Cols * Rows>::reference; | |
reference operator()(std::size_t, std::size_t); | |
bitboard<Cols, Rows>& operator&=(const bitboard<Cols, Rows>&); | |
bitboard<Cols, Rows>& operator|=(const bitboard<Cols, Rows>&); | |
bitboard<Cols, Rows>& operator^=(const bitboard<Cols, Rows>&); | |
bitboard<Cols, Rows>& operator<<=(std::size_t); | |
bitboard<Cols, Rows>& operator>>=(std::size_t); | |
void reset(); | |
void flip(); | |
bitboard<Cols, Rows> operator~() const; | |
bitboard<Cols, Rows> operator<<(std::size_t) const; | |
bitboard<Cols, Rows> operator>>(std::size_t) const; | |
bool operator==(const bitboard<Cols, Rows>&) const; | |
bool operator!=(const bitboard<Cols, Rows>&) const; | |
bool operator()(std::size_t, std::size_t) const; | |
bool all() const; | |
bool none() const; | |
bool any() const; | |
constexpr std::size_t rows() const; | |
constexpr std::size_t columns() const; | |
constexpr std::size_t size() const; | |
std::size_t count() const; | |
}; | |
template<std::size_t Cols, std::size_t Rows> | |
typename bitboard<Cols, Rows>::reference bitboard<Cols, Rows>::operator()(std::size_t col, std::size_t row) { | |
return _bitset[row * Cols + col]; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows>& bitboard<Cols, Rows>::operator&=(const bitboard<Cols, Rows>& rhs) { | |
_bitset &= rhs._bitset; | |
return (*this); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows>& bitboard<Cols, Rows>::operator|=(const bitboard<Cols, Rows>& rhs) { | |
_bitset |= rhs._bitset; | |
return (*this); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows>& bitboard<Cols, Rows>::operator^=(const bitboard<Cols, Rows>& rhs) { | |
_bitset ^= rhs._bitset; | |
return (*this); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows>& bitboard<Cols, Rows>::operator<<=(std::size_t s) { | |
_bitset <<= s; | |
return (*this); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows>& bitboard<Cols, Rows>::operator>>=(std::size_t s) { | |
_bitset >>= s; | |
return (*this); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
void bitboard<Cols, Rows>::reset() { | |
_bitset.reset(); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
void bitboard<Cols, Rows>::flip() { | |
_bitset.flip(); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows> bitboard<Cols, Rows>::operator~() const { | |
bitboard<Cols, Rows> ret; | |
ret._bitset = ~ret._bitset; | |
return ret; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows> bitboard<Cols, Rows>::operator<<(std::size_t s) const { | |
bitboard<Cols, Rows> ret; | |
ret._bitset <<= s; | |
return ret; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows> bitboard<Cols, Rows>::operator>>(std::size_t s) const { | |
bitboard<Cols, Rows> ret; | |
ret._bitset >>= s; | |
return ret; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bool bitboard<Cols, Rows>::operator==(const bitboard<Cols, Rows>& rhs) const { | |
return _bitset == rhs._bitset; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bool bitboard<Cols, Rows>::operator!=(const bitboard<Cols, Rows>& rhs) const { | |
return !((*this) == rhs); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bool bitboard<Cols, Rows>::operator()(std::size_t col, std::size_t row) const { | |
return _bitset[row * Cols + col]; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bool bitboard<Cols, Rows>::all() const { | |
return _bitset.all(); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bool bitboard<Cols, Rows>::none() const { | |
return _bitset.none(); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bool bitboard<Cols, Rows>::any() const { | |
return _bitset.any(); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
constexpr std::size_t bitboard<Cols, Rows>::rows() const { | |
return Rows; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
constexpr std::size_t bitboard<Cols, Rows>::columns() const { | |
return Cols; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
constexpr std::size_t bitboard<Cols, Rows>::size() const { | |
return Cols * Rows; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
std::size_t bitboard<Cols, Rows>::count() const { | |
return _bitset.count(); | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows> operator&(const bitboard<Cols, Rows>& lhs, const bitboard<Cols, Rows>& rhs) { | |
return bitboard<Cols, Rows>(lhs) &= rhs; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows> operator|(const bitboard<Cols, Rows>& lhs, const bitboard<Cols, Rows>& rhs) { | |
return bitboard<Cols, Rows>(lhs) |= rhs; | |
} | |
template<std::size_t Cols, std::size_t Rows> | |
bitboard<Cols, Rows> operator^(const bitboard<Cols, Rows>& lhs, const bitboard<Cols, Rows>& rhs) { | |
return bitboard<Cols, Rows>(lhs) ^= rhs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment