I have a structs, mappings and a function as follow:
// a list of selectors that might be allowed functions
bytes4[] internal knownSelectors;
mapping(bytes4 => bool) internal knownSelector;
struct State {
// a mapping of functions that can be executed when in this state
mapping(bytes4 => bool) allowedFunctions;
// a list of all the roles that have been configured for this state
bytes32[] allowedRoles;
}
mapping(bytes32 => State) internal states;
function getState(bytes32 state)
public
view
returns (
uint256 counter
)
{
State storage s = states[state]; // copy to memory
uint8 counter = 0;
for (uint256 i = 0; i < knownSelectors.length; i++) {
if (states[state].allowedFunctions[knownSelectors[i]]) {
counter += 1;
}
}
}
But I would like to avoid that for loop in the end of the function, do you guys have any idea on how to do that? because if there are a lot of knownSelectors then this loop might be extremely expensive... thanks in advance for the advices!