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 #include "defaultpool.h"
13 #include "taguacast.h"
16 DefaultPool::Key::Key(IType
* type
) : type(type
) { }
18 bool DefaultPool::Key::operator<(const Key
& other
) const {
19 return type
->index() < other
.type
->index();
22 DefaultPool::DefaultPool(const IColor
* owner
)
25 DefaultPool::~DefaultPool() { }
27 IPool
* DefaultPool::clone() const {
28 DefaultPool
* c
= new DefaultPool(m_owner
);
33 bool DefaultPool::equals(const IPool
* other_
) const {
34 TAGUA_CAST(other
, const DefaultPool
, false);
35 return other
->m_data
== m_data
;
38 bool DefaultPool::empty() const {
39 for (Data::const_iterator it
= m_data
.begin(),
40 end
= m_data
.end(); it
!= end
; ++it
) {
41 if (it
.value() > 0) return false;
47 int DefaultPool::size() const {
49 for (Data::const_iterator it
= m_data
.begin(),
50 end
= m_data
.end(); it
!= end
; ++it
) {
56 int DefaultPool::insert(int index
, Piece
& piece
) {
58 for (Data::iterator it
= m_data
.begin(),
60 it
!= end
&& it
.key() < Key(piece
.type());
65 int nump
= add(piece
.type());
69 if (index
>= fill
+ nump
)
70 return fill
+ nump
- 1;
74 Piece
DefaultPool::get(int index
) const {
79 for (Data::const_iterator it
= m_data
.begin(),
80 end
= m_data
.end(); it
!= end
; ++it
) {
81 if (index
< fill
+ it
.value())
82 return Piece(m_owner
, it
.key().type
);
89 // FIXME: code duplication
90 Piece
DefaultPool::get(int index
) {
95 for (Data::iterator it
= m_data
.begin(),
96 end
= m_data
.end(); it
!= end
; ++it
) {
97 if (index
< fill
+ it
.value())
98 return Piece(m_owner
, it
.key().type
);
105 Piece
DefaultPool::take(int index
) {
110 for (Data::const_iterator it
= m_data
.begin(),
111 end
= m_data
.end(); it
!= end
; ++it
) {
112 if (index
< fill
+ it
.value()) {
113 IType
* type
= it
.key().type
;
115 return Piece(m_owner
, type
);
124 bool DefaultPool::take(Piece
& piece
) {
125 if (m_data
[Key(piece
.type())] <= 0) return false;
126 remove(piece
.type());
130 const IColor
* DefaultPool::color() const {
134 int DefaultPool::add(IType
* type
) {
135 return ++m_data
[Key(type
)];
138 int DefaultPool::remove(IType
* type
) {
140 int n
= --m_data
[key
];
149 int DefaultPool::count(IType
* type
) const {
150 return m_data
.value(Key(type
));