aerc/lib/iterator/impl.go
Koni Marti c83ffabf38 iterator: implement iterators over uid/thread data
Implement an iterator framework for uid and thread data. Iterators
ensure that the underlying data is accessed in the right order and
provide an abstraction of the index handling.

Iterators should be used when the order of the uids/threads is
important.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
2022-10-27 22:44:39 +02:00

125 lines
2.1 KiB
Go

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
}