use std::collections::BTreeMap; use crate::NodeIdT; /// A batch of contributions the algorithm has output. #[derive(Clone, Debug)] pub struct Batch { /// This batch's epoch number. Each epoch produces exactly one batch. pub epoch: u64, /// The set of agreed contributions, by the contributor's node ID. pub contributions: BTreeMap, } impl Batch { /// Returns an iterator over references to all transactions included in the batch. pub fn iter<'a>(&'a self) -> impl Iterator::Item> where &'a C: IntoIterator, { self.contributions.values().flatten() } /// Returns an iterator over all transactions included in the batch. Consumes the batch. pub fn into_tx_iter(self) -> impl Iterator::Item> where C: IntoIterator, { self.contributions.into_iter().flat_map(|(_, vec)| vec) } /// Returns the number of transactions in the batch (without detecting duplicates). pub fn len(&self) -> usize where C: AsRef<[T]>, { self.contributions .values() .map(C::as_ref) .map(<[T]>::len) .sum() } /// Returns `true` if the batch contains no transactions. pub fn is_empty(&self) -> bool where C: AsRef<[T]>, { self.contributions .values() .map(C::as_ref) .all(<[T]>::is_empty) } }