Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dlink.H File Reference

Doubly linked circular list implementation. More...

#include <aleph.H>
#include <ah-errors.H>
Include dependency graph for dlink.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::Dlink
 Doubly linked circular list node. More...
 
class  Aleph::Dlink::Iterator
 Iterator on Dlink lists. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Macros

#define DLINK_TO_TYPE(type_name, link_name)
 Generate a conversion function from a Dlink field to a class containing it.
 
#define LINKNAME_TO_TYPE(type_name, link_name)
 Generate a conversion function from a Dlink field pointer to a pointer to the class containing it.
 
#define DLINK_TO_BASE(type_name, link_name)
 Generate a function with name link_to_base() what converts a Dlink pointer to the class or struct that contains it.
 

Detailed Description

Doubly linked circular list implementation.

This file provides the Dlink class, a low-level building block for doubly linked circular lists with a sentinel header node. The design allows O(1) insertion and removal at any position, and O(1) list concatenation and splitting.

The circular design with sentinel node simplifies boundary conditions and enables symmetric forward/backward traversal.

Author
Leandro Rabindranath León

Definition in file dlink.H.

Macro Definition Documentation

◆ DLINK_TO_BASE

#define DLINK_TO_BASE (   type_name,
  link_name 
)
Value:
inline static type_name * dlink_to_base(Dlink * link) noexcept \
{ \
type_name * ptr_zero = 0; \
size_t offset_link = reinterpret_cast<size_t>(&(ptr_zero->link_name)); \
char * address_type = reinterpret_cast<char*>(link) - offset_link; \
return reinterpret_cast<type_name *>(address_type); \
}

Generate a function with name link_to_base() what converts a Dlink pointer to the class or struct that contains it.

This macro could be used when a class have a Dlink field y and from it it is desired to get a pointer to the class. For example, suppose some such as:

struct Record { ... Dlink l; ... };

Then DLINK_TO_BASE(Record, l) will produce the function:

Record * dlink_to_base(Dlink * link)

which receives a pointer to the Dlink field l and return a pointer to the Record object storing l.

Parameters
type_namethe name of class of struct containing the Dlink field.
link_namethe name of Dlink field inside the class or struct

Definition at line 1029 of file dlink.H.

◆ DLINK_TO_TYPE

#define DLINK_TO_TYPE (   type_name,
  link_name 
)
Value:
inline static type_name * dlink_to_##type_name(Dlink * link) noexcept \
{ \
type_name * ptr_zero = 0; \
size_t offset_link = reinterpret_cast<size_t>(&(ptr_zero->link_name)); \
char * address_type = reinterpret_cast<char*>(link) - offset_link; \
return reinterpret_cast<type_name *>(address_type); \
}

Generate a conversion function from a Dlink field to a class containing it.

This macro is used when inside a class exists one o more Dlink fields and it is needed to obtain from a Dlink field a pointer to the class containing it.

For example, suppose the following situation:

struct Record { ... Dlink l; ... };

So, DLINK_TO_TYPE(Record, l) will generate the function:

Record * dlink_to_Record(Dlink * link)

which receives a pointer to the Dlink field l and returns a pointer to the class Record containing l.

Parameters
type_namethe name of class or struct containing the Dlink field
link_namethe name of the Dlink field inside the class or struct.

Definition at line 943 of file dlink.H.

◆ LINKNAME_TO_TYPE

#define LINKNAME_TO_TYPE (   type_name,
  link_name 
)
Value:
inline static type_name * link_name##_to_##type_name(Dlink * link) noexcept \
{ \
type_name * ptr_zero = 0; \
size_t offset_link = reinterpret_cast<size_t>(&(ptr_zero->link_name)); \
char * address_type = reinterpret_cast<char*>(link) - offset_link; \
return reinterpret_cast<type_name *>(address_type); \
}

Generate a conversion function from a Dlink field pointer to a pointer to the class containing it.

The name of the function is literally the name of second parameter

This macro is used when one o more Dlink field are used inside a class and it is desired to obtain pointers to the class from the Dlink fields. Consider for example,

struct Redcordgistro { ... Dlink l1; Dlink l2; ... };

So, LINKNAME_TO_TYPE(Record, l1) and LINKNAME_TO_TYPE(Record, l2) will generate the following functions:

Record * l1_to_type(Dlink * link)

Record * l2_to_type(Dlink * link)

So for converting a Dlink pointer to l1 to a pointer to Record object which contains it, you use l1_to_type(link); analogously with l2_to_type(link).

The idea is having naming schemes allowing to distinguish several Dlink fields.

Note
You could have several Dlink fields. This situation arises if you want that the class simultaneously belongs to several lists. In this case you would declare a Dlink field by each different list.
Parameters
type_namethe name of struct or class containing to the Dlink object.
link_namethe name of Dlink field

Definition at line 993 of file dlink.H.