Debian Codegraph @

interval/interval_example_test.go

raw symbols SCIP
1 // Copyright ©2012 The bíogo Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package interval_test
6
7 import (
8 "fmt"
9
10 "github.com/biogo/store/interval"
11 )
12
13 // Generic intervals
14 type Int int
15
16 func (c Int) Compare(b interval.Comparable) int {
17 return int(c - b.(Int))
18 }
19
20 type Interval struct {
21 start, end Int
22 id uintptr
23 Sub []Interval
24 Payload interface{}
25 }
26
27 func (i Interval) Overlap(b interval.Range) bool {
28 var start, end Int
29 switch bc := b.(type) {
30 case Interval:
31 start, end = bc.start, bc.end
32 case *Mutable:
33 start, end = bc.start, bc.end
34 default:
35 panic("unknown type")
36 }
37
38 // Half-open interval indexing.
39 return i.end > start && i.start < end
40 }
41 func (i Interval) ID() uintptr { return i.id }
42 func (i Interval) Start() interval.Comparable { return i.start }
43 func (i Interval) End() interval.Comparable { return i.end }
44 func (i Interval) NewMutable() interval.Mutable { return &Mutable{i.start, i.end} }
45 func (i Interval) String() string { return fmt.Sprintf("[%d,%d)#%d", i.start, i.end, i.id) }
46
47 type Mutable struct{ start, end Int }
48
49 func (m *Mutable) Start() interval.Comparable { return m.start }
50 func (m *Mutable) End() interval.Comparable { return m.end }
51 func (m *Mutable) SetStart(c interval.Comparable) { m.start = c.(Int) }
52 func (m *Mutable) SetEnd(c interval.Comparable) { m.end = c.(Int) }
53
54 var ivs = []Interval{
55 {start: 0, end: 2},
56 {start: 2, end: 4},
57 {start: 1, end: 6},
58 {start: 3, end: 4},
59 {start: 1, end: 3},
60 {start: 4, end: 6},
61 {start: 5, end: 8},
62 {start: 6, end: 8},
63 {start: 5, end: 7},
64 {start: 8, end: 9},
65 }
66
67 func Example_1() {
68 t := &interval.Tree{}
69 for i, iv := range ivs {
70 iv.id = uintptr(i)
71 err := t.Insert(iv, false)
72 if err != nil {
73 fmt.Println(err)
74 }
75 }
76
77 fmt.Println("Generic interval tree:")
78 fmt.Println(t.Get(Interval{start: 3, end: 6}))
79
80 // Output:
81 // Generic interval tree:
82 // [[1,6)#2 [2,4)#1 [3,4)#3 [4,6)#5 [5,8)#6 [5,7)#8]
83 }