Chromium Code Reviews| 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 |