KDAlgorithms icon indicating copy to clipboard operation
KDAlgorithms copied to clipboard

Range-ify the code

Open dangelog opened this issue 3 years ago • 0 comments

We cannot pretend that ranges don't exist. While rightfully one does NOT want to reimplement the algorithms themselves¹, one should still

  • reason in terms of iterator/sentinel pairs, instead of iterator/iterator
  • detect if <ranges> is available
  • if so, use the algorithm overloads in std::ranges (as they're the only ones that support iterator/sentinel)

Example:

QRegularExpression re = "\\d+";
auto matches = re.globalMatch(string);
auto matchesLengths = KDAlgorithms::transform(matches, [](const auto &m) { return m.capturedLength(); });

¹ even if simple (e.g. find_if). In other words, ranges should be supported up to the algorithmic call, after that one can require the user to be using a C++20 standard library.

dangelog avatar Sep 08 '22 08:09 dangelog