GoCollections/collections/SubList.go

48 lines
898 B
Go
Raw Normal View History

2021-05-20 17:32:35 +08:00
package collections
2021-05-21 16:57:33 +08:00
import "github.com/tursom/GoCollections/exceptions"
2021-05-20 17:32:35 +08:00
type SubList struct {
list List
from, to uint32
}
func NewSubList(list List, from, to uint32) *SubList {
return &SubList{list, from, to}
}
func (s *SubList) Iterator() Iterator {
iterator := s.list.Iterator()
for i := 0; i < int(s.from); i++ {
2021-05-21 09:41:58 +08:00
_, err := iterator.Next()
if err != nil {
return nil
}
2021-05-20 17:32:35 +08:00
}
return iterator
}
func (s *SubList) Size() uint32 {
return s.to - s.from
}
func (s *SubList) IsEmpty() bool {
return s.Size() == 0
}
func (s *SubList) Contains(element interface{}) bool {
return Contains(s, element)
}
func (s *SubList) ContainsAll(c Collection) bool {
return ContainsAll(s, c)
}
2021-05-21 16:57:33 +08:00
func (s *SubList) Get(index uint32) (interface{}, exceptions.Exception) {
2021-05-20 17:32:35 +08:00
return s.list.Get(index + s.from)
}
func (s *SubList) SubList(from, to uint32) List {
return NewSubList(s, from, to)
}