Offsets

Struct Offsets 

Source
pub struct Offsets<O = Vec<usize>>(/* private fields */);
Expand description

A collection of offsets into another collection. This newtype is intended to verify basic invariants about offsets into another collection, namely that the collection is monotonically increasing and non-empty.

Implementations§

Source§

impl<'a> Offsets<&'a [usize]>

Source

pub fn separate_offsets_with_overlap( self, index: usize, ) -> (Offsets<&'a [usize]>, Offsets<&'a [usize]>)

Separate the offsets into two groups overlapping by one chunk at the given index.

Source§

impl<O: RemovePrefix + Set> Offsets<O>

Source

pub unsafe fn remove_prefix_unchecked(&mut self, n: usize)

Unchecked version of RemovePrefix::remove_prefix.

§Safety

This function may cause undefined behaviour in safe APIs if calling this function produces an empty offsets collection.

Source§

impl Offsets<&[usize]>

Source

pub unsafe fn remove_suffix_unchecked(&mut self, n: usize)

Remove n offsets from the end.

§Safety

This function may cause undefined behaviour in safe APIs if calling this function produces an empty offsets collection.

Source§

impl<O: AsRef<[usize]> + Set> Offsets<O>

Source

pub fn sizes(&self) -> Sizes<'_>

Returns an iterator over chunk sizes represented by the stored Offsets.

Source

pub fn iter(&self) -> impl Iterator<Item = usize> + '_

Returns an iterator over offsets.

Source

pub fn values(&self) -> OffsetValues<'_>

Returns an iterator over offset values.

Source

pub fn ranges(&self) -> Ranges<'_>

Returns an iterator over offset ranges.

Source§

impl<O: AsRef<[usize]>> Offsets<O>

Source

pub fn new(offsets: O) -> Self

Construct a set Offsets from a given set of offsets.

§Panics

The given offsets must be a non-empty collection, otherwise this function will panic.

Source

pub unsafe fn from_raw(offsets: O) -> Self

An unchecked version of the new constructor.

§Safety

Calling this function with empty offsets may cause undefined behaviour in safe APIs.

Source§

impl<O> Offsets<O>

Source

pub fn into_inner(self) -> O

Source§

impl<O: AsMut<[usize]>> Offsets<O>

Source

pub fn move_back(&mut self, at: usize, by: usize)

Moves an offset back by a specified amount.

This effectively transfers by elements to the specified at chunk from the preceeding chunk.

§Panics

This function panics if at is out of bounds.

If at it zero, the beginning of the indexed range is simply extended, but an overflow panic will be caused if the first offset is moved below zero since offsets are represented by unsigned integers.

It is a logic error to move an offset past its preceeding offset because this will break the monotonicity of the offset sequence, which can cause panics from other function on the Offsets.

§Example
use flatk::Offsets;
let mut o = Offsets::new(vec![0, 4, 9]);
o.move_back(1, 2);
assert_eq!(o, vec![0, 2, 9].into());
Source

pub fn move_forward(&mut self, at: usize, by: usize)

Moves an offset forward by a specified amount.

This effectively transfers by elements to the specified at chunk from the succeeding chunk.

If at indexes the last offset, then the indexed range is simply increased.

§Panics

This function panics if at is out of bounds.

It is a logic error to move an offset past its succeeding offset because this will break the monotonicity of the offset sequence, which can cause panics from other function on the Offsets.

§Example
use flatk::Offsets;
let mut o = Offsets::new(vec![0, 4, 9]);
o.move_forward(1, 2);
assert_eq!(o, vec![0, 6, 9].into());
Source

pub fn extend_last(&mut self, by: usize)

Extend the last offset.

This effectively increases the last chunk size. This function is the same as self.move_forward(self.len() - 1, by).

§Example
use flatk::Offsets;
let mut o = Offsets::new(vec![0, 4, 9]);
o.extend_last(2);
assert_eq!(o, vec![0, 4, 11].into());
Source

pub fn shrink_last(&mut self, by: usize)

Shrink the last offset.

This effectively decreases the last chunk size. This function is the same as self.move_back(self.len() - 1, by).

§Panics

It is a logic error to move an offset past its preceeding offset because this will break the monotonicity of the offset sequence, which can cause panics from other function on the Offsets.

§Example
use flatk::Offsets;
let mut o = Offsets::new(vec![0, 4, 9]);
o.shrink_last(2);
assert_eq!(o, vec![0, 4, 7].into());

Trait Implementations§

Source§

impl<O: AsMut<[usize]>> AsMut<[usize]> for Offsets<O>

Source§

fn as_mut(&mut self) -> &mut [usize]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<O: AsRef<[usize]>> AsRef<[usize]> for Offsets<O>

Source§

fn as_ref(&self) -> &[usize]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<O: AsRef<[usize]>> BinarySearch<usize> for Offsets<O>

Binary search the offsets for a given offset off.

off is expected to be with respect to the beginning of the range represented by the current offsets. In other words, we are searching for offsets, not raw offset values stored in Offsets.

The semantics of this function are identical to Rust’s std::slice::binary_search.

Source§

impl Clear for Offsets

Source§

fn clear(&mut self)

Remove all elements from the current set without necessarily deallocating the space previously used.
Source§

impl<O: Clone> Clone for Offsets<O>

Source§

fn clone(&self) -> Offsets<O>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<O: Debug> Debug for Offsets<O>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Offsets<Vec<usize>>

A default set of offsets must allocate.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<O: Dummy> Dummy for Offsets<O>

Source§

unsafe fn dummy() -> Self

Constructs a potentially invalid instance of a type. Read more
Source§

impl Extend<usize> for Offsets

Source§

fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)

Extend this set of offsets with a given iterator of offsets.

This operation automatically shifts the merged offsets in the iterator to start from the last offset in self.

Note that there will be 1 less offset added to self than produced by iter since the first offset is only used to determine the relative magnitude of the rest and corresponds to the last offset in self.

Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<O: AsRef<[usize]> + Set> From<ClumpedOffsets<O>> for Offsets

Source§

fn from(clumped_offsets: ClumpedOffsets<O>) -> Self

Convert ClumpedOffsets into owned Offsets.

This function causes allocations.

Source§

impl<O: AsRef<[usize]>> From<O> for Offsets<O>

Source§

fn from(offsets: O) -> Self

Converts to this type from the input type.
Source§

impl<O: AsRef<[usize]> + Set> From<Offsets<O>> for ClumpedOffsets

Source§

fn from(offsets: Offsets<O>) -> Self

Convert Offsets to owned ClumpedOffsets.

This function causes allocations.

§Example
use flatk::{Offsets, ClumpedOffsets};
let offsets = Offsets::new(vec![0, 3, 6, 9, 12, 16, 20, 24, 27, 30, 33, 36, 39]);
let clumped_offsets = ClumpedOffsets::new(vec![0, 4, 7, 12], vec![0, 12, 24, 39]);
assert_eq!(ClumpedOffsets::from(offsets), clumped_offsets);
Source§

impl<O: FromIterator<usize> + AsRef<[usize]>> FromIterator<usize> for Offsets<O>

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = usize>,

Creates a value from an iterator. Read more
Source§

impl<'a, O: Get<'a, usize>> GetIndex<'a, Offsets<O>> for &usize

Source§

type Output = <O as Get<'a, usize>>::Output

Source§

fn get(self, offsets: &Offsets<O>) -> Option<Self::Output>

Gets the value in the set at this index.
Source§

unsafe fn at_unchecked(self, set: &S) -> Self::Output
where Self: Sized,

Gets the value in the set at this index. Read more
Source§

impl<'a, O: Get<'a, Range<usize>>> GetIndex<'a, Offsets<O>> for Range<usize>

Source§

type Output = Offsets<<O as Get<'a, Range<usize>>>::Output>

Source§

fn get(self, offsets: &Offsets<O>) -> Option<Self::Output>

Gets the value in the set at this index.
Source§

unsafe fn at_unchecked(self, set: &S) -> Self::Output
where Self: Sized,

Gets the value in the set at this index. Read more
Source§

impl<'a, O: Get<'a, usize>> GetIndex<'a, Offsets<O>> for usize

Source§

type Output = <O as Get<'a, usize>>::Output

Source§

fn get(self, offsets: &Offsets<O>) -> Option<Self::Output>

Gets the value in the set at this index.
Source§

unsafe fn at_unchecked(self, set: &S) -> Self::Output
where Self: Sized,

Gets the value in the set at this index. Read more
Source§

impl<O: AsRef<[usize]>> GetOffset for Offsets<O>

Source§

unsafe fn offset_value_unchecked(&self, index: usize) -> usize

A version of offset_value without bounds checking.

§Safety

It is assumed that index is strictly less than self.num_offsets().

Source§

fn num_offsets(&self) -> usize

Get the total number of offsets.

This is one more than the number of chunks represented.

Source§

fn chunk_len(&self, chunk_index: usize) -> usize

Get the length of the chunk at the given index. Read more
Source§

unsafe fn chunk_len_unchecked(&self, chunk_index: usize) -> usize

Get the length of the chunk at the given index without bounds checking. Read more
Source§

fn offset_value(&self, index: usize) -> usize

Return the raw value corresponding to the offset at the given index. Read more
Source§

fn offset(&self, index: usize) -> usize

Returns the offset at the given index with respect to (minus) the first offset. This function returns the total length of data if index is equal to self.len(). Read more
Source§

unsafe fn offset_unchecked(&self, index: usize) -> usize

A version of offset without bounds checking. Read more
Source§

fn last_offset(&self) -> usize

Get the last offset. Read more
Source§

fn first_offset(&self) -> usize

Get the first offset. Read more
Source§

fn last_offset_value(&self) -> usize

Get the raw value corresponding to the last offset.
Source§

fn first_offset_value(&self) -> usize

Get the raw value corresponding to the first offset.
Source§

impl<I: SliceIndex<[usize]>, O: AsRef<[usize]>> Index<I> for Offsets<O>

Source§

type Output = <I as SliceIndex<[usize]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<I: SliceIndex<[usize]>, O: AsRef<[usize]> + AsMut<[usize]>> IndexMut<I> for Offsets<O>

Source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<O: AsRef<[usize]> + Set> IndexRange for Offsets<O>

Source§

fn index_range(&self, range: Range<usize>) -> Option<Range<usize>>

Return the [begin..end) bound of the chunk at the given index.

Source§

unsafe fn index_range_unchecked(&self, range: Range<usize>) -> Range<usize>

Safety Read more
Source§

impl<'a> IntoOffsetValuesAndSizes for Offsets<&'a [usize]>

Source§

fn into_offset_values_and_sizes(self) -> OffsetValuesAndSizes<'a>

Returns an iterator over offset value and chunk size pairs.

Source§

type Iter = OffsetValuesAndSizes<'a>

Source§

impl<O: IntoOwned> IntoOwned for Offsets<O>

Source§

type Owned = Offsets<<O as IntoOwned>::Owned>

Source§

fn into_owned(self) -> Self::Owned

Source§

fn clone_into(self, target: &mut Self::Owned)

Source§

impl<'a> IntoParOffsetValuesAndSizes for Offsets<&'a [usize]>

Source§

fn into_par_offset_values_and_sizes(self) -> Self::ParIter

Returns a parallel iterator over chunk sizes represented by the stored Offsets.

Source§

type ParIter = ParOffsetValuesAndSizes<'a>

Source§

impl<'a> IntoRanges for Offsets<&'a [usize]>

Source§

fn into_ranges(self) -> Ranges<'a>

Returns an iterator over offset ranges represented by the stored Offsets.

Source§

type Iter = Ranges<'a>

Source§

impl<'a> IntoSizes for Offsets<&'a [usize]>

Source§

fn into_sizes(self) -> Sizes<'a>

Returns an iterator over chunk sizes represented by the stored Offsets.

Source§

type Iter = Sizes<'a>

Source§

impl<'a> IntoValues for Offsets<&'a [usize]>

Source§

fn into_values(self) -> OffsetValues<'a>

Returns an iterator over offset values represented by the stored Offsets.

Source§

type Iter = OffsetValues<'a>

Source§

impl<O: Isolate<Range<usize>>> IsolateIndex<Offsets<O>> for Range<usize>

Source§

type Output = Offsets<<O as Isolate<Range<usize>>>::Output>

Source§

unsafe fn isolate_unchecked(self, offsets: Offsets<O>) -> Self::Output

Attempts to isolate a value in the given set at this index. Read more
Source§

fn try_isolate(self, offsets: Offsets<O>) -> Option<Self::Output>

Attempts to isolate a value in the given set at this index. Read more
Source§

impl<O: Isolate<usize>> IsolateIndex<Offsets<O>> for usize

Source§

type Output = <O as Isolate<usize>>::Output

Source§

unsafe fn isolate_unchecked(self, offsets: Offsets<O>) -> Self::Output

Attempts to isolate a value in the given set at this index. Read more
Source§

fn try_isolate(self, offsets: Offsets<O>) -> Option<Self::Output>

Attempts to isolate a value in the given set at this index. Read more
Source§

impl<O: PartialEq> PartialEq for Offsets<O>

Source§

fn eq(&self, other: &Offsets<O>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<O: Push<usize>> Push<usize> for Offsets<O>

Source§

fn push(&mut self, item: usize)

Source§

impl<O: RemovePrefix + Set> RemovePrefix for Offsets<O>

Source§

fn remove_prefix(&mut self, n: usize)

Remove the first n offsets.

§Panics

This function will panic if all offsets are removed, which violates the Offsets invariant that there must always be at least one offset.

Source§

impl<O: Reserve> Reserve for Offsets<O>

Source§

fn reserve_with_storage(&mut self, n: usize, storage_n: usize)

Source§

fn reserve(&mut self, n: usize)

Source§

impl<O: Set<Elem = usize, Atom = usize>> Set for Offsets<O>

Source§

type Elem = usize

Owned element of the set.
Source§

type Atom = usize

The most basic element contained by this collection. If this collection contains other collections, this type should be different than Elem.
Source§

fn len(&self) -> usize

Source§

fn is_empty(&self) -> bool

Source§

impl<'a> SplitOffsetsAt for Offsets<&'a [usize]>

Source§

fn split_offsets_with_intersection_at( self, mid: usize, ) -> (Offsets<&'a [usize]>, Offsets<&'a [usize]>, usize)

Same as split_offsets_at, but in addition, return the offset of the middle element (intersection): this is the value offsets[mid] - offsets[0].

§Panics

Calling this function with an empty slice or with mid greater than or equal to its length will cause a panic.

Source§

fn split_offsets_at( self, mid: usize, ) -> (Offsets<&'a [usize]>, Offsets<&'a [usize]>)

Splits a slice of offsets at the given index into two slices such that each slice is a valid slice of offsets. This means that the element at index mid is shared between the two output slices.

§Panics

Calling this function with an empty slice or with mid greater than or equal to its length will cause a panic.

Source§

impl<O: Truncate> Truncate for Offsets<O>

Source§

fn truncate(&mut self, new_len: usize)

Source§

impl<'a, O: AsRef<[usize]>> View<'a> for Offsets<O>

Source§

type Type = Offsets<&'a [usize]>

Source§

fn view(&'a self) -> Self::Type

Source§

impl<'a, O: AsMut<[usize]>> ViewMut<'a> for Offsets<O>

Source§

type Type = Offsets<&'a mut [usize]>

Source§

fn view_mut(&'a mut self) -> Self::Type

Source§

impl<O: Copy> Copy for Offsets<O>

Source§

impl<O> StructuralPartialEq for Offsets<O>

Source§

impl<O: Viewed> Viewed for Offsets<O>

Auto Trait Implementations§

§

impl<O> Freeze for Offsets<O>
where O: Freeze,

§

impl<O> RefUnwindSafe for Offsets<O>
where O: RefUnwindSafe,

§

impl<O> Send for Offsets<O>
where O: Send,

§

impl<O> Sync for Offsets<O>
where O: Sync,

§

impl<O> Unpin for Offsets<O>
where O: Unpin,

§

impl<O> UnwindSafe for Offsets<O>
where O: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, S, I> Get<'a, I> for S
where I: GetIndex<'a, S>,

Source§

type Output = <I as GetIndex<'a, S>>::Output

Source§

fn get(&self, idx: I) -> Option<<I as GetIndex<'a, S>>::Output>

Source§

fn at(&self, idx: I) -> Self::Output

Return a value at the given index. This is provided as the checked version of get that will panic if the equivalent get call is None, which typically means that the given index is out of bounds. Read more
Source§

unsafe fn at_unchecked(&self, idx: I) -> Self::Output

Return a value at the given index. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<S, I> Isolate<I> for S
where I: IsolateIndex<S>,

Source§

type Output = <I as IsolateIndex<S>>::Output

Source§

unsafe fn isolate_unchecked(self, idx: I) -> <S as Isolate<I>>::Output

Unchecked version of isolate. Read more
Source§

fn try_isolate(self, idx: I) -> Option<<S as Isolate<I>>::Output>

Source§

fn isolate(self, idx: I) -> Self::Output
where Self: Sized,

Return a value at the given index. This is provided as the checked version of try_isolate that will panic if the equivalent try_isolate call is None, which typically means that the given index is out of bounds. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, N> PushArrayToVec<N> for T
where T: Clone, N: Array<T>,

Source§

fn push_to_vec(element: <N as Array<T>>::Array, set: &mut Vec<T>)

This method tells this type how it can be pushed to a Vec as an array.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.