De-constify piece types.
[tagua/yd.git] / src / core / defaultpool.cpp
blobbec9fb03841f78fabd4259553a6d2ec3823bb531
1 /*
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.
9 */
11 #include "defaultpool.h"
12 #include "piece.h"
13 #include "taguacast.h"
14 #include "type.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)
23 : m_owner(owner) { }
25 DefaultPool::~DefaultPool() { }
27 IPool* DefaultPool::clone() const {
28 DefaultPool* c = new DefaultPool(m_owner);
29 c->m_data = m_data;
30 return c;
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;
44 return true;
47 int DefaultPool::size() const {
48 int res = 0;
49 for (Data::const_iterator it = m_data.begin(),
50 end = m_data.end(); it != end; ++it) {
51 res += it.value();
53 return res;
56 int DefaultPool::insert(int index, Piece& piece) {
57 int fill = 0;
58 for (Data::iterator it = m_data.begin(),
59 end = m_data.end();
60 it != end && it.key() < Key(piece.type());
61 ++it) {
62 fill += it.value();
65 int nump = add(piece.type());
67 if (index < fill)
68 return fill;
69 if (index >= fill + nump)
70 return fill + nump - 1;
71 return index;
74 Piece DefaultPool::get(int index) const {
75 if (index < 0)
76 return Piece();
78 int fill = 0;
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);
83 fill += it.value();
86 return Piece();
89 // FIXME: code duplication
90 Piece DefaultPool::get(int index) {
91 if (index < 0)
92 return Piece();
94 int fill = 0;
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);
99 fill += it.value();
102 return Piece();
105 Piece DefaultPool::take(int index) {
106 if (index < 0)
107 return Piece();
109 int fill = 0;
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;
114 remove(type);
115 return Piece(m_owner, type);
118 fill += it.value();
121 return Piece();
124 bool DefaultPool::take(Piece& piece) {
125 if (m_data[Key(piece.type())] <= 0) return false;
126 remove(piece.type());
127 return true;
130 const IColor* DefaultPool::color() const {
131 return m_owner;
134 int DefaultPool::add(IType* type) {
135 return ++m_data[Key(type)];
138 int DefaultPool::remove(IType* type) {
139 Key key(type);
140 int n = --m_data[key];
141 if (n <= 0) {
142 m_data.remove(key);
143 return 0;
146 return n;
149 int DefaultPool::count(IType* type) const {
150 return m_data.value(Key(type));