readme/
readme.rs

1use invocation_counter::Counter;
2
3fn main() {
4    const BUCKET_COUNT: usize = 16;
5    const SUB_BUCKET_COUNT: usize = 2;
6    const GROUP_SHIFT_FACTOR: u32 = 4;
7    // 4 is the group_shift_factor
8    // 16 is the number of buckets
9    let counter = Counter::<BUCKET_COUNT, SUB_BUCKET_COUNT>::new(GROUP_SHIFT_FACTOR);
10
11    // Typically you want to use something like `Instant::now().elapsed().as_secs()`
12    let mut now: u64 = 0;
13    counter.increment_by_one(now);
14
15    now += 1; // Simulate a second passing
16    counter.increment_by_one(now);
17
18    assert_eq!(counter.get_count_till(now), 2);
19
20    now += 2_u64.pow(GROUP_SHIFT_FACTOR) * BUCKET_COUNT as u64; // Move forward...
21    counter.increment_by_one(now);
22    // The counter forgot about the counts older than 2 ** 4 * 16 seconds
23    assert_eq!(counter.get_count_till(now), 1);
24}