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

     1  package bmap
     2  
     3  // =====================================================================================================================
     4  // unsafe
     5  
     6  func NewUnsafeComparableBMap[K comparable, V comparable]() *UnsafeComparableBMap[K, V] {
     7  	return &UnsafeComparableBMap[K, V]{
     8  		UnsafeAnyBMap: &UnsafeAnyBMap[K, V]{
     9  			mp: make(map[K]V),
    10  		},
    11  	}
    12  }
    13  
    14  func NewUnsafeComparableBMapByMap[K comparable, V comparable](mp map[K]V)  *UnsafeComparableBMap[K, V] {
    15  	return &UnsafeComparableBMap[K, V]{
    16  		UnsafeAnyBMap: &UnsafeAnyBMap[K, V]{
    17  			mp: mp,
    18  		},
    19  	}
    20  }
    21  
    22  type UnsafeComparableBMap[K comparable, V comparable] struct {
    23  	*UnsafeAnyBMap[K, V]
    24  }
    25  
    26  func (x *UnsafeComparableBMap[K, V]) EqualByMap(m map[K]V) bool {
    27  	return Equal(x.mp, m)
    28  }
    29  
    30  func (x *UnsafeComparableBMap[K, V]) EqualByBMap(m AnyBMap[K, V]) bool {
    31  	return Equal(x.mp, m.ToMetaMap())
    32  }
    33  
    34  // =====================================================================================================================
    35  // safe
    36  
    37  func NewSafeComparableBMap[K comparable, V comparable]() *SafeComparableBMap[K, V] {
    38  	return &SafeComparableBMap[K, V]{
    39  		SafeAnyBMap: NewSafeAnyBMap[K,V](),
    40  	}
    41  }
    42  
    43  func NewSafeComparableBMapByMap[K comparable, V comparable](mp map[K]V) *SafeComparableBMap[K, V] {
    44  	return &SafeComparableBMap[K, V]{
    45  		SafeAnyBMap: NewSafeAnyBMapByMap[K,V](mp),
    46  	}
    47  }
    48  
    49  type SafeComparableBMap[K comparable, V comparable] struct {
    50  	*SafeAnyBMap[K, V]
    51  }
    52  
    53  func (x *SafeComparableBMap[K, V]) EqualByMap(m map[K]V) bool {
    54  	x.rwl.RLock()
    55  	defer x.rwl.RUnlock()
    56  	return Equal(x.mp.mp, m)
    57  }
    58  
    59  func (x *SafeComparableBMap[K, V]) EqualByBMap(m AnyBMap[K, V]) bool {
    60  	x.rwl.RLock()
    61  	defer x.rwl.RUnlock()
    62  	return Equal(x.mp.mp, m.ToMetaMap())
    63  }