github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/treebidimap/treebidimap_safe.go (about)

     1  package treebidimap
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     5  	"github.com/songzhibin97/go-baseutils/structure/maps"
     6  	"sync"
     7  )
     8  
     9  var _ maps.BidiMap[int, int] = (*MapSafe[int, int])(nil)
    10  
    11  
    12  // NewWith instantiates a bidirectional map.
    13  func NewSafeWith[K, V any](keyComparator bcomparator.Comparator[K], valueComparator bcomparator.Comparator[V]) *MapSafe[K, V] {
    14  	return &MapSafe[K, V]{
    15  		unsafe: NewWith[K, V](keyComparator, valueComparator),
    16  	}
    17  }
    18  
    19  func NewSafeWithIntComparators() *MapSafe[int, int] {
    20  	return NewSafeWith(bcomparator.IntComparator(), bcomparator.IntComparator())
    21  }
    22  
    23  func NewSafeWithStringComparators() *MapSafe[string, string] {
    24  	return NewSafeWith(bcomparator.StringComparator(), bcomparator.StringComparator())
    25  }
    26  
    27  
    28  type MapSafe[K any, V any] struct {
    29  	unsafe *Map[K, V]
    30  	lock   sync.Mutex
    31  }
    32  
    33  func (s *MapSafe[K, V]) Put(key K, value V) {
    34  	s.lock.Lock()
    35  	defer s.lock.Unlock()
    36  	s.unsafe.Put(key, value)
    37  
    38  }
    39  
    40  func (s *MapSafe[K, V]) Get(key K) (V, bool) {
    41  	s.lock.Lock()
    42  	defer s.lock.Unlock()
    43  	return s.unsafe.Get(key)
    44  }
    45  
    46  func (s *MapSafe[K, V]) GetKey(value V) (K, bool) {
    47  	s.lock.Lock()
    48  	defer s.lock.Unlock()
    49  	return s.unsafe.GetKey(value)
    50  }
    51  
    52  func (s *MapSafe[K, V]) Remove(key K) {
    53  	s.lock.Lock()
    54  	defer s.lock.Unlock()
    55  	s.unsafe.Remove(key)
    56  
    57  }
    58  
    59  func (s *MapSafe[K, V]) Empty() bool {
    60  	s.lock.Lock()
    61  	defer s.lock.Unlock()
    62  	return s.unsafe.Empty()
    63  }
    64  
    65  func (s *MapSafe[K, V]) Size() int {
    66  	s.lock.Lock()
    67  	defer s.lock.Unlock()
    68  	return s.unsafe.Size()
    69  }
    70  
    71  func (s *MapSafe[K, V]) Keys() []K {
    72  	s.lock.Lock()
    73  	defer s.lock.Unlock()
    74  	return s.unsafe.Keys()
    75  }
    76  
    77  func (s *MapSafe[K, V]) Values() []V {
    78  	s.lock.Lock()
    79  	defer s.lock.Unlock()
    80  	return s.unsafe.Values()
    81  }
    82  
    83  func (s *MapSafe[K, V]) Clear() {
    84  	s.lock.Lock()
    85  	defer s.lock.Unlock()
    86  	s.unsafe.Clear()
    87  
    88  }
    89  
    90  func (s *MapSafe[K, V]) String() string {
    91  	s.lock.Lock()
    92  	defer s.lock.Unlock()
    93  	return s.unsafe.String()
    94  }
    95  
    96  func (s *MapSafe[K, V]) UnmarshalJSON(bytes []byte) error {
    97  	s.lock.Lock()
    98  	defer s.lock.Unlock()
    99  	return s.unsafe.UnmarshalJSON(bytes)
   100  }
   101  
   102  func (s *MapSafe[K, V]) MarshalJSON() ([]byte, error) {
   103  	s.lock.Lock()
   104  	defer s.lock.Unlock()
   105  	return s.unsafe.MarshalJSON()
   106  }