[llvm-branch-commits] [compiler-rt] ababeca - [NFC][sanitizer] Add SortAndDedup function
Vitaly Buka via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Dec 29 14:06:37 PST 2020
Author: Vitaly Buka
Date: 2020-12-29T14:01:43-08:00
New Revision: ababeca34b3f3a355010803d5b59513906a0d590
URL: https://github.com/llvm/llvm-project/commit/ababeca34b3f3a355010803d5b59513906a0d590
DIFF: https://github.com/llvm/llvm-project/commit/ababeca34b3f3a355010803d5b59513906a0d590.diff
LOG: [NFC][sanitizer] Add SortAndDedup function
Added:
Modified:
compiler-rt/lib/sanitizer_common/sanitizer_common.h
compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
Removed:
################################################################################
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index 60d1c62b0681..a6532eee164d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -682,6 +682,27 @@ enum ModuleArch {
kModuleArchRISCV64
};
+// Sorts and removes duplicates from the container.
+template <class Container,
+ class Compare = CompareLess<typename Container::value_type>>
+void SortAndDedup(Container &v, Compare comp = {}) {
+ Sort(v.data(), v.size(), comp);
+ uptr size = v.size();
+ if (size < 2)
+ return;
+ uptr last = 0;
+ for (uptr i = 1; i < size; ++i) {
+ if (comp(v[last], v[i])) {
+ ++last;
+ if (last != i)
+ v[last] = v[i];
+ } else {
+ CHECK(!comp(v[i], v[last]));
+ }
+ }
+ v.resize(last + 1);
+}
+
// Opens the file 'file_name" and reads up to 'max_len' bytes.
// The resulting buffer is mmaped and stored in '*buff'.
// Returns true if file was successfully opened and read.
diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
index 1d0806c4c404..4d9f2c1c831b 100644
--- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
+++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cpp
@@ -269,6 +269,35 @@ TEST(SanitizerCommon, InternalLowerBoundVsStdLowerBound) {
}
}
+class SortAndDedupTest : public ::testing::TestWithParam<std::vector<int>> {};
+
+TEST_P(SortAndDedupTest, SortAndDedup) {
+ std::vector<int> v_std = GetParam();
+ std::sort(v_std.begin(), v_std.end());
+ v_std.erase(std::unique(v_std.begin(), v_std.end()), v_std.end());
+
+ std::vector<int> v = GetParam();
+ SortAndDedup(v);
+
+ EXPECT_EQ(v_std, v);
+}
+
+const std::vector<int> kSortAndDedupTests[] = {
+ {},
+ {1},
+ {1, 1},
+ {1, 1, 1},
+ {1, 2, 3},
+ {3, 2, 1},
+ {1, 2, 2, 3},
+ {3, 3, 2, 1, 2},
+ {3, 3, 2, 1, 2},
+ {1, 2, 1, 1, 2, 1, 1, 1, 2, 2},
+ {1, 3, 3, 2, 3, 1, 3, 1, 4, 4, 2, 1, 4, 1, 1, 2, 2},
+};
+INSTANTIATE_TEST_CASE_P(SortAndDedupTest, SortAndDedupTest,
+ ::testing::ValuesIn(kSortAndDedupTests));
+
#if SANITIZER_LINUX && !SANITIZER_ANDROID
TEST(SanitizerCommon, FindPathToBinary) {
char *true_path = FindPathToBinary("true");
More information about the llvm-branch-commits
mailing list