Skip to content

Commit 685187a

Browse files
authored
Add condition get_hot_bits (#3)
1 parent e178c88 commit 685187a

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "regexsolver"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2021"
55
authors = ["Alexandre van Beurden"]
66
repository = "https://github.com/RegexSolver/regexsolver"

src/fast_automaton/condition/fast_bit_vec/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ pub struct FastBitVec {
66

77
impl std::fmt::Display for FastBitVec {
88
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
9-
for a in &self.bits {
10-
write!(f, "{:b}", a)?;
9+
for i in 0..self.n {
10+
let bit = if self.get(i).unwrap() { 1 } else { 0 };
11+
write!(f, "{}", bit)?;
1112
}
1213
Ok(())
1314
}
@@ -121,4 +122,12 @@ impl FastBitVec {
121122
fn mask_for_bits(bits: usize) -> u64 {
122123
(!0) >> ((64 - bits % 64) % 64)
123124
}
125+
126+
pub fn get_hot_bits(&self) -> Vec<bool> {
127+
let mut hot_bits = Vec::with_capacity(self.n);
128+
for i in 0..self.n {
129+
hot_bits.push(self.get(i).unwrap());
130+
}
131+
hot_bits
132+
}
124133
}

src/fast_automaton/condition/mod.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ impl Condition {
154154
pub fn get_cardinality(&self, spanning_set: &SpanningSet) -> Result<u32, EngineError> {
155155
Ok(self.to_range(spanning_set)?.get_cardinality())
156156
}
157+
158+
pub fn get_hot_bits(&self) -> Vec<bool> {
159+
self.0.get_hot_bits()
160+
}
157161
}
158162

159163
#[cfg(test)]
@@ -190,10 +194,19 @@ mod tests {
190194
fn test_empty_total() -> Result<(), String> {
191195
let spanning_set = get_spanning_set();
192196
let empty = Condition::empty(&spanning_set);
197+
//println!("{empty}");
193198
assert!(empty.is_empty());
199+
assert_eq!(
200+
vec![false, false, false, false],
201+
empty.get_hot_bits()
202+
);
194203
let total = Condition::total(&spanning_set);
195-
println!("{total}");
204+
//println!("{total}");
196205
assert!(total.is_total());
206+
assert_eq!(
207+
vec![true, true, true, true],
208+
total.get_hot_bits()
209+
);
197210

198211
assert_eq!(Range::empty(), empty.to_range(&spanning_set).unwrap());
199212
assert_eq!(Range::total(), total.to_range(&spanning_set).unwrap());
@@ -221,10 +234,19 @@ mod tests {
221234
empty,
222235
Condition::from_range(&Range::empty(), &spanning_set).unwrap()
223236
);
237+
assert_eq!(
238+
vec![false],
239+
empty.get_hot_bits()
240+
);
241+
224242
assert_eq!(
225243
total,
226244
Condition::from_range(&Range::total(), &spanning_set).unwrap()
227245
);
246+
assert_eq!(
247+
vec![true],
248+
total.get_hot_bits()
249+
);
228250

229251
assert_eq!(empty, total.complement());
230252
assert_eq!(total, empty.complement());

src/fast_automaton/convert/to_regex/mod.rs

+30-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{
44
};
55

66
use ahash::{HashMapExt, HashSetExt};
7-
use log::error;
87
use nohash_hasher::IntMap;
98

109
use crate::{error::EngineError, execution_profile::ThreadLocalParams, regex::RegularExpression};
@@ -53,6 +52,7 @@ impl Display for StateEliminationAutomaton<Range> {
5352

5453
impl StateEliminationAutomaton<Range> {
5554
//#[cfg(test)]
55+
#[allow(dead_code)]
5656
#[inline]
5757
pub fn to_dot(&self) {
5858
println!("{}", self);
@@ -258,28 +258,28 @@ impl FastAutomaton {
258258
Ok(automaton) => match self.is_equivalent_of(&automaton) {
259259
Ok(result) => {
260260
if !result {
261-
println!(
261+
/*println!(
262262
"The automaton is not equivalent to the generated regex; automaton={} regex={}",
263263
serde_json::to_string(self).unwrap(),
264264
regex
265-
);
265+
);*/
266266
None
267267
} else {
268268
Some(regex)
269269
}
270270
}
271-
Err(err) => {
272-
println!("{err}");
271+
Err(_) => {
272+
//println!("{err}");
273273
None
274274
}
275275
},
276276
Err(err) => {
277277
if let crate::error::EngineError::RegexSyntaxError(_) = err {
278-
error!(
278+
/*error!(
279279
"The generated regex can not be converted to automaton to be checked for equivalence (Syntax Error); automaton={} regex={}",
280280
serde_json::to_string(self).unwrap(),
281281
regex
282-
);
282+
);*/
283283
}
284284
None
285285
}
@@ -421,4 +421,27 @@ mod tests {
421421

422422
Ok(())
423423
}
424+
425+
/*#[test]
426+
fn test_convert_after_operation_4() -> Result<(), String> {
427+
let automaton1 = RegularExpression::new(".*abc.*")
428+
.unwrap()
429+
.to_automaton()
430+
.unwrap();
431+
let automaton2 = RegularExpression::new(".*def.*")
432+
.unwrap()
433+
.to_automaton()
434+
.unwrap()
435+
.determinize()
436+
.unwrap();
437+
438+
let result = automaton1.subtraction(&automaton2).unwrap();
439+
result.to_dot();
440+
441+
let result = result.to_regex().unwrap();
442+
443+
assert_eq!("(x{3})*x{1,2}", result.to_string());
444+
445+
Ok(())
446+
}*/
424447
}

src/fast_automaton/convert/to_regex/transform.rs

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl StateEliminationAutomaton<Range> {
7777
if self.get_number_of_states() < 2 {
7878
return Ok(None);
7979
}
80+
//self.to_dot();
8081
let mut dot_value =
8182
if let Some(dot_value) = self.get_transition(self.start_state, self.start_state) {
8283
if let Some(dot_value) = dot_value.get_weight() {

0 commit comments

Comments
 (0)