_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

unit_cache.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 2008 by Rafal Bursig
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 
29 #ifndef __UNIT_CACHE_H__
30 #define __UNIT_CACHE_H__
31 
33 
34 /*----------------------------------------------------------------------------
35 -- Includes
36 ----------------------------------------------------------------------------*/
37 
38 #include <vector>
39 #include <algorithm>
40 
41 /*----------------------------------------------------------------------------
42 -- Declarations
43 ----------------------------------------------------------------------------*/
44 
45 class CUnit;
46 class CMap;
51 {
52 public:
53  typedef std::vector<CUnit *>::iterator iterator;
54  typedef std::vector<CUnit *>::const_iterator const_iterator;
55 
56 public:
57  CUnitCache() : Units() {}
58 
59  size_t size() const { return Units.size(); }
60 
61  void clear() { Units.clear(); }
62 
63  const_iterator begin() const { return Units.begin(); }
64  iterator begin() { return Units.begin(); }
65  const_iterator end() const { return Units.end(); }
66  iterator end() { return Units.end(); }
67 
68  CUnit *operator[](const unsigned int index) const
69  {
70  //Assert(index < Units.size());
71  return Units[index];
72  }
73  CUnit *operator[](const unsigned int index)
74  {
75  //Assert(index < Units.size());
76  return Units[index];
77  }
78 
85  template<typename _T>
86  CUnit *find(const _T &pred) const
87  {
88  std::vector<CUnit *>::const_iterator ret = std::find_if(Units.begin(), Units.end(), pred);
89 
90  return ret != Units.end() ? (*ret) : NULL;
91  }
92 
101  template<typename _T>
102  void for_each(const _T functor)
103  {
104  const size_t size = Units.size();
105 
106  for (size_t i = 0; i != size; ++i) {
107  functor(Units[i]);
108  }
109  }
110 
120  template<typename _T>
121  int for_each_if(const _T &functor)
122  {
123  const size_t size = Units.size();
124 
125  for (size_t count = 0; count != size; ++count) {
126  if (functor(Units[count]) == false) {
127  return count;
128  }
129  }
130  return size;
131  }
132 
133 
140  CUnit *Remove(const unsigned int index)
141  {
142  const size_t size = Units.size();
143  Assert(index < size);
144  CUnit *tmp = Units[index];
145  if (size > 1) {
146  Units[index] = Units[size - 1];
147  }
148  Units.pop_back();
149  return tmp;
150  }
151 
157  bool Remove(CUnit *const unit)
158  {
159 #ifndef SECURE_UNIT_REMOVING
160  const size_t size = Units.size();
161  if (size == 1 && unit == Units[0]) {
162  Units.pop_back();
163  return true;
164  } else {
165  for (unsigned int i = 0; i < size; ++i) {
166  // Do we care on unit sequence in tile cache ?
167  if (Units[i] == unit) {
168  Units[i] = Units[size - 1];
169  Units.pop_back();
170  return true;
171  }
172  }
173  }
174 #else
175  for (std::vector<CUnit *>::iterator i(Units.begin()), end(Units.end()); i != end; ++i) {
176  if ((*i) == unit) {
177  Units.erase(i);
178  return true;
179  }
180  }
181 #endif
182  return false;
183  }
184 
190  void RemoveS(CUnit *const unit)
191  {
192  for (std::vector<CUnit *>::iterator i(Units.begin()), end(Units.end()); i != end; ++i) {
193  if ((*i) == unit) {
194  Units.erase(i);
195  return;
196  }
197  }
198  }
199 
207  bool InsertS(CUnit *unit)
208  {
209  if (!binary_search(Units.begin(), Units.end(), unit)) {
210  Units.insert(std::lower_bound(Units.begin(), Units.end(), unit), unit);
211  return true;
212  }
213  return false;
214  }
215 
221  void Insert(CUnit *unit)
222  {
223  Units.push_back(unit);
224  }
225 
226 public:
227  std::vector<CUnit *> Units;
228 };
229 
230 
232 
233 #endif // !__UNIT_CACHE_H__
234 
CUnitCache::end
const_iterator end() const
Definition: unit_cache.h:65
CUnitCache::clear
void clear()
Definition: unit_cache.h:61
CUnitCache
Definition: unit_cache.h:50
CUnitCache::Insert
void Insert(CUnit *unit)
Definition: unit_cache.h:221
CUnitCache::end
iterator end()
Definition: unit_cache.h:66
CUnitCache::for_each
void for_each(const _T functor)
Apply a function to every element of a cache.
Definition: unit_cache.h:102
CUnitCache::find
CUnit * find(const _T &pred) const
Find the first unit in a tile cache for which a predicate is true.
Definition: unit_cache.h:86
CUnitCache::InsertS
bool InsertS(CUnit *unit)
Definition: unit_cache.h:207
CUnitCache::Units
std::vector< CUnit * > Units
Definition: unit_cache.h:227
CUnitCache::CUnitCache
CUnitCache()
Definition: unit_cache.h:57
CUnitCache::const_iterator
std::vector< CUnit * >::const_iterator const_iterator
Definition: unit_cache.h:54
CUnitCache::begin
iterator begin()
Definition: unit_cache.h:64
CUnitCache::Remove
CUnit * Remove(const unsigned int index)
Definition: unit_cache.h:140
CUnitCache::size
size_t size() const
Definition: unit_cache.h:59
CUnitCache::RemoveS
void RemoveS(CUnit *const unit)
Definition: unit_cache.h:190
CUnitCache::operator[]
CUnit * operator[](const unsigned int index) const
Definition: unit_cache.h:68
CUnitCache::Remove
bool Remove(CUnit *const unit)
Definition: unit_cache.h:157
CMap
Describes the world map.
Definition: map.h:154
CUnitCache::for_each_if
int for_each_if(const _T &functor)
Apply a function to every element of a cache.
Definition: unit_cache.h:121
Assert
#define Assert(cond)
Definition: stratagus.h:142
CUnitCache::iterator
std::vector< CUnit * >::iterator iterator
Definition: unit_cache.h:53
CUnit
The big unit structure.
Definition: unit.h:135
CUnitCache::operator[]
CUnit * operator[](const unsigned int index)
Definition: unit_cache.h:73
CUnitCache::begin
const_iterator begin() const
Definition: unit_cache.h:63
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.