-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuffer_lines.go
More file actions
156 lines (130 loc) · 2.37 KB
/
buffer_lines.go
File metadata and controls
156 lines (130 loc) · 2.37 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package wig
import (
"math"
"strings"
"unicode/utf8"
)
// Thanks: https://gist.github.com/pje/90e727f80685c78a6c1cfff35f62155a
type Line []rune
var EOL int = math.MinInt32
func (l Line) IsEmpty() bool {
s := strings.TrimSpace(string(l))
return len(s) == 0
}
func (l Line) String() string {
return string(l)
}
func (l Line) Bytes() int {
size := 0
for _, r := range l {
size += utf8.RuneLen(r)
}
return size
}
func (l Line) Range(from, to int) []rune {
if l.IsEmpty() {
return []rune{}
}
if from < 0 {
return []rune{}
}
if from > len(l) {
return []rune{}
}
if to > len(l) || to == EOL {
return l[from:]
}
if to < from {
return []rune{}
}
return l[from:to]
}
// the zero value is ready to use.
type List[T any] struct {
root Element[T]
Len int
}
type Element[T any] struct {
prev *Element[T]
next *Element[T]
list *List[T]
Value T
}
func (e *Element[T]) Next() *Element[T] {
n := e.next
if e.list == nil || n == &e.list.root {
return nil
}
return n
}
func (e *Element[T]) Prev() *Element[T] {
p := e.prev
if e.list == nil || p == &e.list.root {
return nil
}
return p
}
func (l *List[T]) First() *Element[T] {
if l.Len == 0 {
return nil
}
return l.root.next
}
func (l *List[T]) Last() *Element[T] {
if l.Len == 0 {
return nil
}
return l.root.prev
}
func (l *List[T]) PushFront(v T) *Element[T] {
if l.root.next == nil {
l.init()
}
return l.insertValueAfter(v, &l.root)
}
func (l *List[T]) PushBack(v T) *Element[T] {
if l.root.next == nil {
l.init()
}
return l.insertValueAfter(v, l.root.prev)
}
func (l *List[T]) Remove(e *Element[T]) T {
if e.list == l {
l.remove(e)
}
return e.Value
}
// Constructs a new List[T] from the given slice, returns the List[T].
func FromSlice[T any](slice []T) List[T] {
var list List[T]
for _, e := range slice {
list.PushFront(e)
}
return list
}
func (l *List[T]) init() {
l.root = *new(Element[T])
l.root.next = &l.root
l.root.prev = &l.root
}
func (l *List[T]) insertAfter(e *Element[T], at *Element[T]) *Element[T] {
e.prev = at
e.next = at.next
e.prev.next = e
e.next.prev = e
e.list = l
l.Len++
return e
}
func (l *List[T]) insertValueAfter(v T, at *Element[T]) *Element[T] {
e := Element[T]{Value: v}
return l.insertAfter(&e, at)
}
func (l *List[T]) remove(e *Element[T]) {
e.prev.next = e.next
e.next.prev = e.prev
e.next = nil
e.prev = nil
e.list = nil
l.Len--
}