github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/trees/btree/btree_safe.go (about)

     1  package btree
     2  
     3  import (
     4  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     5  	"github.com/songzhibin97/go-baseutils/structure/trees"
     6  	"sync"
     7  )
     8  
     9  var _ trees.Tree[any] = (*TreeSafe[any, any])(nil)
    10  
    11  
    12  func NewSafeWith[K, V any](order int, comparator bcomparator.Comparator[K]) *TreeSafe[K, V] {
    13  	return &TreeSafe[K, V]{
    14  		unsafe: NewWith[K, V](order, comparator),
    15  	}
    16  }
    17  
    18  func NewSafeWithIntComparator[V any](order int) *TreeSafe[int, V] {
    19  	return &TreeSafe[int, V]{
    20  		unsafe: NewWithIntComparator[V](order),
    21  	}
    22  }
    23  
    24  func NewSafeWithStringComparator[V any](order int) *TreeSafe[string, V] {
    25  	return &TreeSafe[string, V]{
    26  		unsafe: NewWithStringComparator[V](order),
    27  	}
    28  }
    29  
    30  type TreeSafe[K, V any] struct {
    31  	unsafe *Tree[K, V]
    32  	lock   sync.Mutex
    33  }
    34  
    35  func (s *TreeSafe[K, V]) Put(key K, value V) {
    36  	s.lock.Lock()
    37  	defer s.lock.Unlock()
    38  	s.unsafe.Put(key, value)
    39  
    40  }
    41  
    42  func (s *TreeSafe[K, V]) Get(key K) (V, bool) {
    43  	s.lock.Lock()
    44  	defer s.lock.Unlock()
    45  	return s.unsafe.Get(key)
    46  }
    47  
    48  func (s *TreeSafe[K, V]) GetNode(key K) *Node[K, V] {
    49  	s.lock.Lock()
    50  	defer s.lock.Unlock()
    51  	return s.unsafe.GetNode(key)
    52  }
    53  
    54  func (s *TreeSafe[K, V]) Remove(key K) {
    55  	s.lock.Lock()
    56  	defer s.lock.Unlock()
    57  	s.unsafe.Remove(key)
    58  
    59  }
    60  
    61  func (s *TreeSafe[K, V]) Empty() bool {
    62  	s.lock.Lock()
    63  	defer s.lock.Unlock()
    64  	return s.unsafe.Empty()
    65  }
    66  
    67  func (s *TreeSafe[K, V]) Size() int {
    68  	s.lock.Lock()
    69  	defer s.lock.Unlock()
    70  	return s.unsafe.Size()
    71  }
    72  
    73  func (s *TreeSafe[K, V]) Keys() []K {
    74  	s.lock.Lock()
    75  	defer s.lock.Unlock()
    76  	return s.unsafe.Keys()
    77  }
    78  
    79  func (s *TreeSafe[K, V]) Values() []V {
    80  	s.lock.Lock()
    81  	defer s.lock.Unlock()
    82  	return s.unsafe.Values()
    83  }
    84  
    85  func (s *TreeSafe[K, V]) Clear() {
    86  	s.lock.Lock()
    87  	defer s.lock.Unlock()
    88  	s.unsafe.Clear()
    89  
    90  }
    91  
    92  func (s *TreeSafe[K, V]) Height() int {
    93  	s.lock.Lock()
    94  	defer s.lock.Unlock()
    95  	return s.unsafe.Height()
    96  }
    97  
    98  func (s *TreeSafe[K, V]) Left() *Node[K, V] {
    99  	s.lock.Lock()
   100  	defer s.lock.Unlock()
   101  	return s.unsafe.Left()
   102  }
   103  
   104  func (s *TreeSafe[K, V]) LeftKey() K {
   105  	s.lock.Lock()
   106  	defer s.lock.Unlock()
   107  	return s.unsafe.LeftKey()
   108  }
   109  
   110  func (s *TreeSafe[K, V]) LeftValue() V {
   111  	s.lock.Lock()
   112  	defer s.lock.Unlock()
   113  	return s.unsafe.LeftValue()
   114  }
   115  
   116  func (s *TreeSafe[K, V]) Right() *Node[K, V] {
   117  	s.lock.Lock()
   118  	defer s.lock.Unlock()
   119  	return s.unsafe.Right()
   120  }
   121  
   122  func (s *TreeSafe[K, V]) RightKey() K {
   123  	s.lock.Lock()
   124  	defer s.lock.Unlock()
   125  	return s.unsafe.RightKey()
   126  }
   127  
   128  func (s *TreeSafe[K, V]) RightValue() V {
   129  	s.lock.Lock()
   130  	defer s.lock.Unlock()
   131  	return s.unsafe.RightValue()
   132  }
   133  
   134  func (s *TreeSafe[K, V]) String() string {
   135  	s.lock.Lock()
   136  	defer s.lock.Unlock()
   137  	return s.unsafe.String()
   138  }
   139  
   140  func (s *TreeSafe[K, V]) UnmarshalJSON(bytes []byte) error {
   141  	s.lock.Lock()
   142  	defer s.lock.Unlock()
   143  	return s.unsafe.UnmarshalJSON(bytes)
   144  }
   145  
   146  func (s *TreeSafe[K, V]) MarshalJSON() ([]byte, error) {
   147  	s.lock.Lock()
   148  	defer s.lock.Unlock()
   149  	return s.unsafe.MarshalJSON()
   150  }