mirror of https://github.com/emirpasic/gods
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.9 KiB
Go
105 lines
3.9 KiB
Go
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package linkedhashset
|
|
|
|
import (
|
|
"github.com/emirpasic/gods/v2/containers"
|
|
"github.com/emirpasic/gods/v2/lists/doublylinkedlist"
|
|
)
|
|
|
|
// Assert Iterator implementation
|
|
var _ containers.ReverseIteratorWithIndex[int] = (*Iterator[int])(nil)
|
|
|
|
// Iterator holding the iterator's state
|
|
type Iterator[T comparable] struct {
|
|
iterator doublylinkedlist.Iterator[T]
|
|
}
|
|
|
|
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
|
func (set *Set[T]) Iterator() Iterator[T] {
|
|
return Iterator[T]{iterator: set.ordering.Iterator()}
|
|
}
|
|
|
|
// Next moves the iterator to the next element and returns true if there was a next element in the container.
|
|
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
|
|
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator[T]) Next() bool {
|
|
return iterator.iterator.Next()
|
|
}
|
|
|
|
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
|
|
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator[T]) Prev() bool {
|
|
return iterator.iterator.Prev()
|
|
}
|
|
|
|
// Value returns the current element's value.
|
|
// Does not modify the state of the iterator.
|
|
func (iterator *Iterator[T]) Value() T {
|
|
return iterator.iterator.Value()
|
|
}
|
|
|
|
// Index returns the current element's index.
|
|
// Does not modify the state of the iterator.
|
|
func (iterator *Iterator[T]) Index() int {
|
|
return iterator.iterator.Index()
|
|
}
|
|
|
|
// Begin resets the iterator to its initial state (one-before-first)
|
|
// Call Next() to fetch the first element if any.
|
|
func (iterator *Iterator[T]) Begin() {
|
|
iterator.iterator.Begin()
|
|
}
|
|
|
|
// End moves the iterator past the last element (one-past-the-end).
|
|
// Call Prev() to fetch the last element if any.
|
|
func (iterator *Iterator[T]) End() {
|
|
iterator.iterator.End()
|
|
}
|
|
|
|
// First moves the iterator to the first element and returns true if there was a first element in the container.
|
|
// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator[T]) First() bool {
|
|
return iterator.iterator.First()
|
|
}
|
|
|
|
// Last moves the iterator to the last element and returns true if there was a last element in the container.
|
|
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator[T]) Last() bool {
|
|
return iterator.iterator.Last()
|
|
}
|
|
|
|
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
|
|
// passed function, and returns true if there was a next element in the container.
|
|
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool {
|
|
for iterator.Next() {
|
|
index, value := iterator.Index(), iterator.Value()
|
|
if f(index, value) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
|
|
// passed function, and returns true if there was a next element in the container.
|
|
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool {
|
|
for iterator.Prev() {
|
|
index, value := iterator.Index(), iterator.Value()
|
|
if f(index, value) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|