Skip to content

Commit

Permalink
avoid creating default initialised values if key doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
artemp committed Oct 27, 2011
1 parent 28e7c83 commit 0f5ab18
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
15 changes: 13 additions & 2 deletions include/mapnik/attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,31 @@
#ifndef MAPNIK_ATTRIBUTE_HPP
#define MAPNIK_ATTRIBUTE_HPP

// mapnik
#include <mapnik/value.hpp>
// stl
#include <string>

namespace mapnik {

static mapnik::value _null_value;

struct attribute
{
std::string name_;
explicit attribute(std::string const& name)
: name_(name) {}

template <typename V ,typename F>
V value(F const& f) const
V const& value(F const& f) const
{
return f[name_];
typedef typename F::const_iterator const_iterator;
const_iterator itr = f.find(name_);
if (itr != f.end())
{
return itr->second;
}
return _null_value;
}
std::string const& name() const { return name_;}
};
Expand Down
18 changes: 17 additions & 1 deletion include/mapnik/feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct feature : public properties,
std::map<std::string,value> props_;
public:
typedef std::map<std::string,value>::iterator iterator;
typedef std::map<std::string,value>::const_iterator const_iterator;
explicit feature(int id)
: properties(props_),
id_(id),
Expand Down Expand Up @@ -145,7 +146,7 @@ struct feature : public properties,
{
return props_;
}

iterator begin()
{
return props_.begin();
Expand All @@ -155,7 +156,22 @@ struct feature : public properties,
{
return props_.end();
}

const_iterator begin() const
{
return props_.begin();
}

const_iterator end() const
{
return props_.end();
}

const_iterator find(std::string const& key) const
{
return props_.find(key);
}

std::string to_string() const
{
std::stringstream ss;
Expand Down
13 changes: 12 additions & 1 deletion include/mapnik/metawriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ struct placement;
class metawriter_property_map
{
public:
typedef std::map<std::string, UnicodeString> property_map;
typedef property_map::const_iterator const_iterator;

metawriter_property_map() {}
UnicodeString const& operator[](std::string const& key) const;
UnicodeString& operator[](std::string const& key) {return m_[key];}
std::map<std::string, UnicodeString>::const_iterator find(std::string const& key) const
{
return m_.find(key);
}
std::map<std::string, UnicodeString>::const_iterator end() const
{
return m_.end();
}
private:
std::map<std::string, UnicodeString> m_;
property_map m_;
UnicodeString not_found_;
};

Expand Down

0 comments on commit 0f5ab18

Please sign in to comment.