use std::iter::FromIterator;
pub use columnation::*;
use timely::container::PushInto;
pub struct TimelyStack<T: Columnation> {
local: Vec<T>,
inner: T::InnerRegion,
}
impl<T: Columnation> TimelyStack<T> {
pub fn with_capacity(capacity: usize) -> Self {
Self {
local: Vec::with_capacity(capacity),
inner: T::InnerRegion::default(),
}
}
#[inline(always)]
pub fn reserve_items<'a, I>(&mut self, items: I)
where
I: Iterator<Item = &'a T> + Clone,
T: 'a,
{
self.local.reserve(items.clone().count());
self.inner.reserve_items(items);
}
#[inline(always)]
pub fn reserve_regions<'a, I>(&mut self, regions: I)
where
Self: 'a,
I: Iterator<Item = &'a Self> + Clone,
{
self.local
.reserve(regions.clone().map(|cs| cs.local.len()).sum());
self.inner.reserve_regions(regions.map(|cs| &cs.inner));
}
pub fn copy(&mut self, item: &T) {
unsafe {
self.local.push(self.inner.copy(item));
}
}
pub fn clear(&mut self) {
unsafe {
self.local.set_len(0);
self.inner.clear();
}
}
pub fn retain_from<P: FnMut(&T) -> bool>(&mut self, index: usize, mut predicate: P) {
let mut write_position = index;
for position in index..self.local.len() {
if predicate(&self[position]) {
self.local.swap(position, write_position);
write_position += 1;
}
}
unsafe {
self.local.set_len(write_position);
}
}
pub unsafe fn local(&mut self) -> &mut [T] {
&mut self.local[..]
}
#[inline]
pub fn heap_size(&self, mut callback: impl FnMut(usize, usize)) {
let size_of = std::mem::size_of::<T>();
callback(self.local.len() * size_of, self.local.capacity() * size_of);
self.inner.heap_size(callback);
}
#[inline]
pub fn summed_heap_size(&self) -> (usize, usize) {
let (mut length, mut capacity) = (0, 0);
self.heap_size(|len, cap| {
length += len;
capacity += cap
});
(length, capacity)
}
#[inline]
pub fn len(&self) -> usize {
self.local.len()
}
pub fn is_empty(&self) -> bool {
self.local.is_empty()
}
#[inline]
pub fn capacity(&self) -> usize {
self.local.capacity()
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.local.reserve(additional)
}
}
impl<A: Columnation, B: Columnation> TimelyStack<(A, B)> {
pub fn copy_destructured(&mut self, t1: &A, t2: &B) {
unsafe {
self.local.push(self.inner.copy_destructured(t1, t2));
}
}
}
impl<A: Columnation, B: Columnation, C: Columnation> TimelyStack<(A, B, C)> {
pub fn copy_destructured(&mut self, r0: &A, r1: &B, r2: &C) {
unsafe {
self.local.push(self.inner.copy_destructured(r0, r1, r2));
}
}
}
impl<T: Columnation> std::ops::Deref for TimelyStack<T> {
type Target = [T];
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.local[..]
}
}
impl<T: Columnation> Drop for TimelyStack<T> {
fn drop(&mut self) {
self.clear();
}
}
impl<T: Columnation> Default for TimelyStack<T> {
fn default() -> Self {
Self {
local: Vec::new(),
inner: T::InnerRegion::default(),
}
}
}
impl<'a, A: 'a + Columnation> FromIterator<&'a A> for TimelyStack<A> {
fn from_iter<T: IntoIterator<Item = &'a A>>(iter: T) -> Self {
let iter = iter.into_iter();
let mut c = TimelyStack::<A>::with_capacity(iter.size_hint().0);
for element in iter {
c.copy(element);
}
c
}
}
impl<T: Columnation + PartialEq> PartialEq for TimelyStack<T> {
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(&self[..], &other[..])
}
}
impl<T: Columnation + Eq> Eq for TimelyStack<T> {}
impl<T: Columnation + std::fmt::Debug> std::fmt::Debug for TimelyStack<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self[..].fmt(f)
}
}
impl<T: Columnation> Clone for TimelyStack<T> {
fn clone(&self) -> Self {
let mut new: Self = Default::default();
for item in &self[..] {
new.copy(item);
}
new
}
fn clone_from(&mut self, source: &Self) {
self.clear();
for item in &source[..] {
self.copy(item);
}
}
}
impl<T: Columnation> PushInto<T> for TimelyStack<T> {
#[inline]
fn push_into(&mut self, item: T) {
self.copy(&item);
}
}
impl<T: Columnation> PushInto<&T> for TimelyStack<T> {
#[inline]
fn push_into(&mut self, item: &T) {
self.copy(item);
}
}
impl<T: Columnation> PushInto<&&T> for TimelyStack<T> {
#[inline]
fn push_into(&mut self, item: &&T) {
self.copy(*item);
}
}
mod container {
use columnation::Columnation;
use crate::containers::TimelyStack;
impl<T: Columnation> timely::container::Accountable for TimelyStack<T> {
#[inline]
fn record_count(&self) -> i64 {
i64::try_from(self.local.len()).unwrap()
}
#[inline]
fn is_empty(&self) -> bool {
self.local.is_empty()
}
}
impl<T: Columnation> timely::container::DrainContainer for TimelyStack<T> {
type Item<'a>
= &'a T
where
Self: 'a;
type DrainIter<'a>
= std::slice::Iter<'a, T>
where
Self: 'a;
#[inline]
fn drain(&mut self) -> Self::DrainIter<'_> {
(*self).iter()
}
}
impl<T: Columnation> timely::container::SizableContainer for TimelyStack<T> {
fn at_capacity(&self) -> bool {
self.len() == self.capacity()
}
fn ensure_capacity(&mut self, stash: &mut Option<Self>) {
if self.capacity() == 0 {
*self = stash.take().unwrap_or_default();
self.clear();
}
let preferred = timely::container::buffer::default_capacity::<T>();
if self.capacity() < preferred {
self.reserve(preferred - self.capacity());
}
}
}
}