syntax: fold: use move semantics for efficient folding.
This commit is contained in:
parent
cccb6f84a3
commit
9259b022f8
File diff suppressed because it is too large
Load Diff
@ -12,6 +12,8 @@ use std::mem;
|
||||
use std::slice;
|
||||
use std::vec;
|
||||
|
||||
use fold::MoveMap;
|
||||
|
||||
/// A vector type optimized for cases where the size is almost always 0 or 1
|
||||
pub struct SmallVector<T> {
|
||||
repr: SmallVectorRepr<T>,
|
||||
@ -20,7 +22,7 @@ pub struct SmallVector<T> {
|
||||
enum SmallVectorRepr<T> {
|
||||
Zero,
|
||||
One(T),
|
||||
Many(Vec<T> ),
|
||||
Many(Vec<T>),
|
||||
}
|
||||
|
||||
impl<T> Collection for SmallVector<T> {
|
||||
@ -160,6 +162,17 @@ impl<T> Iterator<T> for MoveItems<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MoveMap<T> for SmallVector<T> {
|
||||
fn move_map(self, f: |T| -> T) -> SmallVector<T> {
|
||||
let repr = match self.repr {
|
||||
Zero => Zero,
|
||||
One(v) => One(f(v)),
|
||||
Many(vs) => Many(vs.move_map(f))
|
||||
};
|
||||
SmallVector { repr: repr }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
Loading…
Reference in New Issue
Block a user