Skip to content

Commit

Permalink
using <ranges> instead of <algorithm>
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed Mar 12, 2024
1 parent f5cb1a9 commit 06d694e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 60 deletions.
1 change: 1 addition & 0 deletions change_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
* Aligned the `.clang-format` configuration file and moved formatting scripts to a new directory `<project-root>/scripts`
* Added the `install_clang_format_17.sh` env script
* Added the `format` workflow
* Switched from the `<algorithm>` to the `<ranges>` library for all current container operations
73 changes: 16 additions & 57 deletions include/ap/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ SOFTWARE.

#pragma once

#include <algorithm>
#include <any>
#include <compare>
#include <concepts>
Expand All @@ -45,6 +44,7 @@ SOFTWARE.
#include <iostream>
#include <memory>
#include <optional>
#include <ranges>
#include <sstream>
#include <stdexcept>
#include <string_view>
Expand Down Expand Up @@ -801,8 +801,7 @@ class positional_argument : public detail::argument_interface {
* @return True if the choice valid, false otherwise.
*/
[[nodiscard]] inline bool _is_valid_choice(const value_type& choice) const {
return this->_choices.empty()
or std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end();
return this->_choices.empty() or std::ranges::find(this->_choices, choice) != this->_choices.end();
}

/**
Expand Down Expand Up @@ -1102,8 +1101,7 @@ class optional_argument : public detail::argument_interface {
* @return True if choice is valid, false otherwise.
*/
[[nodiscard]] inline bool _is_valid_choice(const value_type& choice) const {
return this->_choices.empty()
or std::find(this->_choices.begin(), this->_choices.end(), choice) != this->_choices.end();
return this->_choices.empty() or std::ranges::find(this->_choices, choice) != this->_choices.end();
}

/**
Expand Down Expand Up @@ -1380,14 +1378,12 @@ class argument_parser {
if (not arg.has_parsed_values() and arg.has_value())
return std::vector<T>{ std::any_cast<T>(arg.value()) };

const auto& arg_values = arg.values();

std::vector<T> values;
std::transform(
std::begin(arg_values),
std::end(arg_values),
std::back_inserter(values),
[](const std::any& value) { return std::any_cast<T>(value); }
std::ranges::copy(
std::views::transform(
arg.values(), [](const std::any& value) { return std::any_cast<T>(value); }
),
std::back_inserter(values)
);
return values;
}
Expand Down Expand Up @@ -1551,10 +1547,10 @@ class argument_parser {
[[nodiscard]] bool _is_arg_name_used(const std::string_view& name) const {
const auto predicate = this->_name_eq_predicate(name);

if (this->_const_find_positional(predicate) != this->_positional_args.end())
if (std::ranges::find_if(this->_positional_args, predicate) != this->_positional_args.end())
return true;

if (this->_const_find_optional(predicate) != this->_optional_args.end())
if (std::ranges::find_if(this->_optional_args, predicate) != this->_optional_args.end())
return true;

return false;
Expand All @@ -1569,10 +1565,10 @@ class argument_parser {
[[nodiscard]] bool _is_arg_name_used(const std::string_view& name, const std::string_view& short_name) const {
const auto predicate = this->_name_eq_predicate(name, short_name);

if (this->_const_find_positional(predicate) != this->_positional_args.end())
if (std::ranges::find_if(this->_positional_args, predicate) != this->_positional_args.end())
return true;

if (this->_const_find_optional(predicate) != this->_optional_args.end())
if (std::ranges::find_if(this->_optional_args, predicate) != this->_optional_args.end())
return true;

return false;
Expand Down Expand Up @@ -1670,7 +1666,8 @@ class argument_parser {

while (cmd_it != cmd_args.end()) {
if (cmd_it->discriminator == cmd_argument::type_discriminator::flag) {
auto opt_arg_it = this->_find_optional(this->_name_eq_predicate(cmd_it->value));
auto opt_arg_it =
std::ranges::find_if(this->_optional_args, this->_name_eq_predicate(cmd_it->value));

if (opt_arg_it == this->_optional_args.end())
throw error::argument_not_found_error(cmd_it->value);
Expand Down Expand Up @@ -1733,57 +1730,19 @@ class argument_parser {
argument_opt_type _get_argument(const std::string_view& name) const {
const auto predicate = this->_name_eq_predicate(name);

if (auto pos_arg_it = this->_const_find_positional(predicate);
if (auto pos_arg_it = std::ranges::find_if(this->_positional_args, predicate);
pos_arg_it != this->_positional_args.end()) {
return std::ref(**pos_arg_it);
}

if (auto opt_arg_it = this->_const_find_optional(predicate);
if (auto opt_arg_it = std::ranges::find_if(this->_optional_args, predicate);
opt_arg_it != this->_optional_args.end()) {
return std::ref(**opt_arg_it);
}

return std::nullopt;
}

/**
* @brief Find a positional argument based on the provided predicate.
* @param predicate The predicate for finding the argument.
* @return Iterator to the found positional argument.
*/
[[nodiscard]] inline argument_list_iterator_type _find_positional(const argument_predicate_type& predicate) {
return std::find_if(std::begin(this->_positional_args), std::end(this->_positional_args), predicate);
}

/**
* @brief Find an optional argument based on the provided predicate.
* @param predicate The predicate for finding the argument.
* @return Iterator to the found optional argument.
*/
[[nodiscard]] inline argument_list_iterator_type _find_optional(const argument_predicate_type& predicate) {
return std::find_if(std::begin(this->_optional_args), std::end(this->_optional_args), predicate);
}

/**
* @brief Find a positional argument based on the provided predicate (const version).
* @param predicate The predicate for finding the argument.
* @return Iterator to the found positional argument.
*/
[[nodiscard]] inline argument_list_const_iterator_type _const_find_positional(const argument_predicate_type& predicate
) const {
return std::find_if(std::cbegin(this->_positional_args), std::cend(this->_positional_args), predicate);
}

/**
* @brief Find an optional argument based on the provided predicate (const version).
* @param predicate The predicate for finding the argument.
* @return Iterator to the found optional argument.
*/
[[nodiscard]] inline argument_list_const_iterator_type _const_find_optional(const argument_predicate_type& predicate
) const {
return std::find_if(std::cbegin(this->_optional_args), std::cend(this->_optional_args), predicate);
}

std::optional<std::string> _program_name;
std::optional<std::string> _program_description;

Expand Down
4 changes: 2 additions & 2 deletions scripts/format/unix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ run_clang_format() {
}

# Count the number of files to format
file_count=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) | wc -l)
file_count=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) ! -path "*/build/*" | wc -l)
if [[ "$format_check" == true ]]; then
echo "Files to check: $file_count"
else
Expand All @@ -25,7 +25,7 @@ echo

# Iterate over the files and run format/check
file_number=0
find . -type f \( -name "*.hpp" -o -name "*.cpp" \) -print0 | while IFS= read -r -d '' file; do
find . -type f \( -name "*.hpp" -o -name "*.cpp" \) ! -path "*/build/*" -print0 | while IFS= read -r -d '' file; do
((file_number++))
echo "[$file_number/$file_count] $file"
run_clang_format "$file"
Expand Down
2 changes: 1 addition & 1 deletion scripts/format/windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Run-Clang-Format {
}

# Get the list of files to format
$files = Get-ChildItem -Recurse -Include *.hpp, *.cpp
$files = Get-ChildItem -Recurse -Include *.hpp, *.cpp | Where-Object { $_.DirectoryName -notmatch "\\build\\" }

# Count the number of files to format
$fileCount = $files.Count
Expand Down

0 comments on commit 06d694e

Please sign in to comment.