Fix td::unique.

This commit is contained in:
levlam 2021-01-07 04:03:38 +03:00
parent db3875710b
commit 62c18729f1
3 changed files with 9 additions and 1 deletions

View File

@ -98,7 +98,7 @@ void unique(V &v) {
size_t j = 1;
for (size_t i = 1; i < v.size(); i++) {
if (v[i] != v[i - 1]) {
if (v[i] != v[j - 1]) {
if (i != j) {
v[j] = std::move(v[i]);
}

View File

@ -7,6 +7,7 @@
#include "td/utils/translit.h"
#include "td/utils/algorithm.h"
#include "td/utils/format.h"
#include "td/utils/misc.h"
#include "td/utils/utf8.h"

View File

@ -349,8 +349,14 @@ TEST(Misc, remove) {
}
static void test_unique(td::vector<int> v, td::vector<int> expected) {
auto v_str = td::transform(v, &td::to_string<int>);
auto expected_str = td::transform(expected, &td::to_string<int>);
td::unique(v);
ASSERT_EQ(expected, v);
td::unique(v_str);
ASSERT_EQ(expected_str, v_str);
}
TEST(Misc, unique) {
@ -362,6 +368,7 @@ TEST(Misc, unique) {
test_unique({0, 1}, {0, 1});
test_unique({1, 0}, {0, 1});
test_unique({1, 1}, {1});
test_unique({1, 1, 0, 0}, {0, 1});
test_unique({3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 0}, {0, 1, 2, 3});
test_unique({3, 3, 3, 3, 3}, {3});
test_unique({3, 3, -1, 3, 3}, {-1, 3});