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

     1  package bmap
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  	"sync"
     7  
     8  	"github.com/songzhibin97/go-baseutils/base/bternaryexpr"
     9  )
    10  
    11  // =====================================================================================================================
    12  // unsafe
    13  
    14  func NewUnsafeAnyBMap[K comparable, V any]() *UnsafeAnyBMap[K, V] {
    15  	return &UnsafeAnyBMap[K, V]{mp: map[K]V{}}
    16  }
    17  
    18  func NewUnsafeAnyBMapByMap[K comparable, V any](mp map[K]V) *UnsafeAnyBMap[K, V] {
    19  	if mp == nil {
    20  		return NewUnsafeAnyBMap[K, V]()
    21  	}
    22  	return &UnsafeAnyBMap[K, V]{mp: mp}
    23  }
    24  
    25  type UnsafeAnyBMap[K comparable, V any] struct {
    26  	mp map[K]V
    27  }
    28  
    29  func (x *UnsafeAnyBMap[K, V]) ToMetaMap() map[K]V {
    30  	return x.mp
    31  }
    32  
    33  func (x *UnsafeAnyBMap[K, V]) Keys() []K {
    34  	return Keys(x.mp)
    35  }
    36  
    37  func (x *UnsafeAnyBMap[K, V]) Values() []V {
    38  	return Values(x.mp)
    39  }
    40  
    41  func (x *UnsafeAnyBMap[K, V]) EqualFuncByMap(m map[K]V, eq func(V1 V, V2 V) bool) bool {
    42  	return EqualFunc[map[K]V, map[K]V, K, V, V](x.mp, m, eq)
    43  }
    44  
    45  func (x *UnsafeAnyBMap[K, V]) EqualFuncByBMap(m AnyBMap[K, V], eq func(V1 V, V2 V) bool) bool {
    46  	return EqualFunc[map[K]V, map[K]V, K, V, V](x.mp, m.ToMetaMap(), eq)
    47  }
    48  
    49  func (x *UnsafeAnyBMap[K, V]) Clear() {
    50  	x.mp = make(map[K]V)
    51  }
    52  
    53  func (x *UnsafeAnyBMap[K, V]) CloneToMap() map[K]V {
    54  	return Clone(x.mp)
    55  }
    56  
    57  func (x *UnsafeAnyBMap[K, V]) CloneToBMap() AnyBMap[K, V] {
    58  	return NewUnsafeAnyBMapByMap(Clone(x.mp))
    59  }
    60  
    61  func (x *UnsafeAnyBMap[K, V]) CopyByMap(dst map[K]V) {
    62  	Copy(dst, x.mp)
    63  }
    64  
    65  func (x *UnsafeAnyBMap[K, V]) CopyByBMap(dst AnyBMap[K, V]) {
    66  	Copy(dst.ToMetaMap(), x.mp)
    67  }
    68  
    69  func (x *UnsafeAnyBMap[K, V]) DeleteFunc(del func(K, V) bool) {
    70  	DeleteFunc(x.mp, del)
    71  }
    72  
    73  func (x *UnsafeAnyBMap[K, V]) Marshal() ([]byte, error) {
    74  	if x.mp == nil {
    75  		return []byte("{}"), nil
    76  	}
    77  	return json.Marshal(x.mp)
    78  }
    79  
    80  func (x *UnsafeAnyBMap[K, V]) Unmarshal(data []byte) error {
    81  	if x.mp == nil {
    82  		x.mp = make(map[K]V)
    83  	}
    84  	return json.Unmarshal(data, &x.mp)
    85  }
    86  
    87  func (x *UnsafeAnyBMap[K, V]) Size() int {
    88  	return len(x.mp)
    89  }
    90  
    91  func (x *UnsafeAnyBMap[K, V]) IsEmpty() bool {
    92  	return len(x.mp) == 0
    93  }
    94  
    95  func (x *UnsafeAnyBMap[K, V]) IsExist(k K) bool {
    96  	_, ok := x.mp[k]
    97  	return ok
    98  }
    99  
   100  func (x *UnsafeAnyBMap[K, V]) ContainsKey(k K) bool {
   101  	_, ok := x.mp[k]
   102  	return ok
   103  }
   104  
   105  func (x *UnsafeAnyBMap[K, V]) ContainsValue(v V) bool {
   106  	for _, v2 := range x.mp {
   107  		if reflect.DeepEqual(v, v2) {
   108  			return true
   109  		}
   110  	}
   111  	return false
   112  }
   113  
   114  func (x *UnsafeAnyBMap[K, V]) ForEach(f func(K, V)) {
   115  	for k, v := range x.mp {
   116  		f(k, v)
   117  	}
   118  }
   119  
   120  func (x *UnsafeAnyBMap[K, V]) Get(k K) (V, bool) {
   121  	v, ok := x.mp[k]
   122  	return v, ok
   123  }
   124  
   125  func (x *UnsafeAnyBMap[K, V]) GetOrDefault(k K, defaultValue V) V {
   126  	v, ok := x.mp[k]
   127  	return bternaryexpr.TernaryExpr(ok, v, defaultValue)
   128  }
   129  
   130  func (x *UnsafeAnyBMap[K, V]) Put(k K, v V) {
   131  	x.mp[k] = v
   132  }
   133  
   134  func (x *UnsafeAnyBMap[K, V]) PuTIfAbsent(k K, v V) bool {
   135  	_, ok := x.mp[k]
   136  	if ok {
   137  		return false
   138  	}
   139  	x.mp[k] = v
   140  	return true
   141  }
   142  
   143  func (x *UnsafeAnyBMap[K, V]) Delete(k K) {
   144  	delete(x.mp, k)
   145  }
   146  
   147  func (x *UnsafeAnyBMap[K, V]) DeleteIfPresent(k K) (V, bool) {
   148  	v, ok := x.mp[k]
   149  	if ok {
   150  		delete(x.mp, k)
   151  	}
   152  	return v, ok
   153  }
   154  
   155  func (x *UnsafeAnyBMap[K, V]) MergeByMap(m map[K]V, f func(K, V) bool) {
   156  	for k, v := range m {
   157  		ov, ok := x.mp[k]
   158  		if !ok {
   159  			x.mp[k] = v
   160  			continue
   161  		}
   162  		if f != nil && f(k, ov) {
   163  			x.mp[k] = v
   164  		}
   165  	}
   166  }
   167  
   168  func (x *UnsafeAnyBMap[K, V]) MergeByBMap(m AnyBMap[K, V], f func(K, V) bool) {
   169  	for k, v := range m.ToMetaMap() {
   170  		ov, ok := x.mp[k]
   171  		if !ok {
   172  			x.mp[k] = v
   173  			continue
   174  		}
   175  		if f != nil && f(k, ov) {
   176  			x.mp[k] = v
   177  		}
   178  	}
   179  }
   180  
   181  func (x *UnsafeAnyBMap[K, V]) Replace(k K, ov, nv V) bool {
   182  	v, ok := x.mp[k]
   183  	flag := ok && reflect.DeepEqual(v, ov)
   184  	if flag {
   185  		x.mp[k] = nv
   186  	}
   187  	return flag
   188  }
   189  
   190  // =====================================================================================================================
   191  // safe
   192  
   193  func NewSafeAnyBMap[K comparable, V any]() *SafeAnyBMap[K, V] {
   194  	return &SafeAnyBMap[K, V]{mp: NewUnsafeAnyBMap[K,V]()}
   195  }
   196  
   197  func NewSafeAnyBMapByMap[K comparable, V any](mp map[K]V) *SafeAnyBMap[K, V] {
   198  	if mp == nil {
   199  		return NewSafeAnyBMap[K, V]()
   200  	}
   201  	return &SafeAnyBMap[K, V]{mp: NewUnsafeAnyBMapByMap[K,V](mp)}
   202  }
   203  
   204  type SafeAnyBMap[K comparable, V any] struct {
   205  	mp  *UnsafeAnyBMap[K, V]
   206  	rwl sync.RWMutex
   207  }
   208  
   209  func (x *SafeAnyBMap[K, V]) ToMetaMap() map[K]V {
   210  	return x.mp.mp
   211  }
   212  
   213  func (x *SafeAnyBMap[K, V]) Keys() []K {
   214  	x.rwl.Lock()
   215  	defer x.rwl.Unlock()
   216  	return x.mp.Keys()
   217  }
   218  
   219  func (x *SafeAnyBMap[K, V]) Values() []V {
   220  	x.rwl.Lock()
   221  	defer x.rwl.Unlock()
   222  	return x.mp.Values()
   223  }
   224  
   225  func (x *SafeAnyBMap[K, V]) EqualFuncByMap(m map[K]V, eq func(V1 V, V2 V) bool) bool {
   226  	x.rwl.RLock()
   227  	defer x.rwl.RUnlock()
   228  	return x.mp.EqualFuncByMap(m, eq)
   229  }
   230  
   231  func (x *SafeAnyBMap[K, V]) EqualFuncByBMap(m AnyBMap[K, V], eq func(V1 V, V2 V) bool) bool {
   232  	x.rwl.RLock()
   233  	defer x.rwl.RUnlock()
   234  	return x.mp.EqualFuncByBMap(m, eq)
   235  }
   236  
   237  func (x *SafeAnyBMap[K, V]) Clear() {
   238  	x.rwl.Lock()
   239  	defer x.rwl.Unlock()
   240  	x.mp.Clear()
   241  }
   242  
   243  func (x *SafeAnyBMap[K, V]) CloneToMap() map[K]V {
   244  	x.rwl.RLock()
   245  	defer x.rwl.RUnlock()
   246  	return x.mp.CloneToMap()
   247  }
   248  
   249  func (x *SafeAnyBMap[K, V]) CloneToBMap() AnyBMap[K, V] {
   250  	x.rwl.RLock()
   251  	defer x.rwl.RUnlock()
   252  	return x.mp.CloneToBMap()
   253  }
   254  
   255  func (x *SafeAnyBMap[K, V]) CopyByMap(dst map[K]V) {
   256  	x.rwl.RLock()
   257  	defer x.rwl.RUnlock()
   258  	x.mp.CopyByMap(dst)
   259  }
   260  
   261  func (x *SafeAnyBMap[K, V]) CopyByBMap(dst AnyBMap[K, V]) {
   262  	x.rwl.RLock()
   263  	defer x.rwl.RUnlock()
   264  	x.mp.CopyByBMap(dst)
   265  }
   266  
   267  func (x *SafeAnyBMap[K, V]) DeleteFunc(del func(K, V) bool) {
   268  	x.rwl.Lock()
   269  	defer x.rwl.Unlock()
   270  	x.mp.DeleteFunc(del)
   271  }
   272  
   273  func (x *SafeAnyBMap[K, V]) Marshal() ([]byte, error) {
   274  	x.rwl.RLock()
   275  	defer x.rwl.RUnlock()
   276  	return x.mp.Marshal()
   277  }
   278  
   279  func (x *SafeAnyBMap[K, V]) Unmarshal(data []byte) error {
   280  	x.rwl.Lock()
   281  	defer x.rwl.Unlock()
   282  	return x.mp.Unmarshal(data)
   283  }
   284  
   285  func (x *SafeAnyBMap[K, V]) Size() int {
   286  	x.rwl.RLock()
   287  	defer x.rwl.RUnlock()
   288  	return x.mp.Size()
   289  }
   290  
   291  func (x *SafeAnyBMap[K, V]) IsEmpty() bool {
   292  	x.rwl.RLock()
   293  	defer x.rwl.RUnlock()
   294  	return x.mp.IsEmpty()
   295  }
   296  
   297  func (x *SafeAnyBMap[K, V]) IsExist(k K) bool {
   298  	x.rwl.RLock()
   299  	defer x.rwl.RUnlock()
   300  	return x.mp.IsExist(k)
   301  }
   302  
   303  func (x *SafeAnyBMap[K, V]) ContainsKey(k K) bool {
   304  	x.rwl.RLock()
   305  	defer x.rwl.RUnlock()
   306  	return x.mp.ContainsKey(k)
   307  }
   308  
   309  func (x *SafeAnyBMap[K, V]) ContainsValue(v V) bool {
   310  	x.rwl.RLock()
   311  	defer x.rwl.RUnlock()
   312  	return x.mp.ContainsValue(v)
   313  }
   314  
   315  func (x *SafeAnyBMap[K, V]) ForEach(f func(K, V)) {
   316  	x.rwl.RLock()
   317  	defer x.rwl.RUnlock()
   318  	x.mp.ForEach(f)
   319  }
   320  
   321  func (x *SafeAnyBMap[K, V]) Get(k K) (V, bool) {
   322  	x.rwl.RLock()
   323  	defer x.rwl.RUnlock()
   324  	return x.mp.Get(k)
   325  }
   326  
   327  func (x *SafeAnyBMap[K, V]) GetOrDefault(k K, defaultValue V) V {
   328  	x.rwl.RLock()
   329  	defer x.rwl.RUnlock()
   330  	return x.mp.GetOrDefault(k, defaultValue)
   331  }
   332  
   333  func (x *SafeAnyBMap[K, V]) Put(k K, v V) {
   334  	x.rwl.Lock()
   335  	defer x.rwl.Unlock()
   336  	x.mp.Put(k, v)
   337  }
   338  
   339  func (x *SafeAnyBMap[K, V]) PuTIfAbsent(k K, v V) bool {
   340  	x.rwl.Lock()
   341  	defer x.rwl.Unlock()
   342  	return x.mp.PuTIfAbsent(k, v)
   343  }
   344  
   345  func (x *SafeAnyBMap[K, V]) Delete(k K) {
   346  	x.rwl.Lock()
   347  	defer x.rwl.Unlock()
   348  	x.mp.Delete(k)
   349  }
   350  
   351  func (x *SafeAnyBMap[K, V]) DeleteIfPresent(k K) (V, bool) {
   352  	x.rwl.Lock()
   353  	defer x.rwl.Unlock()
   354  	return x.mp.DeleteIfPresent(k)
   355  }
   356  
   357  func (x *SafeAnyBMap[K, V]) MergeByMap(m map[K]V, f func(K, V) bool) {
   358  	x.rwl.Lock()
   359  	defer x.rwl.Unlock()
   360  	x.mp.MergeByMap(m, f)
   361  }
   362  
   363  func (x *SafeAnyBMap[K, V]) MergeByBMap(m AnyBMap[K, V], f func(K, V) bool) {
   364  	x.rwl.Lock()
   365  	defer x.rwl.Unlock()
   366  	x.mp.MergeByBMap(m, f)
   367  }
   368  
   369  func (x *SafeAnyBMap[K, V]) Replace(k K, ov, nv V) bool {
   370  	x.rwl.Lock()
   371  	defer x.rwl.Unlock()
   372  	return x.mp.Replace(k, ov, nv)
   373  }