1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
extern crate rayon;
extern crate num;

use std::ops;

mod operator;
pub use operator::{Neg, Not,
                   Add, Sub, Mul, Div,
                   BitOr, BitAnd, BitXor,
                   Eq, Ne, Lt, Le, Gt, Ge};
mod raw;
pub use raw::{Zip, Map};

pub mod iterators;

mod simple;
pub use simple::{E, Constant, Switch, Rev};

pub mod evaluation;

pub fn evaluate<E>(dst: &mut [E::Element], e: E)
    where E: Expression
{
    evaluation::evaluate(e, evaluation::SetArray(dst))
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Length {
    Finite(usize),
    Infinite,
}

impl Length {
    fn compatible(self, other: Length) -> bool {
        match (self, other) {
            (Length::Finite(a), Length::Finite(b)) => a == b,
            (Length::Infinite, _) | (_, Length::Infinite) => true,
        }
    }
}

pub trait Expression: Send {
    type Element: Send;
    type Values: Iterator<Item = Self::Element> + DoubleEndedIterator;
    type Rev: Expression<Element = Self::Element>;

    fn length(&self) -> Length;

    fn values(self) -> Self::Values;

    fn split(self, round_up: bool) -> (Self, Self);

    fn rev(self) -> Self::Rev;

    fn sum<T>(self) -> Self::Element
        where Self: Sized + Expression<Element = T>, T: num::Zero + ops::Add<T, Output = T>
    {
        evaluation::evaluate(self, evaluation::Sum)
    }

    fn max(self) -> <evaluation::Max as evaluation::Reduce<Self::Element>>::Output
        where Self: Sized, evaluation::Max: evaluation::Reduce<Self::Element>
    {
        evaluation::evaluate(self, evaluation::Max)
    }
    fn min(self) -> <evaluation::Min as evaluation::Reduce<Self::Element>>::Output
        where Self: Sized, evaluation::Min: evaluation::Reduce<Self::Element>
    {
        evaluation::evaluate(self, evaluation::Min)
    }

    // TODO: this need to handle SIMD etc.
    fn zip<E2: Expression>(self, e: E2) -> Zip<Self, E2> where Self: Sized {
        raw::make_zip(self, e)
    }
    // TODO: this needs to handle SIMD etc.
    fn map<O, F: FnMut(Self::Element) -> O>(self, f: F) -> Map<Self, F> where Self: Sized {
        raw::make_map(self, f)
    }

    fn switch<T, E>(self, then: T, else_: E) -> Switch<Self, T, E>
        where Self: Sized + Expression<Element = bool>, T: Expression, E: Expression<Element = T::Element>
    {
        simple::make_switch(self, then, else_)
    }

    fn eq<E>(self, other: E) -> Eq<Self, E>
        where Self: Sized, E: Expression, Self::Element: PartialEq<E::Element>,
    {
        operator::make_eq(self, other)
    }

    fn ne<E>(self, other: E) -> Ne<Self, E>
        where Self: Sized, E: Expression, Self::Element: PartialEq<E::Element>,
    {
        operator::make_ne(self, other)
    }

    fn lt<E>(self, other: E) -> Lt<Self, E>
        where Self: Sized, E: Expression, Self::Element: PartialOrd<E::Element>,
    {
        operator::make_lt(self, other)
    }

    fn le<E>(self, other: E) -> Le<Self, E>
        where Self: Sized, E: Expression, Self::Element: PartialOrd<E::Element>,
    {
        operator::make_le(self, other)
    }

    fn gt<E>(self, other: E) -> Gt<Self, E>
        where Self: Sized, E: Expression, Self::Element: PartialOrd<E::Element>,
    {
        operator::make_gt(self, other)
    }

    fn ge<E>(self, other: E) -> Ge<Self, E>
        where Self: Sized, E: Expression, Self::Element: PartialOrd<E::Element>,
    {
        operator::make_ge(self, other)
    }
}