Skip to main content

Doubling

Struct Doubling 

Source
pub struct Doubling<V> { /* private fields */ }
Expand description

ダブリング

§Examples

use doubling::{Doubling, Transition, Value};

#[derive(Debug, PartialEq)]
struct Sum(i64);

impl Value for Sum {
    fn op(&self, other: &Self) -> Self {
        Sum(self.0 + other.0)
    }
}

struct E {
    to: usize,
    value: i64,
}

// 0, 1, 2, 0, 1, 2, ...
let n = 3;
let to = vec![
    E { to: 1, value: 1 },
    E { to: 2, value: 10 },
    E { to: 0, value: 100 },
];
let doubling = Doubling::new(n, 100, |i| {
    let e = &to[i];
    Transition::new(e.to, Sum(e.value))
});

assert_eq!(
    doubling.fold(0, 4, Sum(0), |acc, t| Sum(acc.0 + t.value.0)),
    // 0 -> 1 -> 2 -> 0 -> 1
    Sum(1 + 10 + 100 + 1)
);

Implementations§

Source§

impl<V> Doubling<V>
where V: Value,

Source

pub fn new<F>(n_state: usize, max_steps: usize, step1: F) -> Self
where F: Fn(usize) -> Transition<V>,

ダブリングのテーブルを構築します。

step1(i)は状態iから1回の遷移における

  • 遷移先の状態
  • その遷移にともなう値

を返す関数。

Source

pub fn get(&self, start: usize, k: usize) -> &Transition<V>

状態startから長さpow(2, k)の遷移を返します。

Source

pub fn fold<A, F>(&self, start: usize, step: usize, init: A, f: F) -> A
where F: FnMut(A, &Transition<V>) -> A,

状態startからstep回の遷移、初期値initから始めてfで畳みこんだ結果を返します。

Trait Implementations§

Source§

impl<V: Clone> Clone for Doubling<V>

Source§

fn clone(&self) -> Doubling<V>

Returns a duplicate 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<V: Debug> Debug for Doubling<V>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V> Freeze for Doubling<V>

§

impl<V> RefUnwindSafe for Doubling<V>
where V: RefUnwindSafe,

§

impl<V> Send for Doubling<V>
where V: Send,

§

impl<V> Sync for Doubling<V>
where V: Sync,

§

impl<V> Unpin for Doubling<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for Doubling<V>

§

impl<V> UnwindSafe for Doubling<V>
where V: UnwindSafe,

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.