From 15011d9fd076703ac8ca39832a1ad3f36b549d26 Mon Sep 17 00:00:00 2001 From: jonas Date: Wed, 13 Jul 2022 10:57:49 +0200 Subject: [PATCH] !refactor(iterator): change order of type parameters and name them properly --- ds/compound_iterator.go | 32 +++++++++++++++-------- ds/iterator.go | 40 ++++++++++++++--------------- lists/arraylist/arraylist.go | 16 ++++++------ lists/arraylist/iterator.go | 2 +- lists/arraylist/reverse_iterator.go | 10 ++++---- 5 files changed, 56 insertions(+), 44 deletions(-) diff --git a/ds/compound_iterator.go b/ds/compound_iterator.go index cfb0ff2..75ac82b 100644 --- a/ds/compound_iterator.go +++ b/ds/compound_iterator.go @@ -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] +} diff --git a/ds/iterator.go b/ds/iterator.go index 8dba3e2..6b89dfd 100644 --- a/ds/iterator.go +++ b/ds/iterator.go @@ -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. @@ -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 } diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index d6dbb1a..78384c9 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -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) } @@ -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) } diff --git a/lists/arraylist/iterator.go b/lists/arraylist/iterator.go index 6a3399f..da4a8aa 100644 --- a/lists/arraylist/iterator.go +++ b/lists/arraylist/iterator.go @@ -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 { diff --git a/lists/arraylist/reverse_iterator.go b/lists/arraylist/reverse_iterator.go index a778255..6eb4cab 100644 --- a/lists/arraylist/reverse_iterator.go +++ b/lists/arraylist/reverse_iterator.go @@ -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 { @@ -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) }