github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/treebidimap/enumerable.go (about)

     1  package treebidimap
     2  
     3  import "github.com/songzhibin97/go-baseutils/structure/containers"
     4  
     5  // Assert Enumerable implementation
     6  var _ containers.EnumerableWithKey[any, any] = (*Map[any, any])(nil)
     7  
     8  // Each calls the given function once for each element, passing that element's key and value.
     9  func (m *Map[K, V]) Each(f func(key K, value V)) {
    10  	iterator := m.Iterator()
    11  	for iterator.Next() {
    12  		f(iterator.Key(), iterator.Value())
    13  	}
    14  }
    15  
    16  // Map invokes the given function once for each element and returns a container
    17  // containing the values returned by the given function as key/value pairs.
    18  func (m *Map[K, V]) Map(f func(key1 K, value1 V) (K, V)) *Map[K, V] {
    19  	newMap := NewWith(m.keyComparator, m.valueComparator)
    20  	iterator := m.Iterator()
    21  	for iterator.Next() {
    22  		key2, value2 := f(iterator.Key(), iterator.Value())
    23  		newMap.Put(key2, value2)
    24  	}
    25  	return newMap
    26  }
    27  
    28  // Select returns a new container containing all elements for which the given function returns a true value.
    29  func (m *Map[K, V]) Select(f func(key K, value V) bool) *Map[K, V] {
    30  	newMap := NewWith(m.keyComparator, m.valueComparator)
    31  	iterator := m.Iterator()
    32  	for iterator.Next() {
    33  		if f(iterator.Key(), iterator.Value()) {
    34  			newMap.Put(iterator.Key(), iterator.Value())
    35  		}
    36  	}
    37  	return newMap
    38  }
    39  
    40  // Any passes each element of the container to the given function and
    41  // returns true if the function ever returns true for any element.
    42  func (m *Map[K, V]) Any(f func(key K, value V) bool) bool {
    43  	iterator := m.Iterator()
    44  	for iterator.Next() {
    45  		if f(iterator.Key(), iterator.Value()) {
    46  			return true
    47  		}
    48  	}
    49  	return false
    50  }
    51  
    52  // All passes each element of the container to the given function and
    53  // returns true if the function returns true for all elements.
    54  func (m *Map[K, V]) All(f func(key K, value V) bool) bool {
    55  	iterator := m.Iterator()
    56  	for iterator.Next() {
    57  		if !f(iterator.Key(), iterator.Value()) {
    58  			return false
    59  		}
    60  	}
    61  	return true
    62  }
    63  
    64  // Find passes each element of the container to the given function and returns
    65  // the first (key,value) for which the function is true or nil,nil otherwise if no element
    66  // matches the criteria.
    67  func (m *Map[K, V]) Find(f func(key K, value V) bool) (K, V) {
    68  	iterator := m.Iterator()
    69  	for iterator.Next() {
    70  		if f(iterator.Key(), iterator.Value()) {
    71  			return iterator.Key(), iterator.Value()
    72  		}
    73  	}
    74  	return m.zeroK, m.zeroV
    75  }