github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/mapobj.go (about)

     1  package isc
     2  
     3  type ISCMap[K comparable, V any] map[K]V
     4  
     5  func NewMap[K comparable, V any]() ISCMap[K, V] {
     6  	return make(ISCMap[K, V])
     7  }
     8  
     9  func NewMapWithMap[K comparable, V any](ma map[K]V) ISCMap[K, V] {
    10  	return ma
    11  }
    12  
    13  func NewMapWithPairs[K comparable, V any](pairs ...Pair[K, V]) ISCMap[K, V] {
    14  	m := make(map[K]V)
    15  	for _, item := range pairs {
    16  		m[item.First] = item.Second
    17  	}
    18  	return m
    19  }
    20  
    21  func (m ISCMap[K, V]) Size() int {
    22  	return len(m)
    23  }
    24  
    25  func (m ISCMap[K, V]) Put(k K, v V) {
    26  	m[k] = v
    27  }
    28  
    29  func (m ISCMap[K, V]) PutPair(item Pair[K, V]) {
    30  	m[item.First] = item.Second
    31  }
    32  
    33  func (m ISCMap[K, V]) PutAllPairs(item ...Pair[K, V]) {
    34  	for _, e := range item {
    35  		m.PutPair(e)
    36  	}
    37  }
    38  
    39  func (m ISCMap[K, V]) Get(k K) V {
    40  	return m[k]
    41  }
    42  
    43  func (m ISCMap[K, V]) GetOrDef(k K, def V) V {
    44  	if v, ok := m[k]; ok {
    45  		return v
    46  	} else {
    47  		return def
    48  	}
    49  }
    50  
    51  func (m ISCMap[K, V]) Delete(k K) {
    52  	delete(m, k)
    53  }
    54  
    55  func (m *ISCMap[K, V]) Clear() {
    56  	*m = make(ISCMap[K, V])
    57  }
    58  
    59  func (m ISCMap[K, V]) ForEach(f func(K, V)) {
    60  	for k, v := range m {
    61  		f(k, v)
    62  	}
    63  }
    64  
    65  func (m ISCMap[K, V]) Filter(f func(K, V) bool) ISCMap[K, V] {
    66  	return MapFilter(m, f)
    67  }
    68  
    69  func (m ISCMap[K, V]) FilterNot(f func(K, V) bool) ISCMap[K, V] {
    70  	return MapFilterNot(m, f)
    71  }
    72  
    73  func (m ISCMap[K, V]) FilterKeys(f func(K) bool) ISCMap[K, V] {
    74  	return MapFilterKeys(m, f)
    75  }
    76  
    77  func (m ISCMap[K, V]) FilterValues(f func(V) bool) ISCMap[K, V] {
    78  	return MapFilterValues(m, f)
    79  }
    80  
    81  func (m ISCMap[K, V]) FilterTo(dest *map[K]V, f func(K, V) bool) ISCMap[K, V] {
    82  	return MapFilterTo(m, dest, f)
    83  }
    84  
    85  func (m ISCMap[K, V]) FilterNotTo(dest *map[K]V, f func(K, V) bool) ISCMap[K, V] {
    86  	return MapFilterNotTo(m, dest, f)
    87  }
    88  
    89  func (m ISCMap[K, V]) Contains(k K, v V) bool {
    90  	return MapContains(m, k, v)
    91  }
    92  
    93  func (m ISCMap[K, V]) ContainsKey(k K) bool {
    94  	return MapContainsKey(m, k)
    95  }
    96  
    97  func (m ISCMap[K, V]) ContainsValue(v V) bool {
    98  	return MapContainsValue(m, v)
    99  }
   100  
   101  func (m ISCMap[K, V]) JoinToString(f func(K, V) string) string {
   102  	return MapJoinToString(m, f)
   103  }
   104  
   105  func (m ISCMap[K, V]) JoinToStringFull(sep string, prefix string, postfix string, f func(K, V) string) string {
   106  	return MapJoinToStringFull(m, sep, prefix, postfix, f)
   107  }
   108  
   109  func (m ISCMap[K, V]) All(f func(K, V) bool) bool {
   110  	return MapAll(m, f)
   111  }
   112  
   113  func (m ISCMap[K, V]) Any(f func(K, V) bool) bool {
   114  	return MapAny(m, f)
   115  }
   116  
   117  func (m ISCMap[K, V]) None(f func(K, V) bool) bool {
   118  	return MapNone(m, f)
   119  }
   120  
   121  func (m ISCMap[K, V]) Count(f func(K, V) bool) int {
   122  	return MapCount(m, f)
   123  }
   124  
   125  func (m ISCMap[K, V]) AllKey(f func(K) bool) bool {
   126  	return MapAllKey(m, f)
   127  }
   128  
   129  func (m ISCMap[K, V]) AnyKey(f func(K) bool) bool {
   130  	return MapAnyKey(m, f)
   131  }
   132  
   133  func (m ISCMap[K, V]) NoneKey(f func(K) bool) bool {
   134  	return MapNoneKey(m, f)
   135  }
   136  
   137  func (m ISCMap[K, V]) CountKey(f func(K) bool) int {
   138  	return MapCountKey(m, f)
   139  }
   140  
   141  func (m ISCMap[K, V]) AllValue(f func(V) bool) bool {
   142  	return MapAllValue(m, f)
   143  }
   144  
   145  func (m ISCMap[K, V]) AnyValue(f func(V) bool) bool {
   146  	return MapAnyValue(m, f)
   147  }
   148  
   149  func (m ISCMap[K, V]) NoneValue(f func(V) bool) bool {
   150  	return MapNoneValue(m, f)
   151  }
   152  
   153  func (m ISCMap[K, V]) CountValue(f func(V) bool) int {
   154  	return MapCountValue(m, f)
   155  }
   156  
   157  func (m ISCMap[K, V]) ToList() []Pair[K, V] {
   158  	var n []Pair[K, V]
   159  	for k, v := range m {
   160  		n = append(n, NewPair(k, v))
   161  	}
   162  	return n
   163  }
   164  
   165  func (m ISCMap[K, V]) Plus(n map[K]V) ISCMap[K, V] {
   166  	return MapPlus(m, n)
   167  }
   168  
   169  func (m ISCMap[K, V]) Minus(n map[K]V) ISCMap[K, V] {
   170  	return MapMinus(m, n)
   171  }
   172  
   173  func (m ISCMap[K, V]) Equals(n map[K]V) bool {
   174  	return MapEquals(m, n)
   175  }
   176  
   177  func (m ISCMap[K, V]) Keys() ISCList[K] {
   178  	i := 0
   179  	keys := make([]K, len(m))
   180  	for k := range m {
   181  		keys[i] = k
   182  		i++
   183  	}
   184  	return keys
   185  }