Skip to content

Commit

Permalink
Merge pull request #4065 from epage/flat
Browse files Browse the repository at this point in the history
refactor: Move off of HashMap/IndexMap to flat containers
  • Loading branch information
epage committed Aug 11, 2022
2 parents b344f1c + e7ced88 commit 084a6df
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 29 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ debug = ["clap_derive/debug", "dep:backtrace"] # Enables debug messages
unstable-doc = ["derive", "cargo", "wrap_help", "env", "unicode", "unstable-replace", "unstable-grouped"] # for docs.rs

# Used in default
std = ["indexmap/std"] # support for no_std in a backwards-compatible way
std = [] # support for no_std in a backwards-compatible way
color = ["dep:atty", "dep:termcolor"]
suggestions = ["dep:strsim"]

Expand All @@ -89,7 +89,6 @@ clap_lex = { path = "./clap_lex", version = "0.2.2" }
bitflags = "1.2"
textwrap = { version = "0.15.0", default-features = false, features = [] }
unicase = { version = "2.6", optional = true }
indexmap = "1.0"
strsim = { version = "0.10", optional = true }
atty = { version = "0.2", optional = true }
termcolor = { version = "1.1.1", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions src/builder/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Std
use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
use std::fmt;
Expand All @@ -20,6 +19,7 @@ use crate::output::fmt::Stream;
use crate::output::{fmt::Colorizer, Help, HelpWriter, Usage};
use crate::parser::{ArgMatcher, ArgMatches, Parser};
use crate::util::ChildGraph;
use crate::util::FlatMap;
use crate::util::{color::ColorChoice, Id, Key};
use crate::{Error, INTERNAL_ERROR_MSG};

Expand Down Expand Up @@ -93,7 +93,7 @@ pub struct Command<'help> {
g_settings: AppFlags,
args: MKeyMap<'help>,
subcommands: Vec<Command<'help>>,
replacers: HashMap<&'help str, &'help [&'help str]>,
replacers: FlatMap<&'help str, &'help [&'help str]>,
groups: Vec<ArgGroup<'help>>,
current_help_heading: Option<&'help str>,
current_disp_ord: Option<usize>,
Expand Down
3 changes: 2 additions & 1 deletion src/builder/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use clap_lex::RawOsStr;

use crate::builder::ValueRange;
use crate::mkeymap::KeyType;
use crate::util::FlatSet;
use crate::util::Id;
use crate::ArgAction;
use crate::INTERNAL_ERROR_MSG;
Expand Down Expand Up @@ -308,7 +309,7 @@ pub(crate) fn assert_app(cmd: &Command) {
detect_duplicate_flags(&long_flags, "long");
detect_duplicate_flags(&short_flags, "short");

let mut subs = indexmap::IndexSet::new();
let mut subs = FlatSet::new();
for sc in cmd.get_subcommands() {
assert!(
subs.insert(sc.get_name()),
Expand Down
2 changes: 1 addition & 1 deletion src/builder/value_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::parser::AnyValueId;
/// use within an application.
///
/// See
/// - [`value_parser!`] for automatically selecting an implementation for a given type
/// - [`value_parser!`][crate::value_parser] for automatically selecting an implementation for a given type
/// - [`ValueParser::new`] for additional [`TypedValueParser`] that can be used
///
/// # Example
Expand Down
4 changes: 2 additions & 2 deletions src/output/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use std::usize;
use crate::builder::PossibleValue;
use crate::builder::{render_arg_val, Arg, Command};
use crate::output::{fmt::Colorizer, Usage};
use crate::util::FlatSet;
use crate::ArgAction;

// Third party
use indexmap::IndexSet;
use textwrap::core::display_width;

/// `clap` Help Writer.
Expand Down Expand Up @@ -821,7 +821,7 @@ impl<'help, 'cmd, 'writer> Help<'help, 'cmd, 'writer> {
.cmd
.get_arguments()
.filter_map(|arg| arg.get_help_heading())
.collect::<IndexSet<_>>();
.collect::<FlatSet<_>>();

let mut first = if !pos.is_empty() {
// Write positional args if any
Expand Down
15 changes: 7 additions & 8 deletions src/output/usage.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use indexmap::IndexSet;

// Internal
use crate::builder::{Arg, ArgPredicate, Command};
use crate::parser::ArgMatcher;
use crate::util::ChildGraph;
use crate::util::FlatSet;
use crate::util::Id;
use crate::INTERNAL_ERROR_MSG;

Expand Down Expand Up @@ -329,16 +328,16 @@ impl<'help, 'cmd> Usage<'help, 'cmd> {
incls: &[Id],
matcher: Option<&ArgMatcher>,
incl_last: bool,
) -> IndexSet<String> {
) -> FlatSet<String> {
debug!(
"Usage::get_required_usage_from: incls={:?}, matcher={:?}, incl_last={:?}",
incls,
matcher.is_some(),
incl_last
);
let mut ret_val = IndexSet::new();
let mut ret_val = FlatSet::new();

let mut unrolled_reqs = IndexSet::new();
let mut unrolled_reqs = FlatSet::new();

let required_owned;
let required = if let Some(required) = self.required {
Expand Down Expand Up @@ -378,9 +377,9 @@ impl<'help, 'cmd> Usage<'help, 'cmd> {
unrolled_reqs
);

let mut required_groups_members = IndexSet::new();
let mut required_opts = IndexSet::new();
let mut required_groups = IndexSet::new();
let mut required_groups_members = FlatSet::new();
let mut required_opts = FlatSet::new();
let mut required_groups = FlatSet::new();
let mut required_positionals = Vec::new();
for req in unrolled_reqs.iter().chain(incls.iter()) {
if let Some(arg) = self.cmd.find(req) {
Expand Down
12 changes: 6 additions & 6 deletions src/parser/arg_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Std
use std::collections::HashMap;
use std::ffi::OsString;
use std::mem;
use std::ops::Deref;
Expand All @@ -10,6 +9,7 @@ use crate::parser::AnyValue;
use crate::parser::Identifier;
use crate::parser::PendingArg;
use crate::parser::{ArgMatches, MatchedArg, SubCommand, ValueSource};
use crate::util::FlatMap;
use crate::util::Id;
use crate::INTERNAL_ERROR_MSG;

Expand Down Expand Up @@ -46,14 +46,14 @@ impl ArgMatcher {
"ArgMatcher::get_global_values: global_arg_vec={:?}",
global_arg_vec
);
let mut vals_map = HashMap::new();
let mut vals_map = FlatMap::new();
self.fill_in_global_values(global_arg_vec, &mut vals_map);
}

fn fill_in_global_values(
&mut self,
global_arg_vec: &[Id],
vals_map: &mut HashMap<Id, MatchedArg>,
vals_map: &mut FlatMap<Id, MatchedArg>,
) {
for global_arg in global_arg_vec {
if let Some(ma) = self.get(global_arg) {
Expand Down Expand Up @@ -99,18 +99,18 @@ impl ArgMatcher {
}

pub(crate) fn remove(&mut self, arg: &Id) {
self.matches.args.swap_remove(arg);
self.matches.args.remove(arg);
}

pub(crate) fn contains(&self, arg: &Id) -> bool {
self.matches.args.contains_key(arg)
}

pub(crate) fn arg_ids(&self) -> indexmap::map::Keys<Id, MatchedArg> {
pub(crate) fn arg_ids(&self) -> std::slice::Iter<'_, Id> {
self.matches.args.keys()
}

pub(crate) fn entry(&mut self, arg: &Id) -> indexmap::map::Entry<Id, MatchedArg> {
pub(crate) fn entry(&mut self, arg: &Id) -> crate::util::Entry<Id, MatchedArg> {
self.matches.args.entry(arg.clone())
}

Expand Down
6 changes: 2 additions & 4 deletions src/parser/matches/arg_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ use std::fmt::Debug;
use std::iter::{Cloned, Flatten, Map};
use std::slice::Iter;

// Third Party
use indexmap::IndexMap;

// Internal
use crate::parser::AnyValue;
use crate::parser::AnyValueId;
use crate::parser::MatchedArg;
use crate::parser::MatchesError;
use crate::parser::ValueSource;
use crate::util::FlatMap;
use crate::util::{Id, Key};
use crate::INTERNAL_ERROR_MSG;

Expand Down Expand Up @@ -68,7 +66,7 @@ pub struct ArgMatches {
pub(crate) valid_args: Vec<Id>,
#[cfg(debug_assertions)]
pub(crate) valid_subcommands: Vec<Id>,
pub(crate) args: IndexMap<Id, MatchedArg>,
pub(crate) args: FlatMap<Id, MatchedArg>,
pub(crate) subcommand: Option<Box<SubCommand>>,
}

Expand Down
6 changes: 4 additions & 2 deletions src/parser/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::output::fmt::Stream;
use crate::output::Usage;
use crate::parser::{ArgMatcher, ParseState};
use crate::util::ChildGraph;
use crate::util::FlatMap;
use crate::util::FlatSet;
use crate::util::Id;
use crate::INTERNAL_ERROR_MSG;

Expand Down Expand Up @@ -153,7 +155,7 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {
}

debug!("Validator::build_conflict_err: name={:?}", name);
let mut seen = std::collections::HashSet::new();
let mut seen = FlatSet::new();
let conflicts = conflict_ids
.iter()
.flat_map(|c_id| {
Expand Down Expand Up @@ -382,7 +384,7 @@ impl<'help, 'cmd> Validator<'help, 'cmd> {

#[derive(Default, Clone, Debug)]
struct Conflicts {
potential: std::collections::HashMap<Id, Vec<Id>>,
potential: FlatMap<Id, Vec<Id>>,
}

impl Conflicts {
Expand Down

0 comments on commit 084a6df

Please sign in to comment.