-
Notifications
You must be signed in to change notification settings - Fork 1
/
rowptr.go
115 lines (94 loc) · 2.35 KB
/
rowptr.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package csvi
import (
"container/list"
"io"
"github.com/hymkor/csvi/uncsv"
)
type RowPtr struct {
*uncsv.Row
element *list.Element
lnum int
list *list.List
}
func (r *RowPtr) Next() *RowPtr {
next := r.element.Next()
if next == nil {
return nil
}
return &RowPtr{Row: next.Value.(*uncsv.Row), element: next, lnum: r.lnum + 1, list: r.list}
}
func (r *RowPtr) Prev() *RowPtr {
prev := r.element.Prev()
if prev == nil {
return nil
}
return &RowPtr{Row: prev.Value.(*uncsv.Row), element: prev, lnum: r.lnum - 1, list: r.list}
}
func (r *RowPtr) Remove() *uncsv.Row {
return r.list.Remove(r.element).(*uncsv.Row)
}
func (r *RowPtr) Clone() *RowPtr {
return &RowPtr{Row: r.element.Value.(*uncsv.Row), element: r.element, lnum: r.lnum, list: r.list}
}
func frontPtr(L *list.List) *RowPtr {
front := L.Front()
if front == nil {
return nil
}
return &RowPtr{Row: front.Value.(*uncsv.Row), element: front, lnum: 0, list: L}
}
func backPtr(L *list.List) *RowPtr {
back := L.Back()
return &RowPtr{Row: back.Value.(*uncsv.Row), element: back, lnum: L.Len() - 1, list: L}
}
func (r *RowPtr) InsertAfter(val *uncsv.Row) *RowPtr {
next := r.list.InsertAfter(val, r.element)
return &RowPtr{Row: next.Value.(*uncsv.Row), element: next, lnum: r.lnum + 1, list: r.list}
}
func (r *RowPtr) InsertBefore(val *uncsv.Row) *RowPtr {
next := r.list.InsertBefore(val, r.element)
r.lnum++
return &RowPtr{Row: next.Value.(*uncsv.Row), element: next, lnum: r.lnum - 1, list: r.list}
}
func (r *RowPtr) Index() int {
return r.lnum
}
type _Application struct {
csvLines *list.List
removedRows []*uncsv.Row
out io.Writer
Pilot
*Config
}
type Result struct {
*_Application
}
func (app *_Application) Write(data []byte) (int, error) {
return app.out.Write(data)
}
func (app *_Application) Front() *RowPtr {
return frontPtr(app.csvLines)
}
func (app *_Application) Back() *RowPtr {
return backPtr(app.csvLines)
}
func (app *_Application) Len() int {
return app.csvLines.Len()
}
func (app *_Application) Push(row *uncsv.Row) {
app.csvLines.PushBack(row)
}
func (app *_Application) Each(callback func(*uncsv.Row) bool) {
for p := app.Front(); p != nil; p = p.Next() {
if !callback(p.Row) {
break
}
}
}
func (app *_Application) RemovedRows(callback func(*uncsv.Row) bool) {
for _, p := range app.removedRows {
if !callback(p) {
break
}
}
}