removed clock pref widget. clock prefs are definitively part of the lua theme.
[kboard.git] / src / variants / xchess / pool.h
blobcd078708875b59bbff5bf9047eda1562d3b569f7
1 #ifndef XCHESS__POOL_H
2 #define XCHESS__POOL_H
4 #include <map>
6 template <typename Position>
7 struct PlayerPoolType {
8 typedef std::map<typename Position::Piece::Type, int> type;
9 };
11 template <typename Position, typename PP>
12 class PoolReferenceBase {
13 protected:
14 typedef typename Position::Piece Piece;
15 typedef typename Piece::Color Color;
16 typedef typename Piece::Type Type;
17 public:
18 typedef std::map<Color, PP> Pool;
19 protected:
20 PP* m_p_pool;
21 const Color m_color;
22 public:
23 PoolReferenceBase(PP* p, Color color)
24 : m_p_pool(p)
25 , m_color(color) { }
27 int size();
28 Piece get(int idx);
31 template <typename Position>
32 class PoolReference : public PoolReferenceBase<Position,
33 typename PlayerPoolType<Position>::type > {
34 public:
35 typedef typename PlayerPoolType<Position>::type PlayerPool;
36 typedef PoolReferenceBase<Position, PlayerPool> Base;
37 typedef typename Base::Piece Piece;
38 typedef typename Base::Type Type;
39 typedef typename Base::Color Color;
41 PoolReference(PlayerPool* p, Color color)
42 : PoolReferenceBase<Position, PlayerPool>(p, color) { }
44 int insert(int idx, const Piece& p);
45 Piece take(int idx);
48 template <typename Position>
49 class PoolConstReference : public PoolReferenceBase<Position,
50 const typename PlayerPoolType<Position>::type> {
51 typedef const typename PlayerPoolType<Position>::type PlayerPool;
52 typedef typename Position::Piece::Color Color;
53 public:
54 PoolConstReference(PlayerPool* p, Color color)
55 : PoolReferenceBase<Position, const PlayerPool>(p, color) { }
58 template <typename Position, typename PP>
59 int PoolReferenceBase<Position, PP>::size() {
60 if (!m_p_pool)
61 return 0;
63 int retv = 0;
64 for (typename PP::const_iterator i = m_p_pool->begin(); i != m_p_pool->end(); ++i)
65 retv += i->second;
66 return retv;
69 template <typename Position, typename PP>
70 typename PoolReferenceBase<Position, PP>::Piece PoolReferenceBase<Position, PP>::get(int idx) {
71 if (idx < 0)
72 return Piece();
74 int fill = 0;
75 for (typename PP::const_iterator i = m_p_pool->begin(); i != m_p_pool->end(); ++i) {
76 if (idx < fill + i->second)
77 return Piece(m_color, i->first);
78 fill += i->second;
80 return Piece();
83 template <typename Position>
84 int PoolReference<Position>::insert(int idx, const Piece& p) {
85 if (this->m_color != p.color()) {
86 ERROR("Inserting a piece in the wrong pool?");
87 return -1;
90 int fill = 0;
91 for (typename PlayerPool::iterator i = this->m_p_pool->begin();
92 (i != this->m_p_pool->end()) && i->first < p.type();
93 ++i) {
94 fill += i->second;
97 int nump = ++(*this->m_p_pool)[p.type()];
99 if (idx < fill)
100 return fill;
101 if (idx >= fill + nump)
102 return fill + nump - 1;
103 return idx;
107 template <typename Position>
108 typename PoolReference<Position>::Piece PoolReference<Position>::take(int idx) {
109 if (idx < 0)
110 return Piece();
112 int fill = 0;
113 for(typename PlayerPool::iterator i = this->m_p_pool->begin();
114 i != this->m_p_pool->end();
115 ++i) {
116 if(idx < fill + i->second) {
117 Type t = i->first;
118 if(!--i->second)
119 this->m_p_pool->erase(i);
120 return Piece(this->m_color, t);
122 fill += i->second;
124 return Piece();
127 #endif // XCHESS__POOL_H