Struct parol::analysis::k_tuple::Terminals

source ·
pub struct Terminals { /* private fields */ }
Expand description

A collection of terminals

The terminals are stored in a 128 bit integer where each terminal is stored in a fixed number of bits. The number of bits is determined by the number of terminals to store. The maximum number of terminals when storing MAX_K terminals in 128 bits is: 128 / MAX_K = 128 / 10 = 12.8 => 12 bits The maximum number of terminals that can be stored is 2^12 = 4096. The maximum value of the bit count is therefore 12 and can safely be stored in four bits. We store a mask to more easily extract the terminals from the 128 bits unsigned integer. The mask to extract single terminals from the 128 bit unsigned integer is calculated as 2^bits - 1 that is equivalent to the expression !(!0u128 << bits) at runtime.

Since we use only 120 bits to store the terminals, we have 8 bits left. We use the 8 bits to store the index of the next insertion as well as the bit count used to calculate the mask. Therefore we split the highest 8 bits of the 128 bits unsigned integer as follows:

  • The higher 4 bits are used to store the number of bits used per terminal
  • The lower 4 bits are used to store the index of the next insertion

Implementations§

source§

impl Terminals

source

pub fn new(max_terminal_index: usize) -> Self

Creates a new item

use parol::analysis::k_tuple::Terminals;
use parol::analysis::compiled_terminal::CompiledTerminal;
let t = Terminals::new(1);
assert!(t.is_empty());
assert_eq!(0, t.len(), "len");
assert_eq!(0, t.k_len(5), "k_len");
assert_eq!(None, t.get(0));
assert_eq!(None, t.get(9));
source

pub fn eps(max_terminal_index: usize) -> Terminals

Creates a new item with epsilon semantic

use parol::analysis::k_tuple::{Terminals, TerminalMappings};
use parol::analysis::compiled_terminal::CompiledTerminal;
let t = Terminals::eps(1);
assert!(!t.is_empty());
assert_eq!(1, t.len(), "len");
assert_eq!(1, t.k_len(5), "k_len");
assert_eq!(Some(CompiledTerminal::eps()), t.get(0));
assert_eq!(None, t.get(1));
assert_eq!(None, t.get(9));
source

pub fn end(max_terminal_index: usize) -> Terminals

Creates a new item with end (EOI) semantic Such a terminal can’t be extended, i.e. you can’t append more terminals

use parol::analysis::k_tuple::{Terminals, TerminalMappings};
use parol::analysis::compiled_terminal::CompiledTerminal;
let t = Terminals::end(1);
assert!(!t.is_empty());
assert_eq!(1, t.len());
assert_eq!(1, t.k_len(5));
assert_eq!(Some(CompiledTerminal::end()), t.get(0));
assert_eq!(None, t.get(1));
assert_eq!(None, t.get(9));
source

pub fn of(k: usize, other: Self) -> Self

Creates a new object with maximum k length from another object

source

pub fn len(&self) -> usize

Returns the length of the collection

source

pub fn is_empty(&self) -> bool

Checks if the collection is empty

source

pub fn next_index(&self) -> u8

Returns the index of the next insertion The highest 8 bits of t are used to store the index of the next insertion in it’s lowest 4 bits.

source

pub fn inc_index(&mut self)

Increments the index of the next insertion

source

pub fn bits(&self) -> u8

Returns the bits used per terminal The highest 8 bits of t are used to store the number of bits used per terminal in it’s highest 4 bits.

use parol::analysis::k_tuple::Terminals;
let t = Terminals::eps(1);
assert_eq!(2, t.bits());
source

pub fn set_bits(&mut self, bits: u8)

Sets the number of bits used per terminal

source

pub fn mask(&self) -> u128

Returns the mask used to extract the terminal at position i The mask is calculated as 2^bits - 1 that is equivalent to the expression !(!0u128 << bits).

source

pub fn is_k_complete(&self, k: usize) -> bool

Checks if the collection is k-complete, i.e. no terminals can be added

use parol::analysis::k_tuple::Terminals;
let t = Terminals::end(1);
assert!(t.is_k_complete(5));
source

pub fn k_len(&self, k: usize) -> usize

Returns the k-length, i.e. the number of symbols that contributes to lookahead sizes

source

pub fn clear(&mut self)

Clears the collection

source

pub fn k_concat(self, other: &Self, k: usize) -> Self

Concatenates two collections with respect to the rules of k-concatenation

use parol::analysis::k_tuple::{Terminals, TerminalMappings};
use parol::analysis::compiled_terminal::CompiledTerminal;
let t1 = Terminals::eps(1);
let t2 = Terminals::end(1);
let t = t1.k_concat(&t2, 5);
assert!(t.is_k_complete(5));
assert_eq!(1, t.len());
assert_eq!(Some(CompiledTerminal::end()), t.get(0));
let t = t2.k_concat(&t1, 5);
assert!(t.is_k_complete(5));
assert_eq!(1, t.len());
assert_eq!(Some(CompiledTerminal::end()), t.get(0));
let mut t1 = Terminals::new(6);
t1.extend([1, 2, 3].iter().cloned());
let mut t2 = Terminals::new(6);
t2.extend([4, 5, 6].iter().cloned());
let t = t1.k_concat(&t2, 5);
assert!(t.is_k_complete(5));
assert_eq!(5, t.len());
assert_eq!(Some(CompiledTerminal(1)), t.get(0));
assert_eq!(Some(CompiledTerminal(2)), t.get(1));
assert_eq!(Some(CompiledTerminal(3)), t.get(2));
assert_eq!(Some(CompiledTerminal(4)), t.get(3));
assert_eq!(Some(CompiledTerminal(5)), t.get(4));
assert_eq!(None, t.get(5));
source

pub fn push(&mut self, t: CompiledTerminal) -> Result<(), String>

Adds a new terminal to self if max size is not reached yet and if last is not EOI

source

pub fn is_eps(&self) -> bool

Checks if self is an Epsilon

source

pub fn iter(&self) -> TermIt

Creates an iterator over the terminals

source

pub fn get(&self, i: usize) -> Option<CompiledTerminal>

Returns the terminal at position i

source

pub fn set(&mut self, i: usize, t: CompiledTerminal)

Sets the terminal at position i

Trait Implementations§

source§

impl Clone for Terminals

source§

fn clone(&self) -> Terminals

Returns a copy 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 Debug for Terminals

source§

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

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

impl Default for Terminals

source§

fn default() -> Terminals

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

impl Display for Terminals

source§

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

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

impl Extend<CompiledTerminal> for Terminals

source§

fn extend<I: IntoIterator<Item = CompiledTerminal>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
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 Extend<u16> for Terminals

source§

fn extend<I: IntoIterator<Item = TerminalIndex>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
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 From<&Terminals> for u128

source§

fn from(t: &Terminals) -> Self

Converts to this type from the input type.
source§

impl Hash for Terminals

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Terminals

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Terminals

source§

fn eq(&self, other: &Terminals) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Terminals

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Copy for Terminals

source§

impl Eq for Terminals

source§

impl StructuralPartialEq for Terminals

Auto Trait Implementations§

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

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> ToOwned for T
where T: Clone,

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<N> NodeTrait for N
where N: Copy + Ord + Hash,

source§

impl<T> Sequence for T
where T: Eq + Hash,