Skip to content

Commit

Permalink
Merge pull request #10 from aidan-bailey/dev/7-rename-all-instances-o…
Browse files Browse the repository at this point in the history
…f-arity-to-cardinality

Rename arity to cardinality
  • Loading branch information
aidan-bailey authored Aug 7, 2024
2 parents 1da1792 + 071a463 commit 473d6c5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
5 changes: 3 additions & 2 deletions kermit-algos/tests/leapfrog_triejoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ mod tests {
// vec![vec![1], vec![2], vec![3]];
// "1-ary"
// )]
// fn test_inputs_outputs(arity: usize, inputs: Vec<Vec<Vec<i32>>>,
// fn test_inputs_outputs(cardinality: usize, inputs: Vec<Vec<Vec<i32>>>,
// expected: Vec<Vec<i32>>) { let tries: Vec<_> = inputs
// .into_iter()
// .map(|input| TrieBuilder::<i32>::new(arity).add_tuples(input).build())
// .map(|input|
// TrieBuilder::<i32>::new(cardinality).add_tuples(input).build())
// .collect();
// let res = leapfrog_triejoin(tries.iter().collect());
// assert_eq!(res, expected);
Expand Down
25 changes: 15 additions & 10 deletions kermit-ds/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use {
std::fmt,
};

fn generate_vector<T: PartialOrd + SampleUniform + Copy>(arity: usize, min: T, max: T) -> Vec<T> {
fn generate_vector<T: PartialOrd + SampleUniform + Copy>(
cardinality: usize, min: T, max: T,
) -> Vec<T> {
let mut rng = rand::thread_rng();
let mut vector = Vec::<T>::new();
for _ in 0..arity {
for _ in 0..cardinality {
vector.push(rng.gen_range(min..max));
}
vector
Expand All @@ -18,7 +20,7 @@ fn generate_vector<T: PartialOrd + SampleUniform + Copy>(arity: usize, min: T, m
fn generate_tuples<T: PartialOrd + SampleUniform + Copy>(params: &BenchParams<T>) -> Vec<Vec<T>> {
let mut vectors = Vec::<Vec<T>>::new();
while vectors.len() < params.size {
let vector = generate_vector(params.arity, params.min, params.max);
let vector = generate_vector(params.cardinality, params.min, params.max);
if !vectors.contains(&vector) {
vectors.push(vector);
}
Expand All @@ -28,16 +30,16 @@ fn generate_tuples<T: PartialOrd + SampleUniform + Copy>(params: &BenchParams<T>

struct BenchParams<T: PartialOrd + SampleUniform + Copy> {
size: usize,
arity: usize,
cardinality: usize,
min: T,
max: T,
}

impl<T: PartialOrd + SampleUniform + Copy + fmt::Display> BenchParams<T> {
fn new(size: usize, arity: usize, min: T, max: T) -> BenchParams<T> {
fn new(size: usize, cardinality: usize, min: T, max: T) -> BenchParams<T> {
BenchParams {
size,
arity,
cardinality,
min,
max,
}
Expand All @@ -48,8 +50,8 @@ impl<T: PartialOrd + SampleUniform + Copy + fmt::Display> fmt::Display for Bench
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"size: {}, arity: {}, min: {}, max: {}",
self.size, self.arity, self.min, self.max
"size: {}, cardinality: {}, min: {}, max: {}",
self.size, self.cardinality, self.min, self.max
)
}
}
Expand Down Expand Up @@ -90,7 +92,10 @@ pub fn criterion_benchmark(c: &mut Criterion) {
b.iter_batched(
|| generate_tuples(bench_param),
|tuples| {
black_box(RelationTrie::from_tuples_presort(bench_param.arity, tuples))
black_box(RelationTrie::from_tuples_presort(
bench_param.cardinality,
tuples,
))
},
BatchSize::SmallInput,
)
Expand All @@ -110,7 +115,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
b.iter_batched(
|| {
RelationTrie::from_tuples_presort(
bench_param.arity,
bench_param.cardinality,
generate_tuples(bench_param),
)
},
Expand Down
2 changes: 1 addition & 1 deletion kermit-ds/src/relation_trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod tests {
#[test]
fn trie_new() {
let empty_tri = RelationTrie::<u64>::new(1);
assert_eq!(empty_tri.arity(), 1);
assert_eq!(empty_tri.cardinality(), 1);
assert!(empty_tri.is_empty());
}

Expand Down
14 changes: 7 additions & 7 deletions kermit-ds/src/relation_trie/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
#[derive(Clone, Debug)]
pub struct Node<KT: PartialOrd + PartialEq + Clone> {
/// Maximum height (1-based index of key in tuple)
arity: usize,
cardinality: usize,
/// Key for tuple value
key: KT,
/// Children
Expand All @@ -28,7 +28,7 @@ impl<KT: PartialOrd + PartialEq + Clone> Node<KT> {
/// Construct a Node with a tuple-value key
fn new(key: KT) -> Node<KT> {
Node {
arity: 0,
cardinality: 0,
key,
children: vec![],
}
Expand All @@ -50,13 +50,13 @@ pub trait TrieFields<KT: PartialOrd + PartialEq + Clone> {
0
}
}
fn arity(&self) -> usize;
fn cardinality(&self) -> usize;
}

impl<KT: PartialOrd + PartialEq + Clone> TrieFields<KT> for Node<KT> {
fn children(&self) -> &Vec<Node<KT>> { &self.children }

fn arity(&self) -> usize { self.arity }
fn cardinality(&self) -> usize { self.cardinality }
}

pub(crate) trait Internal<KT: PartialOrd + PartialEq + Clone>: TrieFields<KT> {
Expand Down Expand Up @@ -201,21 +201,21 @@ mod tests {
fn node_new() {
let node = Node::new(1);
assert_eq!(node.key(), &1);
assert_eq!(node.arity(), 0);
assert_eq!(node.cardinality(), 0);
}

#[test]
fn node_with_child() {
let node = {
let child = Node::new(2);
Node {
arity: child.arity() + 1,
cardinality: child.cardinality() + 1,
key: 1,
children: vec![child],
}
};
assert_eq!(node.key(), &1);
assert_eq!(node.arity(), 1);
assert_eq!(node.cardinality(), 1);
assert_eq!(node.children()[0].key(), &2);
}

Expand Down
22 changes: 11 additions & 11 deletions kermit-ds/src/relation_trie/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
/// Trie root
#[derive(Clone, Debug)]
pub struct RelationTrie<KT: PartialOrd + PartialEq + Clone> {
arity: usize,
cardinality: usize,
children: Vec<Node<KT>>,
}

Expand All @@ -22,22 +22,22 @@ impl<KT: PartialOrd + PartialEq + Clone> IndexMut<usize> for RelationTrie<KT> {

impl<KT: PartialOrd + PartialEq + Clone> RelationTrie<KT> {
/// Construct an empty Trie
pub fn new(arity: usize) -> RelationTrie<KT> {
pub fn new(cardinality: usize) -> RelationTrie<KT> {
RelationTrie {
arity,
cardinality,
children: vec![],
}
}

pub fn from_tuples(arity: usize, tuples: Vec<Vec<KT>>) -> RelationTrie<KT> {
let mut trie = RelationTrie::new(arity);
pub fn from_tuples(cardinality: usize, tuples: Vec<Vec<KT>>) -> RelationTrie<KT> {
let mut trie = RelationTrie::new(cardinality);
for tuple in tuples {
trie.insert(tuple).unwrap();
}
trie
}

pub fn from_tuples_presort(arity: usize, mut tuples: Vec<Vec<KT>>) -> RelationTrie<KT> {
pub fn from_tuples_presort(cardinality: usize, mut tuples: Vec<Vec<KT>>) -> RelationTrie<KT> {
tuples.sort_unstable_by(|a, b| {
for i in 0..a.len() {
if a[i] < b[i] {
Expand All @@ -48,30 +48,30 @@ impl<KT: PartialOrd + PartialEq + Clone> RelationTrie<KT> {
}
std::cmp::Ordering::Equal
});
let mut trie = RelationTrie::new(arity);
let mut trie = RelationTrie::new(cardinality);
for tuple in tuples {
trie.insert(tuple).unwrap();
}
trie
}

pub fn insert(&mut self, tuple: Vec<KT>) -> Result<(), &'static str> {
if tuple.len() != self.arity {
if tuple.len() != self.cardinality {
return Err("Arity doesn't match.");
}
self.insert_linear(tuple);
Ok(())
}

pub fn search(&self, tuple: Vec<KT>) -> Result<Option<&Node<KT>>, &'static str> {
if tuple.len() != self.arity {
if tuple.len() != self.cardinality {
return Err("Arity doesn't match.");
}
Ok(self.search_linear(tuple))
}

pub fn remove(&mut self, tuple: Vec<KT>) -> Result<(), &'static str> {
if tuple.len() != self.arity {
if tuple.len() != self.cardinality {
return Err("Arity doesn't match.");
}
self.remove_deque(tuple.into());
Expand All @@ -82,7 +82,7 @@ impl<KT: PartialOrd + PartialEq + Clone> RelationTrie<KT> {
impl<KT: PartialOrd + PartialEq + Clone> TrieFields<KT> for RelationTrie<KT> {
fn children(&self) -> &Vec<Node<KT>> { &self.children }

fn arity(&self) -> usize { self.arity }
fn cardinality(&self) -> usize { self.cardinality }
}

impl<KT: PartialOrd + PartialEq + Clone> Internal<KT> for RelationTrie<KT> {
Expand Down
8 changes: 4 additions & 4 deletions kermit-ds/src/relation_trie/trie_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ use {
};

pub struct TrieBuilder<KT: PartialOrd + PartialEq + Clone + FromStr + Debug> {
arity: usize,
cardinality: usize,
tuples: Vec<Vec<KT>>,
}

impl<KT: PartialOrd + PartialEq + Clone + FromStr + Debug> TrieBuilder<KT> {
pub fn new(arity: usize) -> TrieBuilder<KT> {
pub fn new(cardinality: usize) -> TrieBuilder<KT> {
TrieBuilder {
arity,
cardinality,
tuples: vec![],
}
}

pub fn build(self) -> RelationTrie<KT> {
RelationTrie::from_tuples_presort(self.arity, self.tuples)
RelationTrie::from_tuples_presort(self.cardinality, self.tuples)
}

pub fn add_tuple(mut self, tuple: Vec<KT>) -> TrieBuilder<KT> {
Expand Down

0 comments on commit 473d6c5

Please sign in to comment.