jj_lib/default_index/
entry.rs#![allow(missing_docs)]
use std::fmt::Debug;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use smallvec::SmallVec;
use super::composite::CompositeIndex;
use super::composite::DynIndexSegment;
use crate::backend::ChangeId;
use crate::backend::CommitId;
use crate::object_id::ObjectId;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub struct IndexPosition(pub(super) u32);
impl IndexPosition {
pub const MIN: Self = IndexPosition(u32::MIN);
pub const MAX: Self = IndexPosition(u32::MAX);
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub(super) struct LocalPosition(pub(super) u32);
pub(super) type SmallIndexPositionsVec = SmallVec<[IndexPosition; 4]>;
pub(super) type SmallLocalPositionsVec = SmallVec<[LocalPosition; 4]>;
#[derive(Clone)]
pub struct IndexEntry<'a> {
source: &'a DynIndexSegment,
pos: IndexPosition,
local_pos: LocalPosition,
}
impl Debug for IndexEntry<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IndexEntry")
.field("pos", &self.pos)
.field("local_pos", &self.local_pos)
.field("commit_id", &self.commit_id().hex())
.finish()
}
}
impl PartialEq for IndexEntry<'_> {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos
}
}
impl Eq for IndexEntry<'_> {}
impl Hash for IndexEntry<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.pos.hash(state);
}
}
impl<'a> IndexEntry<'a> {
pub(super) fn new(
source: &'a DynIndexSegment,
pos: IndexPosition,
local_pos: LocalPosition,
) -> Self {
IndexEntry {
source,
pos,
local_pos,
}
}
pub fn position(&self) -> IndexPosition {
self.pos
}
pub fn generation_number(&self) -> u32 {
self.source.generation_number(self.local_pos)
}
pub fn commit_id(&self) -> CommitId {
self.source.commit_id(self.local_pos)
}
pub fn change_id(&self) -> ChangeId {
self.source.change_id(self.local_pos)
}
pub fn num_parents(&self) -> u32 {
self.source.num_parents(self.local_pos)
}
pub fn parent_positions(&self) -> SmallIndexPositionsVec {
self.source.parent_positions(self.local_pos)
}
pub fn parents(&self) -> impl ExactSizeIterator<Item = IndexEntry<'a>> {
let composite = CompositeIndex::new(self.source);
self.parent_positions()
.into_iter()
.map(move |pos| composite.entry_by_pos(pos))
}
}
#[derive(Clone, Copy, Debug, Ord, PartialOrd)]
pub(super) struct IndexPositionByGeneration {
pub generation: u32, pub pos: IndexPosition, }
impl Eq for IndexPositionByGeneration {}
impl PartialEq for IndexPositionByGeneration {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos
}
}
impl From<&IndexEntry<'_>> for IndexPositionByGeneration {
fn from(entry: &IndexEntry<'_>) -> Self {
IndexPositionByGeneration {
generation: entry.generation_number(),
pos: entry.position(),
}
}
}