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

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