github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bslice/comparable.go (about)

     1  package bslice
     2  
     3  // =====================================================================================================================
     4  // unsafe
     5  
     6  func NewUnsafeComparableBSlice[E comparable]() *UnsafeComparableBSlice[E] {
     7  	return &UnsafeComparableBSlice[E]{
     8  		UnsafeAnyBSlice: NewUnsafeAnyBSlice[E](),
     9  	}
    10  }
    11  
    12  func NewUnsafeComparableBSliceBySlice[E comparable](s []E) *UnsafeComparableBSlice[E] {
    13  	return &UnsafeComparableBSlice[E]{
    14  		UnsafeAnyBSlice: NewUnsafeAnyBSliceBySlice[E](s),
    15  	}
    16  }
    17  
    18  type UnsafeComparableBSlice[E comparable] struct {
    19  	*UnsafeAnyBSlice[E]
    20  }
    21  
    22  func (x *UnsafeComparableBSlice[E]) Contains(e E) bool {
    23  	return Contains(x.e, e)
    24  }
    25  
    26  func (x *UnsafeComparableBSlice[E]) Equal(es []E) bool {
    27  	return Equal(x.e, es)
    28  }
    29  
    30  func (x *UnsafeComparableBSlice[E]) Compact() {
    31  	x.e = Compact(x.e)
    32  }
    33  
    34  // =====================================================================================================================
    35  // safe
    36  
    37  func NewSafeComparableBSlice[E comparable]() *SafeComparableBSlice[E] {
    38  	return &SafeComparableBSlice[E]{
    39  		SafeAnyBSlice: NewSafeAnyBSlice[E](),
    40  	}
    41  }
    42  
    43  func NewSafeComparableBSliceBySlice[E comparable](s []E) *SafeComparableBSlice[E] {
    44  	return &SafeComparableBSlice[E]{
    45  		SafeAnyBSlice: NewSafeAnyBSliceBySlice[E](s),
    46  	}
    47  }
    48  
    49  type SafeComparableBSlice[E comparable] struct {
    50  	*SafeAnyBSlice[E]
    51  }
    52  
    53  func (x *SafeComparableBSlice[E]) Contains(e E) bool {
    54  	x.rwl.RLock()
    55  	defer x.rwl.RUnlock()
    56  	return Contains(x.es.e, e)
    57  }
    58  
    59  func (x *SafeComparableBSlice[E]) Equal(es []E) bool {
    60  	x.rwl.RLock()
    61  	defer x.rwl.RUnlock()
    62  	return Equal(x.es.e, es)
    63  }
    64  
    65  func (x *SafeComparableBSlice[E]) Compact() {
    66  	x.rwl.Lock()
    67  	defer x.rwl.Unlock()
    68  	x.es.e = Compact(x.es.e)
    69  }