github.com/hzck/speedroute@v0.0.0-20201115191102-403b7d0e443f/model/weight.go (about)

     1  package model
     2  
     3  // Weight holds the time and possible requirements to traverse an edge.
     4  type Weight struct {
     5  	time         int
     6  	requirements map[*Reward]int
     7  }
     8  
     9  // Time returns a time.
    10  func (weight *Weight) Time() int {
    11  	return weight.time
    12  }
    13  
    14  // Requirements returns a map of requirements.
    15  func (weight *Weight) Requirements() map[*Reward]int {
    16  	return weight.requirements
    17  }
    18  
    19  // AddRequirement adds a requirement with a quantity.
    20  func (weight *Weight) AddRequirement(reward *Reward, quantity int) {
    21  	weight.requirements[reward] = quantity
    22  }
    23  
    24  // CreateWeight constructs a weight and returns a pointer to it.
    25  func CreateWeight(time int) *Weight {
    26  	weight := new(Weight)
    27  	weight.time = time
    28  	weight.requirements = make(map[*Reward]int)
    29  	return weight
    30  }
    31  
    32  // ByTime is a list of Weights which implements the sort interface.
    33  type ByTime []*Weight
    34  
    35  // Len returns the ByTime length.
    36  func (a ByTime) Len() int {
    37  	return len(a)
    38  }
    39  
    40  // Swap changes position of two weights in the ByTime list.
    41  func (a ByTime) Swap(i, j int) {
    42  	a[i], a[j] = a[j], a[i]
    43  }
    44  
    45  // Less compares two weight times to each other.
    46  func (a ByTime) Less(i, j int) bool {
    47  	return a[i].Time() < a[j].Time()
    48  }