From ee2c6758fd120fe8bca76521acba06d793121d60 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 8 Jul 2012 17:04:05 +0300 Subject: [PATCH] Turn the unique function into a template and make it more efficient --- source/buildinfo.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) 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++); + } } } -- 2.45.2