pub struct DictionaryEncoder<T: Eq + Hash + Clone> {
pub vocab: Vec<T>,
pub unknown_policy: UnknownPolicy,
/* private fields */
}Expand description
Dictionary encoder for a generic value type T.
T must be Eq + Hash + Clone + Ord; the Ord bound is used only for
tie-breaking when two values have the same frequency.
Fields§
§vocab: Vec<T>Vocabulary: vocab[code] → value. Sorted by descending frequency.
unknown_policy: UnknownPolicyPolicy for values not present in the vocabulary.
Implementations§
Source§impl<T: Eq + Hash + Clone + Ord> DictionaryEncoder<T>
impl<T: Eq + Hash + Clone + Ord> DictionaryEncoder<T>
Sourcepub fn new(unknown_policy: UnknownPolicy) -> Self
pub fn new(unknown_policy: UnknownPolicy) -> Self
Create an empty encoder with the given unknown policy.
Sourcepub fn fit(data: &[T], unknown_policy: UnknownPolicy) -> Result<Self>
pub fn fit(data: &[T], unknown_policy: UnknownPolicy) -> Result<Self>
Build a DictionaryEncoder by scanning data and counting frequencies.
The vocabulary is sorted by descending frequency (ties broken by
value ordering). Codes 0..vocab.len() are assigned in that order.
§Errors
Returns an error if the number of unique values exceeds u16::MAX - 1
(= 65534).
Sourcepub fn encode(&self, data: &[T]) -> Result<Vec<u16>>where
T: Debug,
pub fn encode(&self, data: &[T]) -> Result<Vec<u16>>where
T: Debug,
Encode a slice of values to codes.
Behaviour for unknown values is governed by self.unknown_policy.
Sourcepub fn decode(&self, codes: &[u16]) -> Result<Vec<T>>
pub fn decode(&self, codes: &[u16]) -> Result<Vec<T>>
Decode a slice of codes back to values.
Returns an error if any code equals u16::MAX (null sentinel) or is
out of bounds.
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
Return the current vocabulary size.
Sourcepub fn encode_single(&self, value: &T) -> Result<u16>where
T: Debug,
pub fn encode_single(&self, value: &T) -> Result<u16>where
T: Debug,
Encode a single value to its code.
Sourcepub fn encode_single_mut(&mut self, value: T) -> Result<u16>where
T: Debug,
pub fn encode_single_mut(&mut self, value: T) -> Result<u16>where
T: Debug,
Encode a single value, potentially adding it to the vocabulary
(only meaningful when unknown_policy == AddToVocab).
Sourcepub fn decode_single(&self, code: u16) -> Result<&T>
pub fn decode_single(&self, code: u16) -> Result<&T>
Decode a single code to a reference to the corresponding value.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for DictionaryEncoder<T>
impl<T> RefUnwindSafe for DictionaryEncoder<T>where
T: RefUnwindSafe,
impl<T> Send for DictionaryEncoder<T>where
T: Send,
impl<T> Sync for DictionaryEncoder<T>where
T: Sync,
impl<T> Unpin for DictionaryEncoder<T>where
T: Unpin,
impl<T> UnsafeUnpin for DictionaryEncoder<T>
impl<T> UnwindSafe for DictionaryEncoder<T>where
T: UnwindSafe,
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.