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

     1  package isc
     2  
     3  type LinkedHashMap[K comparable, V comparable] OrderMap[K, V]
     4  
     5  type OrderMap[K comparable, V comparable] struct {
     6  	Data    map[K]V
     7  	KeyList []K
     8  }
     9  
    10  func NewOrderMap[K comparable, V comparable]() OrderMap[K, V] {
    11  	return OrderMap[K, V]{
    12  		Data:    make(map[K]V),
    13  		KeyList: []K{},
    14  	}
    15  }
    16  
    17  func (m OrderMap[K, V]) Size() int {
    18  	return len(m.KeyList)
    19  }
    20  
    21  func (m *OrderMap[K, V]) Put(k K, v V) {
    22  	if !ListContains(m.KeyList, k) {
    23  		m.KeyList = append(m.KeyList, k)
    24  	}
    25  	m.Data[k] = v
    26  }
    27  
    28  func (m *OrderMap[K, V]) PutPair(item Pair[K, V]) {
    29  	if !ListContains(m.KeyList, item.First) {
    30  		m.KeyList = append(m.KeyList, item.First)
    31  	}
    32  	m.Data[item.First] = item.Second
    33  }
    34  
    35  func (m *OrderMap[K, V]) PutPairs(item ...Pair[K, V]) {
    36  	for _, pair := range item {
    37  		m.PutPair(pair)
    38  	}
    39  }
    40  
    41  func (m OrderMap[K, V]) Get(k K) V {
    42  	return m.Data[k]
    43  }
    44  
    45  func (m OrderMap[K, V]) GetOrDef(k K, def V) V {
    46  	if v, ok := m.Data[k]; ok {
    47  		return v
    48  	} else {
    49  		return def
    50  	}
    51  }
    52  
    53  func (m *OrderMap[K, V]) Delete(k K) {
    54  	idx := IndexOf(m.KeyList, k)
    55  	if idx != -1 {
    56  		m.KeyList = append(m.KeyList[:idx], m.KeyList[idx+1:]...)
    57  		delete(m.Data, k)
    58  	}
    59  }
    60  
    61  func (m *OrderMap[K, V]) Clear() {
    62  	m.KeyList = []K{}
    63  	m.Data = make(map[K]V)
    64  }
    65  
    66  func (m OrderMap[K, V]) Keys() []K {
    67  	return m.KeyList
    68  }
    69  
    70  func (m OrderMap[K, V]) GetKey(index int) K {
    71  	return m.KeyList[index]
    72  }
    73  
    74  func (m OrderMap[K, V]) GetValue(index int) V {
    75  	key := m.KeyList[index]
    76  	return m.Data[key]
    77  }
    78  
    79  func (m OrderMap[K, V]) ForEach(f func(K, V)) {
    80  	for _, key := range m.KeyList {
    81  		f(key, m.Data[key])
    82  	}
    83  }
    84  
    85  func (m OrderMap[K, V]) ForEachIndexed(f func(int, K, V)) {
    86  	for idx, key := range m.KeyList {
    87  		f(idx, key, m.Data[key])
    88  	}
    89  }
    90  
    91  func (m OrderMap[K, V]) Filter(f func(K, V) bool) OrderMap[K, V] {
    92  	result := NewOrderMap[K, V]()
    93  	for _, key := range m.KeyList {
    94  		if f(key, m.Data[key]) {
    95  			result.Put(key, m.Data[key])
    96  		}
    97  	}
    98  	return result
    99  }
   100  
   101  func (m OrderMap[K, V]) FilterIndexed(f func(int, K, V) bool) OrderMap[K, V] {
   102  	result := NewOrderMap[K, V]()
   103  	for idx, key := range m.KeyList {
   104  		if f(idx, key, m.Data[key]) {
   105  			result.Put(key, m.Data[key])
   106  		}
   107  	}
   108  	return result
   109  }
   110  
   111  func (m OrderMap[K, V]) FilterNot(f func(K, V) bool) OrderMap[K, V] {
   112  	result := NewOrderMap[K, V]()
   113  	for _, key := range m.KeyList {
   114  		if !f(key, m.Data[key]) {
   115  			result.Put(key, m.Data[key])
   116  		}
   117  	}
   118  	return result
   119  }
   120  
   121  func (m OrderMap[K, V]) FilterNotIndexed(f func(int, K, V) bool) OrderMap[K, V] {
   122  	result := NewOrderMap[K, V]()
   123  	for idx, key := range m.KeyList {
   124  		if !f(idx, key, m.Data[key]) {
   125  			result.Put(key, m.Data[key])
   126  		}
   127  	}
   128  	return result
   129  }
   130  
   131  func (m OrderMap[K, V]) FilterKeys(f func(K) bool) OrderMap[K, V] {
   132  	result := NewOrderMap[K, V]()
   133  	for _, key := range m.KeyList {
   134  		if f(key) {
   135  			result.Put(key, m.Data[key])
   136  		}
   137  	}
   138  	return result
   139  }
   140  
   141  func (m OrderMap[K, V]) FilterKeysIndexed(f func(int, K) bool) OrderMap[K, V] {
   142  	result := NewOrderMap[K, V]()
   143  	for idx, key := range m.KeyList {
   144  		if f(idx, key) {
   145  			result.Put(key, m.Data[key])
   146  		}
   147  	}
   148  	return result
   149  }
   150  
   151  func (m OrderMap[K, V]) FilterValues(f func(V) bool) OrderMap[K, V] {
   152  	result := NewOrderMap[K, V]()
   153  	for _, key := range m.KeyList {
   154  		if f(m.Data[key]) {
   155  			result.Put(key, m.Data[key])
   156  		}
   157  	}
   158  	return result
   159  }
   160  
   161  func (m OrderMap[K, V]) FilterValuesIndexed(f func(int, V) bool) OrderMap[K, V] {
   162  	result := NewOrderMap[K, V]()
   163  	for idx, key := range m.KeyList {
   164  		if f(idx, m.Data[key]) {
   165  			result.Put(key, m.Data[key])
   166  		}
   167  	}
   168  	return result
   169  }
   170  
   171  func (m OrderMap[K, V]) FilterTo(dest *OrderMap[K, V], f func(K, V) bool) OrderMap[K, V] {
   172  	result := NewOrderMap[K, V]()
   173  	for _, key := range m.KeyList {
   174  		if f(key, m.Data[key]) {
   175  			dest.Put(key, m.Data[key])
   176  			result.Put(key, m.Data[key])
   177  		}
   178  	}
   179  	return result
   180  }
   181  
   182  func (m OrderMap[K, V]) FilterIndexedTo(dest *OrderMap[K, V], f func(int, K, V) bool) OrderMap[K, V] {
   183  	result := NewOrderMap[K, V]()
   184  	for idx, key := range m.KeyList {
   185  		if f(idx, key, m.Data[key]) {
   186  			dest.Put(key, m.Data[key])
   187  			result.Put(key, m.Data[key])
   188  		}
   189  	}
   190  	return result
   191  }
   192  
   193  func (m OrderMap[K, V]) FilterNotTo(dest *OrderMap[K, V], f func(K, V) bool) OrderMap[K, V] {
   194  	result := NewOrderMap[K, V]()
   195  	for _, key := range m.KeyList {
   196  		if !f(key, m.Data[key]) {
   197  			dest.Put(key, m.Data[key])
   198  			result.Put(key, m.Data[key])
   199  		}
   200  	}
   201  	return result
   202  }
   203  
   204  func (m OrderMap[K, V]) FilterNotIndexedTo(dest *OrderMap[K, V], f func(int, K, V) bool) OrderMap[K, V] {
   205  	result := NewOrderMap[K, V]()
   206  	for idx, key := range m.KeyList {
   207  		if !f(idx, key, m.Data[key]) {
   208  			dest.Put(key, m.Data[key])
   209  			result.Put(key, m.Data[key])
   210  		}
   211  	}
   212  	return result
   213  }
   214  
   215  func (m OrderMap[K, V]) Contains(k K, v V) bool {
   216  	return MapContains(m.Data, k, v)
   217  }
   218  
   219  func (m OrderMap[K, V]) ContainsKey(k K) bool {
   220  	return MapContainsKey(m.Data, k)
   221  }
   222  
   223  func (m OrderMap[K, V]) ContainsValue(v V) bool {
   224  	return MapContainsValue(m.Data, v)
   225  }
   226  
   227  func (m OrderMap[K, V]) JoinToString(f func(K, V) string) string {
   228  	return m.JoinToStringFull(",", "", "", f)
   229  }
   230  
   231  func (m OrderMap[K, V]) JoinToStringFull(sep string, prefix string, postfix string, f func(K, V) string) string {
   232  	buffer := prefix
   233  	var count = 0
   234  	for _, key := range m.KeyList {
   235  		count++
   236  		if count > 1 {
   237  			buffer += sep
   238  		}
   239  		buffer += f(key, m.Data[key])
   240  	}
   241  	buffer += postfix
   242  	return buffer
   243  }
   244  
   245  func (m OrderMap[K, V]) All(f func(K, V) bool) bool {
   246  	for _, key := range m.KeyList {
   247  		if !f(key, m.Data[key]) {
   248  			return false
   249  		}
   250  	}
   251  	return true
   252  }
   253  
   254  func (m OrderMap[K, V]) Any(f func(K, V) bool) bool {
   255  	for _, key := range m.KeyList {
   256  		if f(key, m.Data[key]) {
   257  			return true
   258  		}
   259  	}
   260  	return false
   261  }
   262  
   263  func (m OrderMap[K, V]) None(f func(K, V) bool) bool {
   264  	for _, key := range m.KeyList {
   265  		if f(key, m.Data[key]) {
   266  			return false
   267  		}
   268  	}
   269  	return true
   270  }
   271  
   272  func (m OrderMap[K, V]) Count(f func(K, V) bool) int {
   273  	num := 0
   274  	for _, key := range m.KeyList {
   275  		if f(key, m.Data[key]) {
   276  			num++
   277  		}
   278  	}
   279  	return num
   280  }
   281  
   282  func (m OrderMap[K, V]) AllKey(f func(K) bool) bool {
   283  	for _, key := range m.KeyList {
   284  		if !f(key) {
   285  			return false
   286  		}
   287  	}
   288  	return true
   289  }
   290  
   291  func (m OrderMap[K, V]) AnyKey(f func(K) bool) bool {
   292  	for _, key := range m.KeyList {
   293  		if f(key) {
   294  			return true
   295  		}
   296  	}
   297  	return false
   298  }
   299  
   300  func (m OrderMap[K, V]) NoneKey(f func(K) bool) bool {
   301  	for _, key := range m.KeyList {
   302  		if f(key) {
   303  			return false
   304  		}
   305  	}
   306  	return true
   307  }
   308  
   309  func (m OrderMap[K, V]) CountKey(f func(K) bool) int {
   310  	num := 0
   311  	for _, key := range m.KeyList {
   312  		if f(key) {
   313  			num++
   314  		}
   315  	}
   316  	return num
   317  }
   318  
   319  func (m OrderMap[K, V]) AllValue(f func(V) bool) bool {
   320  	for _, key := range m.KeyList {
   321  		if !f(m.Data[key]) {
   322  			return false
   323  		}
   324  	}
   325  	return true
   326  }
   327  
   328  func (m OrderMap[K, V]) AnyValue(f func(V) bool) bool {
   329  	for _, key := range m.KeyList {
   330  		if f(m.Data[key]) {
   331  			return true
   332  		}
   333  	}
   334  	return false
   335  }
   336  
   337  func (m OrderMap[K, V]) NoneValue(f func(V) bool) bool {
   338  	for _, key := range m.KeyList {
   339  		if f(m.Data[key]) {
   340  			return false
   341  		}
   342  	}
   343  	return true
   344  }
   345  
   346  func (m OrderMap[K, V]) CountValue(f func(V) bool) int {
   347  	num := 0
   348  	for _, key := range m.KeyList {
   349  		if f(m.Data[key]) {
   350  			num++
   351  		}
   352  	}
   353  	return num
   354  }
   355  
   356  func (m OrderMap[K, V]) ToList() []Pair[K, V] {
   357  	var n []Pair[K, V]
   358  	for _, key := range m.KeyList {
   359  		n = append(n, NewPair(key, m.Data[key]))
   360  	}
   361  	return n
   362  }
   363  
   364  func (m OrderMap[K, V]) Plus(n OrderMap[K, V]) OrderMap[K, V] {
   365  	r := NewOrderMap[K, V]()
   366  	for _, key := range m.KeyList {
   367  		r.Put(key, m.Data[key])
   368  	}
   369  	n.ForEach(func(k K, v V) {
   370  		r.Put(k, v)
   371  	})
   372  	return r
   373  }
   374  
   375  func (m OrderMap[K, V]) Minus(n OrderMap[K, V]) OrderMap[K, V] {
   376  	r := NewOrderMap[K, V]()
   377  	for _, key := range m.KeyList {
   378  		if _, ok := n.Data[key]; !ok {
   379  			r.Put(key, m.Data[key])
   380  		}
   381  	}
   382  	return r
   383  }