github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/ds/link2.go (about)

     1  package ds
     2  
     3  import "github.com/15mga/kiwi/util"
     4  
     5  func NewLink2[T comparable]() *Link2[T] {
     6  	return &Link2[T]{
     7  		def: util.Default[T](),
     8  	}
     9  }
    10  
    11  type Link2[T comparable] struct {
    12  	head  *link2Elem[T]
    13  	count uint32
    14  	def   T
    15  }
    16  
    17  func (l *Link2[T]) Count() uint32 {
    18  	return l.count
    19  }
    20  
    21  func (l *Link2[T]) Push(item T) {
    22  	if l.count == 0 {
    23  		e := newLink2Elem[T]()
    24  		l.head = e
    25  	}
    26  	_ = l.head.push(item)
    27  	l.count++
    28  }
    29  
    30  func (l *Link2[T]) Del(item T) bool {
    31  	if l.head == nil {
    32  		return false
    33  	}
    34  	ok := l.head.del(item, l.def)
    35  	if ok {
    36  		l.count--
    37  	}
    38  	return ok
    39  }
    40  
    41  func (l *Link2[T]) Iter(fn func([]T)) {
    42  	if l.head == nil {
    43  		return
    44  	}
    45  	l.head.iter(fn)
    46  }
    47  
    48  func (l *Link2[T]) Reset() {
    49  	if l.head == nil {
    50  		return
    51  	}
    52  	l.head.reset(l.def)
    53  	l.head = nil
    54  	l.count = 0
    55  }
    56  
    57  func (l *Link2[T]) IterAndReset(fn func([]T)) {
    58  	if l.head == nil {
    59  		return
    60  	}
    61  	l.head.iterAndReset(fn, l.def)
    62  	l.head = nil
    63  }
    64  
    65  func newLink2Elem[T comparable]() *link2Elem[T] {
    66  	return &link2Elem[T]{
    67  		slc: make([]T, 32),
    68  		cap: 32,
    69  	}
    70  }
    71  
    72  type link2Elem[T comparable] struct {
    73  	slc  []T
    74  	idx  uint8
    75  	cap  uint8
    76  	next *link2Elem[T]
    77  }
    78  
    79  func (e *link2Elem[T]) push(item T) *link2Elem[T] {
    80  	if e.idx < e.cap {
    81  		e.slc[e.idx] = item
    82  		e.idx++
    83  		return e
    84  	}
    85  	ne := newLink2Elem[T]()
    86  	ne.push(item)
    87  	return ne
    88  }
    89  
    90  func (e *link2Elem[T]) del(item T, def T) bool {
    91  	for i, t := range e.slc {
    92  		if t == item {
    93  			e.idx--
    94  			if uint8(i) == e.idx {
    95  				e.slc[i] = def
    96  				return true
    97  			}
    98  			e.slc[i] = e.slc[e.idx]
    99  			e.slc[e.idx] = def
   100  			return true
   101  		}
   102  	}
   103  	if e.next != nil {
   104  		return e.next.del(item, def)
   105  	}
   106  	return false
   107  }
   108  
   109  func (e *link2Elem[T]) iter(fn func([]T)) {
   110  	fn(e.slc[:e.idx])
   111  	if e.next != nil {
   112  		e.next.iter(fn)
   113  	}
   114  }
   115  
   116  func (e *link2Elem[T]) reset(def T) {
   117  	for i := uint8(0); i < e.idx; i++ {
   118  		e.slc[i] = def
   119  	}
   120  	e.idx = 0
   121  	if e.next != nil {
   122  		e.next.reset(def)
   123  		e.next = nil
   124  	}
   125  }
   126  
   127  func (e *link2Elem[T]) iterAndReset(fn func([]T), def T) {
   128  	fn(e.slc[:e.idx])
   129  	for i := uint8(0); i < e.idx; i++ {
   130  		e.slc[i] = def
   131  	}
   132  	e.idx = 0
   133  	if e.next != nil {
   134  		e.next.iterAndReset(fn, def)
   135  		e.next = nil
   136  	}
   137  }