This repository has been archived on 2022-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
dataupdater/cron/searchable_by_id.go

61 lines
1.1 KiB
Go
Raw Normal View History

package cron
import "github.com/tribalwarshelp/shared/models"
type tribeSearchableByID struct {
*models.Tribe
}
2020-07-27 15:16:13 +00:00
func (t tribeSearchableByID) ID() int {
return t.Tribe.ID
}
type playerSearchableByID struct {
*models.Player
}
2020-07-27 15:16:13 +00:00
func (t playerSearchableByID) ID() int {
return t.Player.ID
}
type searchableByID interface {
2020-07-27 15:16:13 +00:00
ID() int
}
func makePlayersSearchable(players []*models.Player) []searchableByID {
searchable := []searchableByID{}
for _, player := range players {
searchable = append(searchable, playerSearchableByID{player})
}
return searchable
}
func makeTribesSearchable(tribes []*models.Tribe) []searchableByID {
searchable := []searchableByID{}
for _, tribe := range tribes {
searchable = append(searchable, tribeSearchableByID{tribe})
}
return searchable
}
func searchByID(haystack []searchableByID, id int) int {
low := 0
high := len(haystack) - 1
for low <= high {
median := (low + high) / 2
2020-07-27 15:16:13 +00:00
if haystack[median].ID() < id {
low = median + 1
} else {
high = median - 1
}
}
2020-07-27 15:16:13 +00:00
if low == len(haystack) || haystack[low].ID() != id {
return 0
}
return low
}