Struct granite::Chain[][src]

pub struct Chain<T, S, I> where
    I: List<Element = S>,
    S: List<Element = T>, 
{ /* fields omitted */ }

A list data structure wrapping a list of lists used to prevent lag spikes when dealing with huge numbers of elements.

The usual heap array — Vec — has a major weakness if it is used to store large amounts of data: horrible lag spikes will occur during insertions and when capacity is exhausted and more elements need to be inserted (reallocation and relocation of a huge data set). Copying 256 bytes is not a major performance problem, copying 1024 bytes is something that you can afford to do once in a while, and copying 1 GiB is definitely not something you’d want to do, especially in performance-critical code.

Insertion problems can be partially solved by wrapping the Vec in a SparseStorage and its insert_and_shiftfix, but only if remove_and_shiftfix is also used at least exactly as much as insert_and_shiftfix. Insertions without removals and full reallocations, however, are left unsolved.

Chain resolves the issue by limiting the capacity to which a single list storage can be used (technically doesn’t have to be Vec, though that’s what makes the most sense to use), allocating small buffers anew without touching bigger old ones if more space is requested.

Implementations

impl<T, S, I> Chain<T, S, I> where
    S: List<Element = T>,
    I: List<Element = S>, 
[src]

pub fn iter(&self) -> Iter<'_, S, I>

Notable traits for Iter<'a, S, I>

impl<'a, S: List, I: List<Element = S>> Iterator for Iter<'a, S, I> type Item = StorageProxy<'a, S>;
[src]

Creates an iterator over references to the storages of the chain.

This allows for efficient iteration over the elements of a chain, especially if the index collection type (the list which contains the individual sub-lists) is a linked list or any other data structure with O(n) indexing. However, this requires an explicit nested loop, since creating an iterator struct which would flatten iterators into one contiguous iteration sequence is impossible, because that would require modelling a self-referential struct with lifetimes, which isn’t currently possible.

Also, the iterator doesn’t actually iterate over references to storages: it iterates over proxies instead, which are wrappers around references to storages with the sole purpose of making all other trait implementations and methods inaccessible to make sure the chain upholds the ListStorage safety contract in the presence of interior mutability in the used storage type.

pub fn iter_mut(&mut self) -> IterMut<'_, S, I>

Notable traits for IterMut<'a, S, I>

impl<'a, S: List, I: List<Element = S>> Iterator for IterMut<'a, S, I> type Item = StorageProxyMut<'a, S>;
[src]

Creates an iterator over mutable references to the storages of the chain.

This is the same as [iter] — so keep all considerations which apply to it in mind — but the proxies wrap mutable references and not immutable ones.

pub fn set_limit(&mut self, limit: usize)[src]

Sets the limit (in elements, not bytes) to which a buffer will be used before an additional allocation will be performed.

The limit must be even, i.e. a multiple of 2, due to how the limit is stored internally. Check out the source code if you’re curious.

pub fn limit(&self) -> usize[src]

Returns the currently set limit to which a buffer will be used before an additional allocation will be performed.

pub fn allocate_to_limit(&mut self, allocate_to_limit: bool)[src]

Sets whether additional buffers will be allocated to the limit right away or will first allocate as much as needed and only then rellocate to the limit. Enabled by default.

pub fn allocates_to_limit(&self) -> bool[src]

Returns whether allocate_to_limit is enabled.

pub fn num_storages(&self) -> usize[src]

Returns the number of separate storages used.

Trait Implementations

impl<T: Clone, S: Clone, I: Clone> Clone for Chain<T, S, I> where
    I: List<Element = S>,
    S: List<Element = T>, 
[src]

impl<T: Copy, S: Copy, I: Copy> Copy for Chain<T, S, I> where
    I: List<Element = S>,
    S: List<Element = T>, 
[src]

impl<T: Debug, S: Debug, I: Debug> Debug for Chain<T, S, I> where
    I: List<Element = S>,
    S: List<Element = T>, 
[src]

impl<T, S, I> ListStorage for Chain<T, S, I> where
    S: List<Element = T>,
    I: List<Element = S>, 
[src]

type Element = T

The type of values in the container.

fn add(&mut self, element: Self::Element) -> usize[src]

Uses push under the hood. The Chain should be wrapped in SparseStorage, not vice versa.

Auto Trait Implementations

impl<T, S, I> RefUnwindSafe for Chain<T, S, I> where
    I: RefUnwindSafe

impl<T, S, I> Send for Chain<T, S, I> where
    I: Send

impl<T, S, I> Sync for Chain<T, S, I> where
    I: Sync

impl<T, S, I> Unpin for Chain<T, S, I> where
    I: Unpin

impl<T, S, I> UnwindSafe for Chain<T, S, I> where
    I: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Slottable for T where
    T: Copy
[src]

impl<T, E> Storage for T where
    T: ListStorage<Element = E>,
    E: MoveFix
[src]

type Key = usize

The type used for element naming.

type Element = E

The type of the elements stored.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.