Struct metatensor::Labels
source · pub struct Labels { /* private fields */ }
Expand description
A set of labels used to carry metadata associated with a tensor map.
This is similar to a list of named tuples, but stored as a 2D array of shape
(labels.count(), labels.size())
, with a of set names associated with the
columns of this array. Each row/entry in this array is unique, and they are
often (but not always) sorted in lexicographic order.
The main way to construct a new set of labels is to use a LabelsBuilder
.
Labels are internally reference counted and immutable, so cloning a Labels
should be a cheap operation.
Implementations§
source§impl Labels
impl Labels
sourcepub fn new<T, const N: usize>(names: [&str; N], values: &[[T; N]]) -> Labelswhere
T: Copy + Into<LabelValue>,
pub fn new<T, const N: usize>(names: [&str; N], values: &[[T; N]]) -> Labelswhere T: Copy + Into<LabelValue>,
Create a new set of Labels with the given names and values.
This is a convenience function replacing the manual use of
LabelsBuilder
. If you need more flexibility or incremental Labels
construction, use LabelsBuilder
.
Panics
If the set of names is not valid, or any of the value is duplicated
sourcepub fn empty(names: Vec<&str>) -> Labels
pub fn empty(names: Vec<&str>) -> Labels
Create a set of Labels
with the given names, containing no entries.
sourcepub fn single() -> Labels
pub fn single() -> Labels
Create a set of Labels
containing a single entry, to be used when
there is no relevant information to store.
sourcepub fn contains(&self, label: &[LabelValue]) -> bool
pub fn contains(&self, label: &[LabelValue]) -> bool
Check whether the given label
is part of this set of labels
sourcepub fn position(&self, value: &[LabelValue]) -> Option<usize>
pub fn position(&self, value: &[LabelValue]) -> Option<usize>
Get the position (i.e. row index) of the given label in the full labels array, or None.
sourcepub fn union(
&self,
other: &Labels,
first_mapping: Option<&mut [i64]>,
second_mapping: Option<&mut [i64]>
) -> Result<Labels, Error>
pub fn union( &self, other: &Labels, first_mapping: Option<&mut [i64]>, second_mapping: Option<&mut [i64]> ) -> Result<Labels, Error>
Take the union of self
with other
.
If requested, this function can also give the positions in the union
where each entry of the input Labels
ended up.
If first_mapping
(respectively second_mapping
) is Some
, it should
contain a slice of length self.count()
(respectively other.count()
)
that will be filled with the position of the entries in self
(respectively other
) in the union.
sourcepub fn intersection(
&self,
other: &Labels,
first_mapping: Option<&mut [i64]>,
second_mapping: Option<&mut [i64]>
) -> Result<Labels, Error>
pub fn intersection( &self, other: &Labels, first_mapping: Option<&mut [i64]>, second_mapping: Option<&mut [i64]> ) -> Result<Labels, Error>
Take the intersection of self with other
.
If requested, this function can also give the positions in the
intersection where each entry of the input Labels
ended up.
If first_mapping
(respectively second_mapping
) is Some
, it should
contain a slice of length self.count()
(respectively other.count()
)
that will be filled by with the position of the entries in self
(respectively other
) in the intersection. If an entry in self
or
other
are not used in the intersection, the mapping for this entry
will be set to -1
.
sourcepub fn iter(&self) -> LabelsIter<'_> ⓘ
pub fn iter(&self) -> LabelsIter<'_> ⓘ
Iterate over the entries in this set of labels
sourcepub fn iter_fixed_size<const N: usize>(&self) -> LabelsFixedSizeIter<'_, N> ⓘ
pub fn iter_fixed_size<const N: usize>(&self) -> LabelsFixedSizeIter<'_, N> ⓘ
Iterate over the entries in this set of labels as fixed-size arrays
source§impl Labels
impl Labels
sourcepub unsafe fn from_raw(raw: mts_labels_t) -> Labels
pub unsafe fn from_raw(raw: mts_labels_t) -> Labels
Create a new set of Labels
from a raw mts_labels_t
.
This function takes ownership of the mts_labels_t
and will call
mts_labels_free
on it.
Safety
The raw mts_labels_t
must have been returned by one of the function
returning mts_labels_t
in metatensor-core