From 98dbc62a858e8eac4253eb0763aca57525967a9f Mon Sep 17 00:00:00 2001 From: Arendelle Date: Mon, 17 Nov 2025 13:32:34 +0800 Subject: [PATCH] Feature: fast_io::list::is_single, fast_io::list::has_multiple --- include/fast_io_dsal/impl/list.h | 18 +++++++ .../0002.list/list_is_single.cc | 49 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/0026.container/0002.list/list_is_single.cc diff --git a/include/fast_io_dsal/impl/list.h b/include/fast_io_dsal/impl/list.h index e58c566b..87ac8cf6 100644 --- a/include/fast_io_dsal/impl/list.h +++ b/include/fast_io_dsal/impl/list.h @@ -616,6 +616,24 @@ class list return imp.next == __builtin_addressof(imp); } + /** + * @brief Checks if the list contains only one element. + * @return true if the list contains only one element, false otherwise. + */ + [[nodiscard]] inline constexpr bool is_single() const noexcept + { + return imp.next == imp.prev && imp.next != __builtin_addressof(imp); + } + + /** + * @brief Checks if the list contains multiple elements (>= 2). + * @return true if the list contains multiple elements, false otherwise. + */ + [[nodiscard]] inline constexpr bool has_multiple() const noexcept + { + return imp.next != imp.prev; + } + [[nodiscard]] static inline constexpr size_type max_size() noexcept { constexpr size_type mxvl{SIZE_MAX / sizeof(node_type)}; diff --git a/tests/0026.container/0002.list/list_is_single.cc b/tests/0026.container/0002.list/list_is_single.cc new file mode 100644 index 00000000..4247b2cc --- /dev/null +++ b/tests/0026.container/0002.list/list_is_single.cc @@ -0,0 +1,49 @@ +#include + +int main() { + ::fast_io::list const l{}; + if (!l.is_empty()) { + ::fast_io::fast_terminate(); + } + if (l.is_single()) { + ::fast_io::fast_terminate(); + } + if (l.has_multiple()) { + ::fast_io::fast_terminate(); + } + + ::fast_io::list const l2{1}; + if (l2.is_empty()) { + ::fast_io::fast_terminate(); + } + if (!l2.is_single()) { + ::fast_io::fast_terminate(); + } + if (l2.has_multiple()) { + ::fast_io::fast_terminate(); + } + + ::fast_io::list const l3{1, 2}; + if (l3.is_empty()) { + ::fast_io::fast_terminate(); + } + if (l3.is_single()) { + ::fast_io::fast_terminate(); + } + if (!l3.has_multiple()) { + ::fast_io::fast_terminate(); + } + + ::fast_io::list l4{1, 2, 3}; + if (l4.is_empty()) { + ::fast_io::fast_terminate(); + } + if (l4.is_single()) { + ::fast_io::fast_terminate(); + } + if (!l4.has_multiple()) { + ::fast_io::fast_terminate(); + } + + return 0; +}