Implemented squeeze operation

Signed-off-by: Hanif Bin Ariffin <hanif.ariffin.4326@gmail.com>
This commit is contained in:
Hanif Bin Ariffin 2021-07-18 13:34:30 +08:00
parent 50167a33a8
commit c4e04c5384
2 changed files with 85 additions and 7 deletions

View file

@ -304,7 +304,18 @@ impl SymbolTranslatorNew for DeleteOperationNew {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum TranslateOperationNew { pub enum TranslateOperationNew {
Standard(HashMap<char, char>), Standard(HashMap<char, char>),
Complement(u32, Vec<char>, Vec<char>, char, HashMap<char, char>), Complement(
// iter
u32,
// set 1
Vec<char>,
// set 2
Vec<char>,
// fallback
char,
// translation map
HashMap<char, char>,
),
} }
impl TranslateOperationNew { impl TranslateOperationNew {
@ -388,6 +399,70 @@ impl SymbolTranslatorNew for TranslateOperationNew {
} }
} }
#[derive(Debug, Clone)]
pub struct SqueezeOperationNew {
squeeze_set: Vec<char>,
complement: bool,
previous: Option<char>,
}
impl SqueezeOperationNew {
pub fn new(squeeze_set: Vec<Sequence>, complement: bool) -> SqueezeOperationNew {
SqueezeOperationNew {
squeeze_set: squeeze_set
.into_iter()
.flat_map(Sequence::dissolve)
.collect(),
complement,
previous: None,
}
}
}
impl SymbolTranslatorNew for SqueezeOperationNew {
fn translate(&mut self, current: char) -> Option<char> {
if self.complement {
if self.squeeze_set.iter().any(|c| c.eq(&current)) {
Some(current)
} else {
match self.previous {
Some(v) => {
if v.eq(&current) {
None
} else {
self.previous = Some(current);
Some(current)
}
}
None => {
self.previous = Some(current);
Some(current)
}
}
}
} else {
if self.squeeze_set.iter().any(|c| c.eq(&current)) {
match self.previous {
Some(v) => {
if v.eq(&current) {
None
} else {
self.previous = Some(current);
Some(current)
}
}
None => {
self.previous = Some(current);
Some(current)
}
}
} else {
Some(current)
}
}
}
}
pub fn translate_input_new<T>(input: &mut dyn BufRead, output: &mut dyn Write, mut translator: T) pub fn translate_input_new<T>(input: &mut dyn BufRead, output: &mut dyn Write, mut translator: T)
where where
T: SymbolTranslatorNew, T: SymbolTranslatorNew,

View file

@ -20,7 +20,7 @@ mod operation;
use bit_set::BitSet; use bit_set::BitSet;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use fnv::FnvHashMap; use fnv::FnvHashMap;
use operation::{translate_input_new, Sequence, TranslateOperationNew}; use operation::{translate_input_new, Sequence, SqueezeOperationNew, TranslateOperationNew};
use std::io::{stdin, stdout, BufRead, BufWriter, Write}; use std::io::{stdin, stdout, BufRead, BufWriter, Write};
use crate::{expand::ExpandSet, operation::DeleteOperationNew}; use crate::{expand::ExpandSet, operation::DeleteOperationNew};
@ -278,11 +278,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let locked_stdout = stdout.lock(); let locked_stdout = stdout.lock();
let mut buffered_stdout = BufWriter::new(locked_stdout); let mut buffered_stdout = BufWriter::new(locked_stdout);
let set1 = ExpandSet::new(sets[0].as_ref());
if delete_flag { if delete_flag {
if squeeze_flag { if squeeze_flag {
let set2 = ExpandSet::new(sets[1].as_ref()); let op = DeleteAndSqueezeOperation::new(
let op = DeleteAndSqueezeOperation::new(set1, set2, complement_flag); ExpandSet::new(sets[0].as_ref()),
ExpandSet::new(sets[1].as_ref()),
complement_flag,
);
translate_input(&mut locked_stdin, &mut buffered_stdout, op); translate_input(&mut locked_stdin, &mut buffered_stdout, op);
} else { } else {
let op = DeleteOperationNew::new(Sequence::parse_set_string(&sets[0]), complement_flag); let op = DeleteOperationNew::new(Sequence::parse_set_string(&sets[0]), complement_flag);
@ -290,8 +292,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
} }
} else if squeeze_flag { } else if squeeze_flag {
if sets.len() < 2 { if sets.len() < 2 {
let op = SqueezeOperation::new(set1, complement_flag); let op =
translate_input(&mut locked_stdin, &mut buffered_stdout, op); SqueezeOperationNew::new(Sequence::parse_set_string(&sets[0]), complement_flag);
translate_input_new(&mut locked_stdin, &mut buffered_stdout, op);
} else { } else {
let op = TranslateAndSqueezeOperation::new(sets, truncate_set1_flag, complement_flag); let op = TranslateAndSqueezeOperation::new(sets, truncate_set1_flag, complement_flag);
translate_input(&mut locked_stdin, &mut buffered_stdout, op); translate_input(&mut locked_stdin, &mut buffered_stdout, op);