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

     1  package bitset
     2  
     3  import (
     4  	"github.com/djordje200179/extendedlibrary/datastructures/cols/bitarray"
     5  	"github.com/djordje200179/extendedlibrary/datastructures/iter"
     6  	"github.com/djordje200179/extendedlibrary/datastructures/sets"
     7  )
     8  
     9  // Set is a set implementation based on bit array.
    10  type Set struct {
    11  	arr *bitarray.Array
    12  
    13  	elements int
    14  }
    15  
    16  // New creates an empty set with the given size.
    17  func New(size int) *Set {
    18  	return &Set{bitarray.NewWithSize(size), 0}
    19  }
    20  
    21  // NewFromIterable creates a set with the given size from the given iter.Iterable.
    22  func NewFromIterable(size int, iterable iter.Iterable[int]) *Set {
    23  	set := New(size)
    24  
    25  	for it := iterable.Iterator(); it.Valid(); it.Move() {
    26  		set.Add(it.Get())
    27  	}
    28  
    29  	return set
    30  }
    31  
    32  // FromArray creates a set from the given array.
    33  func FromArray(arr *bitarray.Array) *Set {
    34  	return &Set{arr, arr.Count()}
    35  }
    36  
    37  // Size returns the number of elements in the Set.
    38  func (s *Set) Size() int {
    39  	return s.elements
    40  }
    41  
    42  // Add adds the given value to the Set.
    43  func (s *Set) Add(value int) {
    44  	if !s.Contains(value) {
    45  		s.arr.Set(value, true)
    46  		s.elements++
    47  	}
    48  }
    49  
    50  // Remove removes the given value from the Set.
    51  // If the value is not in the Set, this method does nothing.
    52  func (s *Set) Remove(value int) {
    53  	if s.Contains(value) {
    54  		s.arr.Set(value, false)
    55  		s.elements--
    56  	}
    57  }
    58  
    59  // Contains returns true if the Set contains the given value.
    60  func (s *Set) Contains(value int) bool {
    61  	return s.arr.Get(value)
    62  }
    63  
    64  // Clear removes all elements from the Set.
    65  func (s *Set) Clear() {
    66  	s.arr.Clear()
    67  	s.elements = 0
    68  }
    69  
    70  // Clone returns a shallow copy of the Set.
    71  func (s *Set) Clone() sets.Set[int] {
    72  	clonedArray := s.arr.Clone()
    73  	return &Set{clonedArray, s.elements}
    74  }
    75  
    76  // Iterator returns an iter.Iterator over the elements in the Set.
    77  func (s *Set) Iterator() iter.Iterator[int] {
    78  	return s.SetIterator()
    79  }
    80  
    81  // SetIterator returns an iterator over the elements in the Set.
    82  func (s *Set) SetIterator() sets.Iterator[int] {
    83  	return &Iterator{0, s}
    84  }
    85  
    86  // Stream streams the elements of the Set.
    87  func (s *Set) Stream(yield func(int) bool) {
    88  	for i := range s.arr.Size() {
    89  		if !s.arr.Get(i) {
    90  			continue
    91  		}
    92  
    93  		if !yield(i) {
    94  			break
    95  		}
    96  	}
    97  }
    98  
    99  // Array returns the underlying bit array.
   100  func (s *Set) Array() *bitarray.Array {
   101  	return s.arr
   102  }