[PATCH v7 2/4] libstdc++: Implement operator<=> for hive::iterator

Tomasz Kamiński tkaminsk@redhat.com
Fri Dec 4 16:28:43 GMT 2026


The operator<=> is O(blocks) operation, as we do not maintain the
position inside block (this allow efficient splicing). The standard
requires the hive::iterator to model three_way_comparable, but does
not specify the iterator as random access, so it does not require
to be O(1), so this is conforming, but we may want to revisit.

libstdc++-v3/ChangeLog:

	* include/bits/hive.h
	(operator<=>(__hive::_Iterator, __hive_Iterator)): Define.
	* testsuite/23_containers/hive/access.cc: Add test for
	iterator comparison.
---
 libstdc++-v3/include/bits/hive.h              | 23 ++++++++++++++++
 .../testsuite/23_containers/hive/access.cc    | 27 +++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/libstdc++-v3/include/bits/hive.h b/libstdc++-v3/include/bits/hive.h
index 9fd03f4aac2..8a9ed8ffb34 100644
--- a/libstdc++-v3/include/bits/hive.h
+++ b/libstdc++-v3/include/bits/hive.h
@@ -1139,6 +1139,29 @@ namespace __hive
       const _BlockT* _M_block = nullptr;
       size_t _M_pos = 0;
 
+      friend constexpr std::strong_ordering
+      operator<=>(_Iterator __lhs, _Iterator __rhs) noexcept
+      {
+	if (__lhs._M_block == __rhs._M_block)
+	  return __lhs._M_pos <=> __rhs._M_pos;
+	if (!__lhs._M_block || !__rhs._M_block)
+	  {
+	    __glibcxx_assert(!"Comparing element iterator against value-initialized iterator");
+	    return bool(__lhs._M_block) <=> bool(__rhs._M_block);
+	  }
+
+	const _BlockT* __block = __lhs._M_block;
+	while (!__block->_M_sentinel())
+	  {
+	    __block = __block->_M_next;
+	    if (__block == __rhs._M_block)
+	      return std::strong_ordering::less;
+	  }
+
+	// Different range or previous block
+	return std::strong_ordering::greater;
+      }
+
       template<typename, typename, typename>
 	friend struct _Iterator;
       template<typename, typename>
diff --git a/libstdc++-v3/testsuite/23_containers/hive/access.cc b/libstdc++-v3/testsuite/23_containers/hive/access.cc
index 0ad5903e69c..588076d300a 100644
--- a/libstdc++-v3/testsuite/23_containers/hive/access.cc
+++ b/libstdc++-v3/testsuite/23_containers/hive/access.cc
@@ -59,9 +59,36 @@ test_access(std::hive_limits hl)
   }
 }
 
+void
+test_iter_cmp()
+{
+  std::hive<int> h(std::hive_limits(2, 5));
+  VERIFY( h.begin() == h.end() );
+  VERIFY( h.cbegin() == h.cend() );
+  VERIFY( h.begin() == h.cend() );
+  VERIFY( h.cbegin() == h.end() );
+
+  h.insert(20, -1);
+  auto mid = std::next(h.begin(), 10);
+  VERIFY( h.begin() != h.end() ); 
+  VERIFY( h.begin() < h.end() );
+
+  VERIFY( h.begin() != mid );
+  VERIFY( h.begin() < mid );
+  VERIFY( h.end() != mid );
+  VERIFY( h.end() > mid );
+
+  std::hive<int>::iterator it;
+  std::hive<int>::const_iterator cit;
+  VERIFY( it == it );
+  VERIFY( it == cit );
+}
+
 int main()
 {
   test_access(std::hive<int>::block_capacity_default_limits());
   test_access(std::hive_limits(10, 50));
   test_access(std::hive_limits(2, 7));
+
+  test_iter_cmp();
 }
-- 
2.56.1



More information about the Libstdc++ mailing list