From: Mikko Rasa Date: Sun, 8 Jul 2012 14:04:05 +0000 (+0300) Subject: Turn the unique function into a template and make it more efficient X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ee2c6758fd120fe8bca76521acba06d793121d60;p=builder.git Turn the unique function into a template and make it more efficient --- diff --git a/source/buildinfo.cpp b/source/buildinfo.cpp index ec81567..77243ef 100644 --- a/source/buildinfo.cpp +++ b/source/buildinfo.cpp @@ -1,4 +1,5 @@ #include +#include #include "buildinfo.h" using namespace std; @@ -7,17 +8,18 @@ using namespace Msp; namespace { /** Removes any duplicate entries from a list, leaving only the first one. The -order of other elements is preserved. O(n²) efficiency. */ -void unique(StringList &l) +order of other elements is preserved. O(nlogn) efficiency. */ +template +void unique(list &l) { - for(StringList::iterator i=l.begin(); i!=l.end(); ++i) - for(StringList::iterator j=i; j!=l.end();) - { - if(j!=i && *j==*i) - j = l.erase(j); - else - ++j; - } + set seen; + for(typename list::iterator i=l.begin(); i!=l.end(); ) + { + if(seen.count(*i)) + l.erase(i++); + else + seen.insert(*i++); + } } }