]> git.tdb.fi Git - builder.git/commitdiff
Turn the unique function into a template and make it more efficient
authorMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 14:04:05 +0000 (17:04 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 8 Jul 2012 21:08:54 +0000 (00:08 +0300)
source/buildinfo.cpp

index ec8156798d70628d0597e6936ecd39622d9fff58..77243ef365ffcb2cc6b82868c852ae04bfe0e757 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <set>
 #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<typename T>
+void unique(list<T> &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<T> seen;
+       for(typename list<T>::iterator i=l.begin(); i!=l.end(); )
+       {
+               if(seen.count(*i))
+                       l.erase(i++);
+               else
+                       seen.insert(*i++);
+       }
 }
 
 }