container.h
Go to the documentation of this file.
/* carray.h
*/
#ifndef OSL_CONTAINER_H
#define OSL_CONTAINER_H
#include "osl/basic_type.h"
#include "osl/config.h"
#include "osl/bits/construct.h"
#include <algorithm>
#include <cstddef>
#include <cassert>
#include <array>
#include <type_traits>
#define CONSERVATIVE_PLAYER_ACCESS
namespace osl
{
template <typename T, size_t Capacity>
class CArray
{
public:
std::array<T,Capacity> array;
typedef typename std::remove_cv<T>::type T_simple;
T& operator[] (size_t i) {
assert(i < Capacity);
return array[i];
}
T const& operator[] (size_t i) const {
assert(i < Capacity);
return array[i];
}
T& operator[] (Player p) {
assert(1 < Capacity);
#ifndef CONSERVATIVE_PLAYER_ACCESS
// equivalent to operator[](playerToIndex(p))
return *((T*)((char *)&elements[0] +
(p & ((char *)&elements[1]-(char *)&elements[0]))));
#else
return operator[](playerToIndex(p));
#endif
}
const T& operator[] (Player p) const {
assert(1 < Capacity);
#ifndef CONSERVATIVE_PLAYER_ACCESS
return *((T*)((char *)&elements[0] +
(p & ((char *)&elements[1]-(char *)&elements[0]))));
#else
return operator[](playerToIndex(p));
#endif
}
T& operator[] (PtypeO ptypeo) {
assert(PTYPEO_SIZE <= (int)Capacity);
return operator[](ptypeOIndex(ptypeo));
}
const T& operator[] (PtypeO ptypeo) const {
assert(PTYPEO_SIZE <= (int)Capacity);
return operator[](ptypeOIndex(ptypeo));
}
typedef T value_type;
typedef typename std::array<T,Capacity>::iterator iterator;
iterator begin() { return array.begin(); }
iterator end() { return array.end(); }
void fill(const T_simple& value=T_simple()) {
array.fill(value);
}
// for nested CArray
template <class T2, class = typename std::enable_if<!std::is_convertible<T2,T_simple>::value>::type>
void fill(const T2& value=T2()) {
for (auto& a:array)
a.fill(value);
}
static size_t size() { return Capacity; }
typedef typename std::array<T,Capacity>::const_iterator const_iterator;
const_iterator begin() const { return array.begin(); }
const_iterator end() const { return array.end(); }
const_iterator cbegin() const { return array.cbegin(); }
const_iterator cend() const { return array.cend(); }
bool operator==(const CArray& other) const {
return array == other.array;
}
T& front() { return array.front(); }
T& back() { return array.back(); }
const T& front() const { return array.front(); }
const T& back() const { return array.back(); }
};
template <typename T, size_t Capacity1, size_t Capacity2>
using CArray2d = CArray<CArray<T,Capacity2>,Capacity1>;
template <typename T, size_t Capacity1, size_t Capacity2, size_t Capacity3>
using CArray3d = CArray<CArray2d<T,Capacity2,Capacity3>,Capacity1>;
namespace detail
{
template <typename T>
class FixedCapacityVectorPushBack
{
T *ptr;
T **vPtr;
#if ! (defined NDEBUG && defined MINIMAL)
T *limit;
#endif
public:
FixedCapacityVectorPushBack(T** vPtr_, T* limit_)
: ptr(*vPtr_), vPtr(vPtr_)
#if ! (defined NDEBUG && defined MINIMAL)
,limit(limit_)
#endif
{
}
~FixedCapacityVectorPushBack() {
assert( *vPtr == ptr );
*vPtr = ptr;
}
void push_back(const T& e) {
assert(ptr < limit);
assert( *vPtr == ptr );
if(misc::detail::BitCopyTraits<T>::value)
*ptr++ = e;
else
misc::construct(ptr++,e);
#ifndef NDEBUG
(*vPtr)++;
#endif
}
};
} // namespace deteail
template <typename T, size_t Capacity>
class FixedCapacityVector
{
protected:
struct Array : public CArray<T, Capacity> {}
#ifdef __GNUC__
__attribute__((__may_alias__))
#endif
;
typedef Array array_t;
T* ptr;
CArray<int64_t, (sizeof(T[Capacity])+sizeof(int64_t)-1)/sizeof(int64_t)> relements;
private:
const array_t &elements() const {
return *reinterpret_cast<const array_t*>(&relements);
}
array_t &elements() {
return *reinterpret_cast<array_t*>(&relements);
}
public:
typedef typename array_t::value_type value_type;
typedef typename array_t::iterator iterator;
typedef typename array_t::const_iterator const_iterator;
FixedCapacityVector() : ptr(&(elements()[0])) {}
explicit FixedCapacityVector(size_t size) : ptr(&(elements()[0])) {
resize(size);
}
FixedCapacityVector(FixedCapacityVector const& rhs) {
ptr= &*begin()+rhs.size();
std::uninitialized_copy(rhs.begin(),rhs.end(),begin());
}
template <class RangeIterator>
FixedCapacityVector(const RangeIterator& first, const RangeIterator& last)
: ptr(&(elements()[0])) {
push_back(first, last);
}
~FixedCapacityVector() {
misc::destroy(begin(),end());
}
FixedCapacityVector& operator=(FixedCapacityVector const& rhs) {
if (this == &rhs)
return *this;
if(size()>rhs.size()) {
iterator it=std::copy(rhs.begin(),rhs.end(),begin());
misc::destroy(it,end());
}
else {
iterator it=std::copy(&(rhs.elements()[0]),
&(rhs.elements()[0])+size(),begin());
std::uninitialized_copy(&(rhs.elements()[0])+size(),
&(rhs.elements()[0])+rhs.size(),it);
}
ptr= &*begin()+rhs.size();
return *this;
}
T& operator[] (size_t i) {
assert(i <= size());
return elements()[i];
}
iterator begin() { return &elements()[0]; }
iterator end() { return static_cast<iterator>(ptr); }
T& front() { return *begin(); }
T& back() { return *(end() - 1); }
void push_back(const T& e) {
assert(size() < Capacity);
misc::construct(ptr,e);
++ptr;
}
template <class RangeIterator>
void push_back(const RangeIterator& first, const RangeIterator& last);
void pop_back() {
--ptr;
misc::destroy(ptr+1);
}
void clear() {
size_t s=size();
ptr= &(elements()[0]);
// 該当する部分のdestructorを呼ぶ
misc::destroy(begin(),begin()+(int)s);
}
void resize(size_t new_length) {
while (size() < new_length)
push_back(T());
if (new_length < size()) {
misc::destroy(begin()+(int)new_length,end());
ptr= &(elements()[new_length]);
}
}
void erase(const T& e) {
const iterator new_end = std::remove(begin(), end(), e);
ptr= &*new_end;
misc::destroy(new_end,end());
}
void unique() {
std::sort(begin(),end());
iterator last = std::unique(begin(), end());
ptr = &*last;
misc::destroy(last,end());
}
size_t size() const { return ptr-&*begin(); }
bool empty() const { return ptr==&*begin(); }
size_t capacity() const { return Capacity; }
T const& operator[] (size_t i) const {
assert(i < size());
return elements()[i];
}
const_iterator begin() const { return &elements()[0]; }
const_iterator end() const { return ptr; }
const T& front() const { return *begin(); }
const T& back() const { return *(end() - 1); }
bool isMember(const T& e, const_iterator first, const_iterator last) const {
return std::find(first, last, e) != last;
}
bool isMember(const T& e) const {
return isMember(e, begin(), end());
}
detail::FixedCapacityVectorPushBack<T> pushBackHelper() {
return {&ptr, &*begin()+Capacity};
}
};
template <typename T, size_t C> inline
bool operator==(const FixedCapacityVector<T,C>& l, const FixedCapacityVector<T,C>& r)
{
return l.size() == r.size() && std::equal(l.begin(), l.end(), r.begin());
}
template <typename T, size_t C> inline
bool operator<(const FixedCapacityVector<T,C>& l, const FixedCapacityVector<T,C>& r)
{
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
using detail::FixedCapacityVectorPushBack;
} // namespace osl
template <typename T, size_t Capacity>
template <class RangeIterator>
void osl::FixedCapacityVector<T,Capacity>::push_back(const RangeIterator& first, const RangeIterator& last)
{
iterator insert_point = end();
std::uninitialized_copy(first, last, insert_point);
ptr += last-first;
assert(size() <= Capacity);
}
namespace osl
{
class MoveVector : public FixedCapacityVector<Move,Move::MaxUniqMoves>
{
public:
};
std::ostream& operator<<(std::ostream& os,MoveVector const& mv);
bool operator<(const MoveVector& l, const MoveVector& r);
enum { CheckOrEscapeMaxUniqMoves = Move::MaxUniqMoves/4 }; // 150
class CheckMoveVector : public FixedCapacityVector<Move,CheckOrEscapeMaxUniqMoves>
{
};
class PieceVector : public FixedCapacityVector<Piece,Piece::SIZE>
{
public:
void sortByBasic();
void sortByPtype();
};
std::ostream& operator<<(std::ostream& os,const PieceVector&);
class PtypeOSquareVector
: public FixedCapacityVector<std::pair<PtypeO,Square>,Piece::SIZE>
{
public:
void sort();
struct PtypeOSquareLessThan;
};
}
#endif /* OSL_CONTAINER_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
basic_type.h
osl::CArray
Definition container.h:20
osl::CArray::fill
void fill(const T_simple &value=T_simple())
Definition container.h:67
osl::CArray::front
const T & front() const
Definition container.h:89
osl::CArray::size
static size_t size()
Definition container.h:76
osl::CArray::end
iterator end()
Definition container.h:65
osl::CArray< Move, 8 >::const_iterator
std::array< Move, Capacity >::const_iterator const_iterator
Definition container.h:77
osl::CArray::operator[]
T & operator[](size_t i)
Definition container.h:25
osl::CArray< Move, 8 >::array
std::array< Move, Capacity > array
Definition container.h:22
osl::CArray< Move, 8 >::iterator
std::array< Move, Capacity >::iterator iterator
Definition container.h:63
osl::CArray::cbegin
const_iterator cbegin() const
Definition container.h:80
osl::CArray::front
T & front()
Definition container.h:87
osl::CArray::back
const T & back() const
Definition container.h:90
osl::CArray::fill
void fill(const T2 &value=T2())
Definition container.h:72
osl::CArray< Move, 8 >::value_type
Move value_type
Definition container.h:62
osl::CArray::cend
const_iterator cend() const
Definition container.h:81
osl::CArray::operator==
bool operator==(const CArray &other) const
Definition container.h:83
osl::CArray::end
const_iterator end() const
Definition container.h:79
osl::CArray< Move, 8 >::T_simple
std::remove_cv< Move >::type T_simple
Definition container.h:23
osl::CArray::back
T & back()
Definition container.h:88
osl::CArray::begin
const_iterator begin() const
Definition container.h:78
osl::CArray::begin
iterator begin()
Definition container.h:64
osl::CheckMoveVector
Definition container.h:301
osl::FixedCapacityVector
Definition container.h:137
osl::FixedCapacityVector< MoveLogProb, Move::MaxUniqMoves >::size
size_t size() const
Definition container.h:243
osl::FixedCapacityVector::back
const T & back() const
Definition container.h:255
osl::FixedCapacityVector::elements
const array_t & elements() const
Definition container.h:148
osl::FixedCapacityVector::operator[]
T & operator[](size_t i)
Definition container.h:193
osl::FixedCapacityVector::isMember
bool isMember(const T &e) const
Definition container.h:260
osl::FixedCapacityVector::unique
void unique()
重複する要素を取り除く
Definition container.h:236
osl::FixedCapacityVector::front
const T & front() const
Definition container.h:254
osl::FixedCapacityVector::begin
const_iterator begin() const
Definition container.h:251
osl::FixedCapacityVector::end
iterator end()
Definition container.h:199
osl::FixedCapacityVector::FixedCapacityVector
FixedCapacityVector(size_t size)
Definition container.h:160
osl::FixedCapacityVector::push_back
void push_back(const T &e)
Definition container.h:204
osl::FixedCapacityVector::value_type
array_t::value_type value_type
Definition container.h:155
osl::FixedCapacityVector::FixedCapacityVector
FixedCapacityVector()
Definition container.h:159
osl::FixedCapacityVector::array_t
Array array_t
Definition container.h:144
osl::FixedCapacityVector::front
T & front()
Definition container.h:201
osl::FixedCapacityVector::end
const_iterator end() const
Definition container.h:252
osl::FixedCapacityVector::~FixedCapacityVector
~FixedCapacityVector()
Definition container.h:172
osl::FixedCapacityVector::clear
void clear()
Definition container.h:215
osl::FixedCapacityVector::operator=
FixedCapacityVector & operator=(FixedCapacityVector const &rhs)
Definition container.h:175
osl::FixedCapacityVector::iterator
array_t::iterator iterator
Definition container.h:156
osl::FixedCapacityVector::FixedCapacityVector
FixedCapacityVector(FixedCapacityVector const &rhs)
Definition container.h:163
osl::FixedCapacityVector::resize
void resize(size_t new_length)
Definition container.h:221
osl::FixedCapacityVector::pop_back
void pop_back()
Definition container.h:211
osl::FixedCapacityVector::isMember
bool isMember(const T &e, const_iterator first, const_iterator last) const
Definition container.h:257
osl::FixedCapacityVector::back
T & back()
Definition container.h:202
osl::FixedCapacityVector::push_back
void push_back(const RangeIterator &first, const RangeIterator &last)
Definition container.h:282
osl::FixedCapacityVector::empty
bool empty() const
Definition container.h:244
osl::FixedCapacityVector::capacity
size_t capacity() const
Definition container.h:245
osl::FixedCapacityVector< MoveLogProb, Move::MaxUniqMoves >::ptr
MoveLogProb * ptr
Definition container.h:145
osl::FixedCapacityVector::begin
iterator begin()
Definition container.h:198
osl::FixedCapacityVector::const_iterator
array_t::const_iterator const_iterator
Definition container.h:157
osl::FixedCapacityVector::pushBackHelper
detail::FixedCapacityVectorPushBack< T > pushBackHelper()
Definition container.h:263
osl::FixedCapacityVector::erase
void erase(const T &e)
Definition container.h:229
osl::FixedCapacityVector::FixedCapacityVector
FixedCapacityVector(const RangeIterator &first, const RangeIterator &last)
Definition container.h:168
osl::FixedCapacityVector< MoveLogProb, Move::MaxUniqMoves >::relements
CArray< int64_t,(sizeof(MoveLogProb[Capacity])+sizeof(int64_t) -1)/sizeof(int64_t)> relements
Definition container.h:146
osl::FixedCapacityVector::elements
array_t & elements()
Definition container.h:151
osl::MoveVector
Definition container.h:293
osl::Move::MaxUniqMoves
static const unsigned int MaxUniqMoves
一局面辺りの合法手の最大値 重複して手を生成することがある場合は，600では不足かもしれない
Definition basic_type.h:1071
osl::PieceVector
Definition container.h:305
osl::PieceVector::sortByPtype
void sortByPtype()
駒の価値の大きい順に並び替える.
Definition container.cc:46
osl::PieceVector::sortByBasic
void sortByBasic()
駒の価値の小さい順に並び替える.
Definition container.cc:41
osl::PtypeOSquareVector
Definition container.h:322
osl::PtypeOSquareVector::sort
void sort()
駒の価値の小さい順に並び替える
Definition container.cc:74
osl::detail::FixedCapacityVectorPushBack
Definition container.h:104
osl::detail::FixedCapacityVectorPushBack::limit
T * limit
Definition container.h:108
osl::detail::FixedCapacityVectorPushBack::FixedCapacityVectorPushBack
FixedCapacityVectorPushBack(T **vPtr_, T *limit_)
Definition container.h:111
osl::detail::FixedCapacityVectorPushBack::push_back
void push_back(const T &e)
Definition container.h:122
osl::detail::FixedCapacityVectorPushBack::vPtr
T ** vPtr
Definition container.h:106
osl::detail::FixedCapacityVectorPushBack::ptr
T * ptr
Definition container.h:105
osl::detail::FixedCapacityVectorPushBack::~FixedCapacityVectorPushBack
~FixedCapacityVectorPushBack()
Definition container.h:118
config.h
construct.h
osl::detail
Definition container.h:101
osl::misc::construct
void construct(T1 *ptr, const T2 &value, typename boost::enable_if< detail::BitCopyTraits< T1 > >::type *=0)
Definition construct.h:40
osl::misc::destroy
void destroy(T *ptr)
Definition construct.h:57
osl
Definition additionalEffect.h:6
osl::CArray3d
CArray< CArray2d< T, Capacity2, Capacity3 >, Capacity1 > CArray3d
Definition container.h:98
osl::CArray2d
CArray< CArray< T, Capacity2 >, Capacity1 > CArray2d
Definition container.h:95
osl::CheckOrEscapeMaxUniqMoves
@ CheckOrEscapeMaxUniqMoves
Definition container.h:299
osl::PTYPEO_SIZE
const int PTYPEO_SIZE
Definition basic_type.h:308
osl::playerToIndex
constexpr int playerToIndex(Player player)
Definition basic_type.h:16
osl::operator<
bool operator<(Offset l, Offset r)
Definition basic_type.h:520
osl::ptypeOIndex
unsigned int ptypeOIndex(PtypeO ptypeo)
Definition basic_type.h:205
osl::Player
Player
Definition basic_type.h:8
osl::__attribute__
const PtypeO PTYPEO_EDGE __attribute__((unused))
osl::PtypeO
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition basic_type.h:199
osl::operator<<
std::ostream & operator<<(std::ostream &os, Player player)
Definition basic_type.cc:14
osl::operator==
bool operator==(Square l, Square r)
Definition basic_type.h:758
osl::FixedCapacityVector::Array
Definition container.h:139
osl::PtypeOSquareVector::PtypeOSquareLessThan
Definition container.cc:63
osl::misc::detail::BitCopyTraits::value
static const bool value
Definition construct.h:29