Skip to content

Clean up generics for a more consistent naming scheme #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::ops::Deref;
/// because relations have no "recent" tuples, so the fn would be a
/// guaranteed no-op if both arguments were relations. See also
/// `join_into_relation`.
pub(crate) fn join_into<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Ord>(
input1: &Variable<(Key, Val1)>,
input2: impl JoinInput<'me, (Key, Val2)>,
pub(crate) fn join_into<'me, Key: Ord, Value1: Ord, Value2: Ord, Result: Ord>(
input1: &Variable<(Key, Value1)>,
input2: impl JoinInput<'me, (Key, Value2)>,
output: &Variable<Result>,
mut logic: impl FnMut(&Key, &Val1, &Val2) -> Result,
mut logic: impl FnMut(&Key, &Value1, &Value2) -> Result,
) {
let mut results = Vec::new();

Expand All @@ -23,7 +23,7 @@ pub(crate) fn join_into<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Ord>(
{
// scoped to let `closure` drop borrow of `results`.

let mut closure = |k: &Key, v1: &Val1, v2: &Val2| results.push(logic(k, v1, v2));
let mut closure = |k: &Key, v1: &Value1, v2: &Value2| results.push(logic(k, v1, v2));

for batch2 in input2.stable().iter() {
join_helper(&recent1, &batch2, &mut closure);
Expand All @@ -40,10 +40,10 @@ pub(crate) fn join_into<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Ord>(
}

/// Join, but for two relations.
pub(crate) fn join_into_relation<'me, Key: Ord, Val1: Ord, Val2: Ord, Result: Ord>(
input1: &Relation<(Key, Val1)>,
input2: &Relation<(Key, Val2)>,
mut logic: impl FnMut(&Key, &Val1, &Val2) -> Result,
pub(crate) fn join_into_relation<'me, Key: Ord, Value1: Ord, Value2: Ord, Result: Ord>(
input1: &Relation<(Key, Value1)>,
input2: &Relation<(Key, Value2)>,
mut logic: impl FnMut(&Key, &Value1, &Value2) -> Result,
) -> Relation<Result> {
let mut results = Vec::new();

Expand Down Expand Up @@ -75,10 +75,10 @@ pub(crate) fn antijoin<'me, Key: Ord, Val: Ord, Result: Ord>(
Relation::from_vec(results)
}

fn join_helper<K: Ord, V1, V2>(
mut slice1: &[(K, V1)],
mut slice2: &[(K, V2)],
mut result: impl FnMut(&K, &V1, &V2),
fn join_helper<Key: Ord, Value1, Value2>(
mut slice1: &[(Key, Value1)],
mut slice2: &[(Key, Value2)],
mut result: impl FnMut(&Key, &Value1, &Value2),
) {
while !slice1.is_empty() && !slice2.is_empty() {
use std::cmp::Ordering;
Expand Down Expand Up @@ -111,7 +111,7 @@ fn join_helper<K: Ord, V1, V2>(
}
}

pub(crate) fn gallop<T>(mut slice: &[T], mut cmp: impl FnMut(&T) -> bool) -> &[T] {
pub(crate) fn gallop<Tuple>(mut slice: &[Tuple], mut cmp: impl FnMut(&Tuple) -> bool) -> &[Tuple] {
// if empty slice, or already >= element, return
if !slice.is_empty() && cmp(&slice[0]) {
let mut step = 1;
Expand Down
10 changes: 5 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

use super::{Relation, Variable};

pub(crate) fn map_into<T1: Ord, T2: Ord>(
input: &Variable<T1>,
output: &Variable<T2>,
logic: impl FnMut(&T1) -> T2,
pub(crate) fn map_into<Tuple1: Ord, Tuple2: Ord>(
input: &Variable<Tuple1>,
output: &Variable<Tuple2>,
logic: impl FnMut(&Tuple1) -> Tuple2,
) {
let results: Vec<T2> = input.recent.borrow().iter().map(logic).collect();
let results: Vec<Tuple2> = input.recent.borrow().iter().map(logic).collect();

output.insert(Relation::from_vec(results));
}
19 changes: 11 additions & 8 deletions src/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ impl<Tuple: Ord> Relation<Tuple> {
/// `input2` and then applying `logic`. Like
/// [`Variable::from_join`] except for use where the inputs are
/// not varying across iterations.
pub fn from_join<Key: Ord, Val1: Ord, Val2: Ord>(
input1: &Relation<(Key, Val1)>,
input2: &Relation<(Key, Val2)>,
logic: impl FnMut(&Key, &Val1, &Val2) -> Tuple,
pub fn from_join<Key: Ord, Value1: Ord, Value2: Ord>(
input1: &Relation<(Key, Value1)>,
input2: &Relation<(Key, Value2)>,
logic: impl FnMut(&Key, &Value1, &Value2) -> Tuple,
) -> Self {
join::join_into_relation(input1, input2, logic)
}
Expand All @@ -61,18 +61,21 @@ impl<Tuple: Ord> Relation<Tuple> {
/// tuples with the `logic` closure. Like
/// [`Variable::from_antijoin`] except for use where the inputs
/// are not varying across iterations.
pub fn from_antijoin<Key: Ord, Val1: Ord>(
input1: &Relation<(Key, Val1)>,
pub fn from_antijoin<Key: Ord, Value1: Ord>(
input1: &Relation<(Key, Value1)>,
input2: &Relation<Key>,
logic: impl FnMut(&Key, &Val1) -> Tuple,
logic: impl FnMut(&Key, &Value1) -> Tuple,
) -> Self {
join::antijoin(input1, input2, logic)
}

/// Construct a new relation by mapping another one. Equivalent to
/// creating an iterator but perhaps more convenient. Analogous to
/// `Variable::from_map`.
pub fn from_map<T2: Ord>(input: &Relation<T2>, logic: impl FnMut(&T2) -> Tuple) -> Self {
pub fn from_map<Tuple2: Ord>(
input: &Relation<Tuple2>,
logic: impl FnMut(&Tuple2) -> Tuple,
) -> Self {
input.iter().map(logic).collect()
}

Expand Down
2 changes: 1 addition & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn sum_join_via_var(
let output = iteration.variable::<(u32, u32)>("output");

while iteration.changed() {
// output(K1, V1 * 100 + V2) :- input1(K1, V1), input2(K1, V2).
// output(K1, Value1 * 100 + Value2) :- input1(K1, Value1), input2(K1, Value2).
output.from_join(&input1, &input2, |&k1, &v1, &v2| (k1, v1 * 100 + v2));
}

Expand Down
Loading