github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/treeset/treeset_safe.go (about)

     1  package treeset
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     5  	"github.com/songzhibin97/go-baseutils/structure/sets"
     6  	"sync"
     7  )
     8  
     9  var _ sets.Set[any] = (*SetSafe[any])(nil)
    10  
    11  func NewSafeWith[E any](comparator bcomparator.Comparator[E], values ...E) *SetSafe[E] {
    12  	return &SetSafe[E]{
    13  		unsafe: NewWith[E](comparator, values...),
    14  	}
    15  }
    16  
    17  func NewSafeWithIntComparator(values ...int) *SetSafe[int] {
    18  	return &SetSafe[int]{
    19  		unsafe: NewWithIntComparator(values...),
    20  	}
    21  }
    22  
    23  func NewSafeWithStringComparator(values ...string) *SetSafe[string] {
    24  	return &SetSafe[string]{
    25  		unsafe: NewWithStringComparator(values...),
    26  	}
    27  }
    28  
    29  type SetSafe[E any] struct {
    30  	unsafe *Set[E]
    31  	lock   sync.Mutex
    32  }
    33  
    34  func (s *SetSafe[E]) Add(items ...E) {
    35  	s.lock.Lock()
    36  	defer s.lock.Unlock()
    37  	s.unsafe.Add(items...)
    38  
    39  }
    40  
    41  func (s *SetSafe[E]) Remove(items ...E) {
    42  	s.lock.Lock()
    43  	defer s.lock.Unlock()
    44  	s.unsafe.Remove(items...)
    45  
    46  }
    47  
    48  func (s *SetSafe[E]) Contains(items ...E) bool {
    49  	s.lock.Lock()
    50  	defer s.lock.Unlock()
    51  	return s.unsafe.Contains(items...)
    52  }
    53  
    54  func (s *SetSafe[E]) Empty() bool {
    55  	s.lock.Lock()
    56  	defer s.lock.Unlock()
    57  	return s.unsafe.Empty()
    58  }
    59  
    60  func (s *SetSafe[E]) Size() int {
    61  	s.lock.Lock()
    62  	defer s.lock.Unlock()
    63  	return s.unsafe.Size()
    64  }
    65  
    66  func (s *SetSafe[E]) Clear() {
    67  	s.lock.Lock()
    68  	defer s.lock.Unlock()
    69  	s.unsafe.Clear()
    70  
    71  }
    72  
    73  func (s *SetSafe[E]) Values() []E {
    74  	s.lock.Lock()
    75  	defer s.lock.Unlock()
    76  	return s.unsafe.Values()
    77  }
    78  
    79  func (s *SetSafe[E]) String() string {
    80  	s.lock.Lock()
    81  	defer s.lock.Unlock()
    82  	return s.unsafe.String()
    83  }
    84  
    85  func (s *SetSafe[E]) Intersection(another *Set[E]) *Set[E] {
    86  	s.lock.Lock()
    87  	defer s.lock.Unlock()
    88  	return s.unsafe.Intersection(another)
    89  }
    90  
    91  func (s *SetSafe[E]) Union(another *Set[E]) *Set[E] {
    92  	s.lock.Lock()
    93  	defer s.lock.Unlock()
    94  	return s.unsafe.Union(another)
    95  }
    96  
    97  func (s *SetSafe[E]) Difference(another *Set[E]) *Set[E] {
    98  	s.lock.Lock()
    99  	defer s.lock.Unlock()
   100  	return s.unsafe.Difference(another)
   101  }
   102  
   103  func (s *SetSafe[E]) UnmarshalJSON(bytes []byte) error {
   104  	s.lock.Lock()
   105  	defer s.lock.Unlock()
   106  	return s.unsafe.UnmarshalJSON(bytes)
   107  }
   108  
   109  func (s *SetSafe[E]) MarshalJSON() ([]byte, error) {
   110  	s.lock.Lock()
   111  	defer s.lock.Unlock()
   112  	return s.unsafe.MarshalJSON()
   113  }