github.com/koko1123/flow-go-1@v0.29.6/model/flow/filter/id/identifier.go (about)

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