2024-04-26 04:30:43 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// iter-list.go
|
|
|
|
package expr
|
|
|
|
|
2024-05-04 00:35:27 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
2024-04-26 04:30:43 +02:00
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
type ListIterator struct {
|
|
|
|
a *ListType
|
2024-04-26 04:30:43 +02:00
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func NewListIterator(list *ListType) *ListIterator {
|
|
|
|
return &ListIterator{a: list, index: 0}
|
2024-04-26 04:30:43 +02:00
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func NewArrayIterator(array []any) *ListIterator {
|
|
|
|
return &ListIterator{a: (*ListType)(&array), index: 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAnyIterator(value any) (it *ListIterator) {
|
|
|
|
if value == nil {
|
|
|
|
it = NewArrayIterator([]any{})
|
|
|
|
} else if list, ok := value.(*ListType); ok {
|
|
|
|
it = NewListIterator(list)
|
|
|
|
} else if array, ok := value.([]any); ok {
|
|
|
|
it = NewArrayIterator(array)
|
|
|
|
} else if it1, ok := value.(*ListIterator); ok {
|
|
|
|
it = it1
|
|
|
|
} else {
|
|
|
|
it = NewArrayIterator([]any{value})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:35:27 +02:00
|
|
|
func (it *ListIterator) String() string {
|
|
|
|
var l = 0
|
|
|
|
if it.a != nil {
|
|
|
|
l = len(*it.a)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("$(#%d)", l)
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) HasOperation(name string) bool {
|
2024-05-04 00:35:27 +02:00
|
|
|
yes := name == resetName
|
|
|
|
return yes
|
2024-04-28 06:45:20 +02:00
|
|
|
}
|
|
|
|
|
2024-05-04 00:35:27 +02:00
|
|
|
func (it *ListIterator) CallOperation(name string, args []any) (v any, err error) {
|
|
|
|
if name == resetName {
|
|
|
|
v, err = it.Reset()
|
|
|
|
} else {
|
|
|
|
err = errNoOperation(name)
|
|
|
|
}
|
|
|
|
return
|
2024-04-28 06:45:20 +02:00
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) Current() (item any, err error) {
|
|
|
|
a := *(it.a)
|
|
|
|
if it.index >= 0 && it.index < len(a) {
|
|
|
|
item = a[it.index]
|
2024-04-26 04:30:43 +02:00
|
|
|
} else {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) Next() (item any, err error) {
|
2024-04-26 04:30:43 +02:00
|
|
|
if item, err = it.Current(); err != io.EOF {
|
|
|
|
it.index++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-03 06:26:17 +02:00
|
|
|
func (it *ListIterator) Index() int {
|
2024-04-26 04:30:43 +02:00
|
|
|
return it.index - 1
|
|
|
|
}
|
2024-05-04 00:35:27 +02:00
|
|
|
|
|
|
|
func (it *ListIterator) Reset() (bool, error) {
|
|
|
|
it.index = 0
|
|
|
|
return true, nil
|
|
|
|
}
|