pub struct ColumnStore { /* private fields */ }Expand description
Order-preserving, duplicate-tolerant column store.
Step .1 of the duplicate-column-label work (br-frankenpandas-b6orh,
parent br-frankenpandas-ih4t0): pandas lets a DataFrame carry repeated
column labels and a plain BTreeMap<String, Column> structurally cannot.
This type can represent repeats while behaving exactly like the map it
replaces, so the representation lands before any operation is allowed to
build one (that is .4).
§Layout
primary— the FIRST occurrence of every distinct name.BTreeMapiterates name-sorted, which is load-bearing:columns().keys()order is observed by ~40 downstream call sites, while the user-visible column ORDER lives separately inDataFrame::column_order.repeats— every occurrence after the first, kept name-sorted soColumnStore::itermerges the two in one pass. Empty on every path that exists today: nothing in the crate constructs a duplicate yet, so the merge degenerates to a straightprimarywalk.
Single-valued reads (get, contains_key, Index, and everything
reached through Deref) resolve to the FIRST
occurrence. That is unobservable today because duplicates are still
rejected at every construction site; .4 turns it into the documented
rule once ops may create them.
§Why the BTreeMap spine is kept
DataFrame::columns() hands out a &BTreeMap<String, Column> and is
called from 223 in-crate plus 149 downstream sites. A purely Vec-backed
store cannot produce that borrow without materializing and caching a
parallel BTreeMap, which would double column-map memory and add a map
build per frame to the default build. Keeping primary as a real
BTreeMap makes columns() a zero-cost borrow and From<BTreeMap> a
move, so the ~150 frame-rebuild sites keep their current cost exactly.
The fully positional Vec layout becomes affordable in .3, after .2
removes the &BTreeMap leak.
§Not yet duplicate-aware
Serialize writes primary as a map, because a JSON object cannot hold
repeated keys; the wire format has to change in .4 together with the ops
that can produce repeats. Likewise every method reached through Deref
sees primary only. Both are safe while repeats is unreachable, and
.2 removes the Deref escape hatch.
Implementations§
Source§impl ColumnStore
impl ColumnStore
Sourcepub fn from_pairs(pairs: impl IntoIterator<Item = (String, Column)>) -> Self
pub fn from_pairs(pairs: impl IntoIterator<Item = (String, Column)>) -> Self
Build from an ordered pair sequence, PRESERVING repeated names and arrival order.
Sourcepub fn push(&mut self, name: String, column: Column)
pub fn push(&mut self, name: String, column: Column)
Append a column at the next position, allowing repeated names.
Sourcepub fn get_mut(&mut self, name: &str) -> Option<&mut Column>
pub fn get_mut(&mut self, name: &str) -> Option<&mut Column>
Mutable reference to the first column stored under name, if any.
Sourcepub fn get_all<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = &'a Column> + 'a
pub fn get_all<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = &'a Column> + 'a
Every column stored under name, first occurrence first.
Sourcepub fn occurrences(&self, name: &str) -> usize
pub fn occurrences(&self, name: &str) -> usize
How many columns are stored under name (0, 1, or more).
pub fn contains_key(&self, name: &str) -> bool
Sourcepub fn has_duplicates(&self) -> bool
pub fn has_duplicates(&self) -> bool
Whether any column name appears more than once.
pub fn is_empty(&self) -> bool
Sourcepub fn column_at_mut(&mut self, position: usize) -> Option<&mut Column>
pub fn column_at_mut(&mut self, position: usize) -> Option<&mut Column>
O(1) mutable positional column access.
Sourcepub fn position_of(&self, name: &str) -> Option<usize>
pub fn position_of(&self, name: &str) -> Option<usize>
First position of name, if present.
Sourcepub fn positions_of(&self, name: &str) -> &[usize]
pub fn positions_of(&self, name: &str) -> &[usize]
All positions of name, in encounter order.
Sourcepub fn ordered_names(&self) -> impl ExactSizeIterator<Item = &String> + '_
pub fn ordered_names(&self) -> impl ExactSizeIterator<Item = &String> + '_
Iterator over column names in positional (insertion) order.
Sourcepub fn ordered_columns(&self) -> impl ExactSizeIterator<Item = &Column> + '_
pub fn ordered_columns(&self) -> impl ExactSizeIterator<Item = &Column> + '_
Iterator over columns in positional (insertion) order.
Sourcepub fn iter_positional(
&self,
) -> impl ExactSizeIterator<Item = (&String, &Column)> + '_
pub fn iter_positional( &self, ) -> impl ExactSizeIterator<Item = (&String, &Column)> + '_
Iterator over (name, column) pairs in positional (insertion) order.
Sourcepub fn iter(&self) -> ColumnStoreIter<'_> ⓘ
pub fn iter(&self) -> ColumnStoreIter<'_> ⓘ
Name-sorted walk over every column, repeats included.
Sourcepub fn keys(&self) -> impl Iterator<Item = &String> + '_
pub fn keys(&self) -> impl Iterator<Item = &String> + '_
Name-sorted walk over column names, repeats included.
Sourcepub fn values(&self) -> impl Iterator<Item = &Column> + '_
pub fn values(&self) -> impl Iterator<Item = &Column> + '_
Name-sorted walk over columns, repeats included.
Sourcepub fn insert(&mut self, name: String, column: Column) -> Option<Column>
pub fn insert(&mut self, name: String, column: Column) -> Option<Column>
Replace the FIRST column stored under name, or append if absent.
Returns the column it displaced, if any.
Sourcepub fn replace_all(&mut self, name: &str, column: Column)
pub fn replace_all(&mut self, name: &str, column: Column)
Replace ALL columns stored under name with copies of column, or append if absent.
Sourcepub fn remove(&mut self, name: &str) -> Option<Column>
pub fn remove(&mut self, name: &str) -> Option<Column>
Remove EVERY column stored under name, returning the first.
Sourcepub fn iter_mut(&mut self) -> ColumnStoreIterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> ColumnStoreIterMut<'_> ⓘ
Mutable walk over all stored columns in positional order.
Sourcepub fn into_ordered_pairs(self) -> Vec<(String, Column)>
pub fn into_ordered_pairs(self) -> Vec<(String, Column)>
Owned pairs in positional (arrival) order.
Sourcepub fn reorder(&mut self, order: &[String])
pub fn reorder(&mut self, order: &[String])
Reorder columns according to order, keeping any unlisted columns at the end.
Each listed name takes the next of its stored columns in position order; a name listed more often than it is stored repeats its first placed column; a name never stored is skipped. Linear in the column count through the name -> positions lookup: it searched every slot for each name, O(n^2), and a 100k-column frame (the transpose of 100k rows) spent 26 s here.
Trait Implementations§
Source§impl Clone for ColumnStore
impl Clone for ColumnStore
Source§impl Debug for ColumnStore
impl Debug for ColumnStore
Source§impl Default for ColumnStore
impl Default for ColumnStore
Source§impl<'de> Deserialize<'de> for ColumnStore
impl<'de> Deserialize<'de> for ColumnStore
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl From<ColumnStore> for BTreeMap<String, Column>
impl From<ColumnStore> for BTreeMap<String, Column>
Source§fn from(store: ColumnStore) -> Self
fn from(store: ColumnStore) -> Self
Source§impl Index<&str> for ColumnStore
impl Index<&str> for ColumnStore
Source§impl IntoIterator for ColumnStore
impl IntoIterator for ColumnStore
Source§impl<'a> IntoIterator for &'a ColumnStore
impl<'a> IntoIterator for &'a ColumnStore
Source§impl<'a> IntoIterator for &'a mut ColumnStore
impl<'a> IntoIterator for &'a mut ColumnStore
Source§impl PartialEq for ColumnStore
impl PartialEq for ColumnStore
Source§impl Serialize for ColumnStore
impl Serialize for ColumnStore
impl StructuralPartialEq for ColumnStore
Auto Trait Implementations§
impl !RefUnwindSafe for ColumnStore
impl !UnwindSafe for ColumnStore
impl Freeze for ColumnStore
impl Send for ColumnStore
impl Sync for ColumnStore
impl Unpin for ColumnStore
impl UnsafeUnpin for ColumnStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more