0

[base] Remove STLIsSorted

This change removes base::STLIsSorted and replaces existing usages with
the equivalent base::ranges::is_sorted.

TBR=dcheng

Bug: None
Change-Id: I2edbcdb7e27a95b42db31c5789c5891ca46c3e15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2526090
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825784}
This commit is contained in:
Jan Wilken Dörrie
2020-11-10 13:16:43 +00:00
committed by Commit Bot
parent f8346bc03e
commit 03ec116d26
8 changed files with 27 additions and 92 deletions

@ -25,6 +25,7 @@
#include "base/check.h"
#include "base/optional.h"
#include "base/ranges/algorithm.h"
#include "base/template_util.h"
namespace base {
@ -464,29 +465,11 @@ typename Map::iterator TryEmplace(Map& map,
std::forward<Args>(args)...);
}
// Returns true if the container is sorted. Requires constexpr std::begin/end,
// which exists for arrays in C++14.
// Note that std::is_sorted is constexpr beginning C++20 and this should be
// switched to use it when C++20 is supported.
template <typename Container>
constexpr bool STLIsSorted(const Container& cont) {
auto it = std::begin(cont);
const auto end = std::end(cont);
if (it == end)
return true;
for (auto prev = it++; it != end; prev = it++) {
if (*it < *prev)
return false;
}
return true;
}
// Returns a new ResultType containing the difference of two sorted containers.
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
DCHECK(STLIsSorted(a1));
DCHECK(STLIsSorted(a2));
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
ResultType difference;
std::set_difference(a1.begin(), a1.end(),
a2.begin(), a2.end(),
@ -497,8 +480,8 @@ ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
// Returns a new ResultType containing the union of two sorted containers.
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) {
DCHECK(STLIsSorted(a1));
DCHECK(STLIsSorted(a2));
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
ResultType result;
std::set_union(a1.begin(), a1.end(),
a2.begin(), a2.end(),
@ -510,8 +493,8 @@ ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) {
// containers.
template <typename ResultType, typename Arg1, typename Arg2>
ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) {
DCHECK(STLIsSorted(a1));
DCHECK(STLIsSorted(a2));
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
ResultType result;
std::set_intersection(a1.begin(), a1.end(),
a2.begin(), a2.end(),
@ -523,8 +506,8 @@ ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) {
// container |a2|.
template <typename Arg1, typename Arg2>
bool STLIncludes(const Arg1& a1, const Arg2& a2) {
DCHECK(STLIsSorted(a1));
DCHECK(STLIsSorted(a2));
DCHECK(ranges::is_sorted(a1));
DCHECK(ranges::is_sorted(a2));
return std::includes(a1.begin(), a1.end(),
a2.begin(), a2.end());
}

@ -352,43 +352,6 @@ TEST(STLUtilTest, ConstCastIterator) {
RunConstCastIteratorTest<std::unordered_multiset<int>>();
}
TEST(STLUtilTest, STLIsSorted) {
{
std::set<int> set;
set.insert(24);
set.insert(1);
set.insert(12);
EXPECT_TRUE(STLIsSorted(set));
}
{
std::set<ComparableValue> set;
set.insert(ComparableValue(24));
set.insert(ComparableValue(1));
set.insert(ComparableValue(12));
EXPECT_TRUE(STLIsSorted(set));
}
{
std::vector<int> vector;
vector.push_back(1);
vector.push_back(1);
vector.push_back(4);
vector.push_back(64);
vector.push_back(12432);
EXPECT_TRUE(STLIsSorted(vector));
vector.back() = 1;
EXPECT_FALSE(STLIsSorted(vector));
}
{
int array[] = {1, 1, 4, 64, 12432};
EXPECT_TRUE(STLIsSorted(array));
array[4] = 1;
EXPECT_FALSE(STLIsSorted(array));
}
}
TEST(STLUtilTest, STLSetDifference) {
std::set<int> a1;
a1.insert(1);
@ -808,16 +771,5 @@ TEST(STLUtilTest, OptionalOrNullptr) {
EXPECT_NE(nullptr, base::OptionalOrNullptr(optional));
}
TEST(STLUtilTest, STLIsSortedConstexpr) {
constexpr int kArrayAscending[] = {1, 2, 3, 4};
static_assert(base::STLIsSorted(kArrayAscending), "");
constexpr int kArrayDescending[] = {4, 3, 2, 1};
static_assert(!base::STLIsSorted(kArrayDescending), "");
constexpr int kArrayEqual[] = {1, 1, 1, 1};
static_assert(base::STLIsSorted(kArrayEqual), "");
}
} // namespace
} // namespace base

@ -14,7 +14,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/numerics/ranges.h"
#include "base/stl_util.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
@ -973,7 +973,7 @@ void TabStripModel::AddWebContents(
if (group.has_value()) {
auto grouped_tabs = group_model_->GetTabGroup(group.value())->ListTabs();
if (grouped_tabs.size() > 0) {
DCHECK(base::STLIsSorted(grouped_tabs));
DCHECK(base::ranges::is_sorted(grouped_tabs));
index = base::ClampToRange(index, grouped_tabs.front(),
grouped_tabs.back() + 1);
}
@ -2039,7 +2039,7 @@ void TabStripModel::AddToExistingGroupImpl(
std::vector<int> new_indices = SetTabsPinned(indices, false);
std::vector<int> tabs_in_group = group_model_->GetTabGroup(group)->ListTabs();
DCHECK(base::STLIsSorted(tabs_in_group));
DCHECK(base::ranges::is_sorted(tabs_in_group));
// Split |new_indices| into |tabs_left_of_group| and |tabs_right_of_group| to
// be moved to proper destination index. Directly set the group for indices

@ -14,8 +14,8 @@
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/hash/md5.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
@ -1198,7 +1198,7 @@ TEST_F(FakeDriveServiceTest, DownloadFile_ExistingFile) {
ASSERT_TRUE(base::ReadFileToString(output_file_path, &content));
EXPECT_EQ("This is some test content.", content);
ASSERT_TRUE(!download_progress_values.empty());
EXPECT_TRUE(base::STLIsSorted(download_progress_values));
EXPECT_TRUE(base::ranges::is_sorted(download_progress_values));
EXPECT_LE(0, download_progress_values.front().first);
EXPECT_GE(26, download_progress_values.back().first);
EXPECT_EQ(content, get_content_callback.GetConcatenatedData());
@ -2034,7 +2034,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) {
EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
EXPECT_FALSE(entry);
ASSERT_TRUE(!upload_progress_values.empty());
EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
EXPECT_LE(0, upload_progress_values.front().first);
EXPECT_GE(static_cast<int64_t>(contents.size() / 2),
upload_progress_values.back().first);
@ -2053,7 +2053,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) {
EXPECT_EQ(static_cast<int64_t>(contents.size()), entry->file_size());
EXPECT_TRUE(Exists(entry->file_id()));
ASSERT_TRUE(!upload_progress_values.empty());
EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
EXPECT_LE(0, upload_progress_values.front().first);
EXPECT_GE(static_cast<int64_t>(contents.size() - contents.size() / 2),
upload_progress_values.back().first);
@ -2096,7 +2096,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) {
EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
EXPECT_FALSE(entry);
ASSERT_TRUE(!upload_progress_values.empty());
EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
EXPECT_LE(0, upload_progress_values.front().first);
EXPECT_GE(static_cast<int64_t>(contents.size() / 2),
upload_progress_values.back().first);
@ -2115,7 +2115,7 @@ TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) {
EXPECT_EQ(static_cast<int64_t>(contents.size()), entry->file_size());
EXPECT_TRUE(Exists(entry->file_id()));
ASSERT_TRUE(!upload_progress_values.empty());
EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
EXPECT_TRUE(base::ranges::is_sorted(upload_progress_values));
EXPECT_LE(0, upload_progress_values.front().first);
EXPECT_GE(static_cast<int64_t>(contents.size() - contents.size() / 2),
upload_progress_values.back().first);

@ -10,7 +10,7 @@
#include <string>
#include <vector>
#include "base/stl_util.h"
#include "base/ranges/algorithm.h"
#include "courgette/base_test_unittest.h"
class DisassemblerWin32X64Test : public BaseTest {
@ -47,7 +47,7 @@ void DisassemblerWin32X64Test::TestExe() const {
std::vector<courgette::RVA> relocs;
bool can_parse_relocs = disassembler->ParseRelocs(&relocs);
EXPECT_TRUE(can_parse_relocs);
EXPECT_TRUE(base::STLIsSorted(relocs));
EXPECT_TRUE(base::ranges::is_sorted(relocs));
const uint8_t* offset_p = disassembler->FileOffsetToPointer(0);
EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()),

@ -10,7 +10,7 @@
#include <string>
#include <vector>
#include "base/stl_util.h"
#include "base/ranges/algorithm.h"
#include "courgette/base_test_unittest.h"
class DisassemblerWin32X86Test : public BaseTest {
@ -47,7 +47,7 @@ void DisassemblerWin32X86Test::TestExe() const {
std::vector<courgette::RVA> relocs;
bool can_parse_relocs = disassembler->ParseRelocs(&relocs);
EXPECT_TRUE(can_parse_relocs);
EXPECT_TRUE(base::STLIsSorted(relocs));
EXPECT_TRUE(base::ranges::is_sorted(relocs));
const uint8_t* offset_p = disassembler->FileOffsetToPointer(0);
EXPECT_EQ(reinterpret_cast<const void*>(file1.c_str()),

@ -10,7 +10,7 @@
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "base/ranges/algorithm.h"
#include "base/strings/utf_string_conversions.h"
#include "components/crx_file/id_util.h"
#include "extensions/common/api/extensions_manifest_types.h"
@ -204,7 +204,7 @@ ExternallyConnectableInfo::ExternallyConnectableInfo(
bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) {
if (all_ids)
return true;
DCHECK(base::STLIsSorted(ids));
DCHECK(base::ranges::is_sorted(ids));
return std::binary_search(ids.begin(), ids.end(), id);
}

@ -347,7 +347,7 @@ class _SchemasCCGenerator(object):
c.Append('#include <algorithm>')
c.Append('#include <iterator>')
c.Append()
c.Append('#include "base/stl_util.h"')
c.Append('#include "base/ranges/algorithm.h"')
c.Append()
c.Append('namespace {')
for api in self._bundle._api_defs:
@ -393,8 +393,8 @@ class _SchemasCCGenerator(object):
schema_constant_name = _FormatNameAsConstant(namespace)
c.Append('{"%s", %s},' % (namespace, schema_constant_name))
c.Eblock('};')
c.Append('static_assert(base::STLIsSorted(kSchemas), "|kSchemas| should be '
'sorted.");')
c.Append('static_assert(base::ranges::is_sorted(kSchemas), "|kSchemas| '
'should be sorted.");')
c.Sblock('auto it = std::lower_bound(std::begin(kSchemas), '
'std::end(kSchemas),')