From c5fa746e4ed2e05767306794e8d9480243562d7c Mon Sep 17 00:00:00 2001 From: Shannon Skipper Date: Fri, 21 Jun 2024 22:31:18 +0000 Subject: [PATCH] Switch to `IndexMap` for consistent ordering --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 8 ++++---- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 356bb05..354cdf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -289,6 +289,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "float-cmp" version = "0.9.0" @@ -308,6 +314,12 @@ dependencies = [ "crunchy", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "heck" version = "0.5.0" @@ -320,6 +332,16 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "is-terminal" version = "0.4.12" @@ -831,6 +853,7 @@ dependencies = [ "clap", "clap-stdin", "criterion", + "indexmap", "once_cell", "predicates", "serde", diff --git a/Cargo.toml b/Cargo.toml index 30ee2b9..341e558 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ version = "0.7.1" anyhow = "1.0.83" clap = { version = "4.5.4", features = ["derive"] } clap-stdin = "0.4.0" +indexmap = "2.2.6" serde = { version = "1.0.203", features = ["derive"], optional = true } serde_json = { version = "1.0.85", optional = true } unescaper = "0.1.4" diff --git a/src/lib.rs b/src/lib.rs index 1a7b84a..af0471c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,9 +33,9 @@ use clap::ValueEnum; use core::cmp::Reverse; use core::fmt; use core::hash::{Hash, Hasher}; +use indexmap::IndexMap; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use std::collections::HashMap; use std::io::{BufRead, BufReader, Read}; use unicode_segmentation::UnicodeSegmentation; @@ -243,8 +243,8 @@ impl WordTally { } /// Creates a tally of normalized words from an input that implements `Read`. - fn tally_map(input: T, case: Case, chars: Chars) -> HashMap { - let mut tally = HashMap::new(); + fn tally_map(input: T, case: Case, chars: Chars) -> IndexMap { + let mut tally = IndexMap::new(); let lines = BufReader::new(input).lines(); for line in lines.map_while(Result::ok) { @@ -261,7 +261,7 @@ impl WordTally { } /// Removes words from the `tally_map` based on any word `Filters`. - fn filter(tally_map: &mut HashMap, filters: Filters, case: Case) { + fn filter(tally_map: &mut IndexMap, filters: Filters, case: Case) { // Remove any words that lack the minimum number of characters. if filters.count.min > 1 { tally_map.retain(|_, &mut count| count >= filters.count.min);