feat: ranking.List 新增支持默认值的获取排名和分数的函数
This commit is contained in:
parent
6efa9692d9
commit
57ee7ff3ef
|
@ -87,6 +87,16 @@ func (slf *List[CompetitorID, Score]) Size() int {
|
||||||
return slf.competitors.Size()
|
return slf.competitors.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRankDefault 获取竞争者排名,如果竞争者不存在则返回默认值
|
||||||
|
// - 排名从 0 开始
|
||||||
|
func (slf *List[CompetitorID, Score]) GetRankDefault(competitorId CompetitorID, defaultValue int) int {
|
||||||
|
rank, err := slf.GetRank(competitorId)
|
||||||
|
if err != nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return rank
|
||||||
|
}
|
||||||
|
|
||||||
// GetRank 获取竞争者排名
|
// GetRank 获取竞争者排名
|
||||||
// - 排名从 0 开始
|
// - 排名从 0 开始
|
||||||
func (slf *List[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error) {
|
func (slf *List[CompetitorID, Score]) GetRank(competitorId CompetitorID) (int, error) {
|
||||||
|
@ -160,6 +170,15 @@ func (slf *List[CompetitorID, Score]) GetScore(competitorId CompetitorID) (score
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetScoreDefault 获取竞争者成绩,不存在时返回默认值
|
||||||
|
func (slf *List[CompetitorID, Score]) GetScoreDefault(competitorId CompetitorID, defaultValue Score) Score {
|
||||||
|
score, err := slf.GetScore(competitorId)
|
||||||
|
if err != nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return score
|
||||||
|
}
|
||||||
|
|
||||||
// GetAllCompetitor 获取所有竞争者ID
|
// GetAllCompetitor 获取所有竞争者ID
|
||||||
// - 结果为名次有序的
|
// - 结果为名次有序的
|
||||||
func (slf *List[CompetitorID, Score]) GetAllCompetitor() []CompetitorID {
|
func (slf *List[CompetitorID, Score]) GetAllCompetitor() []CompetitorID {
|
||||||
|
|
Loading…
Reference in New Issue