library Role {

    struct RoleContainer {
        address[] bearer;
    }

    /**
     * @param role role storage
     * @return amount of accounts in this role
     */
    function total (RoleContainer storage role) internal view returns (uint count) {
        for (uint i = 0; i < role.bearer.length; i++) {
            count += (role.bearer[i] == address(0)) ? 0 : 1;
        }
        return count;
    }


    /**
     * @dev check if an account has this role
     * @return bool
     */
    function has(RoleContainer storage role, address account) internal view returns (bool) {
        require(account != address(0));
        address[] memory list = role.bearer;
        uint len = role.bearer.length;
        for (uint index = 0; index < len; index++) {
            if (list[index] == account) {
                return true;
            }
        }
        return false;
    }

    /**
     * @dev give an account access to this role
     */
    function add(RoleContainer storage role, address account) internal {
        require(account != address(0));
        require(!has(role, account));

        role.bearer.push(account);
    }

    /**
     * @dev remove an account's access to this role
     */
    function remove(RoleContainer storage role, address account) internal {
        require(account != address(0));
        require(has(role, account));

        removeFromList(role, account);
    }

    /**
     * @dev Helper function. Iterates over array to find and
       remove given account from it
       @param role Role storage
       @param account Expelled account
     */
    function removeFromList(RoleContainer storage role, address account) private {
        address[] storage list = role.bearer;
        uint len = role.bearer.length;

        for (uint index = 0; index <= len; index++) {
            if (list[index] != account) {
                continue;
            }
            list[index] = list[len - 1];
            delete list[len - 1];
            return;
        }
    }
}
function total (RoleContainer storage role) internal view returns (uint count) {
    for (uint i = 0; i < role.bearer.length; i++) {
        count += (role.bearer[i] == address(0)) ? 0 : 1;
    }
    return count;
}

/**
 * @param role role storage
 * @return amount of accounts in this role
 */