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

Unified Diff: base/containers/flat_tree.h

Issue 2807933002: Removing (dest != prev) check from LastUnique algorithm. (Closed)
Patch Set: Minimizing changes. 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..433a04f3d1d436fc8258fa2d126421565a8cbd23 100644
--- a/base/containers/flat_tree.h
+++ b/base/containers/flat_tree.h
@@ -21,25 +21,25 @@ 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);
+
+ if (replacable == last)
return last;
brettw 2017/04/26 21:48:51 Can you append to this line "No duplicate elements
- 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);
+
+ if (first == last)
+ return replacable;
+
+ // adjacent_find will force us to use BidirectionalIterator.
brettw 2017/04/26 21:48:51 This comment is confusing by itself here. I think
dyaroshev 2017/04/27 19:11:46 I've done smth. Looks better?
+ // to get the previous element.
+ for (Iterator next = std::next(first); next != last; ++next, ++first)
brettw 2017/04/26 21:48:51 This should have {} by our style guide (# lines is
dyaroshev 2017/04/27 19:11:46 Done.
+ if (!compare(*first, *next))
+ *replacable++ = std::move(*first);
- if (dest != prev)
- *dest = std::move(*prev);
- return ++dest;
+ // last element should be copied unconditionally.
brettw 2017/04/26 21:48:51 Minor nit: capital letter @ beginning.
dyaroshev 2017/04/27 19:11:46 Done.
+ *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