Changeset - 6f7aa78a47f7
[Not reviewed]
default
0 1 0
Laman - 10 months ago 2024-06-11 22:33:48

added a proper START constant
1 file changed with 6 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/main.rs
Show inline comments
 
use std::collections::{HashMap, HashSet};
 

	
 
const START: usize = usize::MAX;
 

	
 
trait Token {
 
	fn is_skippable(&self) -> bool {false}
 
	fn list_first(&self) -> Vec<usize>;
 
	fn list_last(&self) -> Vec<usize>;
 
	fn list_neighbours(&self) -> Vec<(usize, usize)>;
 
}
 

	
 
struct Symbol {
 
	position: usize,
 
	value: char
 
}
 

	
 
@@ -121,26 +123,26 @@ fn parse(pattern: &String, offset: usize
 
	let mut res: Vec<Box<dyn Token>> = Vec::new();
 
	let mut i = 0;
 
	while i < pattern.len() {
 
		let c = chars[i];
 
		match c {
 
			'(' => {
 
				let j = find_closing_parenthesis(&pattern[i..].to_string()).unwrap() + i;
 
				let inner_content = parse(&pattern[i+1..j].to_string(), offset+i+1);
 
				res.push(Box::new(inner_content));
 
				i = j+1;
 
			}
 
			'*' => {
 
				let segment = res.pop().unwrap();
 
				res.push(Box::new(Asterisk{content: segment}));
 
				let token = res.pop().unwrap();
 
				res.push(Box::new(Asterisk{content: token}));
 
				i += 1;
 
			}
 
			c => {
 
				res.push(Box::new(Symbol{position: i+offset, value: c}));
 
				i += 1;
 
			}
 
		}
 
	}
 

	
 
	return Chain{content: res};
 
}
 

	
 
@@ -148,47 +150,47 @@ struct Regexp {
 
	rules: HashMap<(usize, char), HashSet<usize>>,
 
	end_states: HashSet<usize>
 
}
 

	
 
impl Regexp {
 
	fn new(pattern: &String) -> Regexp {
 
		let r = parse(pattern, 0);
 
		let pattern_chars = Vec::from_iter(pattern.chars());
 
		let mut rules: HashMap<(usize, char), HashSet<usize>> = HashMap::new();
 
		
 
		for i in r.list_first() {
 
			let c = pattern_chars[i];
 
			let key = (99, c);
 
			let key = (START, c);
 
			match rules.get_mut(&key) {
 
				Some(set) => {set.insert(i);},
 
				None => {rules.insert(key, HashSet::from([i]));}
 
			};
 
		}
 

	
 
		for (i, j) in r.list_neighbours() {
 
			let c = pattern_chars[j];
 
			let key = (i, c);
 
			match rules.get_mut(&key) {
 
				Some(set) => {set.insert(j);},
 
				None => {rules.insert(key, HashSet::from([j]));}
 
			};
 
		}
 

	
 
		let end_states = HashSet::from_iter(r.list_last().into_iter());
 

	
 
		return Regexp{rules, end_states};
 
	}
 

	
 
	pub fn eval(&self, s: String) -> bool {
 
		let mut multistate = HashSet::from([99]);
 
		let mut multistate = HashSet::from([START]);
 

	
 
		for c in s.chars() {
 
			let mut new_multistate = HashSet::new();
 

	
 
			for state in multistate {
 
				if let Some(x) = self.rules.get(&(state, c)) {
 
					new_multistate = new_multistate.union(&x).map(|&y| y).collect();
 
				} else if let Some(x) = self.rules.get(&(state, '.')) {
 
					new_multistate = new_multistate.union(&x).map(|&y| y).collect();
 
				}
 
			}
 
			multistate = new_multistate;
0 comments (0 inline, 0 general)