1 #ifndef HLVARIANT__POOL_H
2 #define HLVARIANT__POOL_H
8 template <typename _Piece
>
13 typedef typename
Piece::Color Color
;
14 typedef typename
Piece::Type Type
;
15 typedef std::map
<Type
, int> Data
;
23 virtual bool operator==(const Pool
<Piece
>& other
) const;
24 virtual bool operator!=(const Pool
<Piece
>& other
) const;
26 virtual int count(Type type
) const;
27 virtual int add(Type type
);
28 virtual int remove(Type type
);
30 virtual bool empty() const;
31 virtual int size() const;
32 virtual int insert(int index
, const Piece
& piece
);
33 virtual Piece
get(int index
) const;
34 virtual Piece
take(int index
);
37 const RawData
& rawData() const { return m_data
; }
43 template <typename Piece
>
44 Pool
<Piece
>::Pool(Color owner
)
47 template <typename Piece
>
48 Pool
<Piece
>::~Pool() { }
50 template <typename Piece
>
51 bool Pool
<Piece
>::operator==(const Pool
<Piece
>& other
) const {
52 return m_owner
== other
.m_owner
&& m_data
== other
.m_data
;
55 template <typename Piece
>
56 bool Pool
<Piece
>::operator!=(const Pool
<Piece
>& other
) const {
57 return !((*this) == other
);
60 template <typename Piece
>
61 int Pool
<Piece
>::count(Type type
) const {
62 typename
Data::const_iterator it
= m_data
.find(type
);
63 if (it
!= m_data
.end()) {
71 template <typename Piece
>
72 int Pool
<Piece
>::add(Type type
) {
73 return ++m_data
[type
];
76 template <typename Piece
>
77 int Pool
<Piece
>::remove(Type type
) {
78 int n
= --m_data
[type
];
87 template <typename Piece
>
88 bool Pool
<Piece
>::empty() const {
89 return m_data
.empty();
92 template <typename Piece
>
93 int Pool
<Piece
>::size() const {
95 for (typename
Data::const_iterator end
= m_data
.end(), it
= m_data
.begin(); it
!= end
; ++it
)
100 template <typename Piece
>
101 int Pool
<Piece
>::insert(int index
, const Piece
& piece
) {
102 if (m_owner
!= piece
.color())
106 for (typename
Data::iterator end
= m_data
.end(), i
= m_data
.begin();
107 i
!= end
&& i
->first
< piece
.type();
112 int nump
= add(piece
.type());
116 if (index
>= fill
+ nump
)
117 return fill
+ nump
- 1;
121 template <typename Piece
>
122 Piece Pool
<Piece
>::get(int index
) const {
127 for (typename
Data::const_iterator end
= m_data
.end(), i
= m_data
.begin(); i
!= end
; ++i
) {
128 if (index
< fill
+ i
->second
)
129 return Piece(m_owner
, i
->first
);
136 template <typename Piece
>
137 Piece Pool
<Piece
>::take(int index
) {
142 for (typename
Data::iterator end
= m_data
.end(), i
= m_data
.begin(); i
!= end
; ++i
) {
143 if(index
< fill
+ i
->second
) {
144 Type type
= i
->first
;
146 return Piece(m_owner
, type
);
157 #endif // HLVARIANT__POOL_H