Skip to content

Commit aa2e8aa

Browse files
committed
optimization
1 parent 9c0beaa commit aa2e8aa

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn read_lines_of_file(
2626
Ok(reader.lines().map_while(Result::ok))
2727
}
2828

29-
#[derive(Clone, Eq, PartialEq, Debug)]
29+
#[derive(Clone, Debug)]
3030
struct WordsData {
3131
pub path: String,
3232
pub lang: String,

src/solver/infallible_char_collection.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ pub trait InfallibleCharCollection {
77

88
#[must_use]
99
#[inline]
10+
fn starts_with(&self, prefix: &str) -> bool {
11+
let mut own_chars = self.iter_chars();
12+
for ch in prefix.iter_chars() {
13+
if own_chars.next() != Some(ch) {
14+
return false;
15+
}
16+
}
17+
18+
true
19+
}
20+
21+
#[must_use]
22+
#[inline]
23+
#[allow(dead_code)]
1024
fn first_char(&self) -> Option<char> {
1125
self.iter_chars().next()
1226
}
@@ -42,6 +56,11 @@ impl InfallibleCharCollection for String {
4256
fn iter_chars(&self) -> impl Iterator<Item = char> + '_ {
4357
self.chars()
4458
}
59+
60+
#[inline]
61+
fn starts_with(&self, prefix: &str) -> bool {
62+
str::starts_with::<&str>(self, prefix)
63+
}
4564
}
4665

4766
impl InfallibleCharCollection for str {
@@ -54,6 +73,12 @@ impl InfallibleCharCollection for str {
5473
fn iter_chars(&self) -> impl Iterator<Item = char> + '_ {
5574
self.chars()
5675
}
76+
77+
78+
#[inline]
79+
fn starts_with(&self, prefix: &str) -> bool {
80+
Self::starts_with::<&Self>(self, prefix)
81+
}
5782
}
5883

5984
impl<const L: usize> InfallibleCharCollection for [char; L] {

src/solver/pattern.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use js_sys::JsString;
2121
pub struct Pattern {
2222
invalid_letters: Vec<char>,
2323
pattern: Vec<char>,
24-
first_letter: char,
24+
prefix: Box<str>,
2525
/// true for normal hangman mode
2626
letters_in_pattern_have_no_other_occurrences: bool,
2727
known_letters_count: usize,
@@ -87,7 +87,10 @@ impl Pattern {
8787
}
8888
}
8989

90-
let first_letter = *pattern_as_chars.first().unwrap_or(&char::WILDCARD);
90+
let prefix = pattern_as_chars
91+
.iter()
92+
.take_while(|ch| !ch.is_wildcard())
93+
.collect::<Box<str>>();
9194

9295
let mut invalid_ascii_letters = [false; 128];
9396
let mut invalid_letters_all_ascii: bool = true;
@@ -107,7 +110,7 @@ impl Pattern {
107110
Ok(Self {
108111
invalid_letters: invalid_letters_vec,
109112
pattern: pattern_as_chars,
110-
first_letter,
113+
prefix,
111114
letters_in_pattern_have_no_other_occurrences,
112115
known_letters_count,
113116
invalid_ascii_letters,
@@ -117,23 +120,19 @@ impl Pattern {
117120

118121
#[inline]
119122
#[must_use]
120-
fn first_letter_is_wildcard(&self) -> bool {
121-
debug_assert_eq!(
122-
self.first_letter.is_wildcard(),
123-
self.first_letter.is_normalised_wildcard()
124-
);
125-
self.first_letter.is_normalised_wildcard()
123+
const fn first_letter_is_wildcard(&self) -> bool {
124+
self.prefix.is_empty()
126125
}
127126

128127
#[must_use]
129128
#[inline]
130-
fn first_letter_matches<CC: InfallibleCharCollection + ?Sized>(
129+
fn prefix_matches<CC: InfallibleCharCollection + ?Sized>(
131130
&self,
132131
word: &&CC,
133132
) -> bool {
134133
// This only makes sense if first_letter_is_wildcard is false
135134
debug_assert!(!self.first_letter_is_wildcard());
136-
word.first_char() == Some(self.first_letter)
135+
word.starts_with(&self.prefix)
137136
}
138137

139138
#[inline]
@@ -309,8 +308,8 @@ impl Pattern {
309308
)
310309
} else {
311310
let mut filtered_words = all_words
312-
.skip_while(|word| !self.first_letter_matches(word))
313-
.take_while(|word| self.first_letter_matches(word))
311+
.skip_while(|word| !self.prefix_matches(word))
312+
.take_while(|word| self.prefix_matches(word))
314313
.filter(|word| self.matches(word));
315314
self._collect_count_and_create_letter_frequency(
316315
&mut filtered_words,

0 commit comments

Comments
 (0)