This is an automated email from the ASF dual-hosted git repository. bneradt pushed a commit to branch dev-1-0-14 in repository https://gitbox.apache.org/repos/asf/trafficserver-libswoc.git
commit e8c587796efdf127df5eeb4e82e2048b4aab025d Author: Alan M. Carroll <[email protected]> AuthorDate: Tue Mar 3 09:50:40 2020 -0600 Change IPSpace to return iterator instead of PAYLOAD* --- swoc++/include/swoc/DiscreteRange.h | 15 ++++---- swoc++/include/swoc/swoc_ip.h | 68 +++++++++++++++++++------------------ unit_tests/ex_ipspace_properties.cc | 3 +- unit_tests/test_IntrusiveDList.cc | 6 ++++ unit_tests/test_ip.cc | 59 ++++++++++++++++---------------- 5 files changed, 80 insertions(+), 71 deletions(-) diff --git a/swoc++/include/swoc/DiscreteRange.h b/swoc++/include/swoc/DiscreteRange.h index c76f235..f4dc4d9 100644 --- a/swoc++/include/swoc/DiscreteRange.h +++ b/swoc++/include/swoc/DiscreteRange.h @@ -698,6 +698,7 @@ protected: public: using iterator = typename decltype(_list)::iterator; + using const_iterator = typename decltype(_list)::const_iterator; DiscreteSpace() = default; ~DiscreteSpace(); @@ -756,7 +757,7 @@ public: * @param metric The metric for which to search. * @return The payload for @a metric if found, @c nullptr if not found. */ - PAYLOAD * find(METRIC const &metric); + iterator find(METRIC const &metric); /// @return The number of distinct ranges. size_t count() const; @@ -870,27 +871,27 @@ DiscreteSpace<METRIC, PAYLOAD>::head() -> Node *{ } template <typename METRIC, typename PAYLOAD> -PAYLOAD * -DiscreteSpace<METRIC, PAYLOAD>::find(METRIC const &metric) { +auto +DiscreteSpace<METRIC, PAYLOAD>::find(METRIC const &metric) -> iterator { auto n = _root; // current node to test. while (n) { if (metric < n->min()) { if (n->_hull.contains(metric)) { n = n->left(); } else { - return nullptr; + return this->end(); } } else if (n->max() < metric) { if (n->_hull.contains(metric)) { n = n->right(); } else { - return nullptr; + return this->end(); } } else { - return &n->payload(); + return _list.iterator_for(n); } } - return nullptr; + return this->end(); } template <typename METRIC, typename PAYLOAD> diff --git a/swoc++/include/swoc/swoc_ip.h b/swoc++/include/swoc/swoc_ip.h index 4d40e3f..85c7d8a 100644 --- a/swoc++/include/swoc/swoc_ip.h +++ b/swoc++/include/swoc/swoc_ip.h @@ -1,5 +1,6 @@ #pragma once #pragma once +#pragma once // SPDX-License-Identifier: Apache-2.0 /** @file IP address and network related classes. @@ -1438,38 +1439,6 @@ public: return *this; } - /** Find the payload for an @a addr. - * - * @param addr Address to find. - * @return The payload if any, @c nullptr if the address is not in the space. - */ - PAYLOAD *find(IP4Addr const& addr) { - return _ip4.find(addr); - } - - /** Find the payload for an @a addr. - * - * @param addr Address to find. - * @return The payload if any, @c nullptr if the address is not in the space. - */ - PAYLOAD *find(IP6Addr const& addr) { - return _ip6.find(addr); - } - - /** Find the payload for an @a addr. - * - * @param addr Address to find. - * @return The payload if any, @c nullptr if the address is not in the space. - */ - PAYLOAD *find(IPAddr const& addr) { - if (addr.is_ip4()) { - return _ip4.find(IP4Addr{addr}); - } else if (addr.is_ip6()) { - return _ip6.find(IP6Addr{addr}); - } - return nullptr; - } - /// @return The number of distinct ranges. size_t count() const { return _ip4.count() + _ip6.count(); } @@ -1564,7 +1533,7 @@ public: }; /** Iterator. - * THe value type is a tuple of the IP address range and the @a PAYLOAD. The range is constant + * The value type is a tuple of the IP address range and the @a PAYLOAD. The range is constant * and the @a PAYLOAD is a reference. This can be used to update the @a PAYLOAD for this range. * * @note Range merges are not trigged by modifications of the @a PAYLOAD via an iterator. @@ -1631,6 +1600,39 @@ public: using super_type::super_type; /// Inherit supertype constructors. }; + /** Find the payload for an @a addr. + * + * @param addr Address to find. + * @return Iterator for the range containing @a addr. + */ + iterator find(IPAddr const& addr) { + if (addr.is_ip4()) { + return iterator(_ip4.find(IP4Addr{addr}), _ip6.begin()); + } else if (addr.is_ip6()) { + return iterator(_ip4.end(), _ip6.find(IP6Addr{addr})); + } + return this->end(); + } + + /** Find the payload for an @a addr. + * + * @param addr Address to find. + * @return The payload if any, @c nullptr if the address is not in the space. + */ + iterator find(IP4Addr const& addr) { + auto spot = _ip4.find(addr); + return spot == _ip4.end() ? this->end() : iterator{_ip4.find(addr), _ip6.begin()}; + } + + /** Find the payload for an @a addr. + * + * @param addr Address to find. + * @return The payload if any, @c nullptr if the address is not in the space. + */ + iterator find(IP6Addr const& addr) { + return { _ip4.end(), _ip6.find(addr) }; + } + /// @return A constant iterator to the first element. const_iterator begin() const; diff --git a/unit_tests/ex_ipspace_properties.cc b/unit_tests/ex_ipspace_properties.cc index 3420e6a..c992933 100644 --- a/unit_tests/ex_ipspace_properties.cc +++ b/unit_tests/ex_ipspace_properties.cc @@ -409,7 +409,8 @@ bool Table::parse(TextView src) { } auto Table::find(IPAddr const &addr) -> Row * { - return _space.find(addr); + auto spot = _space.find(addr); + return spot == _space.end() ? nullptr : &std::get<1>(*spot); } bool operator == (Table::Row const&, Table::Row const&) { return false; } diff --git a/unit_tests/test_IntrusiveDList.cc b/unit_tests/test_IntrusiveDList.cc index 15f1c43..3c0c72a 100644 --- a/unit_tests/test_IntrusiveDList.cc +++ b/unit_tests/test_IntrusiveDList.cc @@ -98,6 +98,7 @@ TEST_CASE("IntrusiveDList", "[libswoc][IntrusiveDList]") REQUIRE((*spot++)._payload == "muddle"); REQUIRE((*spot++)._payload == "two"); REQUIRE(spot == list.end()); + spot = list.begin(); // verify assignment works. Thing *thing = list.take_head(); REQUIRE(thing->_payload == "one"); @@ -128,4 +129,9 @@ TEST_CASE("IntrusiveDList", "[libswoc][IntrusiveDList]") list.insert_before(list.end(), new Thing("trailer")); REQUIRE(list.count() == 4); REQUIRE(list.tail()->_payload == "trailer"); + + for ( auto const& elt : list) { + + } + } diff --git a/unit_tests/test_ip.cc b/unit_tests/test_ip.cc index a088e72..0d4b683 100644 --- a/unit_tests/test_ip.cc +++ b/unit_tests/test_ip.cc @@ -532,17 +532,17 @@ TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") { space.mark(IPRange{{IP4Addr("172.16.0.0"), IP4Addr("172.16.0.255")}}, 1); auto result = space.find(IPAddr{"172.16.0.97"}); - REQUIRE(result != nullptr); - REQUIRE(*result == 1); + REQUIRE(result != space.end()); + REQUIRE(std::get<1>(*result) == 1); result = space.find(IPAddr{"172.17.0.97"}); - REQUIRE(result == nullptr); + REQUIRE(result == space.end()); space.mark(IPRange{"172.16.0.12-172.16.0.25"_tv}, 2); result = space.find(IPAddr{"172.16.0.21"}); - REQUIRE(result != nullptr); - REQUIRE(*result == 2); + REQUIRE(result != space.end()); + REQUIRE(std::get<1>(*result) == 2); REQUIRE(space.count() == 3); space.clear(); @@ -561,35 +561,35 @@ TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") { space.blend(r_1, 0x1, BF); REQUIRE(space.count() == 1); - REQUIRE(nullptr == space.find(r_2.min())); - REQUIRE(nullptr != space.find(r_1.min())); - REQUIRE(nullptr != space.find(r_1.max())); - REQUIRE(nullptr != space.find(IP4Addr{"1.1.1.7"})); - CHECK(0x1 == *space.find(IP4Addr{"1.1.1.7"})); + REQUIRE(space.end() == space.find(r_2.min())); + REQUIRE(space.end() != space.find(r_1.min())); + REQUIRE(space.end() != space.find(r_1.max())); + REQUIRE(space.end() != space.find(IP4Addr{"1.1.1.7"})); + CHECK(0x1 == std::get<1>(*space.find(IP4Addr{"1.1.1.7"}))); space.blend(r_2, 0x2, BF); REQUIRE(space.count() == 2); - REQUIRE(nullptr != space.find(r_1.min())); - payload = space.find(r_2.min()); - REQUIRE(payload != nullptr); - REQUIRE(*payload == 0x2); - payload = space.find(r_2.max()); - REQUIRE(payload != nullptr); - REQUIRE(*payload == 0x2); + REQUIRE(space.end() != space.find(r_1.min())); + auto spot = space.find(r_2.min()); + REQUIRE(spot != space.end()); + REQUIRE(std::get<1>(*spot) == 0x2); + spot = space.find(r_2.max()); + REQUIRE(spot != space.end()); + REQUIRE(std::get<1>(*spot) == 0x2); space.blend(r_3, 0x4, BF); REQUIRE(space.count() == 5); - payload = space.find(r_2.min()); - REQUIRE(payload != nullptr); - REQUIRE(*payload == 0x6); + spot = space.find(r_2.min()); + REQUIRE(spot != space.end()); + REQUIRE(std::get<1>(*spot) == 0x6); - payload = space.find(r_3.min()); - REQUIRE(payload != nullptr); - REQUIRE(*payload == 0x4); + spot = space.find(r_3.min()); + REQUIRE(spot != space.end()); + REQUIRE(std::get<1>(*spot) == 0x4); - payload = space.find(r_1.max()); - REQUIRE(payload != nullptr); - REQUIRE(*payload == 0x5); + spot = space.find(r_1.max()); + REQUIRE(spot != space.end()); + REQUIRE(std::get<1>(*spot) == 0x5); space.blend(IPRange{r_2.min(), r_3.max()}, 0x6, BF); REQUIRE(space.count() == 4); @@ -614,9 +614,9 @@ TEST_CASE("IP Space Int", "[libswoc][ip][ipspace]") { } CHECK(7 == space.count()); - CHECK(nullptr != space.find(IP4Addr{"100.0.4.16"})); - CHECK(nullptr != space.find(IPAddr{"100.0.4.16"})); - CHECK(nullptr != space.find(IPAddr{IPEndpoint{"100.0.4.16:80"}})); + CHECK(space.end() != space.find(IP4Addr{"100.0.4.16"})); + CHECK(space.end() != space.find(IPAddr{"100.0.4.16"})); + CHECK(space.end() != space.find(IPAddr{IPEndpoint{"100.0.4.16:80"}})); } TEST_CASE("IPSpace bitset", "[libswoc][ipspace][bitset]") { @@ -686,7 +686,6 @@ TEST_CASE("IPSpace docJJ", "[libswoc][ipspace][docJJ]") { Space space; for (auto && [text, bit_list] : ranges) { - std::cout << W().print("{} = {}\n", text, bit_list); space.blend(IPRange{text}, bit_list, blender); }
