Source file test/typeparam/listimp2.dir/a.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package a
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  // Element is an element of a linked list.
    12  type Element[T any] struct {
    13  	// Next and previous pointers in the doubly-linked list of elements.
    14  	// To simplify the implementation, internally a list l is implemented
    15  	// as a ring, such that &l.root is both the next element of the last
    16  	// list element (l.Back()) and the previous element of the first list
    17  	// element (l.Front()).
    18  	next, prev *Element[T]
    19  
    20  	// The list to which this element belongs.
    21  	list *List[T]
    22  
    23  	// The value stored with this element.
    24  	Value T
    25  }
    26  
    27  // Next returns the next list element or nil.
    28  func (e *Element[T]) Next() *Element[T] {
    29  	if p := e.next; e.list != nil && p != &e.list.root {
    30  		return p
    31  	}
    32  	return nil
    33  }
    34  
    35  // Prev returns the previous list element or nil.
    36  func (e *Element[T]) Prev() *Element[T] {
    37  	if p := e.prev; e.list != nil && p != &e.list.root {
    38  		return p
    39  	}
    40  	return nil
    41  }
    42  
    43  // List represents a doubly linked list.
    44  // The zero value for List is an empty list ready to use.
    45  type List[T any] struct {
    46  	root Element[T] // sentinel list element, only &root, root.prev, and root.next are used
    47  	len  int        // current list length excluding (this) sentinel element
    48  }
    49  
    50  // Init initializes or clears list l.
    51  func (l *List[T]) Init() *List[T] {
    52  	l.root.next = &l.root
    53  	l.root.prev = &l.root
    54  	l.len = 0
    55  	return l
    56  }
    57  
    58  // New returns an initialized list.
    59  func New[T any]() *List[T] { return new(List[T]).Init() }
    60  
    61  // Len returns the number of elements of list l.
    62  // The complexity is O(1).
    63  func (l *List[_]) Len() int { return l.len }
    64  
    65  // Front returns the first element of list l or nil if the list is empty.
    66  func (l *List[T]) Front() *Element[T] {
    67  	if l.len == 0 {
    68  		return nil
    69  	}
    70  	return l.root.next
    71  }
    72  
    73  // Back returns the last element of list l or nil if the list is empty.
    74  func (l *List[T]) Back() *Element[T] {
    75  	if l.len == 0 {
    76  		return nil
    77  	}
    78  	return l.root.prev
    79  }
    80  
    81  // lazyInit lazily initializes a zero List value.
    82  func (l *List[_]) lazyInit() {
    83  	if l.root.next == nil {
    84  		l.Init()
    85  	}
    86  }
    87  
    88  // insert inserts e after at, increments l.len, and returns e.
    89  func (l *List[T]) insert(e, at *Element[T]) *Element[T] {
    90  	e.prev = at
    91  	e.next = at.next
    92  	e.prev.next = e
    93  	e.next.prev = e
    94  	e.list = l
    95  	l.len++
    96  	return e
    97  }
    98  
    99  // insertValue is a convenience wrapper for insert(&Element[T]{Value: v}, at).
   100  func (l *List[T]) insertValue(v T, at *Element[T]) *Element[T] {
   101  	return l.insert(&Element[T]{Value: v}, at)
   102  }
   103  
   104  // remove removes e from its list, decrements l.len, and returns e.
   105  func (l *List[T]) remove(e *Element[T]) *Element[T] {
   106  	e.prev.next = e.next
   107  	e.next.prev = e.prev
   108  	e.next = nil // avoid memory leaks
   109  	e.prev = nil // avoid memory leaks
   110  	e.list = nil
   111  	l.len--
   112  	return e
   113  }
   114  
   115  // move moves e to next to at and returns e.
   116  func (l *List[T]) move(e, at *Element[T]) *Element[T] {
   117  	if e == at {
   118  		return e
   119  	}
   120  	e.prev.next = e.next
   121  	e.next.prev = e.prev
   122  
   123  	e.prev = at
   124  	e.next = at.next
   125  	e.prev.next = e
   126  	e.next.prev = e
   127  
   128  	return e
   129  }
   130  
   131  // Remove removes e from l if e is an element of list l.
   132  // It returns the element value e.Value.
   133  // The element must not be nil.
   134  func (l *List[T]) Remove(e *Element[T]) T {
   135  	if e.list == l {
   136  		// if e.list == l, l must have been initialized when e was inserted
   137  		// in l or l == nil (e is a zero Element) and l.remove will crash
   138  		l.remove(e)
   139  	}
   140  	return e.Value
   141  }
   142  
   143  // PushFront inserts a new element e with value v at the front of list l and returns e.
   144  func (l *List[T]) PushFront(v T) *Element[T] {
   145  	l.lazyInit()
   146  	return l.insertValue(v, &l.root)
   147  }
   148  
   149  // PushBack inserts a new element e with value v at the back of list l and returns e.
   150  func (l *List[T]) PushBack(v T) *Element[T] {
   151  	l.lazyInit()
   152  	return l.insertValue(v, l.root.prev)
   153  }
   154  
   155  // InsertBefore inserts a new element e with value v immediately before mark and returns e.
   156  // If mark is not an element of l, the list is not modified.
   157  // The mark must not be nil.
   158  func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T] {
   159  	if mark.list != l {
   160  		return nil
   161  	}
   162  	// see comment in List.Remove about initialization of l
   163  	return l.insertValue(v, mark.prev)
   164  }
   165  
   166  // InsertAfter inserts a new element e with value v immediately after mark and returns e.
   167  // If mark is not an element of l, the list is not modified.
   168  // The mark must not be nil.
   169  func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T] {
   170  	if mark.list != l {
   171  		return nil
   172  	}
   173  	// see comment in List.Remove about initialization of l
   174  	return l.insertValue(v, mark)
   175  }
   176  
   177  // MoveToFront moves element e to the front of list l.
   178  // If e is not an element of l, the list is not modified.
   179  // The element must not be nil.
   180  func (l *List[T]) MoveToFront(e *Element[T]) {
   181  	if e.list != l || l.root.next == e {
   182  		return
   183  	}
   184  	// see comment in List.Remove about initialization of l
   185  	l.move(e, &l.root)
   186  }
   187  
   188  // MoveToBack moves element e to the back of list l.
   189  // If e is not an element of l, the list is not modified.
   190  // The element must not be nil.
   191  func (l *List[T]) MoveToBack(e *Element[T]) {
   192  	if e.list != l || l.root.prev == e {
   193  		return
   194  	}
   195  	// see comment in List.Remove about initialization of l
   196  	l.move(e, l.root.prev)
   197  }
   198  
   199  // MoveBefore moves element e to its new position before mark.
   200  // If e or mark is not an element of l, or e == mark, the list is not modified.
   201  // The element and mark must not be nil.
   202  func (l *List[T]) MoveBefore(e, mark *Element[T]) {
   203  	if e.list != l || e == mark || mark.list != l {
   204  		return
   205  	}
   206  	l.move(e, mark.prev)
   207  }
   208  
   209  // MoveAfter moves element e to its new position after mark.
   210  // If e or mark is not an element of l, or e == mark, the list is not modified.
   211  // The element and mark must not be nil.
   212  func (l *List[T]) MoveAfter(e, mark *Element[T]) {
   213  	if e.list != l || e == mark || mark.list != l {
   214  		return
   215  	}
   216  	l.move(e, mark)
   217  }
   218  
   219  // PushBackList inserts a copy of an other list at the back of list l.
   220  // The lists l and other may be the same. They must not be nil.
   221  func (l *List[T]) PushBackList(other *List[T]) {
   222  	l.lazyInit()
   223  	for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
   224  		l.insertValue(e.Value, l.root.prev)
   225  	}
   226  }
   227  
   228  // PushFrontList inserts a copy of an other list at the front of list l.
   229  // The lists l and other may be the same. They must not be nil.
   230  func (l *List[T]) PushFrontList(other *List[T]) {
   231  	l.lazyInit()
   232  	for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
   233  		l.insertValue(e.Value, &l.root)
   234  	}
   235  }
   236  
   237  // Transform runs a transform function on a list returning a new list.
   238  func Transform[TElem1, TElem2 any](lst *List[TElem1], f func(TElem1) TElem2) *List[TElem2] {
   239  	ret := New[TElem2]()
   240  	for p := lst.Front(); p != nil; p = p.Next() {
   241  		ret.PushBack(f(p.Value))
   242  	}
   243  	return ret
   244  }
   245  
   246  func CheckListLen[T any](l *List[T], len int) bool {
   247  	if n := l.Len(); n != len {
   248  		panic(fmt.Sprintf("l.Len() = %d, want %d", n, len))
   249  		return false
   250  	}
   251  	return true
   252  }
   253  
   254  func CheckListPointers[T any](l *List[T], es []*Element[T]) {
   255  	root := &l.root
   256  
   257  	if !CheckListLen(l, len(es)) {
   258  		return
   259  	}
   260  
   261  	// zero length lists must be the zero value or properly initialized (sentinel circle)
   262  	if len(es) == 0 {
   263  		if l.root.next != nil && l.root.next != root || l.root.prev != nil && l.root.prev != root {
   264  			panic(fmt.Sprintf("l.root.next = %p, l.root.prev = %p; both should both be nil or %p", l.root.next, l.root.prev, root))
   265  		}
   266  		return
   267  	}
   268  	// len(es) > 0
   269  
   270  	// check internal and external prev/next connections
   271  	for i, e := range es {
   272  		prev := root
   273  		Prev := (*Element[T])(nil)
   274  		if i > 0 {
   275  			prev = es[i-1]
   276  			Prev = prev
   277  		}
   278  		if p := e.prev; p != prev {
   279  			panic(fmt.Sprintf("elt[%d](%p).prev = %p, want %p", i, e, p, prev))
   280  		}
   281  		if p := e.Prev(); p != Prev {
   282  			panic(fmt.Sprintf("elt[%d](%p).Prev() = %p, want %p", i, e, p, Prev))
   283  		}
   284  
   285  		next := root
   286  		Next := (*Element[T])(nil)
   287  		if i < len(es)-1 {
   288  			next = es[i+1]
   289  			Next = next
   290  		}
   291  		if n := e.next; n != next {
   292  			panic(fmt.Sprintf("elt[%d](%p).next = %p, want %p", i, e, n, next))
   293  		}
   294  		if n := e.Next(); n != Next {
   295  			panic(fmt.Sprintf("elt[%d](%p).Next() = %p, want %p", i, e, n, Next))
   296  		}
   297  	}
   298  }
   299  

View as plain text