Skip to content

Commit e4caf61

Browse files
authored
count: add generic count_total_core method (#355)
This follows the pattern of `distinct_core` to allow counting a collection but use a different diff type than `isize`. Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
1 parent 7a8ee3e commit e4caf61

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/operators/count.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,23 @@ pub trait CountTotal<G: Scope, K: ExchangeData, R: Semigroup> where G::Timestamp
4848
/// });
4949
/// }
5050
/// ```
51-
fn count_total(&self) -> Collection<G, (K, R), isize>;
51+
fn count_total(&self) -> Collection<G, (K, R), isize> {
52+
self.count_total_core()
53+
}
54+
55+
/// Count for general integer differences.
56+
///
57+
/// This method allows `count_total` to produce collections whose difference
58+
/// type is something other than an `isize` integer, for example perhaps an
59+
/// `i32`.
60+
fn count_total_core<R2: Semigroup + From<i8>>(&self) -> Collection<G, (K, R), R2>;
5261
}
5362

5463
impl<G: Scope, K: ExchangeData+Hashable, R: ExchangeData+Semigroup> CountTotal<G, K, R> for Collection<G, K, R>
5564
where G::Timestamp: TotalOrder+Lattice+Ord {
56-
fn count_total(&self) -> Collection<G, (K, R), isize> {
65+
fn count_total_core<R2: Semigroup + From<i8>>(&self) -> Collection<G, (K, R), R2> {
5766
self.arrange_by_self_named("Arrange: CountTotal")
58-
.count_total()
67+
.count_total_core()
5968
}
6069
}
6170

@@ -68,7 +77,7 @@ where
6877
T1::Batch: BatchReader<T1::Key, (), G::Timestamp, T1::R>,
6978
T1::Cursor: Cursor<T1::Key, (), G::Timestamp, T1::R>,
7079
{
71-
fn count_total(&self) -> Collection<G, (T1::Key, T1::R), isize> {
80+
fn count_total_core<R2: Semigroup + From<i8>>(&self) -> Collection<G, (T1::Key, T1::R), R2> {
7281

7382
let mut trace = self.trace.clone();
7483
let mut buffer = Vec::new();
@@ -105,14 +114,14 @@ where
105114

106115
if let Some(count) = count.as_ref() {
107116
if !count.is_zero() {
108-
session.give(((key.clone(), count.clone()), time.clone(), -1));
117+
session.give(((key.clone(), count.clone()), time.clone(), R2::from(-1i8)));
109118
}
110119
}
111120
count.as_mut().map(|c| c.plus_equals(diff));
112121
if count.is_none() { count = Some(diff.clone()); }
113122
if let Some(count) = count.as_ref() {
114123
if !count.is_zero() {
115-
session.give(((key.clone(), count.clone()), time.clone(), 1));
124+
session.give(((key.clone(), count.clone()), time.clone(), R2::from(1i8)));
116125
}
117126
}
118127
});

0 commit comments

Comments
 (0)