use Box;
use core::mem;
use core::iter::{Chain, Cycle, FromIterator, Skip, Take};
use core::ptr;
use core::ops::{Index, IndexMut};
use core::slice;
use Vec;
pub trait Slice {
type Element;
fn slice(&self) -> &[Self::Element];
}
pub trait SliceMut: Slice {
fn slice_mut(&mut self) -> &mut [Self::Element];
}
pub trait FixedSizeArray {
const LEN: usize;
}
impl<'a, T> Slice for &'a [T] {
type Element = T;
#[inline]
fn slice(&self) -> &[Self::Element] {
self
}
}
impl<'a, T> Slice for &'a mut [T] {
type Element = T;
#[inline]
fn slice(&self) -> &[Self::Element] {
self
}
}
impl<'a, T> SliceMut for &'a mut [T] {
#[inline]
fn slice_mut(&mut self) -> &mut [Self::Element] {
self
}
}
impl<T> Slice for Box<[T]> {
type Element = T;
#[inline]
fn slice(&self) -> &[Self::Element] {
&self[..]
}
}
impl<T> SliceMut for Box<[T]> {
#[inline]
fn slice_mut(&mut self) -> &mut [Self::Element] {
&mut self[..]
}
}
impl<T> Slice for Vec<T> {
type Element = T;
#[inline]
fn slice(&self) -> &[Self::Element] {
&self[..]
}
}
impl<T> SliceMut for Vec<T> {
#[inline]
fn slice_mut(&mut self) -> &mut [Self::Element] {
&mut self[..]
}
}
macro_rules! impl_slice_for_arrays {
($($N:expr)*) => {
$(
impl<T> Slice for [T; $N] {
type Element = T;
#[inline]
fn slice(&self) -> &[Self::Element] {
&self[..]
}
}
impl<T> SliceMut for [T; $N] {
#[inline]
fn slice_mut(&mut self) -> &mut [Self::Element] {
&mut self[..]
}
}
impl<T> FixedSizeArray for [T; $N] {
const LEN: usize = $N;
}
)*
};
}
impl_slice_for_arrays! {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 256 512 1024 2048 4096 8192
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Fixed<S> {
first: usize,
data: S,
}
impl<S> Fixed<S>
where
S: Slice,
{
#[inline]
pub fn len(&self) -> usize {
self.data.slice().len()
}
pub fn push(&mut self, item: S::Element) -> S::Element
where
S: SliceMut,
{
let mut next_index = self.first + 1;
if next_index == self.len() {
next_index = 0;
}
let old_item =
unsafe { mem::replace(self.data.slice_mut().get_unchecked_mut(self.first), item) };
self.first = next_index;
old_item
}
#[inline]
pub fn get(&self, index: usize) -> &S::Element {
let wrapped_index = (self.first + index) % self.len();
&self.data.slice()[wrapped_index]
}
#[inline]
pub fn get_mut(&mut self, index: usize) -> &mut S::Element
where
S: SliceMut,
{
let wrapped_index = (self.first + index) % self.len();
&mut self.data.slice_mut()[wrapped_index]
}
#[inline]
pub fn set_first(&mut self, index: usize) {
self.first = index % self.len();
}
#[inline]
pub fn slices(&self) -> (&[S::Element], &[S::Element]) {
let (end, start) = self.data.slice().split_at(self.first);
(start, end)
}
#[inline]
pub fn slices_mut(&mut self) -> (&mut [S::Element], &mut [S::Element])
where
S: SliceMut,
{
let (end, start) = self.data.slice_mut().split_at_mut(self.first);
(start, end)
}
#[inline]
pub fn iter_loop(&self) -> Skip<Cycle<slice::Iter<S::Element>>> {
self.data.slice().iter().cycle().skip(self.first)
}
#[inline]
pub fn iter(&self) -> Take<Skip<Cycle<slice::Iter<S::Element>>>> {
self.iter_loop().take(self.data.slice().len())
}
#[inline]
pub fn iter_mut(&mut self) -> Chain<slice::IterMut<S::Element>, slice::IterMut<S::Element>>
where
S: SliceMut,
{
let (start, end) = self.slices_mut();
start.iter_mut().chain(end.iter_mut())
}
#[inline]
pub fn from_raw_parts(first: usize, data: S) -> Self {
assert!(first < data.slice().len());
Fixed { first, data }
}
#[inline]
pub unsafe fn from_raw_parts_unchecked(first: usize, data: S) -> Self {
Fixed { first, data }
}
#[inline]
pub fn into_raw_parts(self) -> (usize, S) {
let Fixed { first, data } = self;
(first, data)
}
}
impl<S> From<S> for Fixed<S>
where
S: Slice,
{
#[inline]
fn from(data: S) -> Self {
Self::from_raw_parts(0, data)
}
}
impl<S, T> FromIterator<T> for Fixed<S>
where
S: Slice<Element = T> + FromIterator<T>,
{
#[inline]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
let data = S::from_iter(iter);
Self::from(data)
}
}
impl<S> Index<usize> for Fixed<S>
where
S: Slice,
{
type Output = S::Element;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
self.get(index)
}
}
impl<S> IndexMut<usize> for Fixed<S>
where
S: SliceMut,
{
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.get_mut(index)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Bounded<S> {
start: usize,
len: usize,
data: S,
}
pub struct DrainBounded<'a, S: 'a> {
bounded: &'a mut Bounded<S>,
}
impl<T> Bounded<Box<[T]>>
where
T: Copy,
{
pub fn boxed_slice(max_len: usize) -> Self {
let mut vec = Vec::new();
vec.reserve_exact(max_len);
unsafe {
vec.set_len(max_len);
let data = vec.into_boxed_slice();
Self::from_raw_parts(0, 0, data)
}
}
}
impl<S> Bounded<S>
where
S: Slice,
S::Element: Copy,
{
pub fn array() -> Self
where
S: FixedSizeArray,
{
unsafe {
let data = mem::uninitialized();
Self::from_raw_parts(0, 0, data)
}
}
pub fn from_full(data: S) -> Self {
Self::from_raw_parts(0, data.slice().len(), data)
}
#[inline]
pub fn max_len(&self) -> usize {
self.data.slice().len()
}
#[inline]
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub fn is_full(&self) -> bool {
self.len == self.max_len()
}
#[inline]
pub fn slices(&self) -> (&[S::Element], &[S::Element]) {
let (end, start) = self.data.slice().split_at(self.start);
if start.len() <= self.len {
let end_len = self.len - start.len();
(start, &end[..end_len])
} else {
(&start[..self.len], &end[..0])
}
}
#[inline]
pub fn slices_mut(&mut self) -> (&mut [S::Element], &mut [S::Element])
where
S: SliceMut,
{
let (end, start) = self.data.slice_mut().split_at_mut(self.start);
if start.len() <= self.len {
let end_len = self.len - start.len();
(start, &mut end[..end_len])
} else {
(&mut start[..self.len], &mut end[..0])
}
}
#[inline]
pub fn iter(&self) -> Chain<slice::Iter<S::Element>, slice::Iter<S::Element>> {
let (start, end) = self.slices();
start.iter().chain(end.iter())
}
#[inline]
pub fn iter_mut(&mut self) -> Chain<slice::IterMut<S::Element>, slice::IterMut<S::Element>>
where
S: SliceMut,
{
let (start, end) = self.slices_mut();
start.iter_mut().chain(end.iter_mut())
}
#[inline]
pub fn get(&self, index: usize) -> Option<&S::Element> {
if index >= self.len {
return None;
}
let wrapped_index = index % self.max_len();
unsafe { Some(self.data.slice().get_unchecked(wrapped_index) as &_) }
}
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<&mut S::Element>
where
S: SliceMut,
{
if index >= self.len {
return None;
}
let wrapped_index = index % self.max_len();
unsafe {
Some(self.data.slice_mut().get_unchecked_mut(wrapped_index) as
&mut _)
}
}
pub fn push(&mut self, elem: S::Element) -> Option<S::Element>
where
S: SliceMut,
{
if self.len == self.max_len() {
let mut next_start = self.start + 1;
if next_start >= self.max_len() {
next_start = 0;
}
let old_elem =
unsafe { mem::replace(self.data.slice_mut().get_unchecked_mut(self.start), elem) };
self.start = next_start;
return Some(old_elem);
}
let index = (self.start + self.len) % self.max_len();
unsafe {
ptr::write(self.data.slice_mut().get_unchecked_mut(index), elem);
}
self.len += 1;
None
}
pub fn pop(&mut self) -> Option<S::Element>
where
S: SliceMut,
{
if self.len == 0 {
return None;
}
let mut next_start = self.start + 1;
if next_start >= self.max_len() {
next_start = 0;
}
let old_elem = unsafe { ptr::read(self.data.slice_mut().get_unchecked_mut(self.start)) };
self.start = next_start;
self.len -= 1;
Some(old_elem)
}
pub fn drain(&mut self) -> DrainBounded<S> {
DrainBounded {
bounded: self,
}
}
#[inline]
pub fn from_raw_parts(start: usize, len: usize, data: S) -> Self {
assert!(start < data.slice().len());
assert!(len <= data.slice().len());
Bounded { start, len, data }
}
#[inline]
pub unsafe fn from_raw_parts_unchecked(start: usize, len: usize, data: S) -> Self {
Bounded { start, len, data }
}
#[inline]
pub unsafe fn into_raw_parts(self) -> (usize, usize, S) {
let Bounded { start, len, data } = self;
(start, len, data)
}
}
impl<S> From<S> for Bounded<S>
where
S: Slice,
S::Element: Copy,
{
#[inline]
fn from(data: S) -> Self {
Self::from_raw_parts(0, 0, data)
}
}
impl<S, T> FromIterator<T> for Bounded<S>
where
S: Slice<Element = T> + FromIterator<T>,
T: Copy,
{
#[inline]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
let data = S::from_iter(iter);
Self::from(data)
}
}
impl<S> Index<usize> for Bounded<S>
where
S: Slice,
S::Element: Copy,
{
type Output = S::Element;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
self.get(index).expect("index out of range")
}
}
impl<S> IndexMut<usize> for Bounded<S>
where
S: SliceMut,
S::Element: Copy,
{
#[inline]
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.get_mut(index).expect("index out of range")
}
}
impl<'a, S> Iterator for DrainBounded<'a, S>
where
S: SliceMut,
S::Element: Copy,
{
type Item = S::Element;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.bounded.pop()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.bounded.len(), Some(self.bounded.len()))
}
}
impl<'a, S> ExactSizeIterator for DrainBounded<'a, S>
where
S: SliceMut,
S::Element: Copy,
{
fn len(&self) -> usize {
self.bounded.len()
}
}