github.com/haraldrudell/parl@v0.4.176/pslices/ordered-pointers.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pslices
     7  
     8  import (
     9  	"github.com/haraldrudell/parl/parli"
    10  	"golang.org/x/exp/constraints"
    11  )
    12  
    13  // OrderedPointers is an ordered list of *E pointers sorted by the referenced values.
    14  // OrderedPointers implements [parl.OrderedPointers][E any].
    15  //   - The OrderedPointers ordered list does not require a comparison function
    16  //   - E is used for large structs.
    17  //   - Insert overwrites duplicates.
    18  //   - for custom sort order, use NewOrderedAny
    19  type OrderedPointers[E constraints.Ordered] struct {
    20  	OrderedAny[*E] // Element() Length() List() Clear() Insert() Delete() Index() Clone()
    21  }
    22  
    23  func NewOrderedPointers[E constraints.Ordered]() (list parli.OrderedPointers[E]) {
    24  	var o = OrderedPointers[E]{}
    25  	o.OrderedAny = *NewOrderedAny(o.Cmp).(*OrderedAny[*E])
    26  	return &o
    27  }
    28  
    29  func (o *OrderedPointers[E]) Cmp(a, b *E) (result int) {
    30  	if *a < *b {
    31  		return -1
    32  	} else if *a > *b {
    33  		return 1
    34  	}
    35  	return 0
    36  }