{
    "Function": "_calculateSubComponents",
    "File": "registries/contracts/UnitRegistry.sol",
    "Parent Contracts": [
        "registries/contracts/GenericRegistry.sol",
        "registries/lib/solmate/src/tokens/ERC721.sol",
        "registries/contracts/interfaces/IErrorsRegistries.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [
        "_getSubComponents"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function _calculateSubComponents(UnitType subcomponentsFromType, uint32[] memory unitIds) internal view virtual\n        returns (uint32[] memory subComponentIds)\n    {\n        uint32 numUnits = uint32(unitIds.length);\n        // Array of numbers of components per each unit Id\n        uint32[] memory numComponents = new uint32[](numUnits);\n        // 2D array of all the sets of components per each unit Id\n        uint32[][] memory components = new uint32[][](numUnits);\n\n        // Get total possible number of components and lists of components\n        uint32 maxNumComponents;\n        for (uint32 i = 0; i < numUnits; ++i) {\n            // Get subcomponents for each unit Id based on the subcomponentsFromType\n            components[i] = _getSubComponents(subcomponentsFromType, unitIds[i]);\n            numComponents[i] = uint32(components[i].length);\n            maxNumComponents += numComponents[i];\n        }\n\n        // Lists of components are sorted, take unique values in ascending order\n        uint32[] memory allComponents = new uint32[](maxNumComponents);\n        // Processed component counter\n        uint32[] memory processedComponents = new uint32[](numUnits);\n        // Minimal component Id\n        uint32 minComponent;\n        // Overall component counter\n        uint32 counter;\n        // Iterate until we process all components, at the maximum of the sum of all the components in all units\n        for (counter = 0; counter < maxNumComponents; ++counter) {\n            // Index of a minimal component\n            uint32 minIdxComponent;\n            // Amount of components identified as the next minimal component number\n            uint32 numComponentsCheck;\n            uint32 tryMinComponent = type(uint32).max;\n            // Assemble an array of all first components from each component array\n            for (uint32 i = 0; i < numUnits; ++i) {\n                // Either get a component that has a higher id than the last one ore reach the end of the processed Ids\n                for (; processedComponents[i] < numComponents[i]; ++processedComponents[i]) {\n                    if (minComponent < components[i][processedComponents[i]]) {\n                        // Out of those component Ids that are higher than the last one, pick the minimal one\n                        if (components[i][processedComponents[i]] < tryMinComponent) {\n                            tryMinComponent = components[i][processedComponents[i]];\n                            minIdxComponent = i;\n                        }\n                        // If we found a minimal component Id, we increase the counter and break to start the search again\n                        numComponentsCheck++;\n                        break;\n                    }\n                }\n            }\n            minComponent = tryMinComponent;\n\n            // If minimal component Id is greater than the last one, it should be added, otherwise we reached the end\n            if (numComponentsCheck > 0) {\n                allComponents[counter] = minComponent;\n                processedComponents[minIdxComponent]++;\n            } else {\n                break;\n            }\n        }\n\n        // Return the exact set of found subcomponents with the counter length\n        subComponentIds = new uint32[](counter);\n        for (uint32 i = 0; i < counter; ++i) {\n            subComponentIds[i] = allComponents[i];\n        }\n    }"
}