Skip to main content

diff_balances_iter

Function diff_balances_iter 

Source
pub fn diff_balances_iter<I1, I2>(base: I1, target: I2) -> BalancesDiff
where I1: ExactSizeIterator<Item = u64>, I2: ExactSizeIterator<Item = u64>,
Expand description

Computes a compact balance delta between two balance iterators.

This is the generic counterpart to diff_balances. It is intended for consensus clients whose balance storage is not represented as a contiguous &[u64].

The iterators must implement ExactSizeIterator, allowing the function to determine the size of the common portion and distinguish existing balances from balances appended to the target.

The iterators are consumed during encoding and do not need to implement Clone.

The common portion is first collected into a compact list of changed balances so that the most frequently occurring balance difference can be selected as the encoding mode. Remaining items in the target iterator are stored as appended balances.

§Complexity

O(n) time and O(k + a) additional space, where:

  • n is the number of balances in the common portion;
  • k is the number of changed balances; and
  • a is the number of balances appended to the target.

§Panics

Panics if an iterator violates the ExactSizeIterator contract and yields a different number of items than reported by len().