Add finer engine tracing.
[tagua/yd.git] / src / hlvariant / pool.h
blob386352faf102d7daf3b247938f2177864a934a00
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 #ifndef HLVARIANT__POOL_H
12 #define HLVARIANT__POOL_H
14 #include <map>
16 namespace HLVariant {
18 template <typename _Piece>
19 class Pool {
20 public:
21 typedef _Piece Piece;
22 private:
23 typedef typename Piece::Color Color;
24 typedef typename Piece::Type Type;
25 typedef std::map<Type, int> Data;
27 Color m_owner;
28 Data m_data;
29 public:
30 Pool(Color owner);
31 virtual ~Pool();
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);
46 typedef Data RawData;
47 const RawData& rawData() const { return m_data; }
51 // IMPLEMENTATION
53 template <typename Piece>
54 Pool<Piece>::Pool(Color owner)
55 : m_owner(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()) {
74 return it->second;
76 else {
77 return 0;
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];
89 if (n <= 0) {
90 m_data.erase(type);
91 return 0;
94 return n;
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 {
104 int count = 0;
105 for (typename Data::const_iterator end = m_data.end(), it = m_data.begin(); it != end; ++it)
106 count += it->second;
107 return count;
110 template <typename Piece>
111 int Pool<Piece>::insert(int index, const Piece& piece) {
112 if (m_owner != piece.color())
113 return -1;
115 int fill = 0;
116 for (typename Data::iterator end = m_data.end(), i = m_data.begin();
117 i != end && i->first < piece.type();
118 ++i) {
119 fill += i->second;
122 int nump = add(piece.type());
124 if (index < fill)
125 return fill;
126 if (index >= fill + nump)
127 return fill + nump - 1;
128 return index;
131 template <typename Piece>
132 Piece Pool<Piece>::get(int index) const {
133 if (index < 0)
134 return Piece();
136 int fill = 0;
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);
140 fill += i->second;
143 return Piece();
146 template <typename Piece>
147 Piece Pool<Piece>::take(int index) {
148 if (index < 0)
149 return Piece();
151 int fill = 0;
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;
155 remove(type);
156 return Piece(m_owner, type);
159 fill += i->second;
162 return Piece();
167 #endif // HLVARIANT__POOL_H