github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/collection/listx/list.go (about)

     1  package listx
     2  
     3  import "container/list"
     4  
     5  // Element is an element of a linked list.
     6  type Element = list.Element
     7  
     8  // List represents a doubly linked list.
     9  // The zero value for List is an empty list ready to use.
    10  type List[T any] list.List
    11  
    12  // NewList returns an initialized list.
    13  func NewList[T any]() *List[T] {
    14  	return (*List[T])(list.New())
    15  }
    16  
    17  // Len returns the number of elements of list l.
    18  // The complexity is O(1).
    19  func (l *List[T]) Len() int {
    20  	return (*list.List)(l).Len()
    21  }
    22  
    23  // Front returns the first element of list l or nil if the list is empty.
    24  func (l *List[T]) Front() *Element {
    25  	return (*list.List)(l).Front()
    26  }
    27  
    28  // Back returns the last element of list l or nil if the list is empty.
    29  func (l *List[T]) Back() *Element {
    30  	return (*list.List)(l).Back()
    31  }
    32  
    33  // Remove removes e from l if e is an element of list l.
    34  // It returns the element value e.Value.
    35  // The element must not be nil.
    36  func (l *List[T]) Remove(e *Element) T {
    37  	return (*list.List)(l).Remove(e).(T)
    38  }
    39  
    40  // PushFront inserts a new element e with value v at the front of list l and returns e.
    41  func (l *List[T]) PushFront(v T) *Element {
    42  	return (*list.List)(l).PushFront(v)
    43  }
    44  
    45  // PushBack inserts a new element e with value v at the back of list l and returns e.
    46  func (l *List[T]) PushBack(v T) *Element {
    47  	return (*list.List)(l).PushBack(v)
    48  }
    49  
    50  // InsertBefore inserts a new element e with value v immediately before mark and returns e.
    51  // If mark is not an element of l, the list is not modified.
    52  // The mark must not be nil.
    53  func (l *List[T]) InsertBefore(v T, mark *Element) *Element {
    54  	return (*list.List)(l).InsertBefore(v, mark)
    55  }
    56  
    57  // InsertAfter inserts a new element e with value v immediately after mark and returns e.
    58  // If mark is not an element of l, the list is not modified.
    59  // The mark must not be nil.
    60  func (l *List[T]) InsertAfter(v T, mark *Element) *Element {
    61  	return (*list.List)(l).InsertAfter(v, mark)
    62  }
    63  
    64  // MoveToFront moves element e to the front of list l.
    65  // If e is not an element of l, the list is not modified.
    66  // The element must not be nil.
    67  func (l *List[T]) MoveToFront(e *Element) {
    68  	(*list.List)(l).MoveToFront(e)
    69  }
    70  
    71  // MoveToBack moves element e to the back of list l.
    72  // If e is not an element of l, the list is not modified.
    73  // The element must not be nil.
    74  func (l *List[T]) MoveToBack(e *Element) {
    75  	(*list.List)(l).MoveToBack(e)
    76  }
    77  
    78  // MoveBefore moves element e to its new position before mark.
    79  // If e or mark is not an element of l, or e == mark, the list is not modified.
    80  // The element and mark must not be nil.
    81  func (l *List[T]) MoveBefore(e, mark *Element) {
    82  	(*list.List)(l).MoveBefore(e, mark)
    83  }
    84  
    85  // MoveAfter moves element e to its new position after mark.
    86  // If e or mark is not an element of l, or e == mark, the list is not modified.
    87  // The element and mark must not be nil.
    88  func (l *List[T]) MoveAfter(e, mark *Element) {
    89  	(*list.List)(l).MoveAfter(e, mark)
    90  }
    91  
    92  // PushBackList inserts a copy of another list at the back of list l.
    93  // The lists l and other may be the same. They must not be nil.
    94  func (l *List[T]) PushBackList(other *List[T]) {
    95  	(*list.List)(l).PushBackList((*list.List)(other))
    96  }
    97  
    98  // PushFrontList inserts a copy of another list at the front of list l.
    99  // The lists l and other may be the same. They must not be nil.
   100  func (l *List[T]) PushFrontList(other *List[T]) {
   101  	(*list.List)(l).PushFrontList((*list.List)(other))
   102  }