Skip to content

Commit

Permalink
Export of internal Abseil changes
Browse files Browse the repository at this point in the history
--
330051e00cd57ee516b4eaf656965656ffbcd0bc by Abseil Team <absl-team@google.com>:

Fix indentation in comment.

PiperOrigin-RevId: 287997504

--
35fb1a893e708031ba4fc0db460875eb0d31820e by Abseil Team <absl-team@google.com>:

Enable compile-time enforcement that absl::Substitute patterns to not contain unescaped $ symbols.

absl::Substitute already considers unescaped $ symbols undefined behavior and crashes when it's passed them in debug builds.  Some code isn't ever built in debug mode, though, and inadvertently used some unescaped $ symbols, which led to surprising results.  This change will prevent that problem from happening in the future.
PiperOrigin-RevId: 287906643

--
c5762833ebde6d7110bf68041a823b571c238e9e by Gennadiy Rozental <rogeeff@google.com>:

Move all the flag data into a single place instead of being split between handle and flag object.

After this change CommandLineFlag will not hold any data anymore. And we also do not need to pass the CommandLineFlag around in Abseil Flag implementation to report flag name and location.

PiperOrigin-RevId: 287899076

--
8b5fb644f1e3d9267b7a75106fe9a72c886db786 by Derek Mauro <dmauro@google.com>:

Upgrade CI testing to Bazel 2.0.0 and Clang 407ac2eb5f13

-fno-sanitize-blacklist is to workaround
bazelbuild/bazel#10510

PiperOrigin-RevId: 287875363

--
a20cc1d58895de2babc3748a6c79d1d6813734ef by Abseil Team <absl-team@google.com>:

Make ABSL_RETIRED_FLAG behave consistently with ABSL_FLAG.

Before the change:
ABSL_RETIRED_FLAG does not compile when there are competing ctors in the type, even when ABSL_FLAG does.

After the change:
ABSL_RETIRED_FLAG compiles when ABSL_FLAG does.

PiperOrigin-RevId: 286483183

--
1cff7e67329d2be9e50bee1f2e76ef9ffd2edde5 by Abseil Team <absl-team@google.com>:

Support C++20 erase_if API in unordered associative containers

See [unord.set.erasure]: https://eel.is/c++draft/unord.set.erasure
See [unord.map.erasure]: https://eel.is/c++draft/unord.map.erasure

PiperOrigin-RevId: 286461140
GitOrigin-RevId: 330051e00cd57ee516b4eaf656965656ffbcd0bc
Change-Id: I5513110b41c2af08a44da54612cff341ac5c6607
  • Loading branch information
Abseil Team authored and Andy Soffer committed Jan 3, 2020
1 parent ad904b6 commit 1de0166
Show file tree
Hide file tree
Showing 27 changed files with 327 additions and 229 deletions.
9 changes: 9 additions & 0 deletions absl/container/flat_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,15 @@ class flat_hash_map : public absl::container_internal::raw_hash_map<
using Base::key_eq;
};

// erase_if(flat_hash_map<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
template <typename K, typename V, typename H, typename E, typename A,
typename Predicate>
void erase_if(flat_hash_map<K, V, H, E, A>& c, Predicate pred) {
container_internal::EraseIf(pred, &c);
}

namespace container_internal {

template <class K, class V>
Expand Down
38 changes: 38 additions & 0 deletions absl/container/flat_hash_map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::_;
using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;

Expand Down Expand Up @@ -215,6 +216,43 @@ TEST(FlatHashMap, MergeExtractInsert) {
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9)));
}

bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }

TEST(FlatHashMap, EraseIf) {
// Erase all elements.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, [](std::pair<const int, int>) { return true; });
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, [](std::pair<const int, int>) { return false; });
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
Pair(4, 4), Pair(5, 5)));
}
// Erase specific elements.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s,
[](std::pair<const int, int> kvp) { return kvp.first % 2 == 1; });
EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
}
// Predicate is function reference.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, FirstIsEven);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
// Predicate is function pointer.
{
flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, &FirstIsEven);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
}

#if (defined(ABSL_USES_STD_ANY) || !defined(_LIBCPP_VERSION)) && \
!defined(__EMSCRIPTEN__)
TEST(FlatHashMap, Any) {
Expand Down
8 changes: 8 additions & 0 deletions absl/container/flat_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,14 @@ class flat_hash_set
using Base::key_eq;
};

// erase_if(flat_hash_set<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
template <typename T, typename H, typename E, typename A, typename Predicate>
void erase_if(flat_hash_set<T, H, E, A>& c, Predicate pred) {
container_internal::EraseIf(pred, &c);
}

namespace container_internal {

template <class T>
Expand Down
36 changes: 36 additions & 0 deletions absl/container/flat_hash_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace {

using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
Expand Down Expand Up @@ -124,6 +125,41 @@ TEST(FlatHashSet, MergeExtractInsert) {
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
}

bool IsEven(int k) { return k % 2 == 0; }

TEST(FlatHashSet, EraseIf) {
// Erase all elements.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, [](int) { return true; });
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, [](int) { return false; });
EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
}
// Erase specific elements.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, [](int k) { return k % 2 == 1; });
EXPECT_THAT(s, UnorderedElementsAre(2, 4));
}
// Predicate is function reference.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, IsEven);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
// Predicate is function pointer.
{
flat_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, &IsEven);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
}

} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
Expand Down
11 changes: 11 additions & 0 deletions absl/container/internal/raw_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,17 @@ class raw_hash_set {
settings_{0, hasher{}, key_equal{}, allocator_type{}};
};

// Erases all elements that satisfy the predicate `pred` from the container `c`.
template <typename P, typename H, typename E, typename A, typename Predicate>
void EraseIf(Predicate pred, raw_hash_set<P, H, E, A>* c) {
for (auto it = c->begin(), last = c->end(); it != last;) {
auto copy_it = it++;
if (pred(*copy_it)) {
c->erase(copy_it);
}
}
}

namespace hashtable_debug_internal {
template <typename Set>
struct HashtableDebugAccess<Set, absl::void_t<typename Set::raw_hash_set>> {
Expand Down
9 changes: 9 additions & 0 deletions absl/container/node_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,15 @@ class node_hash_map
void resize(typename Base::size_type hint) { this->rehash(hint); }
};

// erase_if(node_hash_map<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
template <typename K, typename V, typename H, typename E, typename A,
typename Predicate>
void erase_if(node_hash_map<K, V, H, E, A>& c, Predicate pred) {
container_internal::EraseIf(pred, &c);
}

namespace container_internal {

template <class Key, class Value>
Expand Down
38 changes: 38 additions & 0 deletions absl/container/node_hash_map_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace container_internal {
namespace {

using ::testing::Field;
using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;

Expand Down Expand Up @@ -216,6 +217,43 @@ TEST(NodeHashMap, MergeExtractInsert) {
EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(17, 23)));
}

bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }

TEST(NodeHashMap, EraseIf) {
// Erase all elements.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, [](std::pair<const int, int>) { return true; });
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, [](std::pair<const int, int>) { return false; });
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
Pair(4, 4), Pair(5, 5)));
}
// Erase specific elements.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s,
[](std::pair<const int, int> kvp) { return kvp.first % 2 == 1; });
EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
}
// Predicate is function reference.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, FirstIsEven);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
// Predicate is function pointer.
{
node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
erase_if(s, &FirstIsEven);
EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
}
}

} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
Expand Down
8 changes: 8 additions & 0 deletions absl/container/node_hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ class node_hash_set
void resize(typename Base::size_type hint) { this->rehash(hint); }
};

// erase_if(node_hash_set<>, Pred)
//
// Erases all elements that satisfy the predicate `pred` from the container `c`.
template <typename T, typename H, typename E, typename A, typename Predicate>
void erase_if(node_hash_set<T, H, E, A>& c, Predicate pred) {
container_internal::EraseIf(pred, &c);
}

namespace container_internal {

template <class T>
Expand Down
36 changes: 36 additions & 0 deletions absl/container/node_hash_set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace container_internal {
namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;

Expand Down Expand Up @@ -101,6 +102,41 @@ TEST(NodeHashSet, MergeExtractInsert) {
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
}

bool IsEven(int k) { return k % 2 == 0; }

TEST(NodeHashSet, EraseIf) {
// Erase all elements.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, [](int) { return true; });
EXPECT_THAT(s, IsEmpty());
}
// Erase no elements.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, [](int) { return false; });
EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
}
// Erase specific elements.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, [](int k) { return k % 2 == 1; });
EXPECT_THAT(s, UnorderedElementsAre(2, 4));
}
// Predicate is function reference.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, IsEven);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
// Predicate is function pointer.
{
node_hash_set<int> s = {1, 2, 3, 4, 5};
erase_if(s, &IsEven);
EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
}
}

} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
Expand Down
4 changes: 1 addition & 3 deletions absl/flags/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = ["//visibility:private"],
deps = [
":config",
":handle",
":registry",
"//absl/base:core_headers",
Expand Down Expand Up @@ -121,9 +122,6 @@ cc_library(

cc_library(
name = "handle",
srcs = [
"internal/commandlineflag.cc",
],
hdrs = [
"internal/commandlineflag.h",
],
Expand Down
3 changes: 1 addition & 2 deletions absl/flags/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ absl_cc_library(
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::flags_config
absl::flags_handle
absl::flags_registry
absl::synchronization
Expand Down Expand Up @@ -108,8 +109,6 @@ absl_cc_library(
absl_cc_library(
NAME
flags_handle
SRCS
"internal/commandlineflag.cc"
HDRS
"internal/commandlineflag.h"
COPTS
Expand Down
Loading

0 comments on commit 1de0166

Please sign in to comment.