From 461827ec11736dc869eb1f97c13bcd5479b179b8 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Mon, 10 Nov 2014 20:07:24 +0100 Subject: [PATCH] preserve order in ref_vector::ensureUnique --- include/mitsuba/core/ref.h | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/include/mitsuba/core/ref.h b/include/mitsuba/core/ref.h index 29372364..49891338 100644 --- a/include/mitsuba/core/ref.h +++ b/include/mitsuba/core/ref.h @@ -21,6 +21,7 @@ #define __MITSUBA_CORE_REF_H_ #include "util.h" +#include MTS_NAMESPACE_BEGIN @@ -148,10 +149,21 @@ public: ref_vector(size_t size) : parent_type(size) {} ref_vector(const ref_vector &vec) : parent_type(vec) {} - /// Remove all duplicates (may change the order) + /// Remove all duplicates without changing the order inline void ensureUnique() { - std::sort(this->begin(), this->end(), ref_comparator()); - this->erase(std::unique(this->begin(), this->end()), this->end()); + std::set seen; + + typename parent_type::iterator it1 = this->begin(), it2 = this->begin(); + for (; it1 < this->end(); ++it1) { + if (seen.find(it1->get()) != seen.end()) + continue; + seen.insert(it1->get()); + if (it1 != it2) + *it2++ = *it1; + else + it2++; + } + this->erase(it2, this->end()); } /// Check if a certain pointer is contained in the vector