~dkellner/chronofold

4f66a7259a71afb7775cf7a477d1a1fca7a104c1 — Dominik Kellner 2 years ago e58b75d
Make `log_index` and `timestamp` public
3 files changed, 24 insertions(+), 21 deletions(-)

M src/internal.rs
M src/iter.rs
M src/lib.rs
M src/internal.rs => src/internal.rs +3 -19
@@ 27,7 27,9 @@ impl<A: Author, T> Chronofold<A, T> {
                if let Some((_, idx)) = self
                    .iter_log_indices_causal_range(reference..)
                    .filter(|(_, i)| self.references.get(i) == Some(reference))
                    .filter(|(c, i)| matches!(c, Change::Delete) || self.timestamp(i).unwrap() > id)
                    .filter(|(c, i)| {
                        matches!(c, Change::Delete) || self.timestamp(*i).unwrap() > id
                    })
                    .last()
                {
                    self.iter_subtree(idx).last()


@@ 43,24 45,6 @@ impl<A: Author, T> Chronofold<A, T> {
        }
    }

    pub(crate) fn log_index(&self, timestamp: &Timestamp<A>) -> Option<LogIndex> {
        for i in (timestamp.0).0..self.log.len() {
            if self.timestamp(&LogIndex(i)).unwrap() == *timestamp {
                return Some(LogIndex(i));
            }
        }
        None
    }

    pub(crate) fn timestamp(&self, index: &LogIndex) -> Option<Timestamp<A>> {
        if let (Some(shift), Some(author)) = (self.index_shifts.get(index), self.authors.get(index))
        {
            Some(Timestamp(index - shift, *author))
        } else {
            None
        }
    }

    pub(crate) fn apply_change(
        &mut self,
        id: Timestamp<A>,

M src/iter.rs => src/iter.rs +2 -2
@@ 182,11 182,11 @@ where
        let idx = LogIndex(self.idx_iter.next()?);
        let id = self
            .cfold
            .timestamp(&idx)
            .timestamp(idx)
            .expect("timestamps of already applied ops have to exist");
        let reference = self.cfold.references.get(&idx).map(|r| {
            self.cfold
                .timestamp(&r)
                .timestamp(r)
                .expect("references of already applied ops have to exist")
        });
        let payload = match &self.cfold.log[idx.0] {

M src/lib.rs => src/lib.rs +19 -0
@@ 196,6 196,25 @@ impl<A: Author, T> Chronofold<A, T> {
        Session::new(author, self)
    }

    pub fn log_index(&self, timestamp: &Timestamp<A>) -> Option<LogIndex> {
        for i in (timestamp.0).0..self.log.len() {
            if self.timestamp(LogIndex(i)).unwrap() == *timestamp {
                return Some(LogIndex(i));
            }
        }
        None
    }

    pub fn timestamp(&self, index: LogIndex) -> Option<Timestamp<A>> {
        if let (Some(shift), Some(author)) =
            (self.index_shifts.get(&index), self.authors.get(&index))
        {
            Some(Timestamp(&index - shift, *author))
        } else {
            None
        }
    }

    /// Applies an op to the chronofold.
    pub fn apply<V>(&mut self, op: Op<A, V>) -> Result<(), ChronofoldError<A, V>>
    where