Skip to content

Commit

Permalink
Remove custom hash ahash
Browse files Browse the repository at this point in the history
  • Loading branch information
gulshan committed Nov 23, 2023
1 parent f172ba1 commit 2083ce8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ bench = []
codegen-units = 1

[dependencies]
ahash = { version = "0.7", default-features = false }
emojicon = "0.3"
serde_json = "1.0"
regex = "1.5"
Expand Down
7 changes: 3 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ahash::RandomState;
use std::collections::HashMap;
use std::fs::read;
use emojicon::{Emojicon, BengaliEmoji};
Expand All @@ -7,9 +6,9 @@ use crate::config::Config;

/// Data which is shared between the methods.
pub(crate) struct Data {
table: HashMap<String, Vec<String>, RandomState>,
suffix: HashMap<String, String, RandomState>,
autocorrect: HashMap<String, String, RandomState>,
table: HashMap<String, Vec<String>>,
suffix: HashMap<String, String>,
autocorrect: HashMap<String, String>,
emojicon: Emojicon,
bengali_emoji: BengaliEmoji,
}
Expand Down
11 changes: 5 additions & 6 deletions src/phonetic/method.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Phonetic Method
use ahash::RandomState;
use std::collections::HashMap;
use std::fs::{write, File};
use std::time::SystemTime;
Expand All @@ -16,7 +15,7 @@ pub(crate) struct PhoneticMethod {
buffer: String,
suggestion: PhoneticSuggestion,
// Candidate selections.
selections: HashMap<String, String, RandomState>,
selections: HashMap<String, String>,
// Last modification of the user's auto correct file.
modified: SystemTime,
// Previously selected candidate index of the current suggestion list.
Expand All @@ -31,7 +30,7 @@ impl PhoneticMethod {
{
serde_json::from_slice(&file).unwrap()
} else {
HashMap::with_hasher(RandomState::new())
HashMap::new()
};

// Load user's auto correct file.
Expand All @@ -43,7 +42,7 @@ impl PhoneticMethod {
} else {
(
SystemTime::UNIX_EPOCH,
HashMap::with_hasher(RandomState::new()),
HashMap::new(),
)
}
};
Expand Down Expand Up @@ -154,8 +153,8 @@ impl Default for PhoneticMethod {
fn default() -> Self {
PhoneticMethod {
buffer: String::new(),
suggestion: PhoneticSuggestion::new(HashMap::with_hasher(RandomState::new())),
selections: HashMap::with_hasher(RandomState::new()),
suggestion: PhoneticSuggestion::new(HashMap::new()),
selections: HashMap::new(),
modified: SystemTime::UNIX_EPOCH,
prev_selection: 0,
}
Expand Down
36 changes: 17 additions & 19 deletions src/phonetic/suggestion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Suggestion making module.

use ahash::RandomState;
use okkhor::parser::Parser;
use regex::Regex;
use std::collections::HashMap;
Expand All @@ -20,15 +19,15 @@ pub(crate) struct PhoneticSuggestion {
// for regex conversion every time.
regex: String,
// Cache for storing dictionary searches.
cache: HashMap<String, Vec<Rank>, RandomState>,
cache: HashMap<String, Vec<Rank>>,
phonetic: Parser,
table: HashMap<&'static str, &'static [&'static str], RandomState>,
table: HashMap<&'static str, &'static [&'static str]>,
// The user's auto-correct entries.
pub(crate) user_autocorrect: HashMap<String, String, RandomState>,
pub(crate) user_autocorrect: HashMap<String, String>,
}

impl PhoneticSuggestion {
pub(crate) fn new(user_autocorrect: HashMap<String, String, RandomState>) -> Self {
pub(crate) fn new(user_autocorrect: HashMap<String, String>) -> Self {
let table: Vec<(&str, &[&str])> = vec![
("a", &["a", "aa", "e", "oi", "o", "nya", "y"]),
("b", &["b", "bh"]),
Expand Down Expand Up @@ -63,7 +62,7 @@ impl PhoneticSuggestion {
suggestions: Vec::with_capacity(10),
pbuffer: String::with_capacity(60),
regex: String::with_capacity(1024),
cache: HashMap::with_capacity_and_hasher(20, RandomState::new()),
cache: HashMap::with_capacity(20),
phonetic: Parser::new_phonetic(),
table,
user_autocorrect,
Expand Down Expand Up @@ -143,7 +142,7 @@ impl PhoneticSuggestion {
&mut self,
term: &str,
data: &Data,
selections: &mut HashMap<String, String, RandomState>,
selections: &mut HashMap<String, String>,
config: &Config,
) -> (Vec<Rank>, usize) {
let mut string = SplittedString::split(term, false);
Expand Down Expand Up @@ -256,7 +255,7 @@ impl PhoneticSuggestion {
&self,
string: &SplittedString,
data: &Data,
selections: &mut HashMap<String, String, RandomState>,
selections: &mut HashMap<String, String>,
) -> usize {
let len = string.word().len();
let mut selected = String::with_capacity(len * 3);
Expand Down Expand Up @@ -349,13 +348,12 @@ impl PhoneticSuggestion {
// Implement Default trait on PhoneticSuggestion, actually for testing convenience.
impl Default for PhoneticSuggestion {
fn default() -> Self {
PhoneticSuggestion::new(HashMap::with_hasher(RandomState::new()))
PhoneticSuggestion::new(HashMap::new())
}
}

#[cfg(test)]
mod tests {
use ahash::RandomState;
use std::collections::HashMap;

use super::PhoneticSuggestion;
Expand All @@ -367,7 +365,7 @@ mod tests {
#[test]
fn test_suggestion_with_english() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let mut config = get_phonetic_method_defaults();
let data = Data::new(&config);
config.set_suggestion_include_english(true);
Expand All @@ -391,7 +389,7 @@ mod tests {
#[test]
fn test_suggestion_ansi() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let mut config = get_phonetic_method_defaults();
let data = Data::new(&config);
config.set_suggestion_include_english(true);
Expand All @@ -413,7 +411,7 @@ mod tests {
#[test]
fn test_suggestion_smart_quotes() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let mut config = get_phonetic_method_defaults();
let data = Data::new(&config);
config.set_suggestion_include_english(true);
Expand All @@ -439,7 +437,7 @@ mod tests {
#[test]
fn test_emoticon_and_emoji() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let config = get_phonetic_method_defaults();
let data = Data::new(&config);

Expand All @@ -465,7 +463,7 @@ mod tests {
#[test]
fn test_suggestion() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let config = get_phonetic_method_defaults();
let data = Data::new(&config);

Expand All @@ -491,7 +489,7 @@ mod tests {
#[test]
fn test_suffix_suggestion() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let config = get_phonetic_method_defaults();
let data = Data::new(&config);

Expand Down Expand Up @@ -572,7 +570,7 @@ mod tests {

#[test]
fn test_suffix() {
let mut cache = HashMap::with_hasher(RandomState::new());
let mut cache = HashMap::new();
let config = get_phonetic_method_defaults();
let data = Data::new(&config);

Expand Down Expand Up @@ -627,7 +625,7 @@ mod tests {
#[test]
fn test_prev_selected() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let config = get_phonetic_method_defaults();
let data = Data::new(&config);

Expand Down Expand Up @@ -697,7 +695,7 @@ mod tests {
#[test]
fn test_suggest_special_chars_selections() {
let mut suggestion = PhoneticSuggestion::default();
let mut selections = HashMap::with_hasher(RandomState::new());
let mut selections = HashMap::new();
let config = get_phonetic_method_defaults();
let data = Data::new(&config);
selections.insert("sesh".to_string(), "শেষ".to_string());
Expand Down

0 comments on commit 2083ce8

Please sign in to comment.