Skip to content

Commit

Permalink
terrain_filter caches its unit filter construction
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jul 5, 2014
1 parent 9bfe3b5 commit 27436d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/terrain_filter.cpp
Expand Up @@ -100,6 +100,13 @@ terrain_filter& terrain_filter::operator=(const terrain_filter& other)
return *this ;
}

terrain_filter::terrain_filter_cache::terrain_filter_cache() :
parsed_terrain(NULL),
adjacent_matches(NULL),
adjacent_match_cache(),
ufilter_()
{}

namespace {
struct cfg_isor {
bool operator() (std::pair<const std::string,const vconfig> val) {
Expand Down Expand Up @@ -156,9 +163,12 @@ bool terrain_filter::match_internal(const map_location& loc, const bool ignore_x

//Allow filtering on unit
if(cfg_.has_child("filter")) {
const unit_filter ufilt(vconfig(cfg_.child("filter")), fc_, flat_);
const unit_map::const_iterator u = fc_->get_disp_context().units().find(loc);
if (u == fc_->get_disp_context().units().end() || !ufilt( *u, loc))
if (!u.valid())
return false;
if (!cache_.ufilter_)
cache_.ufilter_.reset(new unit_filter(vconfig(cfg_.child("filter")), fc_, flat_));
if (!cache_.ufilter_->matches(*u, loc))
return false;
}

Expand Down
12 changes: 6 additions & 6 deletions src/terrain_filter.hpp
Expand Up @@ -23,9 +23,12 @@
class config;
class filter_context;
class unit;
class unit_filter;
class unit_map;
class team;

#include <boost/scoped_ptr.hpp> //to memoize unit_filter

//terrain_filter: a class that implements the Standard Location Filter
class terrain_filter : public xy_pred {
public:
Expand Down Expand Up @@ -70,12 +73,7 @@ class terrain_filter : public xy_pred {
const filter_context * fc_;

struct terrain_filter_cache {
terrain_filter_cache() :
parsed_terrain(NULL),
adjacent_matches(NULL),
adjacent_match_cache()
{
}
terrain_filter_cache();

~terrain_filter_cache();

Expand All @@ -87,6 +85,8 @@ class terrain_filter : public xy_pred {

//adjacent_match_cache: optimize handling of [filter_adjacent_location] for match()
std::vector< std::pair<terrain_filter, std::map<map_location,bool> > > adjacent_match_cache;

boost::scoped_ptr<unit_filter> ufilter_;
};

mutable terrain_filter_cache cache_;
Expand Down

0 comments on commit 27436d9

Please sign in to comment.