2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #ifndef HLVARIANT__POOL_H
12 #define HLVARIANT__POOL_H
18 template <typename _Piece
>
23 typedef typename
Piece::Color Color
;
24 typedef typename
Piece::Type Type
;
25 typedef std::map
<Type
, int> Data
;
33 virtual bool operator==(const Pool
<Piece
>& other
) const;
34 virtual bool operator!=(const Pool
<Piece
>& other
) const;
36 virtual int count(Type type
) const;
37 virtual int add(Type type
);
38 virtual int remove(Type type
);
40 virtual bool empty() const;
41 virtual int size() const;
42 virtual int insert(int index
, const Piece
& piece
);
43 virtual Piece
get(int index
) const;
44 virtual Piece
take(int index
);
47 const RawData
& rawData() const { return m_data
; }
53 template <typename Piece
>
54 Pool
<Piece
>::Pool(Color owner
)
57 template <typename Piece
>
58 Pool
<Piece
>::~Pool() { }
60 template <typename Piece
>
61 bool Pool
<Piece
>::operator==(const Pool
<Piece
>& other
) const {
62 return m_owner
== other
.m_owner
&& m_data
== other
.m_data
;
65 template <typename Piece
>
66 bool Pool
<Piece
>::operator!=(const Pool
<Piece
>& other
) const {
67 return !((*this) == other
);
70 template <typename Piece
>
71 int Pool
<Piece
>::count(Type type
) const {
72 typename
Data::const_iterator it
= m_data
.find(type
);
73 if (it
!= m_data
.end()) {
81 template <typename Piece
>
82 int Pool
<Piece
>::add(Type type
) {
83 return ++m_data
[type
];
86 template <typename Piece
>
87 int Pool
<Piece
>::remove(Type type
) {
88 int n
= --m_data
[type
];
97 template <typename Piece
>
98 bool Pool
<Piece
>::empty() const {
99 return m_data
.empty();
102 template <typename Piece
>
103 int Pool
<Piece
>::size() const {
105 for (typename
Data::const_iterator end
= m_data
.end(), it
= m_data
.begin(); it
!= end
; ++it
)
110 template <typename Piece
>
111 int Pool
<Piece
>::insert(int index
, const Piece
& piece
) {
112 if (m_owner
!= piece
.color())
116 for (typename
Data::iterator end
= m_data
.end(), i
= m_data
.begin();
117 i
!= end
&& i
->first
< piece
.type();
122 int nump
= add(piece
.type());
126 if (index
>= fill
+ nump
)
127 return fill
+ nump
- 1;
131 template <typename Piece
>
132 Piece Pool
<Piece
>::get(int index
) const {
137 for (typename
Data::const_iterator end
= m_data
.end(), i
= m_data
.begin(); i
!= end
; ++i
) {
138 if (index
< fill
+ i
->second
)
139 return Piece(m_owner
, i
->first
);
146 template <typename Piece
>
147 Piece Pool
<Piece
>::take(int index
) {
152 for (typename
Data::iterator end
= m_data
.end(), i
= m_data
.begin(); i
!= end
; ++i
) {
153 if(index
< fill
+ i
->second
) {
154 Type type
= i
->first
;
156 return Piece(m_owner
, type
);
167 #endif // HLVARIANT__POOL_H