This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
77 lines (59 loc) · 1.56 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package synccol
import (
"github.com/djordje200179/extendedlibrary/datastructures/cols"
"sync"
)
// Iterator is a wrapper around a cols.Iterator that provides thread-safe
// access to the underlying collection.
type Iterator[T any] struct {
colIt cols.Iterator[T]
mutex *sync.RWMutex
}
// Valid returns true if the iterator is currently pointing to a valid element.
func (it Iterator[T]) Valid() bool {
return it.colIt.Valid()
}
// Move moves the iterator to the next element.
func (it Iterator[T]) Move() {
it.colIt.Move()
}
// GetRef returns a reference to the current element.
func (it Iterator[T]) GetRef() *T {
it.mutex.RLock()
defer it.mutex.RUnlock()
return it.colIt.GetRef()
}
// Get returns the current element.
func (it Iterator[T]) Get() T {
it.mutex.RLock()
defer it.mutex.RUnlock()
return it.colIt.Get()
}
// Set sets the current element.
func (it Iterator[T]) Set(value T) {
it.mutex.Lock()
defer it.mutex.Unlock()
it.colIt.Set(value)
}
// InsertBefore inserts the specified element before the current element.
func (it Iterator[T]) InsertBefore(value T) {
it.mutex.Lock()
defer it.mutex.Unlock()
it.colIt.InsertBefore(value)
}
// InsertAfter inserts the specified element after the current element.
func (it Iterator[T]) InsertAfter(value T) {
it.mutex.Lock()
defer it.mutex.Unlock()
it.colIt.InsertAfter(value)
}
// Remove removes the current element.
func (it Iterator[T]) Remove() {
it.mutex.Lock()
defer it.mutex.Unlock()
it.colIt.Remove()
}
// Index returns the current index.
func (it Iterator[T]) Index() int {
return it.colIt.Index()
}