github.com/onflow/flow-go@v0.33.17/fvm/storage/derived/dependencies.go (about)

     1  package derived
     2  
     3  import (
     4  	"github.com/onflow/cadence/runtime/common"
     5  )
     6  
     7  // ProgramDependencies are the locations of programs that a program depends on.
     8  type ProgramDependencies struct {
     9  	locations map[common.Location]struct{}
    10  	addresses map[common.Address]struct{}
    11  }
    12  
    13  func NewProgramDependencies() ProgramDependencies {
    14  	return ProgramDependencies{
    15  		locations: map[common.Location]struct{}{},
    16  		addresses: map[common.Address]struct{}{},
    17  	}
    18  }
    19  
    20  // Count returns the number of locations dependencies of this program.
    21  func (d ProgramDependencies) Count() int {
    22  	return len(d.locations)
    23  }
    24  
    25  // Add adds the location as a dependency.
    26  func (d ProgramDependencies) Add(location common.Location) ProgramDependencies {
    27  	d.locations[location] = struct{}{}
    28  
    29  	if addressLocation, ok := location.(common.AddressLocation); ok {
    30  		d.addresses[addressLocation.Address] = struct{}{}
    31  	}
    32  
    33  	return d
    34  }
    35  
    36  // Merge merges current dependencies with other dependencies.
    37  func (d ProgramDependencies) Merge(other ProgramDependencies) {
    38  	for loc := range other.locations {
    39  		d.locations[loc] = struct{}{}
    40  	}
    41  	for address := range other.addresses {
    42  		d.addresses[address] = struct{}{}
    43  	}
    44  }
    45  
    46  // ContainsAddress returns true if the address is a dependency.
    47  func (d ProgramDependencies) ContainsAddress(address common.Address) bool {
    48  	_, ok := d.addresses[address]
    49  	return ok
    50  }
    51  
    52  // ContainsLocation returns true if the location is a dependency.
    53  func (d ProgramDependencies) ContainsLocation(location common.Location) bool {
    54  	_, ok := d.locations[location]
    55  	return ok
    56  }