This commit is contained in:
Nathan Sobo
2021-03-09 21:00:51 -07:00
parent a015c61337
commit 356bc41752
36 changed files with 14097 additions and 4 deletions
+85
View File
@@ -0,0 +1,85 @@
use super::Buffer;
use crate::time;
use anyhow::Result;
use std::cmp::Ordering;
use std::ops::Range;
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum Anchor {
Start,
End,
Middle {
insertion_id: time::Local,
offset: usize,
bias: AnchorBias,
},
}
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum AnchorBias {
Left,
Right,
}
impl PartialOrd for AnchorBias {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for AnchorBias {
fn cmp(&self, other: &Self) -> Ordering {
use AnchorBias::*;
if self == other {
return Ordering::Equal;
}
match (self, other) {
(Left, _) => Ordering::Less,
(Right, _) => Ordering::Greater,
}
}
}
impl Anchor {
pub fn cmp(&self, other: &Anchor, buffer: &Buffer) -> Result<Ordering> {
if self == other {
return Ok(Ordering::Equal);
}
Ok(match (self, other) {
(Anchor::Start, _) | (_, Anchor::End) => Ordering::Less,
(Anchor::End, _) | (_, Anchor::Start) => Ordering::Greater,
(
Anchor::Middle {
offset: self_offset,
bias: self_bias,
..
},
Anchor::Middle {
offset: other_offset,
bias: other_bias,
..
},
) => buffer
.fragment_id_for_anchor(self)?
.cmp(buffer.fragment_id_for_anchor(other)?)
.then_with(|| self_offset.cmp(other_offset))
.then_with(|| self_bias.cmp(other_bias)),
})
}
}
pub trait AnchorRangeExt {
fn cmp(&self, b: &Range<Anchor>, buffer: &Buffer) -> Result<Ordering>;
}
impl AnchorRangeExt for Range<Anchor> {
fn cmp(&self, other: &Range<Anchor>, buffer: &Buffer) -> Result<Ordering> {
Ok(match self.start.cmp(&other.start, buffer)? {
Ordering::Equal => other.end.cmp(&self.end, buffer)?,
ord @ _ => ord,
})
}
}
File diff suppressed because it is too large Load Diff
+100
View File
@@ -0,0 +1,100 @@
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Sub},
};
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash)]
pub struct Point {
pub row: u32,
pub column: u32,
}
impl Point {
pub fn new(row: u32, column: u32) -> Self {
Point { row, column }
}
pub fn zero() -> Self {
Point::new(0, 0)
}
pub fn is_zero(&self) -> bool {
self.row == 0 && self.column == 0
}
}
impl<'a> Add<&'a Self> for Point {
type Output = Point;
fn add(self, other: &'a Self) -> Self::Output {
if other.row == 0 {
Point::new(self.row, self.column + other.column)
} else {
Point::new(self.row + other.row, other.column)
}
}
}
impl Add for Point {
type Output = Point;
fn add(self, other: Self) -> Self::Output {
self + &other
}
}
impl<'a> Sub<&'a Self> for Point {
type Output = Point;
fn sub(self, other: &'a Self) -> Self::Output {
debug_assert!(*other <= self);
if self.row == other.row {
Point::new(0, self.column - other.column)
} else {
Point::new(self.row - other.row, self.column)
}
}
}
impl Sub for Point {
type Output = Point;
fn sub(self, other: Self) -> Self::Output {
self - &other
}
}
impl<'a> AddAssign<&'a Self> for Point {
fn add_assign(&mut self, other: &'a Self) {
if other.row == 0 {
self.column += other.column;
} else {
self.row += other.row;
self.column = other.column;
}
}
}
impl PartialOrd for Point {
fn partial_cmp(&self, other: &Point) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Point {
#[cfg(target_pointer_width = "64")]
fn cmp(&self, other: &Point) -> Ordering {
let a = (self.row as usize) << 32 | self.column as usize;
let b = (other.row as usize) << 32 | other.column as usize;
a.cmp(&b)
}
#[cfg(target_pointer_width = "32")]
fn cmp(&self, other: &Point) -> Ordering {
match self.row.cmp(&other.row) {
Ordering::Equal => self.column.cmp(&other.column),
comparison @ _ => comparison,
}
}
}
+445
View File
@@ -0,0 +1,445 @@
use super::Point;
use crate::sum_tree::{self, SeekBias, SumTree};
use arrayvec::ArrayVec;
use std::{
cmp,
fmt::{self, Debug},
ops::{Bound, Index, Range, RangeBounds},
sync::Arc,
};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Run {
Newline,
Chars { len: usize, char_size: u8 },
}
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
struct ByteOffset(usize);
impl sum_tree::Item for Run {
type Summary = TextSummary;
fn summary(&self) -> Self::Summary {
match *self {
Run::Newline => TextSummary {
chars: 1,
bytes: 1,
lines: Point::new(1, 0),
first_line_len: 0,
rightmost_point: Point::new(0, 0),
},
Run::Chars { len, char_size } => TextSummary {
chars: len,
bytes: len * char_size as usize,
lines: Point::new(0, len as u32),
first_line_len: len as u32,
rightmost_point: Point::new(0, len as u32),
},
}
}
}
impl Run {
fn char_size(&self) -> u8 {
match self {
Run::Newline => 1,
Run::Chars { char_size, .. } => *char_size,
}
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TextSummary {
pub chars: usize,
pub bytes: usize,
pub lines: Point,
pub first_line_len: u32,
pub rightmost_point: Point,
}
impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
fn add_assign(&mut self, other: &'a Self) {
let joined_line_len = self.lines.column + other.first_line_len;
if joined_line_len > self.rightmost_point.column {
self.rightmost_point = Point::new(self.lines.row, joined_line_len);
}
if other.rightmost_point.column > self.rightmost_point.column {
self.rightmost_point = self.lines + &other.rightmost_point;
}
if self.lines.row == 0 {
self.first_line_len += other.first_line_len;
}
self.chars += other.chars;
self.bytes += other.bytes;
self.lines += &other.lines;
}
}
impl std::ops::AddAssign<Self> for TextSummary {
fn add_assign(&mut self, other: Self) {
*self += &other;
}
}
impl<'a> sum_tree::Dimension<'a, TextSummary> for TextSummary {
fn add_summary(&mut self, summary: &TextSummary) {
*self += summary;
}
}
impl<'a> sum_tree::Dimension<'a, TextSummary> for Point {
fn add_summary(&mut self, summary: &TextSummary) {
*self += &summary.lines;
}
}
impl<'a> sum_tree::Dimension<'a, TextSummary> for ByteOffset {
fn add_summary(&mut self, summary: &TextSummary) {
self.0 += summary.bytes
}
}
impl<'a> sum_tree::Dimension<'a, TextSummary> for usize {
fn add_summary(&mut self, summary: &TextSummary) {
*self += summary.chars;
}
}
#[derive(Clone)]
pub struct Text {
text: Arc<str>,
runs: SumTree<Run>,
range: Range<usize>,
}
impl From<String> for Text {
fn from(text: String) -> Self {
let mut runs = Vec::new();
let mut chars_len = 0;
let mut run_char_size = 0;
let mut run_chars = 0;
let mut chars = text.chars();
loop {
let ch = chars.next();
let ch_size = ch.map_or(0, |ch| ch.len_utf8());
if run_chars != 0 && (ch.is_none() || ch == Some('\n') || run_char_size != ch_size) {
runs.push(Run::Chars {
len: run_chars,
char_size: run_char_size as u8,
});
run_chars = 0;
}
run_char_size = ch_size;
match ch {
Some('\n') => runs.push(Run::Newline),
Some(_) => run_chars += 1,
None => break,
}
chars_len += 1;
}
let mut tree = SumTree::new();
tree.extend(runs);
Text {
text: text.into(),
runs: tree,
range: 0..chars_len,
}
}
}
impl<'a> From<&'a str> for Text {
fn from(text: &'a str) -> Self {
Self::from(String::from(text))
}
}
impl Debug for Text {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Text").field(&self.text).finish()
}
}
impl PartialEq for Text {
fn eq(&self, other: &Self) -> bool {
self.text == other.text
}
}
impl Eq for Text {}
impl<T: RangeBounds<usize>> Index<T> for Text {
type Output = str;
fn index(&self, range: T) -> &Self::Output {
let start = match range.start_bound() {
Bound::Included(start) => cmp::min(self.range.start + start, self.range.end),
Bound::Excluded(_) => unimplemented!(),
Bound::Unbounded => self.range.start,
};
let end = match range.end_bound() {
Bound::Included(end) => cmp::min(self.range.start + end + 1, self.range.end),
Bound::Excluded(end) => cmp::min(self.range.start + end, self.range.end),
Bound::Unbounded => self.range.end,
};
let byte_start = self.abs_byte_offset_for_offset(start);
let byte_end = self.abs_byte_offset_for_offset(end);
&self.text[byte_start..byte_end]
}
}
impl Text {
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
pub fn as_str(&self) -> &str {
&self[..]
}
pub fn slice<T: RangeBounds<usize>>(&self, range: T) -> Text {
let start = match range.start_bound() {
Bound::Included(start) => cmp::min(self.range.start + start, self.range.end),
Bound::Excluded(_) => unimplemented!(),
Bound::Unbounded => self.range.start,
};
let end = match range.end_bound() {
Bound::Included(end) => cmp::min(self.range.start + end + 1, self.range.end),
Bound::Excluded(end) => cmp::min(self.range.start + end, self.range.end),
Bound::Unbounded => self.range.end,
};
Text {
text: self.text.clone(),
runs: self.runs.clone(),
range: start..end,
}
}
pub fn line_len(&self, row: u32) -> u32 {
let mut cursor = self.runs.cursor::<usize, Point>();
cursor.seek(&self.range.start, SeekBias::Right);
let absolute_row = cursor.start().row + row;
let mut cursor = self.runs.cursor::<Point, usize>();
cursor.seek(&Point::new(absolute_row, 0), SeekBias::Right);
let prefix_len = self.range.start.saturating_sub(*cursor.start());
let line_len = cursor.summary::<usize>(&Point::new(absolute_row + 1, 0), SeekBias::Left);
let suffix_len = cursor.start().saturating_sub(self.range.end);
line_len
.saturating_sub(prefix_len)
.saturating_sub(suffix_len) as u32
}
pub fn len(&self) -> usize {
self.range.end - self.range.start
}
pub fn lines(&self) -> Point {
self.abs_point_for_offset(self.range.end) - &self.abs_point_for_offset(self.range.start)
}
pub fn rightmost_point(&self) -> Point {
let lines = self.lines();
let mut candidates = ArrayVec::<[Point; 3]>::new();
candidates.push(lines);
if lines.row > 0 {
candidates.push(Point::new(0, self.line_len(0)));
if lines.row > 1 {
let mut cursor = self.runs.cursor::<usize, Point>();
cursor.seek(&self.range.start, SeekBias::Right);
let absolute_start_row = cursor.start().row;
let mut cursor = self.runs.cursor::<Point, usize>();
cursor.seek(&Point::new(absolute_start_row + 1, 0), SeekBias::Right);
let summary = cursor.summary::<TextSummary>(
&Point::new(absolute_start_row + lines.row, 0),
SeekBias::Left,
);
candidates.push(Point::new(1, 0) + &summary.rightmost_point);
}
}
candidates.into_iter().max_by_key(|p| p.column).unwrap()
}
pub fn point_for_offset(&self, offset: usize) -> Point {
self.abs_point_for_offset(self.range.start + offset)
- &self.abs_point_for_offset(self.range.start)
}
pub fn offset_for_point(&self, point: Point) -> usize {
let mut cursor = self.runs.cursor::<Point, TextSummary>();
let abs_point = self.abs_point_for_offset(self.range.start) + &point;
cursor.seek(&abs_point, SeekBias::Right);
let overshoot = abs_point - &cursor.start().lines;
let abs_offset = cursor.start().chars + overshoot.column as usize;
abs_offset - self.range.start
}
pub fn summary(&self) -> TextSummary {
TextSummary {
chars: self.range.end - self.range.start,
bytes: self.abs_byte_offset_for_offset(self.range.end)
- self.abs_byte_offset_for_offset(self.range.start),
lines: self.abs_point_for_offset(self.range.end)
- &self.abs_point_for_offset(self.range.start),
first_line_len: self.line_len(0),
rightmost_point: self.rightmost_point(),
}
}
fn abs_point_for_offset(&self, offset: usize) -> Point {
let mut cursor = self.runs.cursor::<usize, TextSummary>();
cursor.seek(&offset, SeekBias::Right);
let overshoot = (offset - cursor.start().chars) as u32;
cursor.start().lines + &Point::new(0, overshoot)
}
fn abs_byte_offset_for_offset(&self, offset: usize) -> usize {
let mut cursor = self.runs.cursor::<usize, TextSummary>();
cursor.seek(&offset, SeekBias::Right);
let overshoot = offset - cursor.start().chars;
cursor.start().bytes + overshoot * cursor.item().map_or(0, |run| run.char_size()) as usize
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;
use std::iter::FromIterator;
#[test]
fn test_basic() {
let text = Text::from(String::from("ab\ncd€\nfghij\nkl¢m"));
assert_eq!(text.len(), 17);
assert_eq!(text.as_str(), "ab\ncd€\nfghij\nkl¢m");
assert_eq!(text.lines(), Point::new(3, 4));
assert_eq!(text.line_len(0), 2);
assert_eq!(text.line_len(1), 3);
assert_eq!(text.line_len(2), 5);
assert_eq!(text.line_len(3), 4);
assert_eq!(text.rightmost_point(), Point::new(2, 5));
let b_to_g = text.slice(1..9);
assert_eq!(b_to_g.as_str(), "b\ncd€\nfg");
assert_eq!(b_to_g.len(), 8);
assert_eq!(b_to_g.lines(), Point::new(2, 2));
assert_eq!(b_to_g.line_len(0), 1);
assert_eq!(b_to_g.line_len(1), 3);
assert_eq!(b_to_g.line_len(2), 2);
assert_eq!(b_to_g.line_len(3), 0);
assert_eq!(b_to_g.rightmost_point(), Point::new(1, 3));
let d_to_i = text.slice(4..11);
assert_eq!(d_to_i.as_str(), "d€\nfghi");
assert_eq!(&d_to_i[1..5], "\nfg");
assert_eq!(d_to_i.len(), 7);
assert_eq!(d_to_i.lines(), Point::new(1, 4));
assert_eq!(d_to_i.line_len(0), 2);
assert_eq!(d_to_i.line_len(1), 4);
assert_eq!(d_to_i.line_len(2), 0);
assert_eq!(d_to_i.rightmost_point(), Point::new(1, 4));
let d_to_j = text.slice(4..=11);
assert_eq!(d_to_j.as_str(), "d€\nfghij");
assert_eq!(&d_to_j[1..], "\nfghij");
assert_eq!(d_to_j.len(), 8);
}
#[test]
fn test_random() {
use rand::prelude::*;
for seed in 0..100 {
println!("buffer::text seed: {}", seed);
let rng = &mut StdRng::seed_from_u64(seed);
let len = rng.gen_range(0, 50);
let mut string = String::new();
for _ in 0..len {
if rng.gen_ratio(1, 5) {
string.push('\n');
} else {
string.push(rng.gen());
}
}
let text = Text::from(string.clone());
for _ in 0..10 {
let start = rng.gen_range(0, text.len() + 1);
let end = rng.gen_range(start, text.len() + 2);
let string_slice = string
.chars()
.skip(start)
.take(end - start)
.collect::<String>();
let expected_line_endpoints = string_slice
.split('\n')
.enumerate()
.map(|(row, line)| Point::new(row as u32, line.chars().count() as u32))
.collect::<Vec<_>>();
let text_slice = text.slice(start..end);
assert_eq!(text_slice.lines(), lines(&string_slice));
let mut rightmost_points: HashSet<Point> = HashSet::new();
for endpoint in &expected_line_endpoints {
if let Some(rightmost_point) = rightmost_points.iter().next().cloned() {
if endpoint.column > rightmost_point.column {
rightmost_points.clear();
}
if endpoint.column >= rightmost_point.column {
rightmost_points.insert(*endpoint);
}
} else {
rightmost_points.insert(*endpoint);
}
assert_eq!(text_slice.line_len(endpoint.row as u32), endpoint.column);
}
assert!(rightmost_points.contains(&text_slice.rightmost_point()));
for _ in 0..10 {
let offset = rng.gen_range(0, string_slice.chars().count() + 1);
let point = lines(&string_slice.chars().take(offset).collect::<String>());
assert_eq!(text_slice.point_for_offset(offset), point);
assert_eq!(text_slice.offset_for_point(point), offset);
if offset < string_slice.chars().count() {
assert_eq!(
&text_slice[offset..offset + 1],
String::from_iter(string_slice.chars().nth(offset)).as_str()
);
}
}
}
}
}
pub fn lines(s: &str) -> Point {
let mut row = 0;
let mut column = 0;
for ch in s.chars() {
if ch == '\n' {
row += 1;
column = 0;
} else {
column += 1;
}
}
Point::new(row, column)
}
}
+765
View File
@@ -0,0 +1,765 @@
use super::{BufferView, DisplayPoint, SelectAction};
use crate::{
app::{AppContext, MutableAppContext, ViewHandle},
fonts::FontCache,
text_layout::{self, LayoutCache},
ui::{
AfterLayoutContext, Bump, Element, Event, EventContext, LayoutContext, PaintContext,
SizeConstraint,
},
};
use pathfinder_canvas::{
ArcDirection, CanvasRenderingContext2D, ColorF, FillRule, FillStyle, Path2D,
};
use pathfinder_color::ColorU;
use pathfinder_geometry::{
rect::RectF,
vector::{vec2f, Vector2F},
};
use smallvec::SmallVec;
use std::{
cmp::{self, Ordering},
sync::Arc,
};
pub struct BufferElement {
view: ViewHandle<BufferView>,
layout: Option<LayoutState>,
paint: Option<PaintState>,
}
impl BufferElement {
pub fn new(view: ViewHandle<BufferView>) -> Self {
Self {
view,
layout: None,
paint: None,
}
}
fn mouse_down(
&self,
position: Vector2F,
cmd: bool,
ctx: &mut EventContext,
app: &AppContext,
) -> bool {
let layout = self.layout.as_ref().unwrap();
let paint = self.paint.as_ref().unwrap();
if paint.text_rect.contains_point(position) {
let view = self.view.as_ref(app);
let position = paint.point_for_position(view, layout, position, ctx.font_cache, app);
ctx.dispatch_action("buffer:select", SelectAction::Begin { position, add: cmd });
true
} else {
false
}
}
fn mouse_up(&self, _position: Vector2F, ctx: &mut EventContext, app: &AppContext) -> bool {
if self.view.as_ref(app).is_selecting() {
ctx.dispatch_action("buffer:select", SelectAction::End);
true
} else {
false
}
}
fn mouse_dragged(&self, position: Vector2F, ctx: &mut EventContext, app: &AppContext) -> bool {
let view = self.view.as_ref(app);
let layout = self.layout.as_ref().unwrap();
let paint = self.paint.as_ref().unwrap();
if view.is_selecting() {
let rect = self.paint.as_ref().unwrap().text_rect;
let mut scroll_delta = Vector2F::zero();
let vertical_margin = view.line_height(ctx.font_cache).min(rect.height() / 3.0);
let top = rect.origin_y() + vertical_margin;
let bottom = rect.lower_left().y() - vertical_margin;
if position.y() < top {
scroll_delta.set_y(-scale_vertical_mouse_autoscroll_delta(top - position.y()))
}
if position.y() > bottom {
scroll_delta.set_y(scale_vertical_mouse_autoscroll_delta(position.y() - bottom))
}
let horizontal_margin = view.line_height(ctx.font_cache).min(rect.width() / 3.0);
let left = rect.origin_x() + horizontal_margin;
let right = rect.upper_right().x() - horizontal_margin;
if position.x() < left {
scroll_delta.set_x(-scale_horizontal_mouse_autoscroll_delta(
left - position.x(),
))
}
if position.x() > right {
scroll_delta.set_x(scale_horizontal_mouse_autoscroll_delta(
position.x() - right,
))
}
ctx.dispatch_action(
"buffer:select",
SelectAction::Update {
position: paint.point_for_position(view, layout, position, ctx.font_cache, app),
scroll_position: (view.scroll_position() + scroll_delta).clamp(
Vector2F::zero(),
self.layout.as_ref().unwrap().scroll_max(
view,
ctx.font_cache,
ctx.text_layout_cache,
app,
),
),
},
);
true
} else {
false
}
}
fn key_down(&self, chars: &str, ctx: &mut EventContext, app: &AppContext) -> bool {
if self.view.is_focused(app) {
if chars.is_empty() {
false
} else {
if chars.chars().any(|c| c.is_control()) {
false
} else {
ctx.dispatch_action("buffer:insert", chars.to_string());
true
}
}
} else {
false
}
}
fn scroll(
&self,
position: Vector2F,
delta: Vector2F,
precise: bool,
ctx: &mut EventContext,
app: &AppContext,
) -> bool {
let paint = self.paint.as_ref().unwrap();
if !paint.rect.contains_point(position) {
return false;
}
if !precise {
todo!("still need to handle non-precise scroll events from a mouse wheel");
}
let view = self.view.as_ref(app);
let font_cache = &ctx.font_cache;
let layout_cache = &ctx.text_layout_cache;
let max_glyph_width = view.em_width(font_cache);
let line_height = view.line_height(font_cache);
let x = (view.scroll_position().x() * max_glyph_width - delta.x()) / max_glyph_width;
let y = (view.scroll_position().y() * line_height - delta.y()) / line_height;
let scroll_position = vec2f(x, y).clamp(
Vector2F::zero(),
self.layout
.as_ref()
.unwrap()
.scroll_max(view, font_cache, layout_cache, app),
);
ctx.dispatch_action("buffer:scroll", scroll_position);
true
}
fn paint_gutter(&mut self, rect: RectF, ctx: &mut PaintContext, app: &AppContext) {
if let Some(layout) = self.layout.as_ref() {
let view = self.view.as_ref(app);
let canvas = &mut ctx.canvas;
let font_cache = &ctx.font_cache;
let line_height = view.line_height(font_cache);
let scroll_top = view.scroll_position().y() * line_height;
canvas.save();
canvas.translate(rect.origin());
canvas.set_fill_style(FillStyle::Color(ColorU::white()));
let rect = RectF::new(Vector2F::zero(), rect.size());
let mut rect_path = Path2D::new();
rect_path.rect(rect);
canvas.clip_path(rect_path, FillRule::EvenOdd);
canvas.fill_rect(rect);
for (ix, line) in layout.line_number_layouts.iter().enumerate() {
let line_origin = vec2f(
rect.width() - line.width - layout.gutter_padding,
ix as f32 * line_height - (scroll_top % line_height),
);
line.paint(
line_origin,
rect,
&[(0..line.len, ColorU::black())],
canvas,
font_cache,
);
}
canvas.restore();
}
}
fn paint_text(&mut self, rect: RectF, ctx: &mut PaintContext, app: &AppContext) {
if let Some(layout) = self.layout.as_ref() {
let canvas = &mut ctx.canvas;
let font_cache = &ctx.font_cache;
canvas.save();
canvas.translate(rect.origin());
canvas.set_fill_style(FillStyle::Color(ColorU::white()));
let rect = RectF::new(Vector2F::zero(), rect.size());
let mut rect_path = Path2D::new();
rect_path.rect(rect);
canvas.clip_path(rect_path, FillRule::EvenOdd);
canvas.fill_rect(rect);
let view = self.view.as_ref(app);
let line_height = view.line_height(font_cache);
let descent = view.font_descent(font_cache);
let start_row = view.scroll_position().y() as u32;
let scroll_top = view.scroll_position().y() * line_height;
let end_row = ((scroll_top + rect.height()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
let max_glyph_width = view.em_width(font_cache);
let scroll_left = view.scroll_position().x() * max_glyph_width;
// Draw selections
canvas.save();
let corner_radius = 2.5;
let mut cursors = SmallVec::<[Cursor; 32]>::new();
for selection in view.selections_in_range(
DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
app,
) {
if selection.start != selection.end {
let range_start = cmp::min(selection.start, selection.end);
let range_end = cmp::max(selection.start, selection.end);
let row_range = if range_end.column() == 0 {
cmp::max(range_start.row(), start_row)..cmp::min(range_end.row(), end_row)
} else {
cmp::max(range_start.row(), start_row)
..cmp::min(range_end.row() + 1, end_row)
};
let selection = Selection {
line_height,
start_y: row_range.start as f32 * line_height - scroll_top,
lines: row_range
.into_iter()
.map(|row| {
let line_layout = &layout.line_layouts[(row - start_row) as usize];
SelectionLine {
start_x: if row == range_start.row() {
line_layout.x_for_index(range_start.column() as usize)
- scroll_left
- descent
} else {
-scroll_left
},
end_x: if row == range_end.row() {
line_layout.x_for_index(range_end.column() as usize)
- scroll_left
- descent
} else {
line_layout.width + corner_radius * 2.0
- scroll_left
- descent
},
}
})
.collect(),
};
selection.paint(canvas);
}
if view.cursors_visible() {
let cursor_position = selection.end;
if (start_row..end_row).contains(&cursor_position.row()) {
let cursor_row_layout =
&layout.line_layouts[(selection.end.row() - start_row) as usize];
cursors.push(Cursor {
x: cursor_row_layout.x_for_index(selection.end.column() as usize)
- scroll_left
- descent,
y: selection.end.row() as f32 * line_height - scroll_top,
line_height,
});
}
}
}
canvas.restore();
// Draw glyphs
canvas.set_fill_style(FillStyle::Color(ColorU::black()));
for (ix, line) in layout.line_layouts.iter().enumerate() {
let row = start_row + ix as u32;
let line_origin = vec2f(
-scroll_left - descent,
row as f32 * line_height - scroll_top,
);
line.paint(
line_origin,
rect,
&[(0..line.len, ColorU::black())],
canvas,
font_cache,
);
}
for cursor in cursors {
cursor.paint(canvas);
}
canvas.restore()
}
}
}
impl<'a> Element<'a> for BufferElement {
fn layout(
&mut self,
constraint: SizeConstraint,
_: &'a Bump,
ctx: &mut LayoutContext,
app: &AppContext,
) -> Vector2F {
let mut size = constraint.max;
if size.y().is_infinite() {
let view = self.view.as_ref(app);
size.set_y((view.max_point(app).row() + 1) as f32 * view.line_height(ctx.font_cache));
}
if size.x().is_infinite() {
unimplemented!("we don't yet handle an infinite width constraint on buffer elements");
}
let view = self.view.as_ref(app);
let font_cache = &ctx.font_cache;
let layout_cache = &ctx.text_layout_cache;
let line_height = view.line_height(font_cache);
let gutter_padding;
let gutter_width;
if view.is_gutter_visible() {
gutter_padding = view.em_width(ctx.font_cache);
match view.max_line_number_width(ctx.font_cache, ctx.text_layout_cache, app) {
Err(error) => {
log::error!("error computing max line number width: {}", error);
return size;
}
Ok(width) => gutter_width = width + gutter_padding * 2.0,
}
} else {
gutter_padding = 0.0;
gutter_width = 0.0
};
let gutter_size = vec2f(gutter_width, size.y());
let text_size = size - vec2f(gutter_width, 0.0);
let autoscroll_horizontally = view.autoscroll_vertically(size.y(), line_height, app);
let line_number_layouts = if view.is_gutter_visible() {
match view.layout_line_numbers(size.y(), ctx.font_cache, ctx.text_layout_cache, app) {
Err(error) => {
log::error!("error laying out line numbers: {}", error);
return size;
}
Ok(layouts) => layouts,
}
} else {
Vec::new()
};
let start_row = view.scroll_position().y() as u32;
let scroll_top = view.scroll_position().y() * line_height;
let end_row = ((scroll_top + size.y()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
let mut max_visible_line_width = 0.0;
let line_layouts =
match view.layout_lines(start_row..end_row, font_cache, layout_cache, app) {
Err(error) => {
log::error!("error laying out lines: {}", error);
return size;
}
Ok(layouts) => {
for line in &layouts {
if line.width > max_visible_line_width {
max_visible_line_width = line.width;
}
}
layouts
}
};
self.layout = Some(LayoutState {
size,
gutter_size,
gutter_padding,
text_size,
line_layouts,
line_number_layouts,
max_visible_line_width,
autoscroll_horizontally,
});
size
}
fn after_layout(&mut self, ctx: &mut AfterLayoutContext, app: &mut MutableAppContext) {
let layout = self.layout.as_ref().unwrap();
let view = self.view.as_ref(app);
view.clamp_scroll_left(
layout
.scroll_max(view, ctx.font_cache, ctx.text_layout_cache, app.ctx())
.x(),
);
if layout.autoscroll_horizontally {
view.autoscroll_horizontally(
view.scroll_position().y() as u32,
layout.text_size.x(),
layout.scroll_width(view, ctx.font_cache, ctx.text_layout_cache, app.ctx()),
view.em_width(ctx.font_cache),
&layout.line_layouts,
app.ctx(),
);
}
}
fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, app: &AppContext) {
let rect;
let gutter_rect;
let text_rect;
{
let layout = self.layout.as_ref().unwrap();
rect = RectF::new(origin, layout.size);
gutter_rect = RectF::new(origin, layout.gutter_size);
text_rect = RectF::new(
origin + vec2f(layout.gutter_size.x(), 0.0),
layout.text_size,
);
}
if self.view.as_ref(app).is_gutter_visible() {
self.paint_gutter(gutter_rect, ctx, app);
}
self.paint_text(text_rect, ctx, app);
self.paint = Some(PaintState { rect, text_rect });
}
fn dispatch_event(&self, event: &Event, ctx: &mut EventContext, app: &AppContext) -> bool {
match event {
Event::LeftMouseDown { position, cmd } => self.mouse_down(*position, *cmd, ctx, app),
Event::LeftMouseUp { position } => self.mouse_up(*position, ctx, app),
Event::LeftMouseDragged { position } => self.mouse_dragged(*position, ctx, app),
Event::ScrollWheel {
position,
delta,
precise,
} => self.scroll(*position, *delta, *precise, ctx, app),
Event::KeyDown { chars, .. } => self.key_down(chars, ctx, app),
}
}
fn size(&self) -> Option<pathfinder_canvas::Vector2F> {
self.layout.as_ref().map(|layout| layout.size)
}
}
struct LayoutState {
size: Vector2F,
gutter_size: Vector2F,
gutter_padding: f32,
text_size: Vector2F,
line_layouts: Vec<Arc<text_layout::Line>>,
line_number_layouts: Vec<Arc<text_layout::Line>>,
max_visible_line_width: f32,
autoscroll_horizontally: bool,
}
impl LayoutState {
fn scroll_width(
&self,
view: &BufferView,
font_cache: &FontCache,
layout_cache: &LayoutCache,
app: &AppContext,
) -> f32 {
let row = view.rightmost_point(app).row();
let longest_line_width = view
.layout_line(row, font_cache, layout_cache, app)
.unwrap()
.width;
longest_line_width.max(self.max_visible_line_width) + view.em_width(font_cache)
}
fn scroll_max(
&self,
view: &BufferView,
font_cache: &FontCache,
layout_cache: &LayoutCache,
app: &AppContext,
) -> Vector2F {
vec2f(
((self.scroll_width(view, font_cache, layout_cache, app) - self.text_size.x())
/ view.em_width(font_cache))
.max(0.0),
view.max_point(app).row().saturating_sub(1) as f32,
)
}
}
struct PaintState {
rect: RectF,
text_rect: RectF,
}
impl PaintState {
fn point_for_position(
&self,
view: &BufferView,
layout: &LayoutState,
position: Vector2F,
font_cache: &FontCache,
app: &AppContext,
) -> DisplayPoint {
let scroll_position = view.scroll_position();
let position = position - self.text_rect.origin();
let y = position.y().max(0.0).min(layout.size.y());
let row = ((y / view.line_height(font_cache)) + scroll_position.y()) as u32;
let row = cmp::min(row, view.max_point(app).row());
let line = &layout.line_layouts[(row - scroll_position.y() as u32) as usize];
let x = position.x() + (scroll_position.x() * view.em_width(font_cache));
let column = if x >= 0.0 {
line.index_for_x(x)
.map(|ix| ix as u32)
.unwrap_or(view.line_len(row, app).unwrap())
} else {
0
};
DisplayPoint::new(row, column)
}
}
struct Cursor {
x: f32,
y: f32,
line_height: f32,
}
impl Cursor {
fn paint(&self, canvas: &mut CanvasRenderingContext2D) {
canvas.set_fill_style(FillStyle::Color(ColorU::black()));
canvas.fill_rect(RectF::new(
vec2f(self.x, self.y),
vec2f(2.0, self.line_height),
));
}
}
#[derive(Debug)]
struct Selection {
start_y: f32,
line_height: f32,
lines: Vec<SelectionLine>,
}
#[derive(Debug)]
struct SelectionLine {
start_x: f32,
end_x: f32,
}
impl Selection {
fn paint(&self, canvas: &mut CanvasRenderingContext2D) {
if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x {
self.paint_lines(self.start_y, &self.lines[0..1], canvas);
self.paint_lines(self.start_y + self.line_height, &self.lines[1..], canvas);
} else {
self.paint_lines(self.start_y, &self.lines, canvas);
}
}
fn paint_lines(
&self,
start_y: f32,
lines: &[SelectionLine],
canvas: &mut CanvasRenderingContext2D,
) {
use Direction::*;
if lines.is_empty() {
return;
}
let mut path = Path2D::new();
let corner_radius = 0.08 * self.line_height;
let first_line = lines.first().unwrap();
let last_line = lines.last().unwrap();
let corner = vec2f(first_line.end_x, start_y);
path.move_to(corner - vec2f(corner_radius, 0.0));
rounded_corner(&mut path, corner, corner_radius, Right, Down);
let mut iter = lines.iter().enumerate().peekable();
while let Some((ix, line)) = iter.next() {
let corner = vec2f(line.end_x, start_y + (ix + 1) as f32 * self.line_height);
if let Some((_, next_line)) = iter.peek() {
let next_corner = vec2f(next_line.end_x, corner.y());
match next_corner.x().partial_cmp(&corner.x()).unwrap() {
Ordering::Equal => {
path.line_to(corner);
}
Ordering::Less => {
path.line_to(corner - vec2f(0.0, corner_radius));
rounded_corner(&mut path, corner, corner_radius, Down, Left);
path.line_to(next_corner + vec2f(corner_radius, 0.0));
rounded_corner(&mut path, next_corner, corner_radius, Left, Down);
}
Ordering::Greater => {
path.line_to(corner - vec2f(0.0, corner_radius));
rounded_corner(&mut path, corner, corner_radius, Down, Right);
path.line_to(next_corner - vec2f(corner_radius, 0.0));
rounded_corner(&mut path, next_corner, corner_radius, Right, Down);
}
}
} else {
path.line_to(corner - vec2f(0.0, corner_radius));
rounded_corner(&mut path, corner, corner_radius, Down, Left);
let corner = vec2f(line.start_x, corner.y());
path.line_to(corner + vec2f(corner_radius, 0.0));
rounded_corner(&mut path, corner, corner_radius, Left, Up);
}
}
if first_line.start_x > last_line.start_x {
let corner = vec2f(last_line.start_x, start_y + self.line_height);
path.line_to(corner + vec2f(0.0, corner_radius));
rounded_corner(&mut path, corner, corner_radius, Up, Right);
let corner = vec2f(first_line.start_x, corner.y());
path.line_to(corner - vec2f(corner_radius, 0.0));
rounded_corner(&mut path, corner, corner_radius, Right, Up);
}
let corner = vec2f(first_line.start_x, start_y);
path.line_to(corner + vec2f(0.0, corner_radius));
rounded_corner(&mut path, corner, corner_radius, Up, Right);
path.close_path();
canvas.set_fill_style(FillStyle::Color(
ColorF::new(0.639, 0.839, 1.0, 1.0).to_u8(),
));
canvas.fill_path(path, FillRule::Winding);
}
}
enum Direction {
Up,
Down,
Left,
Right,
}
fn rounded_corner(
path: &mut Path2D,
corner: Vector2F,
radius: f32,
incoming: Direction,
outgoing: Direction,
) {
use std::f32::consts::PI;
use Direction::*;
match (incoming, outgoing) {
(Down, Right) => path.arc(
corner + vec2f(radius, -radius),
radius,
1.0 * PI,
0.5 * PI,
ArcDirection::CCW,
),
(Down, Left) => path.arc(
corner + vec2f(-radius, -radius),
radius,
0.0,
0.5 * PI,
ArcDirection::CW,
),
(Up, Right) => path.arc(
corner + vec2f(radius, radius),
radius,
1.0 * PI,
1.5 * PI,
ArcDirection::CW,
),
(Up, Left) => path.arc(
corner + vec2f(-radius, radius),
radius,
0.0,
1.5 * PI,
ArcDirection::CCW,
),
(Right, Up) => path.arc(
corner + vec2f(-radius, -radius),
radius,
0.5 * PI,
0.0,
ArcDirection::CCW,
),
(Right, Down) => path.arc(
corner + vec2f(-radius, radius),
radius,
1.5 * PI,
2.0 * PI,
ArcDirection::CW,
),
(Left, Up) => path.arc(
corner + vec2f(radius, -radius),
radius,
0.5 * PI,
PI,
ArcDirection::CW,
),
(Left, Down) => path.arc(
corner + vec2f(radius, radius),
radius,
1.5 * PI,
PI,
ArcDirection::CCW,
),
_ => panic!("invalid incoming and outgoing directions for a corner"),
}
}
fn scale_vertical_mouse_autoscroll_delta(delta: f32) -> f32 {
delta.powf(1.5) / 100.0
}
fn scale_horizontal_mouse_autoscroll_delta(delta: f32) -> f32 {
delta.powf(1.2) / 300.0
}
File diff suppressed because it is too large Load Diff
+698
View File
@@ -0,0 +1,698 @@
use super::{
buffer, Anchor, AnchorRangeExt, Buffer, DisplayPoint, Edit, Point, TextSummary, ToOffset,
};
use crate::{
app::{AppContext, ModelHandle},
sum_tree::{self, Cursor, SumTree},
util::find_insertion_index,
};
use anyhow::{anyhow, Result};
use std::{
cmp::{self, Ordering},
iter::Take,
ops::Range,
};
use sum_tree::{Dimension, SeekBias};
pub struct FoldMap {
buffer: ModelHandle<Buffer>,
transforms: SumTree<Transform>,
folds: Vec<Range<Anchor>>,
}
impl FoldMap {
pub fn new(buffer: ModelHandle<Buffer>, app: &AppContext) -> Self {
let text_summary = buffer.as_ref(app).text_summary();
Self {
buffer,
folds: Vec::new(),
transforms: SumTree::from_item(Transform {
summary: TransformSummary {
buffer: text_summary.clone(),
display: text_summary,
},
display_text: None,
}),
}
}
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
if start_row > self.transforms.summary().display.lines.row {
return Err(anyhow!("invalid display row {}", start_row));
}
let display_point = Point::new(start_row, 0);
let mut cursor = self.transforms.cursor();
cursor.seek(&DisplayPoint(display_point), SeekBias::Left);
Ok(BufferRows {
display_point,
cursor,
})
}
pub fn len(&self) -> usize {
self.transforms.summary().display.chars
}
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
let line_start = self.to_display_offset(DisplayPoint::new(row, 0), ctx)?.0;
let line_end = if row >= self.max_point().row() {
self.len()
} else {
self.to_display_offset(DisplayPoint::new(row + 1, 0), ctx)?
.0
- 1
};
Ok((line_end - line_start) as u32)
}
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
let offset = self.to_display_offset(point, app)?;
let mut cursor = self.transforms.cursor();
cursor.seek(&offset, SeekBias::Right);
let buffer = self.buffer.as_ref(app);
Ok(Chars {
cursor,
offset: offset.0,
buffer,
buffer_chars: None,
})
}
pub fn max_point(&self) -> DisplayPoint {
DisplayPoint(self.transforms.summary().display.lines)
}
pub fn rightmost_point(&self) -> DisplayPoint {
DisplayPoint(self.transforms.summary().display.rightmost_point)
}
pub fn fold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
app: &AppContext,
) -> Result<()> {
let mut edits = Vec::new();
let buffer = self.buffer.as_ref(app);
for range in ranges.into_iter() {
let start = range.start.to_offset(buffer)?;
let end = range.end.to_offset(buffer)?;
edits.push(Edit {
old_range: start..end,
new_range: start..end,
});
let fold = buffer.anchor_after(start)?..buffer.anchor_before(end)?;
let ix = find_insertion_index(&self.folds, |probe| probe.cmp(&fold, buffer))?;
self.folds.insert(ix, fold);
}
edits.sort_unstable_by(|a, b| {
a.old_range
.start
.cmp(&b.old_range.start)
.then_with(|| b.old_range.end.cmp(&a.old_range.end))
});
self.apply_edits(&edits, app)?;
Ok(())
}
pub fn unfold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
app: &AppContext,
) -> Result<()> {
let buffer = self.buffer.as_ref(app);
let mut edits = Vec::new();
for range in ranges.into_iter() {
let start = buffer.anchor_before(range.start.to_offset(buffer)?)?;
let end = buffer.anchor_after(range.end.to_offset(buffer)?)?;
// Remove intersecting folds and add their ranges to edits that are passed to apply_edits
self.folds.retain(|fold| {
if fold.start.cmp(&end, buffer).unwrap() > Ordering::Equal
|| fold.end.cmp(&start, buffer).unwrap() < Ordering::Equal
{
true
} else {
let offset_range =
fold.start.to_offset(buffer).unwrap()..fold.end.to_offset(buffer).unwrap();
edits.push(Edit {
old_range: offset_range.clone(),
new_range: offset_range,
});
false
}
});
}
self.apply_edits(&edits, app)?;
Ok(())
}
pub fn is_line_folded(&self, display_row: u32) -> bool {
let mut cursor = self.transforms.cursor::<DisplayPoint, DisplayPoint>();
cursor.seek(&DisplayPoint::new(display_row, 0), SeekBias::Right);
while let Some(transform) = cursor.item() {
if transform.display_text.is_some() {
return true;
}
if cursor.end().row() == display_row {
cursor.next()
} else {
break;
}
}
false
}
pub fn to_display_offset(
&self,
point: DisplayPoint,
app: &AppContext,
) -> Result<DisplayOffset> {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, SeekBias::Right);
let overshoot = point.0 - cursor.start().display.lines;
let mut offset = cursor.start().display.chars;
if !overshoot.is_zero() {
let transform = cursor
.item()
.ok_or_else(|| anyhow!("display point {:?} is out of range", point))?;
assert!(transform.display_text.is_none());
let end_buffer_offset =
(cursor.start().buffer.lines + overshoot).to_offset(self.buffer.as_ref(app))?;
offset += end_buffer_offset - cursor.start().buffer.chars;
}
Ok(DisplayOffset(offset))
}
pub fn to_buffer_point(&self, display_point: DisplayPoint) -> Point {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&display_point, SeekBias::Right);
let overshoot = display_point.0 - cursor.start().display.lines;
cursor.start().buffer.lines + overshoot
}
pub fn to_display_point(&self, point: Point) -> DisplayPoint {
let mut cursor = self.transforms.cursor::<Point, TransformSummary>();
cursor.seek(&point, SeekBias::Right);
let overshoot = point - cursor.start().buffer.lines;
DisplayPoint(cmp::min(
cursor.start().display.lines + overshoot,
cursor.end().display.lines,
))
}
pub fn apply_edits(&mut self, edits: &[Edit], app: &AppContext) -> Result<()> {
let buffer = self.buffer.as_ref(app);
let mut edits = edits.iter().cloned().peekable();
let mut new_transforms = SumTree::new();
let mut cursor = self.transforms.cursor::<usize, usize>();
cursor.seek(&0, SeekBias::Right);
while let Some(mut edit) = edits.next() {
new_transforms.push_tree(cursor.slice(&edit.old_range.start, SeekBias::Left));
edit.new_range.start -= edit.old_range.start - cursor.start();
edit.old_range.start = *cursor.start();
cursor.seek(&edit.old_range.end, SeekBias::Right);
cursor.next();
let mut delta = edit.delta();
loop {
edit.old_range.end = *cursor.start();
if let Some(next_edit) = edits.peek() {
if next_edit.old_range.start > edit.old_range.end {
break;
}
let next_edit = edits.next().unwrap();
delta += next_edit.delta();
if next_edit.old_range.end > edit.old_range.end {
edit.old_range.end = next_edit.old_range.end;
cursor.seek(&edit.old_range.end, SeekBias::Right);
cursor.next();
}
} else {
break;
}
}
edit.new_range.end =
((edit.new_range.start + edit.old_extent()) as isize + delta) as usize;
let anchor = buffer.anchor_before(edit.new_range.start)?;
let folds_start =
find_insertion_index(&self.folds, |probe| probe.start.cmp(&anchor, buffer))?;
let mut folds = self.folds[folds_start..]
.iter()
.map(|fold| {
fold.start.to_offset(buffer).unwrap()..fold.end.to_offset(buffer).unwrap()
})
.peekable();
while folds
.peek()
.map_or(false, |fold| fold.start < edit.new_range.end)
{
let mut fold = folds.next().unwrap();
let sum = new_transforms.summary();
assert!(fold.start >= sum.buffer.chars);
while folds
.peek()
.map_or(false, |next_fold| next_fold.start <= fold.end)
{
let next_fold = folds.next().unwrap();
if next_fold.end > fold.end {
fold.end = next_fold.end;
}
}
if fold.start > sum.buffer.chars {
let text_summary = buffer.text_summary_for_range(sum.buffer.chars..fold.start);
new_transforms.push(Transform {
summary: TransformSummary {
display: text_summary.clone(),
buffer: text_summary,
},
display_text: None,
});
}
if fold.end > fold.start {
new_transforms.push(Transform {
summary: TransformSummary {
display: TextSummary {
chars: 1,
bytes: ''.len_utf8(),
lines: Point::new(0, 1),
first_line_len: 1,
rightmost_point: Point::new(0, 1),
},
buffer: buffer.text_summary_for_range(fold.start..fold.end),
},
display_text: Some('…'),
});
}
}
let sum = new_transforms.summary();
if sum.buffer.chars < edit.new_range.end {
let text_summary =
buffer.text_summary_for_range(sum.buffer.chars..edit.new_range.end);
new_transforms.push(Transform {
summary: TransformSummary {
display: text_summary.clone(),
buffer: text_summary,
},
display_text: None,
});
}
}
new_transforms.push_tree(cursor.suffix());
drop(cursor);
self.transforms = new_transforms;
Ok(())
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct Transform {
summary: TransformSummary,
display_text: Option<char>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct TransformSummary {
display: TextSummary,
buffer: TextSummary,
}
impl sum_tree::Item for Transform {
type Summary = TransformSummary;
fn summary(&self) -> Self::Summary {
self.summary.clone()
}
}
impl<'a> std::ops::AddAssign<&'a Self> for TransformSummary {
fn add_assign(&mut self, other: &'a Self) {
self.buffer += &other.buffer;
self.display += &other.display;
}
}
impl<'a> Dimension<'a, TransformSummary> for TransformSummary {
fn add_summary(&mut self, summary: &'a TransformSummary) {
*self += summary;
}
}
pub struct BufferRows<'a> {
cursor: Cursor<'a, Transform, DisplayPoint, TransformSummary>,
display_point: Point,
}
impl<'a> Iterator for BufferRows<'a> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
while self.display_point > self.cursor.end().display.lines {
self.cursor.next();
if self.cursor.item().is_none() {
// TODO: Return a bool from next?
break;
}
}
if self.cursor.item().is_some() {
let overshoot = self.display_point - self.cursor.start().display.lines;
let buffer_point = self.cursor.start().buffer.lines + overshoot;
self.display_point.row += 1;
Some(buffer_point.row)
} else {
None
}
}
}
pub struct Chars<'a> {
cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>,
offset: usize,
buffer: &'a Buffer,
buffer_chars: Option<Take<buffer::Chars<'a>>>,
}
impl<'a> Iterator for Chars<'a> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if let Some(c) = self.buffer_chars.as_mut().and_then(|chars| chars.next()) {
self.offset += 1;
return Some(c);
}
if self.offset == self.cursor.end().display.chars {
self.cursor.next();
}
self.cursor.item().and_then(|transform| {
if let Some(c) = transform.display_text {
self.offset += 1;
Some(c)
} else {
let overshoot = self.offset - self.cursor.start().display.chars;
let buffer_start = self.cursor.start().buffer.chars + overshoot;
let char_count = self.cursor.end().buffer.chars - buffer_start;
self.buffer_chars =
Some(self.buffer.chars_at(buffer_start).unwrap().take(char_count));
self.next()
}
})
}
}
impl<'a> Dimension<'a, TransformSummary> for DisplayPoint {
fn add_summary(&mut self, summary: &'a TransformSummary) {
self.0 += &summary.display.lines;
}
}
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
pub struct DisplayOffset(usize);
impl<'a> Dimension<'a, TransformSummary> for DisplayOffset {
fn add_summary(&mut self, summary: &'a TransformSummary) {
self.0 += &summary.display.chars;
}
}
impl<'a> Dimension<'a, TransformSummary> for Point {
fn add_summary(&mut self, summary: &'a TransformSummary) {
*self += &summary.buffer.lines;
}
}
impl<'a> Dimension<'a, TransformSummary> for usize {
fn add_summary(&mut self, summary: &'a TransformSummary) {
*self += &summary.buffer.chars;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app::App;
use crate::test_utils::sample_text;
#[test]
fn test_basic_folds() -> Result<()> {
let mut app = App::new()?;
let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6)));
let mut map = app.read(|app| FoldMap::new(buffer.clone(), app));
app.read(|app| {
map.fold(
vec![
Point::new(0, 2)..Point::new(2, 2),
Point::new(2, 4)..Point::new(4, 1),
],
app,
)?;
assert_eq!(map.text(app), "aa…cc…eeeee");
Ok::<(), anyhow::Error>(())
})?;
let edits = buffer.update(&mut app, |buffer, ctx| {
let start_version = buffer.version.clone();
buffer.edit(
vec![
Point::new(0, 0)..Point::new(0, 1),
Point::new(2, 3)..Point::new(2, 3),
],
"123",
Some(ctx),
)?;
Ok::<_, anyhow::Error>(buffer.edits_since(start_version).collect::<Vec<_>>())
})?;
app.read(|app| {
map.apply_edits(&edits, app)?;
assert_eq!(map.text(app), "123a…c123c…eeeee");
Ok::<(), anyhow::Error>(())
})?;
let edits = buffer.update(&mut app, |buffer, ctx| {
let start_version = buffer.version.clone();
buffer.edit(Some(Point::new(2, 6)..Point::new(4, 3)), "456", Some(ctx))?;
Ok::<_, anyhow::Error>(buffer.edits_since(start_version).collect::<Vec<_>>())
})?;
app.read(|app| {
map.apply_edits(&edits, app)?;
assert_eq!(map.text(app), "123a…c123456eee");
map.unfold(Some(Point::new(0, 4)..Point::new(0, 4)), app)?;
assert_eq!(map.text(app), "123aaaaa\nbbbbbb\nccc123456eee");
Ok(())
})
}
#[test]
fn test_overlapping_folds() -> Result<()> {
let mut app = App::new()?;
let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6)));
app.read(|app| {
let mut map = FoldMap::new(buffer.clone(), app);
map.fold(
vec![
Point::new(0, 2)..Point::new(2, 2),
Point::new(0, 4)..Point::new(1, 0),
Point::new(1, 2)..Point::new(3, 2),
Point::new(3, 1)..Point::new(4, 1),
],
app,
)?;
assert_eq!(map.text(app), "aa…eeeee");
Ok(())
})
}
#[test]
fn test_merging_folds_via_edit() -> Result<()> {
let mut app = App::new()?;
let buffer = app.add_model(|_| Buffer::new(0, sample_text(5, 6)));
let mut map = app.read(|app| FoldMap::new(buffer.clone(), app));
app.read(|app| {
map.fold(
vec![
Point::new(0, 2)..Point::new(2, 2),
Point::new(3, 1)..Point::new(4, 1),
],
app,
)?;
assert_eq!(map.text(app), "aa…cccc\nd…eeeee");
Ok::<(), anyhow::Error>(())
})?;
let edits = buffer.update(&mut app, |buffer, ctx| {
let start_version = buffer.version.clone();
buffer.edit(Some(Point::new(2, 2)..Point::new(3, 1)), "", Some(ctx))?;
Ok::<_, anyhow::Error>(buffer.edits_since(start_version).collect::<Vec<_>>())
})?;
app.read(|app| {
map.apply_edits(&edits, app)?;
assert_eq!(map.text(app), "aa…eeeee");
Ok(())
})
}
#[test]
fn test_random_folds() -> Result<()> {
use crate::buffer::ToPoint;
use crate::util::RandomCharIter;
use rand::prelude::*;
for seed in 0..100 {
println!("{:?}", seed);
let mut rng = StdRng::seed_from_u64(seed);
let mut app = App::new()?;
let buffer = app.add_model(|_| {
let len = rng.gen_range(0, 10);
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
Buffer::new(0, text)
});
let mut map = app.read(|app| FoldMap::new(buffer.clone(), app));
app.read(|app| {
let buffer = buffer.as_ref(app);
let fold_count = rng.gen_range(0, 10);
let mut fold_ranges: Vec<Range<usize>> = Vec::new();
for _ in 0..fold_count {
let end = rng.gen_range(0, buffer.len() + 1);
let start = rng.gen_range(0, end + 1);
fold_ranges.push(start..end);
}
map.fold(fold_ranges, app)?;
let mut expected_text = buffer.text();
for fold_range in map.merged_fold_ranges(app).into_iter().rev() {
expected_text.replace_range(fold_range.start..fold_range.end, "");
}
assert_eq!(map.text(app), expected_text);
for fold_range in map.merged_fold_ranges(app) {
let display_point =
map.to_display_point(fold_range.start.to_point(buffer).unwrap());
assert!(map.is_line_folded(display_point.row()));
}
Ok::<(), anyhow::Error>(())
})?;
let edits = buffer.update(&mut app, |buffer, ctx| {
let start_version = buffer.version.clone();
let edit_count = rng.gen_range(1, 10);
buffer.randomly_edit(&mut rng, edit_count, Some(ctx));
Ok::<_, anyhow::Error>(buffer.edits_since(start_version).collect::<Vec<_>>())
})?;
app.read(|app| {
map.apply_edits(&edits, app)?;
let buffer = map.buffer.as_ref(app);
let mut expected_text = buffer.text();
for fold_range in map.merged_fold_ranges(app).into_iter().rev() {
expected_text.replace_range(fold_range.start..fold_range.end, "");
}
assert_eq!(map.text(app), expected_text);
Ok::<(), anyhow::Error>(())
})?;
}
Ok(())
}
#[test]
fn test_buffer_rows() -> Result<()> {
let mut app = App::new()?;
let text = sample_text(6, 6) + "\n";
let buffer = app.add_model(|_| Buffer::new(0, text));
app.read(|app| {
let mut map = FoldMap::new(buffer.clone(), app);
map.fold(
vec![
Point::new(0, 2)..Point::new(2, 2),
Point::new(3, 1)..Point::new(4, 1),
],
app,
)?;
assert_eq!(map.text(app), "aa…cccc\nd…eeeee\nffffff\n");
assert_eq!(map.buffer_rows(0)?.collect::<Vec<_>>(), vec![0, 3, 5, 6]);
assert_eq!(map.buffer_rows(3)?.collect::<Vec<_>>(), vec![6]);
Ok(())
})
}
impl FoldMap {
fn text(&self, app: &AppContext) -> String {
self.chars_at(DisplayPoint(Point::zero()), app)
.unwrap()
.collect()
}
fn merged_fold_ranges(&self, app: &AppContext) -> Vec<Range<usize>> {
let buffer = self.buffer.as_ref(app);
let mut fold_ranges = self
.folds
.iter()
.map(|fold| {
fold.start.to_offset(buffer).unwrap()..fold.end.to_offset(buffer).unwrap()
})
.peekable();
let mut merged_ranges = Vec::new();
while let Some(mut fold_range) = fold_ranges.next() {
while let Some(next_range) = fold_ranges.peek() {
if fold_range.end >= next_range.start {
if next_range.end > fold_range.end {
fold_range.end = next_range.end;
}
fold_ranges.next();
} else {
break;
}
}
if fold_range.end > fold_range.start {
merged_ranges.push(fold_range);
}
}
merged_ranges
}
}
}
+375
View File
@@ -0,0 +1,375 @@
mod fold_map;
use super::ToPoint;
use super::{buffer, Anchor, AnchorRangeExt, Buffer, Edit, Point, TextSummary, ToOffset};
use crate::app::{AppContext, Entity, ModelContext, ModelHandle};
use anyhow::Result;
pub use fold_map::BufferRows;
use fold_map::FoldMap;
use std::ops::Range;
#[derive(Copy, Clone)]
pub enum Bias {
Left,
Right,
}
pub struct DisplayMap {
buffer: ModelHandle<Buffer>,
fold_map: FoldMap,
tab_size: usize,
}
impl Entity for DisplayMap {
type Event = ();
}
impl DisplayMap {
pub fn new(buffer: ModelHandle<Buffer>, tab_size: usize, ctx: &mut ModelContext<Self>) -> Self {
ctx.subscribe(&buffer, Self::handle_buffer_event);
DisplayMap {
buffer: buffer.clone(),
fold_map: FoldMap::new(buffer, ctx.app()),
tab_size,
}
}
pub fn fold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &mut ModelContext<Self>,
) -> Result<()> {
self.fold_map.fold(ranges, ctx.app())?;
ctx.notify();
Ok(())
}
pub fn unfold<T: ToOffset>(
&mut self,
ranges: impl IntoIterator<Item = Range<T>>,
ctx: &mut ModelContext<Self>,
) -> Result<()> {
self.fold_map.unfold(ranges, ctx.app())?;
ctx.notify();
Ok(())
}
pub fn is_line_folded(&self, display_row: u32) -> bool {
self.fold_map.is_line_folded(display_row)
}
pub fn text(&self, app: &AppContext) -> String {
self.chars_at(DisplayPoint::zero(), app).unwrap().collect()
}
pub fn line(&self, display_row: u32, app: &AppContext) -> Result<String> {
let chars = self.chars_at(DisplayPoint::new(display_row, 0), app)?;
Ok(chars.take_while(|c| *c != '\n').collect())
}
pub fn chars_at<'a>(&'a self, point: DisplayPoint, app: &'a AppContext) -> Result<Chars<'a>> {
let column = point.column() as usize;
let (point, to_next_stop) = point.collapse_tabs(self, Bias::Left, app)?;
let mut fold_chars = self.fold_map.chars_at(point, app)?;
if to_next_stop > 0 {
fold_chars.next();
}
Ok(Chars {
fold_chars,
column,
to_next_stop,
tab_size: self.tab_size,
})
}
pub fn buffer_rows(&self, start_row: u32) -> Result<BufferRows> {
self.fold_map.buffer_rows(start_row)
}
pub fn line_len(&self, row: u32, ctx: &AppContext) -> Result<u32> {
DisplayPoint::new(row, self.fold_map.line_len(row, ctx)?)
.expand_tabs(self, ctx)
.map(|point| point.column())
}
pub fn max_point(&self, app: &AppContext) -> DisplayPoint {
self.fold_map.max_point().expand_tabs(self, app).unwrap()
}
pub fn rightmost_point(&self) -> DisplayPoint {
self.fold_map.rightmost_point()
}
pub fn anchor_before(
&self,
point: DisplayPoint,
bias: Bias,
app: &AppContext,
) -> Result<Anchor> {
self.buffer
.as_ref(app)
.anchor_before(point.to_buffer_point(self, bias, app)?)
}
pub fn anchor_after(
&self,
point: DisplayPoint,
bias: Bias,
app: &AppContext,
) -> Result<Anchor> {
self.buffer
.as_ref(app)
.anchor_after(point.to_buffer_point(self, bias, app)?)
}
fn handle_buffer_event(&mut self, event: &buffer::Event, ctx: &mut ModelContext<Self>) {
match event {
buffer::Event::Edited(edits) => self.fold_map.apply_edits(edits, ctx.app()).unwrap(),
}
}
}
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
pub struct DisplayPoint(Point);
impl DisplayPoint {
pub fn new(row: u32, column: u32) -> Self {
Self(Point::new(row, column))
}
pub fn zero() -> Self {
Self::new(0, 0)
}
pub fn row(self) -> u32 {
self.0.row
}
pub fn column(self) -> u32 {
self.0.column
}
pub fn row_mut(&mut self) -> &mut u32 {
&mut self.0.row
}
pub fn column_mut(&mut self) -> &mut u32 {
&mut self.0.column
}
pub fn to_buffer_point(self, map: &DisplayMap, bias: Bias, app: &AppContext) -> Result<Point> {
Ok(map
.fold_map
.to_buffer_point(self.collapse_tabs(map, bias, app)?.0))
}
fn expand_tabs(mut self, map: &DisplayMap, app: &AppContext) -> Result<Self> {
let chars = map
.fold_map
.chars_at(DisplayPoint(Point::new(self.row(), 0)), app)?;
let expanded = expand_tabs(chars, self.column() as usize, map.tab_size);
*self.column_mut() = expanded as u32;
Ok(self)
}
fn collapse_tabs(
mut self,
map: &DisplayMap,
bias: Bias,
app: &AppContext,
) -> Result<(Self, usize)> {
let chars = map
.fold_map
.chars_at(DisplayPoint(Point::new(self.0.row, 0)), app)?;
let expanded = self.column() as usize;
let (collapsed, to_next_stop) = collapse_tabs(chars, expanded, bias, map.tab_size);
*self.column_mut() = collapsed as u32;
Ok((self, to_next_stop))
}
}
impl Point {
pub fn to_display_point(self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
let mut display_point = map.fold_map.to_display_point(self);
let chars = map
.fold_map
.chars_at(DisplayPoint::new(display_point.row(), 0), app)?;
*display_point.column_mut() =
expand_tabs(chars, display_point.column() as usize, map.tab_size) as u32;
Ok(display_point)
}
}
impl Anchor {
pub fn to_display_point(&self, map: &DisplayMap, app: &AppContext) -> Result<DisplayPoint> {
self.to_point(map.buffer.as_ref(app))?
.to_display_point(map, app)
}
}
pub struct Chars<'a> {
fold_chars: fold_map::Chars<'a>,
column: usize,
to_next_stop: usize,
tab_size: usize,
}
impl<'a> Iterator for Chars<'a> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if self.to_next_stop > 0 {
self.to_next_stop -= 1;
self.column += 1;
Some(' ')
} else {
self.fold_chars.next().map(|c| match c {
'\t' => {
self.to_next_stop = self.tab_size - self.column % self.tab_size - 1;
self.column += 1;
' '
}
'\n' => {
self.column = 0;
c
}
_ => {
self.column += 1;
c
}
})
}
}
}
pub fn expand_tabs(chars: impl Iterator<Item = char>, column: usize, tab_size: usize) -> usize {
let mut expanded = 0;
for c in chars.take(column) {
if c == '\t' {
expanded += tab_size - expanded % tab_size;
} else {
expanded += 1;
}
}
expanded
}
pub fn collapse_tabs(
mut chars: impl Iterator<Item = char>,
column: usize,
bias: Bias,
tab_size: usize,
) -> (usize, usize) {
let mut expanded = 0;
let mut collapsed = 0;
while let Some(c) = chars.next() {
if expanded == column {
break;
}
if c == '\t' {
expanded += tab_size - (expanded % tab_size);
if expanded > column {
return match bias {
Bias::Left => (collapsed, expanded - column),
Bias::Right => (collapsed + 1, 0),
};
}
collapsed += 1;
} else {
expanded += 1;
collapsed += 1;
}
}
(collapsed, 0)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app::App;
use crate::test_utils::*;
use anyhow::Error;
#[test]
fn test_chars_at() -> Result<()> {
let mut app = App::new()?;
let text = sample_text(6, 6);
let buffer = app.add_model(|_| Buffer::new(0, text));
let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
buffer.update(&mut app, |buffer, ctx| {
buffer.edit(
vec![
Point::new(1, 0)..Point::new(1, 0),
Point::new(1, 1)..Point::new(1, 1),
Point::new(2, 1)..Point::new(2, 1),
],
"\t",
Some(ctx),
)
})?;
map.read(&app, |map, ctx| {
assert_eq!(
map.chars_at(DisplayPoint::new(1, 0), ctx)?
.take(10)
.collect::<String>(),
" b bb"
);
assert_eq!(
map.chars_at(DisplayPoint::new(1, 2), ctx)?
.take(10)
.collect::<String>(),
" b bbbb"
);
assert_eq!(
map.chars_at(DisplayPoint::new(1, 6), ctx)?
.take(13)
.collect::<String>(),
" bbbbb\nc c"
);
Ok::<(), Error>(())
})?;
Ok(())
}
#[test]
fn test_expand_tabs() {
assert_eq!(expand_tabs("\t".chars(), 0, 4), 0);
assert_eq!(expand_tabs("\t".chars(), 1, 4), 4);
assert_eq!(expand_tabs("\ta".chars(), 2, 4), 5);
}
#[test]
fn test_collapse_tabs() {
assert_eq!(collapse_tabs("\t".chars(), 0, Bias::Left, 4), (0, 0));
assert_eq!(collapse_tabs("\t".chars(), 0, Bias::Right, 4), (0, 0));
assert_eq!(collapse_tabs("\t".chars(), 1, Bias::Left, 4), (0, 3));
assert_eq!(collapse_tabs("\t".chars(), 1, Bias::Right, 4), (1, 0));
assert_eq!(collapse_tabs("\t".chars(), 2, Bias::Left, 4), (0, 2));
assert_eq!(collapse_tabs("\t".chars(), 2, Bias::Right, 4), (1, 0));
assert_eq!(collapse_tabs("\t".chars(), 3, Bias::Left, 4), (0, 1));
assert_eq!(collapse_tabs("\t".chars(), 3, Bias::Right, 4), (1, 0));
assert_eq!(collapse_tabs("\t".chars(), 4, Bias::Left, 4), (1, 0));
assert_eq!(collapse_tabs("\t".chars(), 4, Bias::Right, 4), (1, 0));
assert_eq!(collapse_tabs("\ta".chars(), 5, Bias::Left, 4), (2, 0));
assert_eq!(collapse_tabs("\ta".chars(), 5, Bias::Right, 4), (2, 0));
}
#[test]
fn test_max_point() -> Result<()> {
let mut app = App::new()?;
let buffer = app.add_model(|_| Buffer::new(0, "aaa\n\t\tbbb"));
let map = app.add_model(|ctx| DisplayMap::new(buffer.clone(), 4, ctx));
map.read(&app, |map, app| {
assert_eq!(map.max_point(app), DisplayPoint::new(1, 11))
});
Ok(())
}
}
+25
View File
@@ -0,0 +1,25 @@
mod buffer;
mod buffer_element;
pub mod buffer_view;
pub mod display_map;
pub mod movement;
pub use buffer::*;
pub use buffer_element::*;
pub use buffer_view::*;
pub use display_map::DisplayPoint;
use display_map::*;
use std::{cmp, ops::Range};
trait RangeExt<T> {
fn sorted(&self) -> (T, T);
}
impl<T: Ord + Clone> RangeExt<T> for Range<T> {
fn sorted(&self) -> (T, T) {
(
cmp::min(&self.start, &self.end).clone(),
cmp::max(&self.start, &self.end).clone(),
)
}
}
+60
View File
@@ -0,0 +1,60 @@
use super::{DisplayMap, DisplayPoint};
use crate::app::AppContext;
use anyhow::Result;
use std::cmp;
pub fn left(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
if point.column() > 0 {
*point.column_mut() -= 1;
} else if point.row() > 0 {
*point.row_mut() -= 1;
*point.column_mut() = map.line_len(point.row(), app)?;
}
Ok(point)
}
pub fn right(map: &DisplayMap, mut point: DisplayPoint, app: &AppContext) -> Result<DisplayPoint> {
let max_column = map.line_len(point.row(), app).unwrap();
if point.column() < max_column {
*point.column_mut() += 1;
} else if point.row() < map.max_point(app).row() {
*point.row_mut() += 1;
*point.column_mut() = 0;
}
Ok(point)
}
pub fn up(
map: &DisplayMap,
mut point: DisplayPoint,
goal_column: Option<u32>,
app: &AppContext,
) -> Result<(DisplayPoint, Option<u32>)> {
let goal_column = goal_column.or(Some(point.column()));
if point.row() > 0 {
*point.row_mut() -= 1;
*point.column_mut() = cmp::min(goal_column.unwrap(), map.line_len(point.row(), app)?);
} else {
point = DisplayPoint::new(0, 0);
}
Ok((point, goal_column))
}
pub fn down(
map: &DisplayMap,
mut point: DisplayPoint,
goal_column: Option<u32>,
app: &AppContext,
) -> Result<(DisplayPoint, Option<u32>)> {
let goal_column = goal_column.or(Some(point.column()));
let max_point = map.max_point(app);
if point.row() < max_point.row() {
*point.row_mut() += 1;
*point.column_mut() = cmp::min(goal_column.unwrap(), map.line_len(point.row(), app)?)
} else {
point = max_point;
}
Ok((point, goal_column))
}
+3
View File
@@ -0,0 +1,3 @@
// mod editor;
mod sum_tree;
mod time;
+752
View File
@@ -0,0 +1,752 @@
use super::*;
use arrayvec::ArrayVec;
use std::{cmp::Ordering, sync::Arc};
#[derive(Clone)]
struct StackEntry<'a, T: Item, S, U> {
tree: &'a SumTree<T>,
index: usize,
seek_dimension: S,
sum_dimension: U,
}
#[derive(Clone)]
pub struct Cursor<'a, T: Item, S, U> {
tree: &'a SumTree<T>,
stack: ArrayVec<[StackEntry<'a, T, S, U>; 16]>,
seek_dimension: S,
sum_dimension: U,
did_seek: bool,
at_end: bool,
}
impl<'a, T, S, U> Cursor<'a, T, S, U>
where
T: Item,
S: Dimension<'a, T::Summary>,
U: Dimension<'a, T::Summary>,
{
pub fn new(tree: &'a SumTree<T>) -> Self {
Self {
tree,
stack: ArrayVec::new(),
seek_dimension: S::default(),
sum_dimension: U::default(),
did_seek: false,
at_end: false,
}
}
fn reset(&mut self) {
self.did_seek = false;
self.at_end = false;
self.stack.truncate(0);
self.seek_dimension = S::default();
self.sum_dimension = U::default();
}
pub fn start(&self) -> &U {
&self.sum_dimension
}
pub fn end(&self) -> U {
if let Some(item_summary) = self.item_summary() {
let mut end = self.start().clone();
end.add_summary(item_summary);
end
} else {
self.start().clone()
}
}
pub fn item(&self) -> Option<&'a T> {
assert!(self.did_seek, "Must seek before calling this method");
if let Some(entry) = self.stack.last() {
match *entry.tree.0 {
Node::Leaf { ref items, .. } => {
if entry.index == items.len() {
None
} else {
Some(&items[entry.index])
}
}
_ => unreachable!(),
}
} else {
None
}
}
fn item_summary(&self) -> Option<&'a T::Summary> {
assert!(self.did_seek, "Must seek before calling this method");
if let Some(entry) = self.stack.last() {
match *entry.tree.0 {
Node::Leaf {
ref item_summaries, ..
} => {
if entry.index == item_summaries.len() {
None
} else {
Some(&item_summaries[entry.index])
}
}
_ => unreachable!(),
}
} else {
None
}
}
pub fn prev_item(&self) -> Option<&'a T> {
assert!(self.did_seek, "Must seek before calling this method");
if let Some(entry) = self.stack.last() {
if entry.index == 0 {
if let Some(prev_leaf) = self.prev_leaf() {
Some(prev_leaf.0.items().last().unwrap())
} else {
None
}
} else {
match *entry.tree.0 {
Node::Leaf { ref items, .. } => Some(&items[entry.index - 1]),
_ => unreachable!(),
}
}
} else if self.at_end {
self.tree.last()
} else {
None
}
}
fn prev_leaf(&self) -> Option<&'a SumTree<T>> {
for entry in self.stack.iter().rev().skip(1) {
if entry.index != 0 {
match *entry.tree.0 {
Node::Internal {
ref child_trees, ..
} => return Some(child_trees[entry.index - 1].rightmost_leaf()),
Node::Leaf { .. } => unreachable!(),
};
}
}
None
}
pub fn prev(&mut self) {
assert!(self.did_seek, "Must seek before calling this method");
if self.at_end {
self.seek_dimension = S::default();
self.sum_dimension = U::default();
self.descend_to_last_item(self.tree);
self.at_end = false;
} else {
while let Some(entry) = self.stack.pop() {
if entry.index > 0 {
let new_index = entry.index - 1;
if let Some(StackEntry {
seek_dimension,
sum_dimension,
..
}) = self.stack.last()
{
self.seek_dimension = seek_dimension.clone();
self.sum_dimension = sum_dimension.clone();
} else {
self.seek_dimension = S::default();
self.sum_dimension = U::default();
}
match entry.tree.0.as_ref() {
Node::Internal {
child_trees,
child_summaries,
..
} => {
for summary in &child_summaries[0..new_index] {
self.seek_dimension.add_summary(summary);
self.sum_dimension.add_summary(summary);
}
self.stack.push(StackEntry {
tree: entry.tree,
index: new_index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
self.descend_to_last_item(&child_trees[new_index]);
}
Node::Leaf { item_summaries, .. } => {
for item_summary in &item_summaries[0..new_index] {
self.seek_dimension.add_summary(item_summary);
self.sum_dimension.add_summary(item_summary);
}
self.stack.push(StackEntry {
tree: entry.tree,
index: new_index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
}
}
break;
}
}
}
}
pub fn next(&mut self) {
self.next_internal(|_| true)
}
fn next_internal<F>(&mut self, filter_node: F)
where
F: Fn(&T::Summary) -> bool,
{
assert!(self.did_seek, "Must seek before calling this method");
if self.stack.is_empty() {
if !self.at_end {
self.descend_to_first_item(self.tree, filter_node);
}
} else {
while self.stack.len() > 0 {
let new_subtree = {
let entry = self.stack.last_mut().unwrap();
match entry.tree.0.as_ref() {
Node::Internal {
child_trees,
child_summaries,
..
} => {
while entry.index < child_summaries.len() {
entry
.seek_dimension
.add_summary(&child_summaries[entry.index]);
entry
.sum_dimension
.add_summary(&child_summaries[entry.index]);
entry.index += 1;
if let Some(next_summary) = child_summaries.get(entry.index) {
if filter_node(next_summary) {
break;
} else {
self.seek_dimension.add_summary(next_summary);
self.sum_dimension.add_summary(next_summary);
}
}
}
child_trees.get(entry.index)
}
Node::Leaf { item_summaries, .. } => loop {
let item_summary = &item_summaries[entry.index];
self.seek_dimension.add_summary(item_summary);
entry.seek_dimension.add_summary(item_summary);
self.sum_dimension.add_summary(item_summary);
entry.sum_dimension.add_summary(item_summary);
entry.index += 1;
if let Some(next_item_summary) = item_summaries.get(entry.index) {
if filter_node(next_item_summary) {
return;
}
} else {
break None;
}
},
}
};
if let Some(subtree) = new_subtree {
self.descend_to_first_item(subtree, filter_node);
break;
} else {
self.stack.pop();
}
}
}
self.at_end = self.stack.is_empty();
}
pub fn descend_to_first_item<F>(&mut self, mut subtree: &'a SumTree<T>, filter_node: F)
where
F: Fn(&T::Summary) -> bool,
{
self.did_seek = true;
loop {
subtree = match *subtree.0 {
Node::Internal {
ref child_trees,
ref child_summaries,
..
} => {
let mut new_index = None;
for (index, summary) in child_summaries.iter().enumerate() {
if filter_node(summary) {
new_index = Some(index);
break;
}
self.seek_dimension.add_summary(summary);
self.sum_dimension.add_summary(summary);
}
if let Some(new_index) = new_index {
self.stack.push(StackEntry {
tree: subtree,
index: new_index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
&child_trees[new_index]
} else {
break;
}
}
Node::Leaf {
ref item_summaries, ..
} => {
let mut new_index = None;
for (index, item_summary) in item_summaries.iter().enumerate() {
if filter_node(item_summary) {
new_index = Some(index);
break;
}
self.seek_dimension.add_summary(item_summary);
self.sum_dimension.add_summary(item_summary);
}
if let Some(new_index) = new_index {
self.stack.push(StackEntry {
tree: subtree,
index: new_index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
}
break;
}
}
}
}
fn descend_to_last_item(&mut self, mut subtree: &'a SumTree<T>) {
self.did_seek = true;
loop {
match subtree.0.as_ref() {
Node::Internal {
child_trees,
child_summaries,
..
} => {
for summary in &child_summaries[0..child_summaries.len() - 1] {
self.seek_dimension.add_summary(summary);
self.sum_dimension.add_summary(summary);
}
self.stack.push(StackEntry {
tree: subtree,
index: child_trees.len() - 1,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
subtree = child_trees.last().unwrap();
}
Node::Leaf { item_summaries, .. } => {
let last_index = item_summaries.len().saturating_sub(1);
for item_summary in &item_summaries[0..last_index] {
self.seek_dimension.add_summary(item_summary);
self.sum_dimension.add_summary(item_summary);
}
self.stack.push(StackEntry {
tree: subtree,
index: last_index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
break;
}
}
}
}
}
impl<'a, T, S, U> Cursor<'a, T, S, U>
where
T: Item,
S: Dimension<'a, T::Summary> + Ord,
U: Dimension<'a, T::Summary>,
{
pub fn seek(&mut self, pos: &S, bias: SeekBias) -> bool {
self.reset();
self.seek_internal::<()>(pos, bias, &mut SeekAggregate::None)
}
pub fn seek_forward(&mut self, pos: &S, bias: SeekBias) -> bool {
self.seek_internal::<()>(pos, bias, &mut SeekAggregate::None)
}
pub fn slice(&mut self, end: &S, bias: SeekBias) -> SumTree<T> {
let mut slice = SeekAggregate::Slice(SumTree::new());
self.seek_internal::<()>(end, bias, &mut slice);
if let SeekAggregate::Slice(slice) = slice {
slice
} else {
unreachable!()
}
}
pub fn suffix(&mut self) -> SumTree<T> {
let extent = self.tree.extent::<S>();
let mut slice = SeekAggregate::Slice(SumTree::new());
self.seek_internal::<()>(&extent, SeekBias::Right, &mut slice);
if let SeekAggregate::Slice(slice) = slice {
slice
} else {
unreachable!()
}
}
pub fn summary<D>(&mut self, end: &S, bias: SeekBias) -> D
where
D: Dimension<'a, T::Summary>,
{
let mut summary = SeekAggregate::Summary(D::default());
self.seek_internal(end, bias, &mut summary);
if let SeekAggregate::Summary(summary) = summary {
summary
} else {
unreachable!()
}
}
fn seek_internal<D>(
&mut self,
target: &S,
bias: SeekBias,
aggregate: &mut SeekAggregate<T, D>,
) -> bool
where
D: Dimension<'a, T::Summary>,
{
debug_assert!(target >= &self.seek_dimension);
let mut containing_subtree = None;
if self.did_seek {
'outer: while let Some(entry) = self.stack.last_mut() {
{
match *entry.tree.0 {
Node::Internal {
ref child_summaries,
ref child_trees,
..
} => {
entry.index += 1;
for (child_tree, child_summary) in child_trees[entry.index..]
.iter()
.zip(&child_summaries[entry.index..])
{
let mut child_end = self.seek_dimension.clone();
child_end.add_summary(&child_summary);
let comparison = target.cmp(&child_end);
if comparison == Ordering::Greater
|| (comparison == Ordering::Equal && bias == SeekBias::Right)
{
self.seek_dimension.add_summary(child_summary);
self.sum_dimension.add_summary(child_summary);
match aggregate {
SeekAggregate::None => {}
SeekAggregate::Slice(slice) => {
slice.push_tree(child_tree.clone());
}
SeekAggregate::Summary(summary) => {
summary.add_summary(child_summary);
}
}
entry.index += 1;
} else {
containing_subtree = Some(child_tree);
break 'outer;
}
}
}
Node::Leaf {
ref items,
ref item_summaries,
..
} => {
let mut slice_items = ArrayVec::<[T; 2 * TREE_BASE]>::new();
let mut slice_item_summaries =
ArrayVec::<[T::Summary; 2 * TREE_BASE]>::new();
let mut slice_items_summary = match aggregate {
SeekAggregate::Slice(_) => Some(T::Summary::default()),
_ => None,
};
for (item, item_summary) in items[entry.index..]
.iter()
.zip(&item_summaries[entry.index..])
{
let mut item_end = self.seek_dimension.clone();
item_end.add_summary(item_summary);
let comparison = target.cmp(&item_end);
if comparison == Ordering::Greater
|| (comparison == Ordering::Equal && bias == SeekBias::Right)
{
self.seek_dimension.add_summary(item_summary);
self.sum_dimension.add_summary(item_summary);
match aggregate {
SeekAggregate::None => {}
SeekAggregate::Slice(_) => {
slice_items.push(item.clone());
slice_item_summaries.push(item_summary.clone());
*slice_items_summary.as_mut().unwrap() += item_summary;
}
SeekAggregate::Summary(summary) => {
summary.add_summary(item_summary);
}
}
entry.index += 1;
} else {
if let SeekAggregate::Slice(slice) = aggregate {
slice.push_tree(SumTree(Arc::new(Node::Leaf {
summary: slice_items_summary.unwrap(),
items: slice_items,
item_summaries: slice_item_summaries,
})));
}
break 'outer;
}
}
if let SeekAggregate::Slice(slice) = aggregate {
if !slice_items.is_empty() {
slice.push_tree(SumTree(Arc::new(Node::Leaf {
summary: slice_items_summary.unwrap(),
items: slice_items,
item_summaries: slice_item_summaries,
})));
}
}
}
}
}
self.stack.pop();
}
} else {
self.did_seek = true;
containing_subtree = Some(self.tree);
}
if let Some(mut subtree) = containing_subtree {
loop {
let mut next_subtree = None;
match *subtree.0 {
Node::Internal {
ref child_summaries,
ref child_trees,
..
} => {
for (index, (child_tree, child_summary)) in
child_trees.iter().zip(child_summaries).enumerate()
{
let mut child_end = self.seek_dimension.clone();
child_end.add_summary(child_summary);
let comparison = target.cmp(&child_end);
if comparison == Ordering::Greater
|| (comparison == Ordering::Equal && bias == SeekBias::Right)
{
self.seek_dimension.add_summary(child_summary);
self.sum_dimension.add_summary(child_summary);
match aggregate {
SeekAggregate::None => {}
SeekAggregate::Slice(slice) => {
slice.push_tree(child_trees[index].clone());
}
SeekAggregate::Summary(summary) => {
summary.add_summary(child_summary);
}
}
} else {
self.stack.push(StackEntry {
tree: subtree,
index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
next_subtree = Some(child_tree);
break;
}
}
}
Node::Leaf {
ref items,
ref item_summaries,
..
} => {
let mut slice_items = ArrayVec::<[T; 2 * TREE_BASE]>::new();
let mut slice_item_summaries =
ArrayVec::<[T::Summary; 2 * TREE_BASE]>::new();
let mut slice_items_summary = match aggregate {
SeekAggregate::Slice(_) => Some(T::Summary::default()),
_ => None,
};
for (index, (item, item_summary)) in
items.iter().zip(item_summaries).enumerate()
{
let mut child_end = self.seek_dimension.clone();
child_end.add_summary(item_summary);
let comparison = target.cmp(&child_end);
if comparison == Ordering::Greater
|| (comparison == Ordering::Equal && bias == SeekBias::Right)
{
self.seek_dimension.add_summary(item_summary);
self.sum_dimension.add_summary(item_summary);
match aggregate {
SeekAggregate::None => {}
SeekAggregate::Slice(_) => {
slice_items.push(item.clone());
*slice_items_summary.as_mut().unwrap() += item_summary;
slice_item_summaries.push(item_summary.clone());
}
SeekAggregate::Summary(summary) => {
summary.add_summary(item_summary);
}
}
} else {
self.stack.push(StackEntry {
tree: subtree,
index,
seek_dimension: self.seek_dimension.clone(),
sum_dimension: self.sum_dimension.clone(),
});
break;
}
}
if let SeekAggregate::Slice(slice) = aggregate {
if !slice_items.is_empty() {
slice.push_tree(SumTree(Arc::new(Node::Leaf {
summary: slice_items_summary.unwrap(),
items: slice_items,
item_summaries: slice_item_summaries,
})));
}
}
}
};
if let Some(next_subtree) = next_subtree {
subtree = next_subtree;
} else {
break;
}
}
}
self.at_end = self.stack.is_empty();
if bias == SeekBias::Left {
let mut end = self.seek_dimension.clone();
if let Some(summary) = self.item_summary() {
end.add_summary(summary);
}
*target == end
} else {
*target == self.seek_dimension
}
}
}
impl<'a, T, S, U> Iterator for Cursor<'a, T, S, U>
where
T: Item,
S: Dimension<'a, T::Summary>,
U: Dimension<'a, T::Summary>,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if !self.did_seek {
self.descend_to_first_item(self.tree, |_| true);
}
if let Some(item) = self.item() {
self.next();
Some(item)
} else {
None
}
}
}
pub struct FilterCursor<'a, F: Fn(&T::Summary) -> bool, T: Item, U> {
cursor: Cursor<'a, T, (), U>,
filter_node: F,
}
impl<'a, F, T, U> FilterCursor<'a, F, T, U>
where
F: Fn(&T::Summary) -> bool,
T: Item,
U: Dimension<'a, T::Summary>,
{
pub fn new(tree: &'a SumTree<T>, filter_node: F) -> Self {
let mut cursor = tree.cursor::<(), U>();
if filter_node(&tree.summary()) {
cursor.descend_to_first_item(tree, &filter_node);
} else {
cursor.did_seek = true;
cursor.at_end = true;
}
Self {
cursor,
filter_node,
}
}
pub fn start(&self) -> &U {
self.cursor.start()
}
pub fn item(&self) -> Option<&'a T> {
self.cursor.item()
}
pub fn next(&mut self) {
self.cursor.next_internal(&self.filter_node);
}
}
impl<'a, F, T, U> Iterator for FilterCursor<'a, F, T, U>
where
F: Fn(&T::Summary) -> bool,
T: Item,
U: Dimension<'a, T::Summary>,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
if let Some(item) = self.item() {
self.cursor.next_internal(&self.filter_node);
Some(item)
} else {
None
}
}
}
enum SeekAggregate<T: Item, D> {
None,
Slice(SumTree<T>),
Summary(D),
}
+820
View File
@@ -0,0 +1,820 @@
mod cursor;
use arrayvec::ArrayVec;
pub use cursor::Cursor;
pub use cursor::FilterCursor;
use std::{fmt, iter::FromIterator, ops::AddAssign, sync::Arc};
#[cfg(test)]
const TREE_BASE: usize = 2;
#[cfg(not(test))]
const TREE_BASE: usize = 6;
pub trait Item: Clone + Eq + fmt::Debug {
type Summary: for<'a> AddAssign<&'a Self::Summary> + Default + Clone + fmt::Debug;
fn summary(&self) -> Self::Summary;
}
pub trait KeyedItem: Item {
type Key: for<'a> Dimension<'a, Self::Summary> + Ord;
fn key(&self) -> Self::Key;
}
pub trait Dimension<'a, Summary: Default>: 'a + Clone + fmt::Debug + Default {
fn add_summary(&mut self, summary: &'a Summary);
}
impl<'a, T: Default> Dimension<'a, T> for () {
fn add_summary(&mut self, _: &'a T) {}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum SeekBias {
Left,
Right,
}
#[derive(Debug, Clone)]
pub struct SumTree<T: Item>(Arc<Node<T>>);
impl<T: Item> SumTree<T> {
pub fn new() -> Self {
SumTree(Arc::new(Node::Leaf {
summary: T::Summary::default(),
items: ArrayVec::new(),
item_summaries: ArrayVec::new(),
}))
}
pub fn from_item(item: T) -> Self {
let mut tree = Self::new();
tree.push(item);
tree
}
pub fn items(&self) -> Vec<T> {
let mut cursor = self.cursor::<(), ()>();
cursor.descend_to_first_item(self, |_| true);
cursor.cloned().collect()
}
pub fn cursor<'a, S, U>(&'a self) -> Cursor<T, S, U>
where
S: Dimension<'a, T::Summary>,
U: Dimension<'a, T::Summary>,
{
Cursor::new(self)
}
pub fn filter<'a, F, U>(&'a self, filter_node: F) -> FilterCursor<F, T, U>
where
F: Fn(&T::Summary) -> bool,
U: Dimension<'a, T::Summary>,
{
FilterCursor::new(self, filter_node)
}
#[allow(dead_code)]
pub fn first(&self) -> Option<&T> {
self.leftmost_leaf().0.items().first()
}
pub fn last(&self) -> Option<&T> {
self.rightmost_leaf().0.items().last()
}
pub fn extent<'a, D: Dimension<'a, T::Summary>>(&'a self) -> D {
let mut extent = D::default();
match self.0.as_ref() {
Node::Internal { summary, .. } | Node::Leaf { summary, .. } => {
extent.add_summary(summary)
}
}
extent
}
pub fn summary(&self) -> T::Summary {
match self.0.as_ref() {
Node::Internal { summary, .. } => summary.clone(),
Node::Leaf { summary, .. } => summary.clone(),
}
}
#[cfg(test)]
pub fn is_empty(&self) -> bool {
match self.0.as_ref() {
Node::Internal { .. } => false,
Node::Leaf { items, .. } => items.is_empty(),
}
}
pub fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = T>,
{
let mut leaf: Option<Node<T>> = None;
for item in iter {
if leaf.is_some() && leaf.as_ref().unwrap().items().len() == 2 * TREE_BASE {
self.push_tree(SumTree(Arc::new(leaf.take().unwrap())));
}
if leaf.is_none() {
leaf = Some(Node::Leaf::<T> {
summary: T::Summary::default(),
items: ArrayVec::new(),
item_summaries: ArrayVec::new(),
});
}
if let Some(Node::Leaf {
summary,
items,
item_summaries,
}) = leaf.as_mut()
{
let item_summary = item.summary();
*summary += &item_summary;
items.push(item);
item_summaries.push(item_summary);
} else {
unreachable!()
}
}
if leaf.is_some() {
self.push_tree(SumTree(Arc::new(leaf.take().unwrap())));
}
}
pub fn push(&mut self, item: T) {
let summary = item.summary();
self.push_tree(SumTree::from_child_trees(vec![SumTree(Arc::new(
Node::Leaf {
summary: summary.clone(),
items: ArrayVec::from_iter(Some(item)),
item_summaries: ArrayVec::from_iter(Some(summary)),
},
))]))
}
pub fn push_tree(&mut self, other: Self) {
let other_node = other.0.clone();
if !other_node.is_leaf() || other_node.items().len() > 0 {
if self.0.height() < other_node.height() {
for tree in other_node.child_trees() {
self.push_tree(tree.clone());
}
} else if let Some(split_tree) = self.push_tree_recursive(other) {
*self = Self::from_child_trees(vec![self.clone(), split_tree]);
}
}
}
fn push_tree_recursive(&mut self, other: SumTree<T>) -> Option<SumTree<T>> {
match Arc::make_mut(&mut self.0) {
Node::Internal {
height,
summary,
child_summaries,
child_trees,
..
} => {
let other_node = other.0.clone();
*summary += other_node.summary();
let height_delta = *height - other_node.height();
let mut summaries_to_append = ArrayVec::<[T::Summary; 2 * TREE_BASE]>::new();
let mut trees_to_append = ArrayVec::<[SumTree<T>; 2 * TREE_BASE]>::new();
if height_delta == 0 {
summaries_to_append.extend(other_node.child_summaries().iter().cloned());
trees_to_append.extend(other_node.child_trees().iter().cloned());
} else if height_delta == 1 && !other_node.is_underflowing() {
summaries_to_append.push(other_node.summary().clone());
trees_to_append.push(other)
} else {
let tree_to_append = child_trees.last_mut().unwrap().push_tree_recursive(other);
*child_summaries.last_mut().unwrap() =
child_trees.last().unwrap().0.summary().clone();
if let Some(split_tree) = tree_to_append {
summaries_to_append.push(split_tree.0.summary().clone());
trees_to_append.push(split_tree);
}
}
let child_count = child_trees.len() + trees_to_append.len();
if child_count > 2 * TREE_BASE {
let left_summaries: ArrayVec<_>;
let right_summaries: ArrayVec<_>;
let left_trees;
let right_trees;
let midpoint = (child_count + child_count % 2) / 2;
{
let mut all_summaries = child_summaries
.iter()
.chain(summaries_to_append.iter())
.cloned();
left_summaries = all_summaries.by_ref().take(midpoint).collect();
right_summaries = all_summaries.collect();
let mut all_trees =
child_trees.iter().chain(trees_to_append.iter()).cloned();
left_trees = all_trees.by_ref().take(midpoint).collect();
right_trees = all_trees.collect();
}
*summary = sum(left_summaries.iter());
*child_summaries = left_summaries;
*child_trees = left_trees;
Some(SumTree(Arc::new(Node::Internal {
height: *height,
summary: sum(right_summaries.iter()),
child_summaries: right_summaries,
child_trees: right_trees,
})))
} else {
child_summaries.extend(summaries_to_append);
child_trees.extend(trees_to_append);
None
}
}
Node::Leaf {
summary,
items,
item_summaries,
} => {
let other_node = other.0;
let child_count = items.len() + other_node.items().len();
if child_count > 2 * TREE_BASE {
let left_items;
let right_items;
let left_summaries;
let right_summaries: ArrayVec<[T::Summary; 2 * TREE_BASE]>;
let midpoint = (child_count + child_count % 2) / 2;
{
let mut all_items = items.iter().chain(other_node.items().iter()).cloned();
left_items = all_items.by_ref().take(midpoint).collect();
right_items = all_items.collect();
let mut all_summaries = item_summaries
.iter()
.chain(other_node.child_summaries())
.cloned();
left_summaries = all_summaries.by_ref().take(midpoint).collect();
right_summaries = all_summaries.collect();
}
*items = left_items;
*item_summaries = left_summaries;
*summary = sum(item_summaries.iter());
Some(SumTree(Arc::new(Node::Leaf {
items: right_items,
summary: sum(right_summaries.iter()),
item_summaries: right_summaries,
})))
} else {
*summary += other_node.summary();
items.extend(other_node.items().iter().cloned());
item_summaries.extend(other_node.child_summaries().iter().cloned());
None
}
}
}
}
fn from_child_trees(child_trees: Vec<SumTree<T>>) -> Self {
let height = child_trees[0].0.height() + 1;
let mut child_summaries = ArrayVec::new();
for child in &child_trees {
child_summaries.push(child.0.summary().clone());
}
let summary = sum(child_summaries.iter());
SumTree(Arc::new(Node::Internal {
height,
summary,
child_summaries,
child_trees: ArrayVec::from_iter(child_trees),
}))
}
fn leftmost_leaf(&self) -> &Self {
match *self.0 {
Node::Leaf { .. } => self,
Node::Internal {
ref child_trees, ..
} => child_trees.first().unwrap().leftmost_leaf(),
}
}
fn rightmost_leaf(&self) -> &Self {
match *self.0 {
Node::Leaf { .. } => self,
Node::Internal {
ref child_trees, ..
} => child_trees.last().unwrap().rightmost_leaf(),
}
}
}
impl<T: KeyedItem> SumTree<T> {
pub fn insert(&mut self, item: T) {
*self = {
let mut cursor = self.cursor::<T::Key, ()>();
let mut new_tree = cursor.slice(&item.key(), SeekBias::Left);
new_tree.push(item);
new_tree.push_tree(cursor.suffix());
new_tree
};
}
pub fn edit(&mut self, edits: &mut [Edit<T>]) {
if edits.is_empty() {
return;
}
edits.sort_unstable_by_key(|item| item.key());
*self = {
let mut cursor = self.cursor::<T::Key, ()>();
let mut new_tree = SumTree::new();
let mut buffered_items = Vec::new();
cursor.seek(&T::Key::default(), SeekBias::Left);
for edit in edits {
let new_key = edit.key();
let mut old_item = cursor.item();
if old_item
.as_ref()
.map_or(false, |old_item| old_item.key() < new_key)
{
new_tree.extend(buffered_items.drain(..));
let slice = cursor.slice(&new_key, SeekBias::Left);
new_tree.push_tree(slice);
old_item = cursor.item();
}
if old_item.map_or(false, |old_item| old_item.key() == new_key) {
cursor.next();
}
match edit {
Edit::Insert(item) => {
buffered_items.push(item.clone());
}
Edit::Remove(_) => {}
}
}
new_tree.extend(buffered_items);
new_tree.push_tree(cursor.suffix());
new_tree
};
}
}
#[derive(Clone, Debug)]
pub enum Node<T: Item> {
Internal {
height: u8,
summary: T::Summary,
child_summaries: ArrayVec<[T::Summary; 2 * TREE_BASE]>,
child_trees: ArrayVec<[SumTree<T>; 2 * TREE_BASE]>,
},
Leaf {
summary: T::Summary,
items: ArrayVec<[T; 2 * TREE_BASE]>,
item_summaries: ArrayVec<[T::Summary; 2 * TREE_BASE]>,
},
}
impl<T: Item> Node<T> {
fn is_leaf(&self) -> bool {
match self {
Node::Leaf { .. } => true,
_ => false,
}
}
fn height(&self) -> u8 {
match self {
Node::Internal { height, .. } => *height,
Node::Leaf { .. } => 0,
}
}
fn summary(&self) -> &T::Summary {
match self {
Node::Internal { summary, .. } => summary,
Node::Leaf { summary, .. } => summary,
}
}
fn child_summaries(&self) -> &[T::Summary] {
match self {
Node::Internal {
child_summaries, ..
} => child_summaries.as_slice(),
Node::Leaf { item_summaries, .. } => item_summaries.as_slice(),
}
}
fn child_trees(&self) -> &ArrayVec<[SumTree<T>; 2 * TREE_BASE]> {
match self {
Node::Internal { child_trees, .. } => child_trees,
Node::Leaf { .. } => panic!("Leaf nodes have no child trees"),
}
}
fn items(&self) -> &ArrayVec<[T; 2 * TREE_BASE]> {
match self {
Node::Leaf { items, .. } => items,
Node::Internal { .. } => panic!("Internal nodes have no items"),
}
}
fn is_underflowing(&self) -> bool {
match self {
Node::Internal { child_trees, .. } => child_trees.len() < TREE_BASE,
Node::Leaf { items, .. } => items.len() < TREE_BASE,
}
}
}
#[derive(Debug)]
pub enum Edit<T: KeyedItem> {
Insert(T),
Remove(T),
}
impl<T: KeyedItem> Edit<T> {
fn key(&self) -> T::Key {
match self {
Edit::Insert(item) | Edit::Remove(item) => item.key(),
}
}
}
fn sum<'a, T, I>(iter: I) -> T
where
T: 'a + Default + AddAssign<&'a T>,
I: Iterator<Item = &'a T>,
{
let mut sum = T::default();
for value in iter {
sum += value;
}
sum
}
#[cfg(test)]
mod tests {
use super::*;
use std::ops::Add;
#[test]
fn test_extend_and_push_tree() {
let mut tree1 = SumTree::new();
tree1.extend(0..20);
let mut tree2 = SumTree::new();
tree2.extend(50..100);
tree1.push_tree(tree2);
assert_eq!(tree1.items(), (0..20).chain(50..100).collect::<Vec<u8>>());
}
#[test]
fn test_random() {
for seed in 0..100 {
use rand::{distributions, prelude::*};
let rng = &mut StdRng::seed_from_u64(seed);
let mut tree = SumTree::<u8>::new();
let count = rng.gen_range(0..10);
tree.extend(rng.sample_iter(distributions::Standard).take(count));
for _ in 0..5 {
let splice_end = rng.gen_range(0..tree.extent::<Count>().0 + 1);
let splice_start = rng.gen_range(0..splice_end + 1);
let count = rng.gen_range(0..3);
let tree_end = tree.extent::<Count>();
let new_items = rng
.sample_iter(distributions::Standard)
.take(count)
.collect::<Vec<u8>>();
let mut reference_items = tree.items();
reference_items.splice(splice_start..splice_end, new_items.clone());
tree = {
let mut cursor = tree.cursor::<Count, ()>();
let mut new_tree = cursor.slice(&Count(splice_start), SeekBias::Right);
new_tree.extend(new_items);
cursor.seek(&Count(splice_end), SeekBias::Right);
new_tree.push_tree(cursor.slice(&tree_end, SeekBias::Right));
new_tree
};
assert_eq!(tree.items(), reference_items);
let mut filter_cursor = tree.filter::<_, Count>(|summary| summary.contains_even);
let mut reference_filter = tree
.items()
.into_iter()
.enumerate()
.filter(|(_, item)| (item & 1) == 0);
while let Some(actual_item) = filter_cursor.item() {
let (reference_index, reference_item) = reference_filter.next().unwrap();
assert_eq!(actual_item, &reference_item);
assert_eq!(filter_cursor.start().0, reference_index);
filter_cursor.next();
}
assert!(reference_filter.next().is_none());
let mut pos = rng.gen_range(0..tree.extent::<Count>().0 + 1);
let mut before_start = false;
let mut cursor = tree.cursor::<Count, Count>();
cursor.seek(&Count(pos), SeekBias::Right);
for i in 0..10 {
assert_eq!(cursor.start().0, pos);
if pos > 0 {
assert_eq!(cursor.prev_item().unwrap(), &reference_items[pos - 1]);
} else {
assert_eq!(cursor.prev_item(), None);
}
if pos < reference_items.len() && !before_start {
assert_eq!(cursor.item().unwrap(), &reference_items[pos]);
} else {
assert_eq!(cursor.item(), None);
}
if i < 5 {
cursor.next();
if pos < reference_items.len() {
pos += 1;
before_start = false;
}
} else {
cursor.prev();
if pos == 0 {
before_start = true;
}
pos = pos.saturating_sub(1);
}
}
}
for _ in 0..10 {
let end = rng.gen_range(0..tree.extent::<Count>().0 + 1);
let start = rng.gen_range(0..end + 1);
let start_bias = if rng.gen() {
SeekBias::Left
} else {
SeekBias::Right
};
let end_bias = if rng.gen() {
SeekBias::Left
} else {
SeekBias::Right
};
let mut cursor = tree.cursor::<Count, ()>();
cursor.seek(&Count(start), start_bias);
let slice = cursor.slice(&Count(end), end_bias);
cursor.seek(&Count(start), start_bias);
let summary = cursor.summary::<Sum>(&Count(end), end_bias);
assert_eq!(summary, slice.summary().sum);
}
}
}
#[test]
fn test_cursor() {
// Empty tree
let tree = SumTree::<u8>::new();
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(
cursor.slice(&Count(0), SeekBias::Right).items(),
Vec::<u8>::new()
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
// Single-element tree
let mut tree = SumTree::<u8>::new();
tree.extend(vec![1]);
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(
cursor.slice(&Count(0), SeekBias::Right).items(),
Vec::<u8>::new()
);
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
cursor.next();
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
cursor.prev();
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(cursor.slice(&Count(1), SeekBias::Right).items(), [1]);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
cursor.seek(&Count(0), SeekBias::Right);
assert_eq!(
cursor
.slice(&tree.extent::<Count>(), SeekBias::Right)
.items(),
[1]
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
// Multiple-element tree
let mut tree = SumTree::new();
tree.extend(vec![1, 2, 3, 4, 5, 6]);
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(cursor.slice(&Count(2), SeekBias::Right).items(), [1, 2]);
assert_eq!(cursor.item(), Some(&3));
assert_eq!(cursor.prev_item(), Some(&2));
assert_eq!(cursor.start(), &Sum(3));
cursor.next();
assert_eq!(cursor.item(), Some(&4));
assert_eq!(cursor.prev_item(), Some(&3));
assert_eq!(cursor.start(), &Sum(6));
cursor.next();
assert_eq!(cursor.item(), Some(&5));
assert_eq!(cursor.prev_item(), Some(&4));
assert_eq!(cursor.start(), &Sum(10));
cursor.next();
assert_eq!(cursor.item(), Some(&6));
assert_eq!(cursor.prev_item(), Some(&5));
assert_eq!(cursor.start(), &Sum(15));
cursor.next();
cursor.next();
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&6));
assert_eq!(cursor.start(), &Sum(21));
cursor.prev();
assert_eq!(cursor.item(), Some(&6));
assert_eq!(cursor.prev_item(), Some(&5));
assert_eq!(cursor.start(), &Sum(15));
cursor.prev();
assert_eq!(cursor.item(), Some(&5));
assert_eq!(cursor.prev_item(), Some(&4));
assert_eq!(cursor.start(), &Sum(10));
cursor.prev();
assert_eq!(cursor.item(), Some(&4));
assert_eq!(cursor.prev_item(), Some(&3));
assert_eq!(cursor.start(), &Sum(6));
cursor.prev();
assert_eq!(cursor.item(), Some(&3));
assert_eq!(cursor.prev_item(), Some(&2));
assert_eq!(cursor.start(), &Sum(3));
cursor.prev();
assert_eq!(cursor.item(), Some(&2));
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
cursor.prev();
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
cursor.prev();
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
cursor.next();
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(
cursor
.slice(&tree.extent::<Count>(), SeekBias::Right)
.items(),
tree.items()
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&6));
assert_eq!(cursor.start(), &Sum(21));
cursor.seek(&Count(3), SeekBias::Right);
assert_eq!(
cursor
.slice(&tree.extent::<Count>(), SeekBias::Right)
.items(),
[4, 5, 6]
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&6));
assert_eq!(cursor.start(), &Sum(21));
// Seeking can bias left or right
cursor.seek(&Count(1), SeekBias::Left);
assert_eq!(cursor.item(), Some(&1));
cursor.seek(&Count(1), SeekBias::Right);
assert_eq!(cursor.item(), Some(&2));
// Slicing without resetting starts from where the cursor is parked at.
cursor.seek(&Count(1), SeekBias::Right);
assert_eq!(cursor.slice(&Count(3), SeekBias::Right).items(), vec![2, 3]);
assert_eq!(cursor.slice(&Count(6), SeekBias::Left).items(), vec![4, 5]);
assert_eq!(cursor.slice(&Count(6), SeekBias::Right).items(), vec![6]);
}
#[derive(Clone, Default, Debug)]
pub struct IntegersSummary {
count: Count,
sum: Sum,
contains_even: bool,
}
#[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
struct Count(usize);
#[derive(Ord, PartialOrd, Default, Eq, PartialEq, Clone, Debug)]
struct Sum(usize);
impl Item for u8 {
type Summary = IntegersSummary;
fn summary(&self) -> Self::Summary {
IntegersSummary {
count: Count(1),
sum: Sum(*self as usize),
contains_even: (*self & 1) == 0,
}
}
}
impl<'a> AddAssign<&'a Self> for IntegersSummary {
fn add_assign(&mut self, other: &Self) {
self.count.0 += &other.count.0;
self.sum.0 += &other.sum.0;
self.contains_even |= other.contains_even;
}
}
impl<'a> Dimension<'a, IntegersSummary> for Count {
fn add_summary(&mut self, summary: &IntegersSummary) {
self.0 += summary.count.0;
}
}
// impl<'a> Add<&'a Self> for Count {
// type Output = Self;
//
// fn add(mut self, other: &Self) -> Self {
// self.0 += other.0;
// self
// }
// }
impl<'a> Dimension<'a, IntegersSummary> for Sum {
fn add_summary(&mut self, summary: &IntegersSummary) {
self.0 += summary.sum.0;
}
}
impl<'a> Add<&'a Self> for Sum {
type Output = Self;
fn add(mut self, other: &Self) -> Self {
self.0 += other.0;
self
}
}
}
+140
View File
@@ -0,0 +1,140 @@
use std::cmp::{self, Ordering};
use std::collections::HashMap;
use std::mem;
use std::ops::{Add, AddAssign};
use std::sync::Arc;
pub type ReplicaId = u16;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, Ord, PartialOrd)]
pub struct Local {
pub replica_id: ReplicaId,
pub value: u64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Global(Arc<HashMap<ReplicaId, u64>>);
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Lamport {
pub value: u64,
pub replica_id: ReplicaId,
}
impl Local {
pub fn new(replica_id: ReplicaId) -> Self {
Self {
replica_id,
value: 1,
}
}
pub fn tick(&mut self) -> Self {
let timestamp = *self;
self.value += 1;
timestamp
}
pub fn observe(&mut self, timestamp: Self) {
if timestamp.replica_id == self.replica_id {
self.value = cmp::max(self.value, timestamp.value + 1);
}
}
}
impl<'a> Add<&'a Self> for Local {
type Output = Local;
fn add(self, other: &'a Self) -> Self::Output {
cmp::max(&self, other).clone()
}
}
impl<'a> AddAssign<&'a Local> for Local {
fn add_assign(&mut self, other: &Self) {
if *self < *other {
*self = other.clone();
}
}
}
impl Global {
pub fn new() -> Self {
Global(Arc::new(HashMap::new()))
}
pub fn get(&self, replica_id: ReplicaId) -> u64 {
*self.0.get(&replica_id).unwrap_or(&0)
}
pub fn observe(&mut self, timestamp: Local) {
let map = Arc::make_mut(&mut self.0);
let value = map.entry(timestamp.replica_id).or_insert(0);
*value = cmp::max(*value, timestamp.value);
}
pub fn observe_all(&mut self, other: &Self) {
for (replica_id, value) in other.0.as_ref() {
self.observe(Local {
replica_id: *replica_id,
value: *value,
});
}
}
pub fn observed(&self, timestamp: Local) -> bool {
self.get(timestamp.replica_id) >= timestamp.value
}
pub fn changed_since(&self, other: &Self) -> bool {
self.0
.iter()
.any(|(replica_id, value)| *value > other.get(*replica_id))
}
}
impl PartialOrd for Global {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let mut global_ordering = Ordering::Equal;
for replica_id in self.0.keys().chain(other.0.keys()) {
let ordering = self.get(*replica_id).cmp(&other.get(*replica_id));
if ordering != Ordering::Equal {
if global_ordering == Ordering::Equal {
global_ordering = ordering;
} else if ordering != global_ordering {
return None;
}
}
}
Some(global_ordering)
}
}
impl Lamport {
pub fn new(replica_id: ReplicaId) -> Self {
Self {
value: 1,
replica_id,
}
}
pub fn tick(&mut self) -> Self {
let timestamp = *self;
self.value += 1;
timestamp
}
pub fn observe(&mut self, timestamp: Self) {
self.value = cmp::max(self.value, timestamp.value) + 1;
}
pub fn to_bytes(&self) -> [u8; 24] {
let mut bytes = [0; 24];
bytes[0..8].copy_from_slice(unsafe { &mem::transmute::<u64, [u8; 8]>(self.value.to_be()) });
bytes[8..10]
.copy_from_slice(unsafe { &mem::transmute::<u16, [u8; 2]>(self.replica_id.to_be()) });
bytes
}
}