Skip to content

Commit

Permalink
Switch to IndexMap for consistent ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Jun 21, 2024
1 parent ae49df8 commit c5fa746
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -243,8 +243,8 @@ impl WordTally {
}

/// Creates a tally of normalized words from an input that implements `Read`.
fn tally_map<T: Read>(input: T, case: Case, chars: Chars) -> HashMap<String, u64> {
let mut tally = HashMap::new();
fn tally_map<T: Read>(input: T, case: Case, chars: Chars) -> IndexMap<String, u64> {
let mut tally = IndexMap::new();
let lines = BufReader::new(input).lines();

for line in lines.map_while(Result::ok) {
Expand All @@ -261,7 +261,7 @@ impl WordTally {
}

/// Removes words from the `tally_map` based on any word `Filters`.
fn filter(tally_map: &mut HashMap<String, u64>, filters: Filters, case: Case) {
fn filter(tally_map: &mut IndexMap<String, u64>, 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);
Expand Down

0 comments on commit c5fa746

Please sign in to comment.