pub mod spine_fueled;
pub mod merge_batcher;
pub mod merge_batcher_col;
pub use self::merge_batcher::MergeBatcher as Batcher;
pub mod ord_neu;
pub mod rhh;
pub mod huffman_container;
pub use self::ord_neu::OrdValSpine as ValSpine;
pub use self::ord_neu::OrdKeySpine as KeySpine;
use std::borrow::{ToOwned};
use timely::container::columnation::{Columnation, TimelyStack};
use crate::lattice::Lattice;
use crate::difference::Semigroup;
pub trait Update {
type Key: Ord + Clone + 'static;
type Val: Ord + Clone + 'static;
type Time: Ord+Lattice+timely::progress::Timestamp+Clone;
type Diff: Semigroup+Clone;
}
impl<K,V,T,R> Update for ((K, V), T, R)
where
K: Ord+Clone+'static,
V: Ord+Clone+'static,
T: Ord+Lattice+timely::progress::Timestamp+Clone,
R: Semigroup+Clone,
{
type Key = K;
type Val = V;
type Time = T;
type Diff = R;
}
pub trait Layout {
type Target: Update + ?Sized;
type KeyContainer:
BatchContainer<PushItem=<Self::Target as Update>::Key>;
type ValContainer:
BatchContainer<PushItem=<Self::Target as Update>::Val>;
type UpdContainer:
for<'a> BatchContainer<PushItem=(<Self::Target as Update>::Time, <Self::Target as Update>::Diff), ReadItem<'a> = &'a (<Self::Target as Update>::Time, <Self::Target as Update>::Diff)>;
}
pub struct Vector<U: Update> {
phantom: std::marker::PhantomData<U>,
}
impl<U: Update> Layout for Vector<U>
where
U::Key: 'static,
U::Val: 'static,
{
type Target = U;
type KeyContainer = Vec<U::Key>;
type ValContainer = Vec<U::Val>;
type UpdContainer = Vec<(U::Time, U::Diff)>;
}
pub struct TStack<U: Update> {
phantom: std::marker::PhantomData<U>,
}
impl<U: Update> Layout for TStack<U>
where
U::Key: Columnation + 'static,
U::Val: Columnation + 'static,
U::Time: Columnation,
U::Diff: Columnation,
{
type Target = U;
type KeyContainer = TimelyStack<U::Key>;
type ValContainer = TimelyStack<U::Val>;
type UpdContainer = TimelyStack<(U::Time, U::Diff)>;
}
pub trait PreferredContainer : ToOwned {
type Container: BatchContainer<PushItem=Self::Owned>;
}
impl<T: Ord + Clone + 'static> PreferredContainer for T {
type Container = Vec<T>;
}
impl<T: Ord + Clone + 'static> PreferredContainer for [T] {
type Container = SliceContainer2<T>;
}
pub struct Preferred<K: ?Sized, V: ?Sized, T, D> {
phantom: std::marker::PhantomData<(Box<K>, Box<V>, T, D)>,
}
impl<K,V,T,R> Update for Preferred<K, V, T, R>
where
K: ToOwned + ?Sized,
K::Owned: Ord+Clone+'static,
V: ToOwned + ?Sized + 'static,
V::Owned: Ord+Clone,
T: Ord+Lattice+timely::progress::Timestamp+Clone,
R: Semigroup+Clone,
{
type Key = K::Owned;
type Val = V::Owned;
type Time = T;
type Diff = R;
}
impl<K, V, T, D> Layout for Preferred<K, V, T, D>
where
K: Ord+ToOwned+PreferredContainer + ?Sized,
K::Owned: Ord+Clone+'static,
V: Ord+ToOwned+PreferredContainer + ?Sized + 'static,
V::Owned: Ord+Clone,
T: Ord+Lattice+timely::progress::Timestamp+Clone,
D: Semigroup+Clone,
{
type Target = Preferred<K, V, T, D>;
type KeyContainer = K::Container;
type ValContainer = V::Container;
type UpdContainer = Vec<(T, D)>;
}
use std::convert::TryInto;
use abomonation_derive::Abomonation;
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Abomonation)]
pub struct OffsetList {
pub smol: Vec<u32>,
pub chonk: Vec<u64>,
}
impl OffsetList {
pub fn push(&mut self, offset: usize) {
if self.chonk.is_empty() {
if let Ok(smol) = offset.try_into() {
self.smol.push(smol);
}
else {
self.chonk.push(offset.try_into().unwrap())
}
}
else {
self.chonk.push(offset.try_into().unwrap())
}
}
pub fn index(&self, index: usize) -> usize {
if index < self.smol.len() {
self.smol[index].try_into().unwrap()
}
else {
self.chonk[index - self.smol.len()].try_into().unwrap()
}
}
pub fn set(&mut self, index: usize, offset: usize) {
if index < self.smol.len() {
if let Ok(off) = offset.try_into() {
self.smol[index] = off;
}
else {
self.chonk.splice(0..0, self.smol.drain(index ..).map(|x| x.try_into().unwrap()));
self.chonk[index - self.smol.len()] = offset.try_into().unwrap();
}
}
else {
self.chonk[index - self.smol.len()] = offset.try_into().unwrap();
}
}
pub fn last(&self) -> Option<usize> {
if self.chonk.is_empty() {
self.smol.last().map(|x| (*x).try_into().unwrap())
}
else {
self.chonk.last().map(|x| (*x).try_into().unwrap())
}
}
pub fn len(&self) -> usize {
self.smol.len() + self.chonk.len()
}
pub fn with_capacity(cap: usize) -> Self {
Self {
smol: Vec::with_capacity(cap),
chonk: Vec::new(),
}
}
pub fn truncate(&mut self, length: usize) {
if length > self.smol.len() {
self.chonk.truncate(length - self.smol.len());
}
else {
assert!(self.chonk.is_empty());
self.smol.truncate(length);
}
}
}
pub use self::containers::{BatchContainer, SliceContainer, SliceContainer2};
pub mod containers {
use timely::container::columnation::{Columnation, TimelyStack};
use std::borrow::{Borrow, ToOwned};
use crate::trace::MyTrait;
pub trait BatchContainer: Default + 'static {
type PushItem;
type ReadItem<'a>: Copy + MyTrait<'a, Owned = Self::PushItem> + for<'b> PartialOrd<Self::ReadItem<'b>>;
fn push(&mut self, item: Self::PushItem);
fn copy_push(&mut self, item: &Self::PushItem);
fn copy(&mut self, item: Self::ReadItem<'_>);
fn copy_slice(&mut self, slice: &[Self::PushItem]);
fn copy_range(&mut self, other: &Self, start: usize, end: usize);
fn with_capacity(size: usize) -> Self;
fn reserve(&mut self, additional: usize);
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self;
fn index(&self, index: usize) -> Self::ReadItem<'_>;
fn len(&self) -> usize;
fn last(&self) -> Option<Self::ReadItem<'_>> {
if self.len() > 0 {
Some(self.index(self.len()-1))
}
else {
None
}
}
fn advance<F: for<'a> Fn(Self::ReadItem<'a>)->bool>(&self, start: usize, end: usize, function: F) -> usize {
let small_limit = 8;
if end > start + small_limit && function(self.index(start + small_limit)) {
let mut index = small_limit + 1;
if start + index < end && function(self.index(start + index)) {
let mut step = 1;
while start + index + step < end && function(self.index(start + index + step)) {
index += step;
step <<= 1;
}
step >>= 1;
while step > 0 {
if start + index + step < end && function(self.index(start + index + step)) {
index += step;
}
step >>= 1;
}
index += 1;
}
index
}
else {
let limit = std::cmp::min(end, start + small_limit);
(start .. limit).filter(|x| function(self.index(*x))).count()
}
}
}
impl<T: Ord + Clone + 'static> BatchContainer for Vec<T> {
type PushItem = T;
type ReadItem<'a> = &'a Self::PushItem;
fn push(&mut self, item: T) {
self.push(item);
}
fn copy_push(&mut self, item: &T) {
self.copy(item);
}
fn copy(&mut self, item: &T) {
self.push(item.clone());
}
fn copy_slice(&mut self, slice: &[T]) {
self.extend_from_slice(slice);
}
fn copy_range(&mut self, other: &Self, start: usize, end: usize) {
self.extend_from_slice(&other[start .. end]);
}
fn with_capacity(size: usize) -> Self {
Vec::with_capacity(size)
}
fn reserve(&mut self, additional: usize) {
self.reserve(additional);
}
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
Vec::with_capacity(cont1.len() + cont2.len())
}
fn index(&self, index: usize) -> Self::ReadItem<'_> {
&self[index]
}
fn len(&self) -> usize {
self[..].len()
}
}
impl<T: Ord + Columnation + ToOwned<Owned = T> + 'static> BatchContainer for TimelyStack<T> {
type PushItem = T;
type ReadItem<'a> = &'a Self::PushItem;
fn push(&mut self, item: Self::PushItem) {
self.copy(item.borrow());
}
fn copy_push(&mut self, item: &Self::PushItem) {
self.copy(item);
}
fn copy(&mut self, item: &T) {
self.copy(item);
}
fn copy_slice(&mut self, slice: &[Self::PushItem]) {
self.reserve_items(slice.iter());
for item in slice.iter() {
self.copy(item);
}
}
fn copy_range(&mut self, other: &Self, start: usize, end: usize) {
let slice = &other[start .. end];
self.reserve_items(slice.iter());
for item in slice.iter() {
self.copy(item);
}
}
fn with_capacity(size: usize) -> Self {
Self::with_capacity(size)
}
fn reserve(&mut self, _additional: usize) {
}
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
let mut new = Self::default();
new.reserve_regions(std::iter::once(cont1).chain(std::iter::once(cont2)));
new
}
fn index(&self, index: usize) -> Self::ReadItem<'_> {
&self[index]
}
fn len(&self) -> usize {
self[..].len()
}
}
pub struct SliceContainer<B> {
offsets: Vec<usize>,
inner: Vec<B>,
}
impl<B> BatchContainer for SliceContainer<B>
where
B: Ord + Clone + Sized + 'static,
{
type PushItem = Vec<B>;
type ReadItem<'a> = &'a [B];
fn push(&mut self, item: Vec<B>) {
for x in item.into_iter() {
self.inner.push(x);
}
self.offsets.push(self.inner.len());
}
fn copy_push(&mut self, item: &Vec<B>) {
self.copy(&item[..]);
}
fn copy(&mut self, item: Self::ReadItem<'_>) {
for x in item.iter() {
self.inner.copy(x);
}
self.offsets.push(self.inner.len());
}
fn copy_slice(&mut self, slice: &[Vec<B>]) {
for item in slice {
self.copy(item);
}
}
fn copy_range(&mut self, other: &Self, start: usize, end: usize) {
for index in start .. end {
self.copy(other.index(index));
}
}
fn with_capacity(size: usize) -> Self {
let mut offsets = Vec::with_capacity(size + 1);
offsets.push(0);
Self {
offsets,
inner: Vec::with_capacity(size),
}
}
fn reserve(&mut self, _additional: usize) {
}
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
let mut offsets = Vec::with_capacity(cont1.inner.len() + cont2.inner.len() + 1);
offsets.push(0);
Self {
offsets,
inner: Vec::with_capacity(cont1.inner.len() + cont2.inner.len()),
}
}
fn index(&self, index: usize) -> Self::ReadItem<'_> {
let lower = self.offsets[index];
let upper = self.offsets[index+1];
&self.inner[lower .. upper]
}
fn len(&self) -> usize {
self.offsets.len() - 1
}
}
impl<B> Default for SliceContainer<B> {
fn default() -> Self {
Self {
offsets: vec![0],
inner: Default::default(),
}
}
}
pub struct SliceContainer2<B> {
text: String,
offsets: Vec<usize>,
inner: Vec<B>,
}
pub struct Greetings<'a, B> {
pub text: Option<&'a str>,
pub slice: &'a [B],
}
impl<'a, B> Copy for Greetings<'a, B> { }
impl<'a, B> Clone for Greetings<'a, B> {
fn clone(&self) -> Self { *self }
}
use std::cmp::Ordering;
impl<'a, 'b, B: Ord> PartialEq<Greetings<'a, B>> for Greetings<'b, B> {
fn eq(&self, other: &Greetings<'a, B>) -> bool {
self.slice.eq(other.slice)
}
}
impl<'a, B: Ord> Eq for Greetings<'a, B> { }
impl<'a, 'b, B: Ord> PartialOrd<Greetings<'a, B>> for Greetings<'b, B> {
fn partial_cmp(&self, other: &Greetings<'a, B>) -> Option<Ordering> {
self.slice.partial_cmp(other.slice)
}
}
impl<'a, B: Ord> Ord for Greetings<'a, B> {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl<'a, B: Ord + Clone> MyTrait<'a> for Greetings<'a, B> {
type Owned = Vec<B>;
fn into_owned(self) -> Self::Owned { self.slice.to_vec() }
fn clone_onto(&self, other: &mut Self::Owned) {
self.slice.clone_into(other);
}
fn compare(&self, other: &Self::Owned) -> std::cmp::Ordering {
self.slice.cmp(&other[..])
}
fn borrow_as(other: &'a Self::Owned) -> Self {
Self {
text: None,
slice: &other[..],
}
}
}
impl<B> BatchContainer for SliceContainer2<B>
where
B: Ord + Clone + Sized + 'static,
{
type PushItem = Vec<B>;
type ReadItem<'a> = Greetings<'a, B>;
fn push(&mut self, item: Vec<B>) {
for x in item.into_iter() {
self.inner.push(x);
}
self.offsets.push(self.inner.len());
}
fn copy_push(&mut self, item: &Vec<B>) {
self.copy(<_ as MyTrait>::borrow_as(item));
}
fn copy(&mut self, item: Self::ReadItem<'_>) {
for x in item.slice.iter() {
self.inner.copy(x);
}
self.offsets.push(self.inner.len());
}
fn copy_slice(&mut self, slice: &[Vec<B>]) {
for item in slice {
self.copy_push(item);
}
}
fn copy_range(&mut self, other: &Self, start: usize, end: usize) {
for index in start .. end {
self.copy(other.index(index));
}
}
fn with_capacity(size: usize) -> Self {
let mut offsets = Vec::with_capacity(size + 1);
offsets.push(0);
Self {
text: format!("Hello!"),
offsets,
inner: Vec::with_capacity(size),
}
}
fn reserve(&mut self, _additional: usize) {
}
fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
let mut offsets = Vec::with_capacity(cont1.inner.len() + cont2.inner.len() + 1);
offsets.push(0);
Self {
text: format!("Hello!"),
offsets,
inner: Vec::with_capacity(cont1.inner.len() + cont2.inner.len()),
}
}
fn index(&self, index: usize) -> Self::ReadItem<'_> {
let lower = self.offsets[index];
let upper = self.offsets[index+1];
Greetings {
text: Some(&self.text),
slice: &self.inner[lower .. upper],
}
}
fn len(&self) -> usize {
self.offsets.len() - 1
}
}
impl<B> Default for SliceContainer2<B> {
fn default() -> Self {
Self {
text: format!("Hello!"),
offsets: vec![0],
inner: Default::default(),
}
}
}
}