iterator.go 563 B

123456789101112131415161718192021222324252627282930313233
  1. //go:build linux || darwin
  2. package libbox
  3. import "github.com/sagernet/sing/common"
  4. type StringIterator interface {
  5. Next() string
  6. HasNext() bool
  7. }
  8. var _ StringIterator = (*iterator[string])(nil)
  9. type iterator[T any] struct {
  10. values []T
  11. }
  12. func newIterator[T any](values []T) *iterator[T] {
  13. return &iterator[T]{values}
  14. }
  15. func (i *iterator[T]) Next() T {
  16. if len(i.values) == 0 {
  17. return common.DefaultValue[T]()
  18. }
  19. nextValue := i.values[0]
  20. i.values = i.values[1:]
  21. return nextValue
  22. }
  23. func (i *iterator[T]) HasNext() bool {
  24. return len(i.values) > 0
  25. }