Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2381)

Unified Diff: base/containers/flat_tree.h

Issue 2807933002: Removing (dest != prev) check from LastUnique algorithm. (Closed)
Patch Set: Comments 2. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/containers/container_test_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/containers/flat_tree.h
diff --git a/base/containers/flat_tree.h b/base/containers/flat_tree.h
index c3a234fa784c4f0e0e151d9711d0265b756473fd..8e85f2377655f954c585be08bea9749edd79240c 100644
--- a/base/containers/flat_tree.h
+++ b/base/containers/flat_tree.h
@@ -21,25 +21,28 @@ namespace internal {
// selects only the last of consecutive values instead of the first.
template <class Iterator, class BinaryPredicate>
Iterator LastUnique(Iterator first, Iterator last, BinaryPredicate compare) {
- if (first == last)
+ Iterator replacable = std::adjacent_find(first, last, compare);
+
+ // No duplicate elements found.
+ if (replacable == last)
return last;
- Iterator dest = first;
- Iterator cur = first;
- Iterator prev = cur;
- while (++cur != last) {
- if (!compare(*prev, *cur)) {
- // Non-identical one.
- if (dest != prev)
- *dest = std::move(*prev);
- ++dest;
- }
- prev = cur;
+ first = std::next(replacable);
+
+ // Last element is a duplicate but all others are unique.
+ if (first == last)
+ return replacable;
+
+ // This loop is based on std::adjacent_find but std::adjacent_find doesn't
+ // quite cut it.
+ for (Iterator next = std::next(first); next != last; ++next, ++first) {
+ if (!compare(*first, *next))
+ *replacable++ = std::move(*first);
}
- if (dest != prev)
- *dest = std::move(*prev);
- return ++dest;
+ // Last element should be copied unconditionally.
+ *replacable++ = std::move(*first);
+ return replacable;
}
// Implementation of a sorted vector for backing flat_set and flat_map. Do not
« no previous file with comments | « base/containers/container_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698