new 4475edb243ed4627f4c5f2c470ca40b3def034d4
[tagua/yd.git] / src / core / defaultpool.cpp
blobc3608f3012f61be2cf6a0835fc73a4b8deab6aa2
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(const 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, const Piece& piece) {
57 int fill = 0;
58 for (Data::const_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 Piece DefaultPool::take(int index) {
90 if (index < 0)
91 return Piece();
93 int fill = 0;
94 for (Data::const_iterator it = m_data.begin(),
95 end = m_data.end(); it != end; ++it) {
96 if (index < fill + it.value()) {
97 const IType* type = it.key().type;
98 remove(type);
99 return Piece(m_owner, type);
102 fill += it.value();
105 return Piece();
108 bool DefaultPool::take(const Piece& piece) {
109 if (m_data[Key(piece.type())] <= 0) return false;
110 remove(piece.type());
111 return true;
114 const IColor* DefaultPool::color() const {
115 return m_owner;
118 int DefaultPool::add(const IType* type) {
119 return ++m_data[Key(type)];
122 int DefaultPool::remove(const IType* type) {
123 Key key(type);
124 int n = --m_data[key];
125 if (n <= 0) {
126 m_data.remove(key);
127 return 0;
130 return n;
133 int DefaultPool::count(const IType* type) const {
134 return m_data.value(Key(type));