github.com/haraldrudell/parl@v0.4.176/pqs/aggregate-priority.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // pqs provides legacy priority-queue implementation likely to be deprecated
     7  package pqs
     8  
     9  import (
    10  	"github.com/haraldrudell/parl"
    11  	"golang.org/x/exp/constraints"
    12  )
    13  
    14  // AggregatePriority provides cached priority values and order function
    15  type AggregatePriority[V any, P constraints.Ordered] struct {
    16  	assignedPriority AssignedPriority[V, P]
    17  	aggregator       parl.Aggregator[V, P]
    18  }
    19  
    20  // NewAggregatePriority returns an object providing cached priority values and order function
    21  func NewAggregatePriority[V any, P constraints.Ordered](
    22  	value *V,
    23  	index int,
    24  	aggregator parl.Aggregator[V, P],
    25  ) (aggregatePriority parl.AggregatePriority[V, P]) {
    26  	return &AggregatePriority[V, P]{
    27  		assignedPriority: *NewAssignedPriority(aggregator.Priority(), index, value),
    28  		aggregator:       aggregator,
    29  	}
    30  }
    31  
    32  var _ = ((parl.AggregatePriority[int, int])(&AggregatePriority[int, int]{})).Aggregator
    33  
    34  // Aggregator returns the object calculating values
    35  func (a *AggregatePriority[V, P]) Aggregator() (aggregator parl.Aggregator[V, P]) {
    36  	return a.aggregator
    37  }
    38  
    39  var _ = ((parl.AggregatePriority[int, int])(&AggregatePriority[int, int]{})).Update
    40  
    41  // Update caches the current priority from the aggregator
    42  func (a *AggregatePriority[V, P]) Update() {
    43  	a.assignedPriority.SetPriority(a.aggregator.Priority())
    44  }
    45  
    46  var _ = ((parl.AggregatePriority[int, int])(&AggregatePriority[int, int]{})).CachedPriority
    47  
    48  // Priority returns the effective cached priority
    49  func (a *AggregatePriority[V, P]) CachedPriority() (priority P) {
    50  	return a.assignedPriority.Priority
    51  }
    52  
    53  // Priority returns the effective cached priority
    54  func (a *AggregatePriority[V, P]) Index() (index int) {
    55  	return a.assignedPriority.Index
    56  }
    57  
    58  // Cmp returns a comparison of two AggregatePriority objects that represents value elements.
    59  //   - Cmp is a custom comparison function to be used with pslices and slices packages
    60  //   - Cmp makes AggregatePriority ordered
    61  //   - the Priority used is uncached value
    62  func (x *AggregatePriority[V, P]) Cmp(a, b parl.AggregatePriority[V, P]) (result int) {
    63  	aPriority := a.CachedPriority()
    64  	bPriority := b.CachedPriority()
    65  	if aPriority > bPriority { // highest priority first
    66  		return -1
    67  	} else if aPriority < bPriority {
    68  		return 1
    69  	}
    70  	aIndex := a.Index()
    71  	bIndex := b.Index()
    72  	if aIndex < bIndex { // lowest index first
    73  		return -1
    74  	} else if aIndex > bIndex {
    75  		return 1
    76  	}
    77  	return 0
    78  }