forked from moby/moby
/
ranges.go
98 lines (78 loc) · 1.72 KB
/
ranges.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// +build linux,amd64
package devmapper
import (
"container/list"
"fmt"
)
type Range struct {
begin uint64
end uint64
}
type Ranges struct {
*list.List
}
func NewRanges() *Ranges {
return &Ranges{list.New()}
}
func (r *Ranges) ToString() string {
s := ""
for e := r.Front(); e != nil; e = e.Next() {
r := e.Value.(*Range)
if s != "" {
s = s + ","
}
s = fmt.Sprintf("%s%d-%d", s, r.begin, r.end)
}
return s
}
func (r *Ranges) Clear() {
r.Init()
}
func (r *Ranges) Add(begin, end uint64) {
var next *list.Element
for e := r.Front(); e != nil; e = next {
next = e.Next()
existing := e.Value.(*Range)
// If existing range is fully to the left, skip
if existing.end < begin {
continue
}
// If new range is fully to the left, just insert
if end < existing.begin {
r.InsertBefore(&Range{begin, end}, e)
return
}
// Now we know the two ranges somehow intersect (or at least touch)
// Extend existing range with the new range
if begin < existing.begin {
existing.begin = begin
}
// If the new range is completely covered by existing range, we're done
if end <= existing.end {
return
}
// Otherwise strip r from new range
begin = existing.end
// We're now touching r at the end, and so we need to either extend r
// or merge with next
if next == nil {
// Nothing after, extend
existing.end = end
return
}
nextR := next.Value.(*Range)
if end < nextR.begin {
// Fits, Just extend
existing.end = end
return
}
// The new region overlaps the next, merge the two
nextR.begin = existing.begin
r.Remove(e)
}
// nothing in list or everything to the left, just append the rest
if begin < end {
r.PushBack(&Range{begin, end})
return
}
}