Use a BTreeSet of hashesh instead of a HashMap of unit

This commit is contained in:
Austen Adler 2022-03-06 21:29:15 -05:00
parent 1780b2a60d
commit 2e2fdb200d
5 changed files with 31 additions and 25 deletions

7
Cargo.lock generated
View File

@ -76,6 +76,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "evalexpr"
version = "7.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90c8b61e4acbb2e4fbcf9d4c7af4d431f38c2a60b975b1d03d0276fbb032ec5d"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.4" version = "0.2.4"
@ -124,6 +130,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"alphanumeric-sort", "alphanumeric-sort",
"clap", "clap",
"evalexpr",
"rand", "rand",
"regex", "regex",
"shellwords", "shellwords",

View File

@ -14,6 +14,7 @@ clap = {version = "3", features = ["derive", "env"]}
alphanumeric-sort = "1" alphanumeric-sort = "1"
shellwords = "1" shellwords = "1"
rand = "0.8" rand = "0.8"
evalexpr = "7"
[profile.release] [profile.release]
lto = true lto = true

View File

@ -43,12 +43,10 @@ define-command sort-selections -params 0.. %{
regex="${1:-.*}" regex="${1:-.*}"
# use kak_command_fifo kak_response_fifo; # use kak_command_fifo kak_response_fifo;
~/syncthing/code/kakutils-rs/target/debug/kakutils-rs sort "$regex" -- "$@" ~/syncthing/code/kakutils-rs/target/debug/kakutils-rs sort "$regex" -- "$@"
} }
exec R exec R
} }
} }
---- ----
== Usage == Usage

View File

@ -17,6 +17,7 @@ mod uniq;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use errors::KakMessage; use errors::KakMessage;
use std::{ use std::{
convert::Into,
env, fs, env, fs,
fs::{File, OpenOptions}, fs::{File, OpenOptions},
io::Write, io::Write,
@ -168,7 +169,7 @@ fn run() -> Result<KakMessage, KakMessage> {
pub fn kak_exec(cmd: &str) -> Result<(), KakMessage> { pub fn kak_exec(cmd: &str) -> Result<(), KakMessage> {
let mut f = open_command_fifo()?; let mut f = open_command_fifo()?;
write!(f, "{}", cmd).map_err(|e| e.into()) write!(f, "{}", cmd).map_err(Into::into)
} }
pub fn kak_response(msg: &str) -> Result<Vec<String>, KakMessage> { pub fn kak_response(msg: &str) -> Result<Vec<String>, KakMessage> {
@ -187,7 +188,7 @@ pub fn open_command_fifo() -> Result<File, KakMessage> {
.write(true) .write(true)
.append(true) .append(true)
.open(&get_var("kak_command_fifo")?) .open(&get_var("kak_command_fifo")?)
.map_err(|e| e.into()) .map_err(Into::into)
} }
pub fn get_var(var_name: &str) -> Result<String, KakMessage> { pub fn get_var(var_name: &str) -> Result<String, KakMessage> {
@ -229,13 +230,13 @@ mod test {
#[test] #[test]
fn test_contains() { fn test_contains() {
assert_true!(sd.contains(sd)); assert_true!(sd.contains(sd));
assert_false!(sd.contains(SelectionDesc::from_str("17.9,10.1").unwrap())) assert_false!(sd.contains(SelectionDesc::from_str("17.9,10.1").unwrap()));
assert_false!(sd.contains(SelectionDesc::from_str("18.8,10.1").unwrap())) assert_false!(sd.contains(SelectionDesc::from_str("18.8,10.1").unwrap()));
assert_false!(sd.contains(SelectionDesc::from_str("18.9,11.1").unwrap())) assert_false!(sd.contains(SelectionDesc::from_str("18.9,11.1").unwrap()));
assert_false!(sd.contains(SelectionDesc::from_str("18.9,10.2").unwrap())) assert_false!(sd.contains(SelectionDesc::from_str("18.9,10.2").unwrap()));
assert_true!(sd.contains(SelectionDesc::from_str("19.9,10.1").unwrap())) assert_true!(sd.contains(SelectionDesc::from_str("19.9,10.1").unwrap()));
assert_true!(sd.contains(SelectionDesc::from_str("18.10,10.1").unwrap())) assert_true!(sd.contains(SelectionDesc::from_str("18.10,10.1").unwrap()));
assert_true!(sd.contains(SelectionDesc::from_str("18.9,9.1").unwrap())) assert_true!(sd.contains(SelectionDesc::from_str("18.9,9.1").unwrap()));
assert_true!(sd.contains(SelectionDesc::from_str("18.9,10.0").unwrap())) assert_true!(sd.contains(SelectionDesc::from_str("18.9,10.0").unwrap()));
} }
} }

View File

@ -1,6 +1,7 @@
use crate::{kak_response, open_command_fifo, KakMessage}; use crate::{kak_response, open_command_fifo, KakMessage};
use std::{ use std::{
collections::{hash_map::Entry, HashMap}, collections::{hash_map::DefaultHasher, BTreeSet},
hash::{Hash, Hasher},
io::Write, io::Write,
}; };
#[derive(clap::StructOpt, Debug)] #[derive(clap::StructOpt, Debug)]
@ -17,7 +18,7 @@ pub fn uniq(options: &Options) -> Result<KakMessage, KakMessage> {
let mut f = open_command_fifo()?; let mut f = open_command_fifo()?;
write!(f, "reg '\"'")?; write!(f, "reg '\"'")?;
for i in selections.iter().scan(HashMap::new(), |state, s| { for i in selections.iter().scan(BTreeSet::new(), |state, s| {
let key = if options.no_skip_whitespace { let key = if options.no_skip_whitespace {
s s
} else { } else {
@ -31,18 +32,16 @@ pub fn uniq(options: &Options) -> Result<KakMessage, KakMessage> {
key.to_string() key.to_string()
}; };
let ret = match state.entry(key) { let mut hasher = DefaultHasher::new();
Entry::Vacant(e) => { key.hash(&mut hasher);
e.insert(());
s
}
Entry::Occupied(_) => {
// We've seen this selection before, so empty it
""
}
};
Some(ret) Some(if state.insert(hasher.finish()) {
// True if this is a new line
s
} else {
// Nothing was inserted because we already saw this line
""
})
}) { }) {
let new_selection = i.replace('\'', "''"); let new_selection = i.replace('\'', "''");
write!(f, " '{}'", new_selection)?; write!(f, " '{}'", new_selection)?;