1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::fmt;
use std::ops::Index;
use crate::{Author, Change, Chronofold};
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LogIndex(pub usize);
impl<A: Author, T> Index<LogIndex> for Chronofold<A, T> {
type Output = Change<T>;
fn index(&self, index: LogIndex) -> &Self::Output {
&self.log[index.0]
}
}
impl fmt::Display for LogIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<A: Author, T> Chronofold<A, T> {
pub fn last_index(&self) -> Option<LogIndex> {
if !self.log.is_empty() {
Some(LogIndex(self.log.len() - 1))
} else {
None
}
}
pub(crate) fn index_before(&self, index: LogIndex) -> Option<LogIndex> {
self.iter_log_indices_causal_range(..index).last()
}
pub(crate) fn index_after(&self, index: LogIndex) -> Option<LogIndex> {
self.next_indices.get(index.0).copied().unwrap_or(None)
}
}