neo-go/pkg/vm/interop_iterators.go
Evgenii Stratonikov b0d07c3031 vm: make Iterator interface public
There is nothing wrong with iterators being implemented in other parts
of code (e.g. Storage.Find). In this case type assertions can
prevent bugs at compile-time.
2020-05-27 11:40:46 +03:00

124 lines
1.8 KiB
Go

package vm
type (
enumerator interface {
Next() bool
Value() StackItem
}
arrayWrapper struct {
index int
value []StackItem
}
concatEnum struct {
current enumerator
second enumerator
}
)
type (
// Iterator defined public interface for VM's iterator type.
Iterator interface {
enumerator
Key() StackItem
}
mapWrapper struct {
index int
m []MapElement
}
concatIter struct {
current Iterator
second Iterator
}
keysWrapper struct {
iter Iterator
}
valuesWrapper struct {
iter Iterator
}
)
func (a *arrayWrapper) Next() bool {
if next := a.index + 1; next < len(a.value) {
a.index = next
return true
}
return false
}
func (a *arrayWrapper) Value() StackItem {
return a.value[a.index]
}
func (a *arrayWrapper) Key() StackItem {
return makeStackItem(a.index)
}
func (c *concatEnum) Next() bool {
if c.current.Next() {
return true
}
c.current = c.second
return c.current.Next()
}
func (c *concatEnum) Value() StackItem {
return c.current.Value()
}
func (i *concatIter) Next() bool {
if i.current.Next() {
return true
}
i.current = i.second
return i.second.Next()
}
func (i *concatIter) Value() StackItem {
return i.current.Value()
}
func (i *concatIter) Key() StackItem {
return i.current.Key()
}
func (m *mapWrapper) Next() bool {
if next := m.index + 1; next < len(m.m) {
m.index = next
return true
}
return false
}
func (m *mapWrapper) Value() StackItem {
return m.m[m.index].Value
}
func (m *mapWrapper) Key() StackItem {
return m.m[m.index].Key
}
func (e *keysWrapper) Next() bool {
return e.iter.Next()
}
func (e *keysWrapper) Value() StackItem {
return e.iter.Key()
}
func (e *valuesWrapper) Next() bool {
return e.iter.Next()
}
func (e *valuesWrapper) Value() StackItem {
return e.iter.Value()
}