salsa/tests/gc/group.rs

56 lines
1.2 KiB
Rust
Raw Normal View History

use crate::log::HasLog;
#[salsa::query_group(Gc)]
pub(crate) trait GcDatabase: salsa::Database + HasLog {
#[salsa::input]
fn min(&self) -> usize;
2018-10-24 08:38:38 +00:00
#[salsa::input]
fn max(&self) -> usize;
2018-10-24 08:38:38 +00:00
#[salsa::input]
fn use_triangular(&self, key: usize) -> bool;
2018-10-24 08:38:38 +00:00
fn fibonacci(&self, key: usize) -> usize;
2018-10-24 08:38:38 +00:00
fn triangular(&self, key: usize) -> usize;
2018-10-24 08:38:38 +00:00
fn compute(&self, key: usize) -> usize;
2018-10-24 08:38:38 +00:00
fn compute_all(&self) -> Vec<usize>;
2018-10-24 08:38:38 +00:00
}
fn fibonacci(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("fibonacci({:?})", key));
2018-10-24 08:38:38 +00:00
if key == 0 {
0
} else if key == 1 {
1
} else {
db.fibonacci(key - 1) + db.fibonacci(key - 2)
}
}
fn triangular(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("triangular({:?})", key));
2018-10-24 08:38:38 +00:00
if key == 0 {
0
} else {
db.triangular(key - 1) + key
}
}
fn compute(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("compute({:?})", key));
2018-10-24 08:38:38 +00:00
if db.use_triangular(key) {
db.triangular(key)
} else {
db.fibonacci(key)
}
}
fn compute_all(db: &impl GcDatabase) -> Vec<usize> {
db.log().add("compute_all()");
2018-10-24 08:38:38 +00:00
(db.min()..db.max()).map(|v| db.compute(v)).collect()
}