Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/fast_io_dsal/impl/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
Expand Down
49 changes: 49 additions & 0 deletions tests/0026.container/0002.list/list_is_single.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <fast_io_dsal/list.h>

int main() {
::fast_io::list<int> 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<int> 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<int> 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<int> 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;
}