]> Sergey Matveev's repositories - btrtrc.git/blob - multiless.go
20ba2bd7255323cf09d2125587fd20d03b7be70f
[btrtrc.git] / multiless.go
1 package torrent
2
3 func strictCmp(same, less bool) cmper {
4         return func() (bool, bool) { return same, less }
5 }
6
7 type (
8         cmper     func() (same, less bool)
9         multiLess struct {
10                 ok   bool
11                 less bool
12         }
13 )
14
15 func (me *multiLess) Final() bool {
16         if !me.ok {
17                 panic("undetermined")
18         }
19         return me.less
20 }
21
22 func (me *multiLess) FinalOk() (left, ok bool) {
23         return me.less, me.ok
24 }
25
26 func (me *multiLess) Next(f cmper) {
27         if me.ok {
28                 return
29         }
30         same, less := f()
31         if same {
32                 return
33         }
34         me.ok = true
35         me.less = less
36 }
37
38 func (me *multiLess) StrictNext(same, less bool) {
39         if me.ok {
40                 return
41         }
42         me.Next(func() (bool, bool) { return same, less })
43 }
44
45 func (me *multiLess) NextBool(l, r bool) {
46         me.StrictNext(l == r, l)
47 }