From: Mikko Rasa Date: Thu, 4 Feb 2010 14:12:45 +0000 (+0000) Subject: Make Tag directly comparable and use it as a key in relevant maps X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=56c9bfa0b47583fb9067705a69c3639a17d4a097;p=libs%2Fgl.git Make Tag directly comparable and use it as a key in relevant maps --- diff --git a/source/pipeline.cpp b/source/pipeline.cpp index 5969246b..9d5870cd 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -37,17 +37,17 @@ Pipeline::~Pipeline() PipelinePass &Pipeline::add_pass(const Tag &tag) { - if(passes.count(tag.id)) + if(passes.count(tag)) throw KeyError("Pass already exists"); - PipelinePass &pass=passes[tag.id]; + PipelinePass &pass=passes[tag]; pass_order.push_back(tag); return pass; } PipelinePass &Pipeline::get_pass(const Tag &tag) { - map::iterator i=passes.find(tag.id); + PassMap::iterator i=passes.find(tag); if(i==passes.end()) throw KeyError("Unknown pass"); return i->second; @@ -55,7 +55,7 @@ PipelinePass &Pipeline::get_pass(const Tag &tag) const PipelinePass &Pipeline::get_pass(const Tag &tag) const { - map::const_iterator i=passes.find(tag.id); + PassMap::const_iterator i=passes.find(tag); if(i==passes.end()) throw KeyError("Unknown pass"); return i->second; diff --git a/source/pipeline.h b/source/pipeline.h index afa9ed5e..8edd6795 100644 --- a/source/pipeline.h +++ b/source/pipeline.h @@ -24,7 +24,9 @@ class Texture2D; class Pipeline: public Renderable { private: - std::map passes; + typedef std::map PassMap; + + PassMap passes; std::vector pass_order; std::vector renderables; std::vector effects; diff --git a/source/tag.cpp b/source/tag.cpp index 8e29bee1..76adf7d1 100644 --- a/source/tag.cpp +++ b/source/tag.cpp @@ -1,28 +1,35 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2010 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ #include "tag.h" +namespace { + +template +inline unsigned hash(T begin, T end) +{ + unsigned result=0; + for(T i=begin; (i!=end && *i); ++i) + result=((result>>29)|(result<<5))^static_cast(*i); + return result; +} + +} + namespace Msp { namespace GL { Tag::Tag(const char *s): - id(0) -{ - for(const char *i=s; *i; ++i) - id=id*id+*i; -} + id(s ? hash(s, 0) : 0) +{ } Tag::Tag(const std::string &s): - id(0) -{ - for(std::string::const_iterator i=s.begin(); i!=s.end(); ++i) - id=id*id+*i; -} + id(hash(s.begin(), s.end())) +{ } } // namespace GL } // namespace Msp diff --git a/source/tag.h b/source/tag.h index b2d4fd51..47c4643e 100644 --- a/source/tag.h +++ b/source/tag.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2010 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -20,6 +20,9 @@ struct Tag Tag(): id(0) { } Tag(const char *); Tag(const std::string &s); + + bool operator<(const Tag &t) const { return id::iterator i=passes.begin(); i!=passes.end(); ++i) + for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i) delete i->second.shdata; } bool Technique::has_pass(const GL::Tag &tag) const { - return passes.count(tag.id); + return passes.count(tag); } const ObjectPass &Technique::get_pass(const GL::Tag &tag) const { - map::const_iterator i=passes.find(tag.id); + PassMap::const_iterator i=passes.find(tag); if(i==passes.end()) throw KeyError("Unknown pass"); return i->second; @@ -76,7 +76,7 @@ Technique::Loader::Loader(Technique &t, Collection &c): void Technique::Loader::finish() { - for(map::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i) + for(PassMap::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i) if(i->second.shdata) { for(unsigned j=0; j PassMap; + std::vector textures; const Texture *main_texture; - std::map passes; + PassMap passes; ObjectPass *normal_pass; const Material *material;