aboutsummaryrefslogtreecommitdiffstats
path: root/lib/iterator/impl.go
blob: 5e68516694e1314bf9f041e717e3b8784eb3612f (plain) (blame)
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
package iterator

import (
	"errors"

	"git.sr.ht/~rjarry/aerc/worker/types"
)

// defaultFactory
type defaultFactory struct{}

func (df *defaultFactory) NewIterator(a interface{}) Iterator {
	switch data := a.(type) {
	case []uint32:
		return &defaultUid{data: data, index: len(data)}
	case []*types.Thread:
		return &defaultThread{data: data, index: len(data)}
	}
	panic(errors.New("a iterator for this type is not implemented yet"))
}

// defaultUid
type defaultUid struct {
	data  []uint32
	index int
}

func (du *defaultUid) Next() bool {
	du.index--
	return du.index >= 0
}

func (du *defaultUid) Value() interface{} {
	return du.data[du.index]
}

func (du *defaultUid) StartIndex() int {
	return len(du.data) - 1
}

func (du *defaultUid) EndIndex() int {
	return 0
}

// defaultThread
type defaultThread struct {
	data  []*types.Thread
	index int
}

func (dt *defaultThread) Next() bool {
	dt.index--
	return dt.index >= 0
}

func (dt *defaultThread) Value() interface{} {
	return dt.data[dt.index]
}

func (dt *defaultThread) StartIndex() int {
	return len(dt.data) - 1
}

func (dt *defaultThread) EndIndex() int {
	return 0
}

// reverseFactory
type reverseFactory struct{}

func (rf *reverseFactory) NewIterator(a interface{}) Iterator {
	switch data := a.(type) {
	case []uint32:
		return &reverseUid{data: data, index: -1}
	case []*types.Thread:
		return &reverseThread{data: data, index: -1}
	}
	panic(errors.New("an iterator for this type is not implemented yet"))
}

// reverseUid
type reverseUid struct {
	data  []uint32
	index int
}

func (ru *reverseUid) Next() bool {
	ru.index++
	return ru.index < len(ru.data)
}

func (ru *reverseUid) Value() interface{} {
	return ru.data[ru.index]
}

func (ru *reverseUid) StartIndex() int {
	return 0
}

func (ru *reverseUid) EndIndex() int {
	return len(ru.data) - 1
}

// reverseThread
type reverseThread struct {
	data  []*types.Thread
	index int
}

func (rt *reverseThread) Next() bool {
	rt.index++
	return rt.index < len(rt.data)
}

func (rt *reverseThread) Value() interface{} {
	return rt.data[rt.index]
}

func (rt *reverseThread) StartIndex() int {
	return 0
}

func (rt *reverseThread) EndIndex() int {
	return len(rt.data) - 1
}