use super::functions::*;
use crate::{Expr, KernelError, Name};
use std::collections::HashMap;
#[allow(dead_code)]
pub struct VersionedRecord<T: Clone> {
history: Vec<T>,
}
#[allow(dead_code)]
impl<T: Clone> VersionedRecord<T> {
pub fn new(initial: T) -> Self {
Self {
history: vec![initial],
}
}
pub fn update(&mut self, val: T) {
self.history.push(val);
}
pub fn current(&self) -> &T {
self.history
.last()
.expect("VersionedRecord history is always non-empty after construction")
}
pub fn at_version(&self, n: usize) -> Option<&T> {
self.history.get(n)
}
pub fn version(&self) -> usize {
self.history.len() - 1
}
pub fn has_history(&self) -> bool {
self.history.len() > 1
}
}
#[allow(dead_code)]
pub struct PathBuf {
components: Vec<String>,
}
#[allow(dead_code)]
impl PathBuf {
pub fn new() -> Self {
Self {
components: Vec::new(),
}
}
pub fn push(&mut self, comp: impl Into<String>) {
self.components.push(comp.into());
}
pub fn pop(&mut self) {
self.components.pop();
}
pub fn as_str(&self) -> String {
self.components.join("/")
}
pub fn depth(&self) -> usize {
self.components.len()
}
pub fn clear(&mut self) {
self.components.clear();
}
}
#[allow(dead_code)]
pub struct StatSummary {
count: u64,
sum: f64,
min: f64,
max: f64,
}
#[allow(dead_code)]
impl StatSummary {
pub fn new() -> Self {
Self {
count: 0,
sum: 0.0,
min: f64::INFINITY,
max: f64::NEG_INFINITY,
}
}
pub fn record(&mut self, val: f64) {
self.count += 1;
self.sum += val;
if val < self.min {
self.min = val;
}
if val > self.max {
self.max = val;
}
}
pub fn mean(&self) -> Option<f64> {
if self.count == 0 {
None
} else {
Some(self.sum / self.count as f64)
}
}
pub fn min(&self) -> Option<f64> {
if self.count == 0 {
None
} else {
Some(self.min)
}
}
pub fn max(&self) -> Option<f64> {
if self.count == 0 {
None
} else {
Some(self.max)
}
}
pub fn count(&self) -> u64 {
self.count
}
}
#[allow(dead_code)]
pub struct NonEmptyVec<T> {
head: T,
tail: Vec<T>,
}
#[allow(dead_code)]
impl<T> NonEmptyVec<T> {
pub fn singleton(val: T) -> Self {
Self {
head: val,
tail: Vec::new(),
}
}
pub fn push(&mut self, val: T) {
self.tail.push(val);
}
pub fn first(&self) -> &T {
&self.head
}
pub fn last(&self) -> &T {
self.tail.last().unwrap_or(&self.head)
}
pub fn len(&self) -> usize {
1 + self.tail.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn to_vec(&self) -> Vec<&T> {
let mut v = vec![&self.head];
v.extend(self.tail.iter());
v
}
}
#[derive(Debug, Clone)]
pub struct QuotientKernel {
quot_types: Vec<QuotientType>,
}
impl QuotientKernel {
pub fn new() -> Self {
Self {
quot_types: Vec::new(),
}
}
pub fn register(&mut self, qt: QuotientType) {
self.quot_types.push(qt);
}
pub fn find_by_base(&self, base: &Expr) -> Option<&QuotientType> {
self.quot_types.iter().find(|qt| &qt.base_type == base)
}
pub fn is_quot_type(&self, expr: &Expr) -> bool {
if is_quot_type_expr(expr) {
return true;
}
self.quot_types.iter().any(|qt| &qt.quot_type == expr)
}
pub fn reduce(&self, head: &Expr, args: &[Expr]) -> Option<Expr> {
try_reduce_quot(head, args)
}
pub fn count(&self) -> usize {
self.quot_types.len()
}
}
#[allow(dead_code)]
pub struct QuotientNormalizer {
max_steps: usize,
steps_taken: usize,
}
impl QuotientNormalizer {
#[allow(dead_code)]
pub fn new(max_steps: usize) -> Self {
Self {
max_steps,
steps_taken: 0,
}
}
#[allow(dead_code)]
pub fn normalize(&mut self, expr: Expr) -> (Expr, bool) {
let mut current = expr;
let mut changed = false;
while self.steps_taken < self.max_steps {
let (next, did_change) = self.step(¤t);
if !did_change {
break;
}
current = next;
changed = true;
self.steps_taken += 1;
}
(current, changed)
}
fn step(&self, expr: &Expr) -> (Expr, bool) {
match expr {
Expr::App(f, _) => {
let args = collect_args_norm(expr);
if let Expr::Const(name, _) = &args[0] {
if *name == Name::str("Quot.lift") && args.len() >= 4 {
if let Some(r) = reduce_quot_lift(&args[1..]) {
return (r, true);
}
}
if *name == Name::str("Quot.ind") && args.len() >= 3 {
if let Some(r) = reduce_quot_ind(&args[1..]) {
return (r, true);
}
}
}
let (f2, cf) = self.step(f);
let arg = args.last().cloned().unwrap_or(Expr::BVar(0));
let (a2, ca) = self.step(&arg);
if cf || ca {
return (Expr::App(Box::new(f2), Box::new(a2)), true);
}
(expr.clone(), false)
}
_ => (expr.clone(), false),
}
}
#[allow(dead_code)]
pub fn steps_taken(&self) -> usize {
self.steps_taken
}
#[allow(dead_code)]
pub fn reset(&mut self) {
self.steps_taken = 0;
}
}
#[allow(dead_code)]
pub struct StringPool {
free: Vec<String>,
}
#[allow(dead_code)]
impl StringPool {
pub fn new() -> Self {
Self { free: Vec::new() }
}
pub fn take(&mut self) -> String {
self.free.pop().unwrap_or_default()
}
pub fn give(&mut self, mut s: String) {
s.clear();
self.free.push(s);
}
pub fn free_count(&self) -> usize {
self.free.len()
}
}
pub struct QuotientDescription {
pub base_name: String,
pub relation_name: String,
pub finite_model_size: Option<usize>,
}
impl QuotientDescription {
pub fn new(base_name: impl Into<String>, relation_name: impl Into<String>) -> Self {
Self {
base_name: base_name.into(),
relation_name: relation_name.into(),
finite_model_size: None,
}
}
pub fn with_model_size(mut self, n: usize) -> Self {
self.finite_model_size = Some(n);
self
}
pub fn display(&self) -> String {
match self.finite_model_size {
Some(n) => {
format!(
"Quot {} {} ({} elements)",
self.base_name, self.relation_name, n
)
}
None => format!("Quot {} {}", self.base_name, self.relation_name),
}
}
}
#[allow(dead_code)]
pub struct TransformStat {
before: StatSummary,
after: StatSummary,
}
#[allow(dead_code)]
impl TransformStat {
pub fn new() -> Self {
Self {
before: StatSummary::new(),
after: StatSummary::new(),
}
}
pub fn record_before(&mut self, v: f64) {
self.before.record(v);
}
pub fn record_after(&mut self, v: f64) {
self.after.record(v);
}
pub fn mean_ratio(&self) -> Option<f64> {
let b = self.before.mean()?;
let a = self.after.mean()?;
if b.abs() < f64::EPSILON {
return None;
}
Some(a / b)
}
}
#[allow(dead_code)]
pub struct LabelSet {
labels: Vec<String>,
}
#[allow(dead_code)]
impl LabelSet {
pub fn new() -> Self {
Self { labels: Vec::new() }
}
pub fn add(&mut self, label: impl Into<String>) {
let s = label.into();
if !self.labels.contains(&s) {
self.labels.push(s);
}
}
pub fn has(&self, label: &str) -> bool {
self.labels.iter().any(|l| l == label)
}
pub fn count(&self) -> usize {
self.labels.len()
}
pub fn all(&self) -> &[String] {
&self.labels
}
}
pub struct QuotientReducer {
steps: Vec<QuotReductionStep>,
#[allow(dead_code)]
cache: QuotLiftCache,
pub(crate) max_steps: usize,
}
impl QuotientReducer {
pub fn new(max_steps: usize) -> Self {
Self {
steps: Vec::new(),
cache: QuotLiftCache::new(),
max_steps,
}
}
pub fn reduce(&mut self, expr: &Expr) -> (Expr, bool) {
match expr {
Expr::App(f, _arg) => {
if let Expr::Const(name, _) = f.as_ref() {
if *name == Name::str("Quot.lift") {
let args = collect_args(expr);
if let Some((reduced, kind)) = try_reduce_quot_full(&args[0], &args[1..]) {
let step = QuotReductionStep::new(kind, expr.clone(), reduced.clone());
if self.steps.len() < self.max_steps {
self.steps.push(step);
}
return (reduced, true);
}
}
}
(expr.clone(), false)
}
_ => (expr.clone(), false),
}
}
pub fn steps(&self) -> &[QuotReductionStep] {
&self.steps
}
pub fn clear_steps(&mut self) {
self.steps.clear();
}
pub fn step_count(&self) -> usize {
self.steps.len()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct QuotientType {
pub base_type: Expr,
pub relation: Expr,
pub quot_type: Expr,
}
impl QuotientType {
pub fn new(base_type: Expr, relation: Expr) -> Self {
let quot_type = Expr::App(
Box::new(Expr::App(
Box::new(Expr::Const(Name::str("Quot"), vec![])),
Box::new(base_type.clone()),
)),
Box::new(relation.clone()),
);
Self {
base_type,
relation,
quot_type,
}
}
pub fn mk_const(&self) -> Expr {
Expr::Const(Name::str("Quot.mk"), vec![])
}
pub fn mk_apply(&self, elem: Expr) -> Expr {
Expr::App(Box::new(self.mk_const()), Box::new(elem))
}
pub fn lift_const(&self) -> Expr {
Expr::Const(Name::str("Quot.lift"), vec![])
}
pub fn ind_const(&self) -> Expr {
Expr::Const(Name::str("Quot.ind"), vec![])
}
pub fn sound_const(&self) -> Expr {
Expr::Const(Name::str("Quot.sound"), vec![])
}
pub fn lift_apply(&self, f: Expr, h: Expr, q: Expr) -> Expr {
Expr::App(
Box::new(Expr::App(
Box::new(Expr::App(Box::new(self.lift_const()), Box::new(f))),
Box::new(h),
)),
Box::new(q),
)
}
}
#[derive(Clone, Debug, Default)]
pub struct QuotLiftCache {
cache: std::collections::HashMap<String, Expr>,
}
impl QuotLiftCache {
pub fn new() -> Self {
Self {
cache: std::collections::HashMap::new(),
}
}
pub fn get(&self, f: &Expr, a: &Expr) -> Option<&Expr> {
let key = format!("{:?}-{:?}", f, a);
self.cache.get(&key)
}
pub fn put(&mut self, f: &Expr, a: &Expr, result: Expr) {
let key = format!("{:?}-{:?}", f, a);
self.cache.insert(key, result);
}
pub fn clear(&mut self) {
self.cache.clear();
}
pub fn len(&self) -> usize {
self.cache.len()
}
pub fn is_empty(&self) -> bool {
self.cache.is_empty()
}
}
#[allow(dead_code)]
pub struct TokenBucket {
capacity: u64,
tokens: u64,
refill_per_ms: u64,
last_refill: std::time::Instant,
}
#[allow(dead_code)]
impl TokenBucket {
pub fn new(capacity: u64, refill_per_ms: u64) -> Self {
Self {
capacity,
tokens: capacity,
refill_per_ms,
last_refill: std::time::Instant::now(),
}
}
pub fn try_consume(&mut self, n: u64) -> bool {
self.refill();
if self.tokens >= n {
self.tokens -= n;
true
} else {
false
}
}
fn refill(&mut self) {
let now = std::time::Instant::now();
let elapsed_ms = now.duration_since(self.last_refill).as_millis() as u64;
if elapsed_ms > 0 {
let new_tokens = elapsed_ms * self.refill_per_ms;
self.tokens = (self.tokens + new_tokens).min(self.capacity);
self.last_refill = now;
}
}
pub fn available(&self) -> u64 {
self.tokens
}
pub fn capacity(&self) -> u64 {
self.capacity
}
}
#[derive(Clone, Debug, Default)]
pub struct EquivClassSystem {
reps: std::collections::HashMap<String, Expr>,
}
impl EquivClassSystem {
pub fn new() -> Self {
Self {
reps: std::collections::HashMap::new(),
}
}
pub fn insert(&mut self, expr: Expr) {
let key = format!("{:?}", expr);
self.reps.entry(key).or_insert(expr);
}
pub fn merge(&mut self, a: &Expr, b: &Expr) {
let key_b = format!("{:?}", b);
let rep_a = self.rep_of(a).unwrap_or_else(|| a.clone());
self.reps.insert(key_b, rep_a);
}
pub fn rep_of(&self, expr: &Expr) -> Option<Expr> {
let key = format!("{:?}", expr);
self.reps.get(&key).cloned()
}
pub fn same_class(&self, a: &Expr, b: &Expr) -> bool {
match (self.rep_of(a), self.rep_of(b)) {
(Some(ra), Some(rb)) => ra == rb,
_ => a == b,
}
}
pub fn len(&self) -> usize {
self.reps.len()
}
pub fn is_empty(&self) -> bool {
self.reps.is_empty()
}
}
#[allow(dead_code)]
pub struct WindowIterator<'a, T> {
pub(super) data: &'a [T],
pub(super) pos: usize,
pub(super) window: usize,
}
#[allow(dead_code)]
impl<'a, T> WindowIterator<'a, T> {
pub fn new(data: &'a [T], window: usize) -> Self {
Self {
data,
pos: 0,
window,
}
}
}
#[derive(Debug, Default)]
pub struct QuotientBuilder {
base_type: Option<Expr>,
relation: Option<Expr>,
}
impl QuotientBuilder {
pub fn new() -> Self {
Self {
base_type: None,
relation: None,
}
}
pub fn base(mut self, ty: Expr) -> Self {
self.base_type = Some(ty);
self
}
pub fn relation(mut self, rel: Expr) -> Self {
self.relation = Some(rel);
self
}
pub fn build(self) -> Option<QuotientType> {
Some(QuotientType::new(self.base_type?, self.relation?))
}
}
#[allow(dead_code)]
pub struct SlidingSum {
window: Vec<f64>,
capacity: usize,
pos: usize,
sum: f64,
count: usize,
}
#[allow(dead_code)]
impl SlidingSum {
pub fn new(capacity: usize) -> Self {
Self {
window: vec![0.0; capacity],
capacity,
pos: 0,
sum: 0.0,
count: 0,
}
}
pub fn push(&mut self, val: f64) {
let oldest = self.window[self.pos];
self.sum -= oldest;
self.sum += val;
self.window[self.pos] = val;
self.pos = (self.pos + 1) % self.capacity;
if self.count < self.capacity {
self.count += 1;
}
}
pub fn sum(&self) -> f64 {
self.sum
}
pub fn mean(&self) -> Option<f64> {
if self.count == 0 {
None
} else {
Some(self.sum / self.count as f64)
}
}
pub fn count(&self) -> usize {
self.count
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum QuotReductionKind {
Lift,
Ind,
Sound,
}
impl QuotReductionKind {
pub fn description(&self) -> &'static str {
match self {
QuotReductionKind::Lift => "Quot.lift reduction",
QuotReductionKind::Ind => "Quot.ind reduction",
QuotReductionKind::Sound => "Quot.sound",
}
}
}
#[allow(dead_code)]
pub struct ConfigNode {
key: String,
value: Option<String>,
children: Vec<ConfigNode>,
}
#[allow(dead_code)]
impl ConfigNode {
pub fn leaf(key: impl Into<String>, value: impl Into<String>) -> Self {
Self {
key: key.into(),
value: Some(value.into()),
children: Vec::new(),
}
}
pub fn section(key: impl Into<String>) -> Self {
Self {
key: key.into(),
value: None,
children: Vec::new(),
}
}
pub fn add_child(&mut self, child: ConfigNode) {
self.children.push(child);
}
pub fn key(&self) -> &str {
&self.key
}
pub fn value(&self) -> Option<&str> {
self.value.as_deref()
}
pub fn num_children(&self) -> usize {
self.children.len()
}
pub fn lookup(&self, path: &str) -> Option<&str> {
let mut parts = path.splitn(2, '.');
let head = parts.next()?;
let tail = parts.next();
if head != self.key {
return None;
}
match tail {
None => self.value.as_deref(),
Some(rest) => self.children.iter().find_map(|c| c.lookup_relative(rest)),
}
}
fn lookup_relative(&self, path: &str) -> Option<&str> {
let mut parts = path.splitn(2, '.');
let head = parts.next()?;
let tail = parts.next();
if head != self.key {
return None;
}
match tail {
None => self.value.as_deref(),
Some(rest) => self.children.iter().find_map(|c| c.lookup_relative(rest)),
}
}
}
#[allow(dead_code)]
pub struct FocusStack<T> {
items: Vec<T>,
}
#[allow(dead_code)]
impl<T> FocusStack<T> {
pub fn new() -> Self {
Self { items: Vec::new() }
}
pub fn focus(&mut self, item: T) {
self.items.push(item);
}
pub fn blur(&mut self) -> Option<T> {
self.items.pop()
}
pub fn current(&self) -> Option<&T> {
self.items.last()
}
pub fn depth(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[derive(Debug, Default, Clone)]
pub struct QuotStats {
pub mk_count: usize,
pub lift_count: usize,
pub ind_count: usize,
pub sound_count: usize,
}
impl QuotStats {
pub fn compute(expr: &Expr) -> Self {
let mut stats = Self::default();
Self::walk(expr, &mut stats);
stats
}
fn walk(expr: &Expr, stats: &mut Self) {
match expr {
Expr::Const(name, _) => {
if *name == Name::str("Quot.mk") {
stats.mk_count += 1;
} else if *name == Name::str("Quot.lift") {
stats.lift_count += 1;
} else if *name == Name::str("Quot.ind") {
stats.ind_count += 1;
} else if *name == Name::str("Quot.sound") {
stats.sound_count += 1;
}
}
Expr::App(f, a) => {
Self::walk(f, stats);
Self::walk(a, stats);
}
Expr::Lam(_, _, ty, body) | Expr::Pi(_, _, ty, body) => {
Self::walk(ty, stats);
Self::walk(body, stats);
}
Expr::Let(_, ty, val, body) => {
Self::walk(ty, stats);
Self::walk(val, stats);
Self::walk(body, stats);
}
_ => {}
}
}
pub fn total(&self) -> usize {
self.mk_count + self.lift_count + self.ind_count + self.sound_count
}
pub fn has_quot(&self) -> bool {
self.total() > 0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EquivProperty {
Reflexive,
Symmetric,
Transitive,
}
#[allow(dead_code)]
pub struct SimpleDag {
edges: Vec<Vec<usize>>,
}
#[allow(dead_code)]
impl SimpleDag {
pub fn new(n: usize) -> Self {
Self {
edges: vec![Vec::new(); n],
}
}
pub fn add_edge(&mut self, from: usize, to: usize) {
if from < self.edges.len() {
self.edges[from].push(to);
}
}
pub fn successors(&self, node: usize) -> &[usize] {
self.edges.get(node).map(|v| v.as_slice()).unwrap_or(&[])
}
pub fn can_reach(&self, from: usize, to: usize) -> bool {
let mut visited = vec![false; self.edges.len()];
self.dfs(from, to, &mut visited)
}
fn dfs(&self, cur: usize, target: usize, visited: &mut Vec<bool>) -> bool {
if cur == target {
return true;
}
if cur >= visited.len() || visited[cur] {
return false;
}
visited[cur] = true;
for &next in self.successors(cur) {
if self.dfs(next, target, visited) {
return true;
}
}
false
}
pub fn topological_sort(&self) -> Option<Vec<usize>> {
let n = self.edges.len();
let mut in_degree = vec![0usize; n];
for succs in &self.edges {
for &s in succs {
if s < n {
in_degree[s] += 1;
}
}
}
let mut queue: std::collections::VecDeque<usize> =
(0..n).filter(|&i| in_degree[i] == 0).collect();
let mut order = Vec::new();
while let Some(node) = queue.pop_front() {
order.push(node);
for &s in self.successors(node) {
if s < n {
in_degree[s] -= 1;
if in_degree[s] == 0 {
queue.push_back(s);
}
}
}
}
if order.len() == n {
Some(order)
} else {
None
}
}
pub fn num_nodes(&self) -> usize {
self.edges.len()
}
}
#[allow(dead_code)]
pub struct RawFnPtr {
ptr: usize,
arity: usize,
name: String,
}
#[allow(dead_code)]
impl RawFnPtr {
pub fn new(ptr: usize, arity: usize, name: impl Into<String>) -> Self {
Self {
ptr,
arity,
name: name.into(),
}
}
pub fn arity(&self) -> usize {
self.arity
}
pub fn name(&self) -> &str {
&self.name
}
pub fn raw(&self) -> usize {
self.ptr
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum QuotUsageKind {
Lift,
Ind,
}
#[allow(dead_code)]
pub struct SmallMap<K: Ord + Clone, V: Clone> {
entries: Vec<(K, V)>,
}
#[allow(dead_code)]
impl<K: Ord + Clone, V: Clone> SmallMap<K, V> {
pub fn new() -> Self {
Self {
entries: Vec::new(),
}
}
pub fn insert(&mut self, key: K, val: V) {
match self.entries.binary_search_by_key(&&key, |(k, _)| k) {
Ok(i) => self.entries[i].1 = val,
Err(i) => self.entries.insert(i, (key, val)),
}
}
pub fn get(&self, key: &K) -> Option<&V> {
self.entries
.binary_search_by_key(&key, |(k, _)| k)
.ok()
.map(|i| &self.entries[i].1)
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn keys(&self) -> Vec<&K> {
self.entries.iter().map(|(k, _)| k).collect()
}
pub fn values(&self) -> Vec<&V> {
self.entries.iter().map(|(_, v)| v).collect()
}
}
#[allow(dead_code)]
pub struct TransitiveClosure {
adj: Vec<Vec<usize>>,
n: usize,
}
#[allow(dead_code)]
impl TransitiveClosure {
pub fn new(n: usize) -> Self {
Self {
adj: vec![Vec::new(); n],
n,
}
}
pub fn add_edge(&mut self, from: usize, to: usize) {
if from < self.n {
self.adj[from].push(to);
}
}
pub fn reachable_from(&self, start: usize) -> Vec<usize> {
let mut visited = vec![false; self.n];
let mut queue = std::collections::VecDeque::new();
queue.push_back(start);
while let Some(node) = queue.pop_front() {
if node >= self.n || visited[node] {
continue;
}
visited[node] = true;
for &next in &self.adj[node] {
queue.push_back(next);
}
}
(0..self.n).filter(|&i| visited[i]).collect()
}
pub fn can_reach(&self, from: usize, to: usize) -> bool {
self.reachable_from(from).contains(&to)
}
}
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct RewriteRule {
pub name: String,
pub lhs: String,
pub rhs: String,
pub conditional: bool,
}
#[allow(dead_code)]
impl RewriteRule {
pub fn unconditional(
name: impl Into<String>,
lhs: impl Into<String>,
rhs: impl Into<String>,
) -> Self {
Self {
name: name.into(),
lhs: lhs.into(),
rhs: rhs.into(),
conditional: false,
}
}
pub fn conditional(
name: impl Into<String>,
lhs: impl Into<String>,
rhs: impl Into<String>,
) -> Self {
Self {
name: name.into(),
lhs: lhs.into(),
rhs: rhs.into(),
conditional: true,
}
}
pub fn display(&self) -> String {
format!("{}: {} → {}", self.name, self.lhs, self.rhs)
}
}
#[derive(Clone, Debug)]
pub struct QuotReductionStep {
pub kind: QuotReductionKind,
pub before: Expr,
pub after: Expr,
}
impl QuotReductionStep {
pub fn new(kind: QuotReductionKind, before: Expr, after: Expr) -> Self {
Self {
kind,
before,
after,
}
}
}
#[allow(dead_code)]
pub struct RewriteRuleSet {
rules: Vec<RewriteRule>,
}
#[allow(dead_code)]
impl RewriteRuleSet {
pub fn new() -> Self {
Self { rules: Vec::new() }
}
pub fn add(&mut self, rule: RewriteRule) {
self.rules.push(rule);
}
pub fn len(&self) -> usize {
self.rules.len()
}
pub fn is_empty(&self) -> bool {
self.rules.is_empty()
}
pub fn conditional_rules(&self) -> Vec<&RewriteRule> {
self.rules.iter().filter(|r| r.conditional).collect()
}
pub fn unconditional_rules(&self) -> Vec<&RewriteRule> {
self.rules.iter().filter(|r| !r.conditional).collect()
}
pub fn get(&self, name: &str) -> Option<&RewriteRule> {
self.rules.iter().find(|r| r.name == name)
}
}