/* Copyright 2023 Equinor ASA This file is part of the Open Porous Media Project (OPM). OPM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OPM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OPM. If not, see . */ #include #include #include #include #include namespace { template std::pair valueRange(const std::vector& x) { auto mmPos = std::minmax_element(x.begin(), x.end()); return { *mmPos.first, *mmPos.second }; } void compressAndCountPartitionIDs(std::vector& partition, int& num_domains) { const auto& [low, high] = valueRange(partition); auto seen = std::vector(high - low + 1, false); for (const auto& domain : partition) { seen[domain - low] = domain >= 0; } auto compressed = std::vector(seen.size(), -1); for (auto i = 0*compressed.size(); i < compressed.size(); ++i) { if (seen[i]) { compressed[i] = num_domains++; } } for (auto& domain : partition) { if (domain >= 0) { domain = compressed[domain - low]; } } } } // Anonymous namespace std::pair, int> Opm::util::compressAndCountPartitionIDs(std::vector&& parts0) { auto parts = std::pair, int> { std::move(parts0), 0 }; if (! parts.first.empty()) { ::compressAndCountPartitionIDs(parts.first, parts.second); } return parts; } std::vector Opm::util::compressPartitionIDs(std::vector&& parts0) { return compressAndCountPartitionIDs(std::move(parts0)).first; } void Opm::util::compressPartitionIDs(std::vector& parts0) { [[maybe_unused]] auto num_domains = 0; ::compressAndCountPartitionIDs(parts0, num_domains); }