Skip to content

Commit

Permalink
!refactor(iterator): change order of type parameters and name them pr…
Browse files Browse the repository at this point in the history
…operly
  • Loading branch information
JonasMuehlmann committed Jul 13, 2022
1 parent 03f94aa commit 15011d9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 44 deletions.
32 changes: 22 additions & 10 deletions ds/compound_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,52 @@

package ds

// NOTE: Abbreviations:
// NOTE: Abbreviations and order:
// ReadableIterator = Read
// WritableIterator = Write

// OrderedIterator = Ord
// UnorderedIterator =Unord

// ComparableIterator = Comp
// CollectionIterator = Coll
// WritableIterator = Write
// ReadableIterator = Read

// ForwardsIterator = For
// BackwardIterator = Back
// ReverseIterator = Rev
// BidirectionalIterator = Bid

// RandomAccessIterator = Rand
// CollectionIterator = Coll

type ReadWriteOrdCompBidRandCollIterator[T any, TIndex any] interface {
type ReadWriteOrdCompBidRandCollIterator[TIndex any, TValue any] interface {
OrderedIterator
ComparableIterator

BidirectionalIterator

RandomAccessReadableIterator[TIndex, T]
RandomAccessWriteableIterator[TIndex, T]
RandomAccessReadableIterator[TIndex, TValue]
RandomAccessWriteableIterator[TIndex, TValue]
}

type ReadWriteOrdCompBidRevRandCollIterator[T any, TIndex any] interface {
type ReadWriteOrdCompBidRevRandCollIterator[TIndex any, TValue any] interface {
OrderedIterator
ComparableIterator

ReversedIterator
BackwardIterator

RandomAccessReadableIterator[TIndex, T]
RandomAccessWriteableIterator[TIndex, T]
RandomAccessReadableIterator[TIndex, TValue]
RandomAccessWriteableIterator[TIndex, TValue]
}

const (
CanOnlyCompareEqualIteratorTypes = "Can only compare iterators of equal concrete type"
)

type ReadWriteCompForRandCollIterator[TIndex any, TValue any] interface {
ComparableIterator
CollectionIterator[TIndex]
ForwardIterator
RandomAccessReadableIterator[TIndex, TValue]
RandomAccessWriteableIterator[TIndex, TValue]
}
40 changes: 20 additions & 20 deletions ds/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ type CollectionIterator[TIndex any] interface {
}

// WritableIterator defines an Iterator, which can be used to write to the underlying values.
type WritableIterator[T any] interface {
type WritableIterator[TIndex any] interface {
// ********************* Inherited methods ********************//
Iterator
// ************************ Own methods ***********************//

// Set sets the value at the iterator's position.
Set(value T) bool
Set(value TIndex) bool
}

// ReadableIterator defines an Iterator, which can be used to read the underlying values.
type ReadableIterator[T any] interface {
type ReadableIterator[TValue any] interface {
// ********************* Inherited methods ********************//
Iterator
// ************************ Own methods ***********************//

// Get returns the value at the iterator's position.
// found will be false if the iterator is in an invalid state or the collection is empty.
Get() (value T, found bool)
Get() (value TValue, found bool)
}

// ForwardIterator defines an ordered Iterator, which can be moved forward according to the indexes ordering.
Expand Down Expand Up @@ -206,45 +206,45 @@ type UnorderedRandomAccessIterator[TIndex any] interface {
}

// RandomAccessReadableIterator defines a RandomAccessIterator and ReadableIterator, which can read from every index in the iterator.
type RandomAccessReadableIterator[T any, V any] interface {
type RandomAccessReadableIterator[TIndex any, TValue any] interface {
// ********************* Inherited methods ********************//
RandomAccessIterator[T]
ReadableIterator[V]
RandomAccessIterator[TIndex]
ReadableIterator[TValue]
// ************************ Own methods ***********************//

// GetAt returns the value at the given index of the iterator.
GetAt(i T) (value V, found bool)
GetAt(i TIndex) (value TValue, found bool)
}

// RandomAccessWriteableIterator defines a RandomAccessIterator and WritableIterator, which can write from every index in the iterator.
type RandomAccessWriteableIterator[T any, V any] interface {
type RandomAccessWriteableIterator[TIndex any, TValue any] interface {
// ********************* Inherited methods ********************//
RandomAccessIterator[T]
WritableIterator[V]
RandomAccessIterator[TIndex]
WritableIterator[TValue]
// ************************ Own methods ***********************//

// GetAt sets the value at the given index of the iterator.
SetAt(i T, value V) bool
SetAt(i TIndex, value TValue) bool
}

// UnorderedRandomAccessReadableIterator defines a RandomAccessIterator and ReadableIterator, which can read from every index in the iterator.
type UnorderedRandomAccessReadableIterator[T any, V any] interface {
type UnorderedRandomAccessReadableIterator[TIndex any, TValue any] interface {
// ********************* Inherited methods ********************//
UnorderedRandomAccessIterator[T]
ReadableIterator[V]
UnorderedRandomAccessIterator[TIndex]
ReadableIterator[TValue]
// ************************ Own methods ***********************//

// GetAt returns the value at the given index of the iterator.
GetAt(i T) (value V, found bool)
GetAt(i TIndex) (value TValue, found bool)
}

// UnorderedRandomAccessWriteableIterator defines a RandomAccessIterator and WritableIterator, which can write from every index in the iterator.
type UnorderedRandomAccessWriteableIterator[T any, V any] interface {
type UnorderedRandomAccessWriteableIterator[TIndex any, TValue any] interface {
// ********************* Inherited methods ********************//
UnorderedRandomAccessIterator[T]
WritableIterator[V]
UnorderedRandomAccessIterator[TIndex]
WritableIterator[TValue]
// ************************ Own methods ***********************//

// GetAt sets the value at the given index of the iterator.
SetAt(i T, value V) bool
SetAt(i TIndex, value TValue) bool
}
16 changes: 8 additions & 8 deletions lists/arraylist/arraylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,23 @@ func (list *List[T]) shrink() {

// Begin returns an initialized iterator, which points to one element before it's first.
// Unless Next() is called, the iterator is in an invalid state.
func (list *List[T]) Begin() ds.ReadWriteOrdCompBidRandCollIterator[T, int] {
func (list *List[T]) Begin() ds.ReadWriteOrdCompBidRandCollIterator[int, T] {
return list.NewIterator(list, -1)
}

// End returns an initialized iterator, which points to one element afrer it's last.
// Unless Previous() is called, the iterator is in an invalid state.
func (list *List[T]) End() ds.ReadWriteOrdCompBidRandCollIterator[T, int] {
func (list *List[T]) End() ds.ReadWriteOrdCompBidRandCollIterator[int, T] {
return list.NewIterator(list, list.size)
}

// First returns an initialized iterator, which points to it's first element.
func (list *List[T]) First() ds.ReadWriteOrdCompBidRandCollIterator[T, int] {
func (list *List[T]) First() ds.ReadWriteOrdCompBidRandCollIterator[int, T] {
return list.NewIterator(list, 0)
}

// Last returns an initialized iterator, which points to it's last element.
func (list *List[T]) Last() ds.ReadWriteOrdCompBidRandCollIterator[T, int] {
func (list *List[T]) Last() ds.ReadWriteOrdCompBidRandCollIterator[int, T] {
return list.NewIterator(list, list.size-1)
}

Expand All @@ -307,22 +307,22 @@ func (list *List[T]) Last() ds.ReadWriteOrdCompBidRandCollIterator[T, int] {

// ReverseBegin returns an initialized, reversed iterator, which points to one element before it's first.
// Unless Next() is called, the iterator is in an invalid state.
func (list *List[T]) ReverseBegin() ds.ReadWriteOrdCompBidRevRandCollIterator[T, int] {
func (list *List[T]) ReverseBegin() ds.ReadWriteOrdCompBidRevRandCollIterator[int, T] {
return list.NewReverseIterator(list, list.size)
}

// ReverseEnd returns an initialized,reversed iterator, which points to one element afrer it's last.
// Unless Previous() is called, the iterator is in an invalid state.
func (list *List[T]) ReverseEnd() ds.ReadWriteOrdCompBidRevRandCollIterator[T, int] {
func (list *List[T]) ReverseEnd() ds.ReadWriteOrdCompBidRevRandCollIterator[int, T] {
return list.NewReverseIterator(list, -1)
}

// ReverseFirst returns an initialized, reversed iterator, which points to it's first element.
func (list *List[T]) ReverseFirst() ds.ReadWriteOrdCompBidRevRandCollIterator[T, int] {
func (list *List[T]) ReverseFirst() ds.ReadWriteOrdCompBidRevRandCollIterator[int, T] {
return list.NewReverseIterator(list, list.size-1)
}

// ReverseLast returns an initialized, reversed iterator, which points to it's last element.
func (list *List[T]) ReverseLast() ds.ReadWriteOrdCompBidRevRandCollIterator[T, int] {
func (list *List[T]) ReverseLast() ds.ReadWriteOrdCompBidRevRandCollIterator[int, T] {
return list.NewReverseIterator(list, 0)
}
2 changes: 1 addition & 1 deletion lists/arraylist/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package arraylist
import "github.com/JonasMuehlmann/datastructures.go/ds"

// Assert Iterator implementation
var _ ds.ReadWriteOrdCompBidRandCollIterator[any, int] = (*Iterator[any])(nil)
var _ ds.ReadWriteOrdCompBidRandCollIterator[int, any] = (*Iterator[any])(nil)

// Iterator holding the iterator's state
type Iterator[T any] struct {
Expand Down
10 changes: 5 additions & 5 deletions lists/arraylist/reverse_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package arraylist
import "github.com/JonasMuehlmann/datastructures.go/ds"

// Assert Iterator implementation
var _ ds.ReadWriteOrdCompBidRevRandCollIterator[any, int] = (*ReverseIterator[any])(nil)
var _ ds.ReadWriteOrdCompBidRevRandCollIterator[int, any] = (*ReverseIterator[any])(nil)

// ReverseIterator holding the iterator's state
type ReverseIterator[T any] struct {
Expand Down Expand Up @@ -48,28 +48,28 @@ func (it *ReverseIterator[T]) Set(value T) bool {
// DistanceTo implements ds.ReadWriteOrdCompBidRandCollReverseIterator
// If other is of type CollectionIterator, CollectionIterator.Index() will be used, possibly executing in O(1)
func (it *ReverseIterator[T]) DistanceTo(other ds.OrderedIterator) int {
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[T, int](it)
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[int, T](it)

return -forwardIterator.DistanceTo(other)
}

// IsAfter implements ds.ReadWriteOrdCompBidRandCollReverseIterator
func (it *ReverseIterator[T]) IsAfter(other ds.OrderedIterator) bool {
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[T, int](it)
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[int, T](it)

return !forwardIterator.IsAfter(other)
}

// IsBefore implements ds.ReadWriteOrdCompBidRandCollReverseIterator
func (it *ReverseIterator[T]) IsBefore(other ds.OrderedIterator) bool {
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[T, int](it)
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[int, T](it)

return !forwardIterator.IsBefore(other)
}

// IsEqual implements ds.ReadWriteOrdCompBidRandCollReverseIterator
func (it *ReverseIterator[T]) IsEqual(other ds.ComparableIterator) bool {
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[T, int](it)
forwardIterator := ds.ReadWriteOrdCompBidRandCollIterator[int, T](it)

return forwardIterator.IsEqual(other)
}
Expand Down

0 comments on commit 15011d9

Please sign in to comment.