Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef UNIT_MAP_H_INCLUDED
00020 #define UNIT_MAP_H_INCLUDED
00021
00022 #include "utils/reference_counter.hpp"
00023 #include "map_location.hpp"
00024
00025 #include <cassert>
00026 #include <list>
00027 #include <boost/unordered_map.hpp>
00028
00029 class unit;
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 class unit_map {
00090
00091
00092 struct unit_pod {
00093
00094 unit_pod()
00095 : unit(NULL)
00096 , ref_count()
00097 , deleted_uid(0)
00098 {
00099 }
00100
00101 class unit * unit;
00102 mutable n_ref_counter::t_ref_counter<signed int> ref_count;
00103
00104 unsigned deleted_uid;
00105 };
00106
00107
00108
00109
00110 typedef std::list<unit_pod> t_ilist;
00111
00112
00113
00114 typedef boost::unordered_map<size_t, t_ilist::iterator> t_umap;
00115 typedef boost::unordered_map<map_location, t_ilist::iterator> t_lmap;
00116
00117 public:
00118
00119
00120
00121 struct standard_iter_types {
00122 typedef unit_map container_type;
00123 typedef unit_map::t_ilist::iterator iterator_type;
00124 typedef unit value_type;
00125 };
00126
00127 struct const_iter_types {
00128 typedef unit_map const container_type;
00129 typedef unit_map::t_ilist::iterator iterator_type;
00130 typedef const unit value_type;
00131 };
00132
00133 template<typename iter_types>
00134 struct iterator_base
00135 {
00136 typedef std::forward_iterator_tag iterator_category;
00137 typedef int difference_type;
00138 typedef typename iter_types::value_type value_type;
00139 typedef value_type* pointer;
00140 typedef value_type& reference;
00141 typedef typename iter_types::container_type container_type;
00142 typedef typename iter_types::iterator_type iterator_type;
00143
00144 ~iterator_base() { dec(); }
00145
00146 iterator_base(): i_(), tank_(NULL) { }
00147
00148 iterator_base(iterator_type i, container_type *m) : i_(i), tank_(m) {
00149 inc();
00150 valid_exit();
00151 }
00152
00153 iterator_base(const iterator_base &that) : i_(that.i_), tank_(that.tank_) {
00154 inc();
00155 valid_exit();
00156 }
00157
00158 iterator_base &operator=(const iterator_base &that) {
00159 if(*this != that){
00160 dec();
00161 tank_ = that.tank_;
00162 i_ = that.i_;
00163 inc();
00164 valid_exit();
00165 }
00166 return *this;
00167 }
00168
00169 operator iterator_base<const_iter_types> () const {
00170 return iterator_base<const_iter_types>(i_, tank_);
00171 }
00172
00173 private:
00174
00175 iterator_base(t_umap::iterator ui, container_type *m) : i_(ui->second), tank_(m) {
00176 inc();
00177 valid_exit();
00178 }
00179
00180
00181 iterator_base(t_lmap::iterator ui, container_type *m) : i_(ui->second), tank_(m) {
00182 inc();
00183 valid_exit();
00184 }
00185
00186 public:
00187 pointer operator->() const {
00188 assert(valid());
00189 tank_->self_check();
00190 return i_->unit; }
00191 reference operator*() const {
00192 tank_->self_check();
00193 #if 0
00194
00195 if(!valid()){
00196 if(!tank_){std::cerr<<"tank is NULL"<<"\n";}
00197 if(i_==the_end()){std::cerr<<"i_ is the end"<<"\n";}
00198 if(i_->unit==NULL){std::cerr<<"i_ unit is NULL with uid="<<i_->deleted_uid<<"\n";}
00199 }
00200 #endif
00201 assert(valid());
00202 return *i_->unit; }
00203
00204 iterator_base& operator++() {
00205 assert( valid_entry() );
00206 tank_->self_check();
00207 iterator_type new_i(i_);
00208 do{
00209 ++new_i;
00210 }while ((new_i->unit == NULL) && (new_i != the_end() )) ;
00211 dec();
00212 i_ = new_i;
00213 inc();
00214 valid_exit();
00215 return *this;
00216 }
00217
00218 iterator_base operator++(int) {
00219 iterator_base temp(*this);
00220 operator++();
00221 return temp;
00222 }
00223
00224 iterator_base& operator--() {
00225 assert( tank_ && i_ != the_list().begin() );
00226 tank_->self_check();
00227 iterator_type begin(the_list().begin());
00228 dec();
00229 do {
00230 --i_ ;
00231 }while(i_ != begin && (i_->unit == NULL));
00232 inc();
00233
00234 valid_exit();
00235 return *this;
00236 }
00237
00238 iterator_base operator--(int) {
00239 iterator_base temp(*this);
00240 operator--();
00241 return temp;
00242 }
00243
00244 bool valid() const {
00245 if(valid_for_dereference()) {
00246 if(i_->unit == NULL){
00247 recover_unit_iterator(); }
00248 return i_->unit != NULL;
00249 }
00250 return false; }
00251
00252 bool operator==(const iterator_base &rhs) const { return (tank_ == rhs.tank_) && ( i_ == rhs.i_ ); }
00253 bool operator!=(const iterator_base &rhs) const { return !operator==(rhs); }
00254
00255
00256
00257 template<typename Y> friend struct iterator_base;
00258
00259 private:
00260 bool valid_for_dereference() const { return (tank_ != NULL) && (i_ != the_end()); }
00261 bool valid_entry() const { return ((tank_ != NULL) && (i_ != the_end())) ; }
00262 void valid_exit() const {
00263 if(tank_ != NULL) {
00264 assert(!the_list().empty());
00265 assert(i_ != the_list().end());
00266 if(i_ != the_end()){
00267 assert(i_->ref_count > 0);
00268 } else {
00269 assert(i_->ref_count == 1);
00270 }
00271 }}
00272 bool valid_ref_count() const { return (tank_ != NULL) && (i_ != the_end()) ; }
00273
00274
00275 void inc() { if(valid_ref_count()) { ++(i_->ref_count); } }
00276
00277
00278
00279
00280 void dec() {
00281 if( valid_ref_count() ){
00282 assert(i_->ref_count != 0);
00283 if( (--(i_->ref_count) == 0) && (i_->unit == NULL) ){
00284 if(tank_->umap_.erase(i_->deleted_uid) != 1){
00285 tank_->error_recovery_externally_changed_uid(i_); }
00286 i_ = the_list().erase(i_);
00287 } } }
00288
00289 unit_map::t_ilist & the_list() const { return tank_->ilist_; }
00290
00291
00292
00293 iterator_type the_end() const { return tank_->the_end_; }
00294
00295
00296
00297
00298
00299 void recover_unit_iterator() const {
00300 assert(i_->deleted_uid != 0);
00301 iterator_base new_this( tank_->find( i_->deleted_uid ));
00302 const_cast<iterator_base *>(this)->operator=( new_this );
00303 }
00304 friend class unit_map;
00305
00306 iterator_type i_;
00307 container_type* tank_;
00308 };
00309
00310
00311
00312
00313
00314 unit_map();
00315 unit_map(const unit_map &that);
00316 unit_map& operator=(const unit_map &that);
00317
00318 ~unit_map();
00319 void swap(unit_map& o);
00320
00321 typedef iterator_base<standard_iter_types> unit_iterator;
00322 typedef iterator_base<const_iter_types> const_unit_iterator;
00323
00324
00325 typedef unit_iterator iterator;
00326 typedef const_unit_iterator const_iterator;
00327
00328 unit_iterator find(size_t id);
00329 unit_iterator find(const map_location &loc);
00330
00331 const_unit_iterator find(const map_location &loc) const { return const_cast<unit_map *>(this)->find(loc); }
00332 const_unit_iterator find(size_t id) const { return const_cast<unit_map *>(this)->find(id); }
00333
00334 unit_iterator find_leader(int side);
00335 const_unit_iterator find_leader(int side) const { return const_cast<unit_map *>(this)->find_leader(side); }
00336 unit_iterator find_first_leader(int side);
00337
00338 std::vector<unit_iterator> find_leaders(int side);
00339 std::vector<const_unit_iterator> find_leaders(int side) const;
00340
00341 size_t count(const map_location& loc) const { return static_cast<size_t>(lmap_.count(loc)); }
00342
00343 unit_iterator begin() { return make_unit_iterator( begin_core() ); }
00344 const_unit_iterator begin() const { return make_const_unit_iterator( begin_core() ); }
00345
00346 unit_iterator end() { return make_unit_iterator(the_end_); }
00347 const_unit_iterator end() const { return make_const_unit_iterator(the_end_); }
00348
00349 size_t size() const { return lmap_.size(); }
00350 size_t num_iters() const ;
00351
00352 void clear(bool force = false);
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 std::pair<unit_iterator, bool> add(const map_location &l, const unit &u);
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 std::pair<unit_iterator, bool> insert(unit *p);
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 std::pair<unit_iterator, bool> move(const map_location &src, const map_location &dst);
00386
00387
00388
00389
00390
00391
00392 std::pair<unit_iterator, bool> replace(const map_location &l, const unit &u);
00393
00394
00395
00396
00397
00398 size_t erase(const map_location &l);
00399
00400
00401
00402
00403
00404 template <typename T>
00405 size_t erase(const T &iter);
00406
00407
00408
00409
00410
00411
00412 unit *extract(const map_location &loc);
00413
00414
00415 bool self_check() const
00416 #ifndef DEBUG
00417 { return true; }
00418 #endif
00419 ;
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 bool has_unit(const unit * const u);
00431
00432 private:
00433
00434 void init_end(){
00435 assert(ilist_.empty());
00436 unit_pod upod;
00437 upod.unit = NULL;
00438 upod.deleted_uid = 0;
00439 ++upod.ref_count;
00440 ilist_.push_front(upod);
00441 the_end_ = ilist_.begin();
00442 };
00443
00444 t_ilist::iterator begin_core() const ;
00445
00446 bool is_valid(const t_ilist::const_iterator &i) const {
00447 return i != the_end_ && is_found(i) && (i->unit != NULL); }
00448 bool is_valid(const t_umap::const_iterator &i) const {
00449 return is_found(i) && (i->second->unit != NULL); }
00450 bool is_valid(const t_lmap::const_iterator &i) const {
00451 return is_found(i) && (i->second->unit != NULL); }
00452
00453 bool is_found(const t_ilist::const_iterator &i) const { return i != ilist_.end(); }
00454 bool is_found(const t_umap::const_iterator &i) const { return i != umap_.end() ; }
00455 bool is_found(const t_lmap::const_iterator &i) const { return i != lmap_.end(); }
00456
00457 template <typename X>
00458 unit_map::unit_iterator make_unit_iterator(X const & i) {
00459 if (!is_found( i )) { return unit_iterator(the_end_, this); }
00460 return unit_iterator(i , this); }
00461 template <typename X>
00462 unit_map::const_unit_iterator make_const_unit_iterator(X const & i) const {
00463 if (!is_found( i )) { return const_unit_iterator(the_end_, this); }
00464 return const_unit_iterator(i , this); }
00465
00466
00467
00468 void error_recovery_externally_changed_uid(t_ilist::iterator const & lit) const;
00469
00470
00471
00472
00473
00474 mutable t_umap umap_;
00475
00476
00477
00478
00479 t_lmap lmap_;
00480
00481
00482
00483
00484 mutable t_ilist ilist_;
00485
00486
00487 t_ilist::iterator the_end_;
00488
00489 };
00490
00491 template <typename T>
00492 size_t unit_map::erase(const T& iter) {
00493 assert(iter.valid());
00494
00495 return erase(iter->get_location());
00496 }
00497
00498
00499
00500 #endif // UNIT_MAP_H_INCLUDED