github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/sets/syncset/iterator.go (about)

     1  package syncset
     2  
     3  import (
     4  	"github.com/djordje200179/extendedlibrary/datastructures/sets"
     5  	"sync"
     6  )
     7  
     8  // Iterator is a wrapper around an iterator that provides thread-safe access to the
     9  // underlying collection.
    10  type Iterator[T any] struct {
    11  	setIt sets.Iterator[T]
    12  
    13  	mutex *sync.RWMutex
    14  }
    15  
    16  // Valid returns true if the iterator is currently pointing to a valid element.
    17  func (it Iterator[T]) Valid() bool {
    18  	return it.setIt.Valid()
    19  }
    20  
    21  // Move moves the iterator to the next element.
    22  func (it Iterator[T]) Move() {
    23  	it.setIt.Move()
    24  }
    25  
    26  // Get returns the current element.
    27  func (it Iterator[T]) Get() T {
    28  	it.mutex.RLock()
    29  	defer it.mutex.RUnlock()
    30  
    31  	return it.setIt.Get()
    32  }
    33  
    34  // Remove removes the current element.
    35  func (it Iterator[T]) Remove() {
    36  	it.mutex.Lock()
    37  	defer it.mutex.Unlock()
    38  
    39  	it.setIt.Remove()
    40  }