github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/filter/id/identifier.go (about)

     1  package id
     2  
     3  import "github.com/onflow/flow-go/model/flow"
     4  
     5  // Any is a flow.IdentifierFilter. It accepts all identifiers.
     6  func Any(flow.Identifier) bool {
     7  	return true
     8  }
     9  
    10  // False is a flow.IdentifierFilter. It accepts no identifier.
    11  func False(flow.Identifier) bool {
    12  	return false
    13  }
    14  
    15  // And combines two or more filters that all need to be true.
    16  func And(filters ...flow.IdentifierFilter) flow.IdentifierFilter {
    17  	return func(id flow.Identifier) bool {
    18  		for _, filter := range filters {
    19  			if !filter(id) {
    20  				return false
    21  			}
    22  		}
    23  		return true
    24  	}
    25  }
    26  
    27  // Or combines two or more filters and only needs one of them to be true.
    28  func Or(filters ...flow.IdentifierFilter) flow.IdentifierFilter {
    29  	return func(id flow.Identifier) bool {
    30  		for _, filter := range filters {
    31  			if filter(id) {
    32  				return true
    33  			}
    34  		}
    35  		return false
    36  	}
    37  }
    38  
    39  // Not returns a filter equivalent to the inverse of the input filter.
    40  func Not(filter flow.IdentifierFilter) flow.IdentifierFilter {
    41  	return func(id flow.Identifier) bool {
    42  		return !filter(id)
    43  	}
    44  }
    45  
    46  // In constructs a filter that returns true if and only if
    47  // the Identifier is in the provided list.
    48  func In(ids ...flow.Identifier) flow.IdentifierFilter {
    49  	lookup := make(map[flow.Identifier]struct{})
    50  	for _, nodeID := range ids {
    51  		lookup[nodeID] = struct{}{}
    52  	}
    53  	return func(id flow.Identifier) bool {
    54  		_, ok := lookup[id]
    55  		return ok
    56  	}
    57  }
    58  
    59  // Is constructs a filter that returns true if and only if
    60  // the Identifier is identical to the expectedID.
    61  func Is(expectedID flow.Identifier) flow.IdentifierFilter {
    62  	return func(id flow.Identifier) bool {
    63  		return id == expectedID
    64  	}
    65  }
    66  
    67  // InSet constructs a filter that returns true if and only if
    68  // the Identifier is in the provided map.
    69  // CAUTION: input map is _not_ copied
    70  func InSet(ids map[flow.Identifier]struct{}) flow.IdentifierFilter {
    71  	return func(id flow.Identifier) bool {
    72  		_, ok := ids[id]
    73  		return ok
    74  	}
    75  }