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
/
set.go
102 lines (83 loc) · 2.21 KB
/
set.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package bitset
import (
"github.com/djordje200179/extendedlibrary/datastructures/cols/bitarray"
"github.com/djordje200179/extendedlibrary/datastructures/iter"
"github.com/djordje200179/extendedlibrary/datastructures/sets"
)
// Set is a set implementation based on bit array.
type Set struct {
arr *bitarray.Array
elements int
}
// New creates an empty set with the given size.
func New(size int) *Set {
return &Set{bitarray.NewWithSize(size), 0}
}
// NewFromIterable creates a set with the given size from the given iter.Iterable.
func NewFromIterable(size int, iterable iter.Iterable[int]) *Set {
set := New(size)
for it := iterable.Iterator(); it.Valid(); it.Move() {
set.Add(it.Get())
}
return set
}
// FromArray creates a set from the given array.
func FromArray(arr *bitarray.Array) *Set {
return &Set{arr, arr.Count()}
}
// Size returns the number of elements in the Set.
func (s *Set) Size() int {
return s.elements
}
// Add adds the given value to the Set.
func (s *Set) Add(value int) {
if !s.Contains(value) {
s.arr.Set(value, true)
s.elements++
}
}
// Remove removes the given value from the Set.
// If the value is not in the Set, this method does nothing.
func (s *Set) Remove(value int) {
if s.Contains(value) {
s.arr.Set(value, false)
s.elements--
}
}
// Contains returns true if the Set contains the given value.
func (s *Set) Contains(value int) bool {
return s.arr.Get(value)
}
// Clear removes all elements from the Set.
func (s *Set) Clear() {
s.arr.Clear()
s.elements = 0
}
// Clone returns a shallow copy of the Set.
func (s *Set) Clone() sets.Set[int] {
clonedArray := s.arr.Clone()
return &Set{clonedArray, s.elements}
}
// Iterator returns an iter.Iterator over the elements in the Set.
func (s *Set) Iterator() iter.Iterator[int] {
return s.SetIterator()
}
// SetIterator returns an iterator over the elements in the Set.
func (s *Set) SetIterator() sets.Iterator[int] {
return &Iterator{0, s}
}
// Stream streams the elements of the Set.
func (s *Set) Stream(yield func(int) bool) {
for i := range s.arr.Size() {
if !s.arr.Get(i) {
continue
}
if !yield(i) {
break
}
}
}
// Array returns the underlying bit array.
func (s *Set) Array() *bitarray.Array {
return s.arr
}