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 #ifndef HLVARIANT__POOLCOLLECTION_H
12 #define HLVARIANT__POOLCOLLECTION_H
16 template <typename _Pool
>
17 class PoolCollection
{
21 typedef typename
Pool::Piece Piece
;
22 typedef typename
Piece::Color Color
;
23 typedef std::map
<Color
, Pool
> Pools
;
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;
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())
53 else if (i
->first
> j
->first
) {
54 if (!j
->second
.empty())
60 // same key, compare values
61 if (i
->second
!= j
->second
)
71 while (i
!= m_pools
.end()) {
72 if (!i
->second
.empty())
76 while (j
!= other
.m_pools
.end()) {
77 if (!j
->second
.empty())
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()) {
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