github.com/mymmsc/gox@v1.3.33/util/treemap/enumerable.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package treemap
     6  
     7  import (
     8  	"github.com/mymmsc/gox/util"
     9  	rbt "github.com/mymmsc/gox/util/redblacktree"
    10  )
    11  
    12  func assertEnumerableImplementation() {
    13  	var _ util.EnumerableWithKey = (*Map)(nil)
    14  }
    15  
    16  // Each calls the given function once for each element, passing that element's key and value.
    17  func (m *Map) Each(f func(key interface{}, value interface{})) {
    18  	iterator := m.Iterator()
    19  	for iterator.Next() {
    20  		f(iterator.Key(), iterator.Value())
    21  	}
    22  }
    23  
    24  // Map invokes the given function once for each element and returns a container
    25  // containing the values returned by the given function as key/value pairs.
    26  func (m *Map) Map(f func(key1 interface{}, value1 interface{}) (interface{}, interface{})) *Map {
    27  	newMap := &Map{tree: rbt.NewWith(m.tree.Comparator)}
    28  	iterator := m.Iterator()
    29  	for iterator.Next() {
    30  		key2, value2 := f(iterator.Key(), iterator.Value())
    31  		newMap.Put(key2, value2)
    32  	}
    33  	return newMap
    34  }
    35  
    36  // Select returns a new container containing all elements for which the given function returns a true value.
    37  func (m *Map) Select(f func(key interface{}, value interface{}) bool) *Map {
    38  	newMap := &Map{tree: rbt.NewWith(m.tree.Comparator)}
    39  	iterator := m.Iterator()
    40  	for iterator.Next() {
    41  		if f(iterator.Key(), iterator.Value()) {
    42  			newMap.Put(iterator.Key(), iterator.Value())
    43  		}
    44  	}
    45  	return newMap
    46  }
    47  
    48  // Any passes each element of the container to the given function and
    49  // returns true if the function ever returns true for any element.
    50  func (m *Map) Any(f func(key interface{}, value interface{}) bool) bool {
    51  	iterator := m.Iterator()
    52  	for iterator.Next() {
    53  		if f(iterator.Key(), iterator.Value()) {
    54  			return true
    55  		}
    56  	}
    57  	return false
    58  }
    59  
    60  // All passes each element of the container to the given function and
    61  // returns true if the function returns true for all elements.
    62  func (m *Map) All(f func(key interface{}, value interface{}) bool) bool {
    63  	iterator := m.Iterator()
    64  	for iterator.Next() {
    65  		if !f(iterator.Key(), iterator.Value()) {
    66  			return false
    67  		}
    68  	}
    69  	return true
    70  }
    71  
    72  // Find passes each element of the container to the given function and returns
    73  // the first (key,value) for which the function is true or nil,nil otherwise if no element
    74  // matches the criteria.
    75  func (m *Map) Find(f func(key interface{}, value interface{}) bool) (interface{}, interface{}) {
    76  	iterator := m.Iterator()
    77  	for iterator.Next() {
    78  		if f(iterator.Key(), iterator.Value()) {
    79  			return iterator.Key(), iterator.Value()
    80  		}
    81  	}
    82  	return nil, nil
    83  }