github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/router_gen/prec.go (about)

     1  package main
     2  
     3  type Prec struct {
     4  	Sets      []map[string]int
     5  	NameToSet map[string]int
     6  }
     7  
     8  func NewPrec() *Prec {
     9  	return &Prec{NameToSet: make(map[string]int)}
    10  }
    11  
    12  func (p *Prec) AddSet(precs ...string) {
    13  	set := make(map[string]int)
    14  	setIndex, i := len(p.Sets), 0
    15  	for _, prec := range precs {
    16  		set[prec] = i
    17  		p.NameToSet[prec] = setIndex
    18  		i++
    19  	}
    20  	p.Sets = append(p.Sets, set)
    21  }
    22  
    23  func (p *Prec) InAnySet(name string) bool {
    24  	_, ok := p.NameToSet[name]
    25  	return ok
    26  }
    27  
    28  func (p *Prec) InSameSet(n, n2 string) bool {
    29  	ok, ok2 := p.InAnySet(n), p.InAnySet(n2)
    30  	if !ok || !ok2 {
    31  		return false
    32  	}
    33  	set1, set2 := p.NameToSet[n], p.NameToSet[n2]
    34  	return set1 == set2
    35  }
    36  
    37  func (p *Prec) GreaterThan(greater, lesser string) bool {
    38  	if !p.InSameSet(greater, lesser) {
    39  		return false
    40  	}
    41  	set := p.Sets[p.NameToSet[greater]]
    42  	return set[greater] > set[lesser]
    43  }
    44  
    45  func (p *Prec) LessThanItem(greater string) (l []string) {
    46  	if len(p.Sets) == 0 {
    47  		return nil
    48  	}
    49  
    50  	setIndex := p.NameToSet[greater]
    51  	set := p.Sets[setIndex]
    52  	ref := set[greater]
    53  	for name, value := range set {
    54  		if value < ref {
    55  			l = append(l, name)
    56  		}
    57  	}
    58  
    59  	return l
    60  }