| 1 | // Copyright (c) 2015, Emir Pasic. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package treeset |
| 6 | |
| 7 | import ( |
| 8 | "github.com/emirpasic/gods/v2/containers" |
| 9 | rbt "github.com/emirpasic/gods/v2/trees/redblacktree" |
| 10 | ) |
| 11 | |
| 12 | // Assert Iterator implementation |
| 13 | var _ containers.ReverseIteratorWithIndex[int] = (*Iterator[int])(nil) |
| 14 | |
| 15 | // Iterator returns a stateful iterator whose values can be fetched by an index. |
| 16 | type Iterator[T comparable] struct { |
| 17 | index int |
| 18 | iterator *rbt.Iterator[T, struct{}] |
| 19 | tree *rbt.Tree[T, struct{}] |
| 20 | } |
| 21 | |
| 22 | // Iterator holding the iterator's state |
| 23 | func (set *Set[T]) Iterator() Iterator[T] { |
| 24 | return Iterator[T]{index: -1, iterator: set.tree.Iterator(), tree: set.tree} |
| 25 | } |
| 26 | |
| 27 | // Next moves the iterator to the next element and returns true if there was a next element in the container. |
| 28 | // If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 29 | // If Next() was called for the first time, then it will point the iterator to the first element if it exists. |
| 30 | // Modifies the state of the iterator. |
| 31 | func (iterator *Iterator[T]) Next() bool { |
| 32 | if iterator.index < iterator.tree.Size() { |
| 33 | iterator.index++ |
| 34 | } |
| 35 | return iterator.iterator.Next() |
| 36 | } |
| 37 | |
| 38 | // Prev moves the iterator to the previous element and returns true if there was a previous element in the container. |
| 39 | // If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). |
| 40 | // Modifies the state of the iterator. |
| 41 | func (iterator *Iterator[T]) Prev() bool { |
| 42 | if iterator.index >= 0 { |
| 43 | iterator.index-- |
| 44 | } |
| 45 | return iterator.iterator.Prev() |
| 46 | } |
| 47 | |
| 48 | // Value returns the current element's value. |
| 49 | // Does not modify the state of the iterator. |
| 50 | func (iterator *Iterator[T]) Value() T { |
| 51 | return iterator.iterator.Key() |
| 52 | } |
| 53 | |
| 54 | // Index returns the current element's index. |
| 55 | // Does not modify the state of the iterator. |
| 56 | func (iterator *Iterator[T]) Index() int { |
| 57 | return iterator.index |
| 58 | } |
| 59 | |
| 60 | // Begin resets the iterator to its initial state (one-before-first) |
| 61 | // Call Next() to fetch the first element if any. |
| 62 | func (iterator *Iterator[T]) Begin() { |
| 63 | iterator.index = -1 |
| 64 | iterator.iterator.Begin() |
| 65 | } |
| 66 | |
| 67 | // End moves the iterator past the last element (one-past-the-end). |
| 68 | // Call Prev() to fetch the last element if any. |
| 69 | func (iterator *Iterator[T]) End() { |
| 70 | iterator.index = iterator.tree.Size() |
| 71 | iterator.iterator.End() |
| 72 | } |
| 73 | |
| 74 | // First moves the iterator to the first element and returns true if there was a first element in the container. |
| 75 | // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). |
| 76 | // Modifies the state of the iterator. |
| 77 | func (iterator *Iterator[T]) First() bool { |
| 78 | iterator.Begin() |
| 79 | return iterator.Next() |
| 80 | } |
| 81 | |
| 82 | // Last moves the iterator to the last element and returns true if there was a last element in the container. |
| 83 | // If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). |
| 84 | // Modifies the state of the iterator. |
| 85 | func (iterator *Iterator[T]) Last() bool { |
| 86 | iterator.End() |
| 87 | return iterator.Prev() |
| 88 | } |
| 89 | |
| 90 | // NextTo moves the iterator to the next element from current position that satisfies the condition given by the |
| 91 | // passed function, and returns true if there was a next element in the container. |
| 92 | // If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 93 | // Modifies the state of the iterator. |
| 94 | func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool { |
| 95 | for iterator.Next() { |
| 96 | index, value := iterator.Index(), iterator.Value() |
| 97 | if f(index, value) { |
| 98 | return true |
| 99 | } |
| 100 | } |
| 101 | return false |
| 102 | } |
| 103 | |
| 104 | // PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the |
| 105 | // passed function, and returns true if there was a next element in the container. |
| 106 | // If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value(). |
| 107 | // Modifies the state of the iterator. |
| 108 | func (iterator *Iterator[T]) PrevTo(f func(index int, value T) bool) bool { |
| 109 | for iterator.Prev() { |
| 110 | index, value := iterator.Index(), iterator.Value() |
| 111 | if f(index, value) { |
| 112 | return true |
| 113 | } |
| 114 | } |
| 115 | return false |
| 116 | } |