-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
60 lines (50 loc) · 1023 Bytes
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package multiverse
import "sort"
type CardList []*Card
type sortableCardList struct {
*CardList
less Comparison
}
func (c CardList) Len() int {
return len(c)
}
// Sort the results based on the given comparison.
func (c *CardList) Sort(f Comparison) {
sortable := sortableCardList{
c,
f,
}
sort.Sort(sortable)
}
func (c *CardList) addEnsureNonDuplicate(candidate *Card) {
for _, card := range *c {
if card == candidate {
return
}
}
c.add(candidate)
}
func (c *CardList) add(card *Card) {
l := len(*c)
if l < cap(*c) {
*c = (*c)[:l+1]
} else {
newList := make(CardList, l+1, (l+1)*2)
copy(newList, *c)
*c = newList
}
(*c)[l] = card
}
func (c *CardList) trim() {
if len(*c) < cap(*c) {
newList := make(CardList, len(*c))
copy(newList, *c)
*c = newList
}
}
func (s sortableCardList) Swap(i, j int) {
(*s.CardList)[i], (*s.CardList)[j] = (*s.CardList)[j], (*s.CardList)[i]
}
func (s sortableCardList) Less(i, j int) bool {
return s.less((*s.CardList)[i], (*s.CardList)[j])
}