Skip to content

Commit

Permalink
Added etl::create_linked_list and etl::detach_linked_list to the intr…
Browse files Browse the repository at this point in the history
…usive link utilities
  • Loading branch information
John Wellbelove committed Feb 27, 2025
1 parent e1b263a commit a28cbdd
Show file tree
Hide file tree
Showing 2 changed files with 533 additions and 0 deletions.
196 changes: 196 additions & 0 deletions include/etl/intrusive_links.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,112 @@ namespace etl
}
}

#if ETL_USING_CPP17
//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_forward_link<TLink>::value, TLink*>::type
create_linked_list(TLink& first, TLinks&... links)
{
TLink* current = &first;
((current->etl_next = &links, current = &links), ...);

return current;
}

//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_forward_link<TLink>::value, TLink*>::type
create_linked_list(TLink* first, TLinks*... links)
{
if (first != ETL_NULLPTR)
{
return create_linked_list(*first, (*links)...);
}
else
{
return nullptr;
}
}
#elif ETL_USING_CPP11
//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_forward_link<TLink>::value, TLink*>::type
create_linked_list(TLink& first)
{
return &first;
}

//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_forward_link<TLink>::value, TLink*>::type
create_linked_list(TLink& first, TLink& next, TLinks&... links)
{
first.etl_next = &next;
return create_linked_list(next, static_cast<TLink&>(links)...);
}

//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_forward_link<TLink>::value, TLink*>::type
create_linked_list(TLink* first)
{
if (first != ETL_NULLPTR)
{
return create_linked_list(*first);
}
else
{
return ETL_NULLPTR;
}
}

//***************************************************************************
/// Create a linked list from a number of forward_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_forward_link<TLink>::value, TLink*>::type
create_linked_list(TLink* first, TLink* next, TLinks*... links)
{
if (first != ETL_NULLPTR)
{
return create_linked_list(*first, *next, static_cast<TLink&>(*links)...);
}
else
{
return ETL_NULLPTR;
}
}
#endif

//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_forward_link<TLink>::value, void>::type
detach_linked_list(TLink& first)
{
link_clear_range(first);
}

//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_forward_link<TLink>::value, void>::type
detach_linked_list(TLink* first)
{
if (first != ETL_NULLPTR)
{
detach_linked_list(*first);
}
}

//***************************************************************************
/// A bidirectional link.
//***************************************************************************
Expand Down Expand Up @@ -829,6 +935,96 @@ namespace etl
etl::link_clear_range(*start);
}

#if ETL_USING_CPP17
//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, TLink*>::type
create_linked_list(TLink& first, TLinks&... links)
{
TLink* current = &first;
((current->etl_next = &links, static_cast<TLink&>(links).etl_previous = current, current = &links), ...);
current->etl_next = ETL_NULLPTR;

return current;
}

//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, TLink*>::type
create_linked_list(TLink* first, TLinks*... links)
{
TLink* current = first;
((current->etl_next = links, static_cast<TLink*>(links)->etl_previous = current, current = links), ...);
current->etl_next = ETL_NULLPTR;

return current;
}
#elif ETL_USING_CPP11

//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, TLink*>::type
create_linked_list(TLink& first)
{
return &first;
}

//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, TLink*>::type
create_linked_list(TLink& first, TLink& next, TLinks&... links)
{
first.etl_next = &next;
next.etl_previous = &first;

return create_linked_list(next, static_cast<TLink&>(links)...);
}

//***************************************************************************
/// Create a linked list from a number of bidirectional_link nodes.
//***************************************************************************
template <typename TLink, typename... TLinks>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, TLink*>::type
create_linked_list(TLink* first, TLink* next, TLinks*... links)
{
if (first != ETL_NULLPTR)
{
return create_linked_list(*first, *next, static_cast<TLink&>(*links)...);
}
else
{
return ETL_NULLPTR;
}
}
#endif

//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, void>::type
detach_linked_list(TLink& first)
{
link_clear_range(first);
}

//***************************************************************************
template <typename TLink>
typename etl::enable_if<etl::is_bidirectional_link<TLink>::value, void>::type
detach_linked_list(TLink* first)
{
if (first != ETL_NULLPTR)
{
detach_linked_list(*first);
}
}

//***************************************************************************
/// A binary tree link.
//***************************************************************************
Expand Down
Loading

0 comments on commit a28cbdd

Please sign in to comment.