Add finer engine tracing.
[tagua/yd.git] / src / hlvariant / poolcollection.h
blobb5605ad3c332e73e50ec38ad3917944608deb465
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__POOLCOLLECTION_H
12 #define HLVARIANT__POOLCOLLECTION_H
14 namespace HLVariant {
16 template <typename _Pool>
17 class PoolCollection {
18 public:
19 typedef _Pool Pool;
20 private:
21 typedef typename Pool::Piece Piece;
22 typedef typename Piece::Color Color;
23 typedef std::map<Color, Pool> Pools;
25 Pools m_pools;
26 public:
27 virtual ~PoolCollection();
29 virtual bool operator==(const PoolCollection<Pool>& other) const;
30 virtual bool operator!=(const PoolCollection<Pool>& other) const;
32 virtual Pool& pool(Color player);
33 virtual const Pool& pool(Color player) const;
36 // IMPLEMENTATION
38 template <typename Pool>
39 PoolCollection<Pool>::~PoolCollection() { }
41 template <typename Pool>
42 bool PoolCollection<Pool>::operator==(const PoolCollection<Pool>& other) const {
43 typename Pools::const_iterator i = m_pools.begin();
44 typename Pools::const_iterator j = other.m_pools.begin();
46 while (i != m_pools.end() && j != other.m_pools.end()) {
47 if (i->first < j->first) {
48 if (!i->second.empty())
49 return false;
50 else
51 ++i;
53 else if (i->first > j->first) {
54 if (!j->second.empty())
55 return false;
56 else
57 ++j;
59 else {
60 // same key, compare values
61 if (i->second != j->second)
62 return false;
63 else {
64 ++i;
65 ++j;
70 // check tail
71 while (i != m_pools.end()) {
72 if (!i->second.empty())
73 return false;
74 ++i;
76 while (j != other.m_pools.end()) {
77 if (!j->second.empty())
78 return false;
79 ++j;
82 return true;
85 template <typename Pool>
86 bool PoolCollection<Pool>::operator!=(const PoolCollection<Pool>& other) const {
87 return !((*this) == other);
90 template <typename Pool>
91 Pool& PoolCollection<Pool>::pool(Color player) {
92 // return pool if it exists
93 typename Pools::iterator it = m_pools.find(player);
94 if (it != m_pools.end()) {
95 return it->second;
98 // if not, create it
99 m_pools.insert(std::make_pair(player, Pool(player)));
100 return m_pools.find(player)->second;
103 template <typename Pool>
104 const Pool& PoolCollection<Pool>::pool(Color player) const {
105 // here we use const cast, because our semantics of PoolCollection
106 // considers an empty pool the same thing as no pool.
107 return const_cast<PoolCollection*>(this)->pool(player);
112 #endif // HLVARIANT__POOLCOLLECTION_H