<commit_before>#![feature(allocator)]

#![allocator]
#![no_std]

#![feature(const_fn)]

use spin::Mutex;
use linked_list_allocator::Heap;

extern crate spin;
extern crate linked_list_allocator;

pub const HEAP_START: usize = 0o_000_001_000_000_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB

#[macro_use]
extern crate lazy_static;

lazy_static! {
    static ref HEAP: Mutex<Heap> = Mutex::new(unsafe {
        Heap::new(HEAP_START, HEAP_SIZE)
    });
}

#[no_mangle]
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
    HEAP.lock().allocate_first_fit(size, align).expect("out of memory")
}

#[no_mangle]
pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) {
    unsafe { HEAP.lock().deallocate(ptr, size, align) };
}

#[no_mangle]
pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
    size
}

#[no_mangle]
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, size: usize,
                                        _new_size: usize, _align: usize) -> usize
{
    size
}

#[no_mangle]
pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize,
                                align: usize) -> *mut u8 {
    use core::{ptr, cmp};

    // from: https://github.com/rust-lang/rust/blob/
    //     c66d2380a810c9a2b3dbb4f93a830b101ee49cc2/
    //     src/liballoc_system/lib.rs#L98-L101

    let new_ptr = __rust_allocate(new_size, align);
    unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) };
    __rust_deallocate(ptr, size, align);
    new_ptr
}

#[test]
fn it_works() {
}
<commit_msg>Implement the new __rust_allocate_zeroed function<commit_after>#![feature(allocator)]

#![allocator]
#![no_std]

#![feature(const_fn)]

use spin::Mutex;
use linked_list_allocator::Heap;

extern crate spin;
extern crate linked_list_allocator;

pub const HEAP_START: usize = 0o_000_001_000_000_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB

#[macro_use]
extern crate lazy_static;

lazy_static! {
    static ref HEAP: Mutex<Heap> = Mutex::new(unsafe {
        Heap::new(HEAP_START, HEAP_SIZE)
    });
}

#[no_mangle]
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
    HEAP.lock().allocate_first_fit(size, align).expect("out of memory")
}

#[no_mangle]
pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) {
    unsafe { HEAP.lock().deallocate(ptr, size, align) };
}

#[no_mangle]
pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
    size
}

#[no_mangle]
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, size: usize,
                                        _new_size: usize, _align: usize) -> usize
{
    size
}

#[no_mangle]
pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize,
                                align: usize) -> *mut u8 {
    use core::{ptr, cmp};

    // from: https://github.com/rust-lang/rust/blob/
    //     c66d2380a810c9a2b3dbb4f93a830b101ee49cc2/
    //     src/liballoc_system/lib.rs#L98-L101

    let new_ptr = __rust_allocate(new_size, align);
    unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) };
    __rust_deallocate(ptr, size, align);
    new_ptr
}

#[no_mangle]
pub extern fn __rust_allocate_zeroed(size: usize, _align: usize) -> *mut u8 {
    use core::ptr;

    // allocate the needed memory
    let new_ptr = __rust_allocate(size, _align);

    // zero the new allocated memory
    unsafe { ptr::write_bytes(new_ptr, 0, size); }

    new_ptr
}

#[test]
fn it_works() {
}
<|endoftext|>


<commit_before>use std::cmp;

use lz77::LongestMatch;
use util::{ZOPFLI_CACHE_LENGTH, ZOPFLI_MAX_MATCH, ZOPFLI_MIN_MATCH};

// Cache used by ZopfliFindLongestMatch to remember previously found length/dist
// values.
// This is needed because the squeeze runs will ask these values multiple times for
// the same position.
// Uses large amounts of memory, since it has to remember the distance belonging
// to every possible shorter-than-the-best length (the so called "sublen" array).
pub struct ZopfliLongestMatchCache {
    length: Vec<u16>,
    dist: Vec<u16>,
    sublen: Vec<u8>,
}

impl ZopfliLongestMatchCache {
    pub fn new(blocksize: usize) -> ZopfliLongestMatchCache {
        ZopfliLongestMatchCache {
            /* length > 0 and dist 0 is invalid combination, which indicates on purpose
            that this cache value is not filled in yet. */
            length: vec![1; blocksize],
            dist: vec![0; blocksize],
            /* Rather large amount of memory. */
            sublen: vec![0; ZOPFLI_CACHE_LENGTH * blocksize * 3],
        }
    }

    pub fn length_at(&self, pos: usize) -> u16 {
        self.length[pos]
    }

    pub fn dist_at(&self, pos: usize) -> u16 {
        self.dist[pos]
    }

    pub fn store_length_at(&mut self, pos: usize, val: u16) {
        self.length[pos] = val;
    }

    pub fn store_dist_at(&mut self, pos: usize, val: u16) {
        self.dist[pos] = val;
    }

    /// Returns the length up to which could be stored in the cache.
    pub fn max_sublen(&self, pos: usize) -> u32 {
        let start = ZOPFLI_CACHE_LENGTH * pos * 3;
        if self.sublen[start + 1] == 0 && self.sublen[start + 2] == 0 {
            return 0;  // No sublen cached.
        }
        self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)] as u32 + 3
    }

    /// Stores sublen array in the cache.
    pub fn store_sublen(&mut self, sublen: &[u16], pos: usize, length: usize) {
        if length < 3 {
            return;
        }

        let start = ZOPFLI_CACHE_LENGTH * pos * 3;
        let mut i = 3;
        let mut j = 0;
        let mut bestlength = 0;
        while i <= length {
            if i == length || sublen[i] != sublen[i + 1] {
                self.sublen[start + (j * 3)] = (i - 3) as u8;
                self.sublen[start + (j * 3 + 1)] = sublen[i].wrapping_rem(256) as u8;
                self.sublen[start + (j * 3 + 2)] = ((sublen[i] >> 8)).wrapping_rem(256) as u8;
                bestlength = i as u32;
                j += 1;
                if j >= ZOPFLI_CACHE_LENGTH {
                    break;
                }
            }
            i += 1;
        }

        if j < ZOPFLI_CACHE_LENGTH {
            assert!(bestlength == length as u32);
            self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)] = (bestlength - 3) as u8;
        } else {
            assert!(bestlength <= length as u32);
        }
        assert!(bestlength == self.max_sublen(pos));
    }

    /// Extracts sublen array from the cache.
    pub fn fetch_sublen(&self, pos: usize, length: usize, sublen: &mut [u16]) {
        if length < 3 {
            return;
        }

        let start = ZOPFLI_CACHE_LENGTH * pos * 3;
        let maxlength = self.max_sublen(pos) as usize;
        let mut prevlength = 0;

        for j in 0..ZOPFLI_CACHE_LENGTH {
            let length = self.sublen[start + (j * 3)] as usize + 3;
            let dist = self.sublen[start + (j * 3 + 1)] as u16 + 256 * self.sublen[start + (j * 3 + 2)] as u16;

            let mut i = prevlength;
            while i <= length {
                sublen[i] = dist;
                i += 1;
            }
            if length == maxlength {
                break;
            }
            prevlength = length + 1;
        }
    }
}

pub trait Cache {
    fn try_get(&self, pos: usize, limit: usize, sublen: &mut Option<&mut [u16]>, blockstart: usize) -> LongestMatch;
    fn store(&mut self, pos: usize, limit: usize, sublen: &mut Option<&mut [u16]>, distance: u16, length: u16, blockstart: usize);
}

pub struct NoCache;

impl Cache for NoCache {
    fn try_get(&self, _: usize, limit: usize, _: &mut Option<&mut [u16]>, _: usize) -> LongestMatch {
        LongestMatch::new(limit)
    }

    fn store(&mut self, _: usize, _: usize, _: &mut Option<&mut [u16]>, _: u16, _: u16, _: usize) {
        // Nowhere to store
    }
}

impl Cache for ZopfliLongestMatchCache {
    fn try_get(&self, pos: usize, mut limit: usize, sublen: &mut Option<&mut [u16]>, blockstart: usize) -> LongestMatch {
        let mut longest_match = LongestMatch::new(limit);

        /* The LMC cache starts at the beginning of the block rather than the
        beginning of the whole array. */
        let lmcpos = pos - blockstart;

        /* Length > 0 and dist 0 is invalid combination, which indicates on purpose
        that this cache value is not filled in yet. */
        let length_lmcpos = self.length_at(lmcpos);
        let dist_lmcpos = self.dist_at(lmcpos);
        let cache_available = length_lmcpos == 0 || dist_lmcpos != 0;
        let max_sublen = self.max_sublen(lmcpos);
        let limit_ok_for_cache = cache_available &&
            (limit == ZOPFLI_MAX_MATCH || length_lmcpos <= limit as u16 ||
             (sublen.is_some() && max_sublen >= limit as u32));

        if limit_ok_for_cache && cache_available {
            if sublen.is_none() || length_lmcpos as u32 <= max_sublen {
                let length = cmp::min(length_lmcpos, limit as u16);
                let distance;
                if let &mut Some(ref mut subl) = sublen {
                    self.fetch_sublen(lmcpos, length as usize, subl);
                    distance = subl[length as usize];

                    if limit == ZOPFLI_MAX_MATCH && length >= ZOPFLI_MIN_MATCH as u16 {
                        assert!(subl[length as usize] == dist_lmcpos);
                    }
                } else {
                    distance = dist_lmcpos;
                }
                longest_match.distance = distance;
                longest_match.length = length;
                longest_match.from_cache = true;
                return longest_match;
            }
            /* Can't use much of the cache, since the "sublens" need to be calculated,
            but at least we already know when to stop. */
            limit = length_lmcpos as usize;
            longest_match.limit = limit;
        }

        longest_match
    }

    fn store(&mut self, pos: usize, limit: usize, sublen: &mut Option<&mut [u16]>, distance: u16, length: u16, blockstart: usize) {
        if let &mut Some(ref mut subl) = sublen {
            /* Length > 0 and dist 0 is invalid combination, which indicates on purpose
            that this cache value is not filled in yet. */
            let lmcpos = pos - blockstart;
            let cache_available = self.length_at(lmcpos) == 0 || self.dist_at(lmcpos) != 0;

            if limit == ZOPFLI_MAX_MATCH && !cache_available {
                assert!(self.length_at(lmcpos) == 1 && self.dist_at(lmcpos) == 0);
                if length < ZOPFLI_MIN_MATCH as u16 {
                    self.store_dist_at(lmcpos, 0);
                    self.store_length_at(lmcpos, 0);
                } else {
                    self.store_dist_at(lmcpos, distance);
                    self.store_length_at(lmcpos, length);
                }
                assert!(!(self.length_at(lmcpos) == 1 && self.dist_at(lmcpos) == 0));
                self.store_sublen(subl, lmcpos, length as usize);
            }
        }
    }
}
<commit_msg>Remove a duplicated checked condition<commit_after>use std::cmp;

use lz77::LongestMatch;
use util::{ZOPFLI_CACHE_LENGTH, ZOPFLI_MAX_MATCH, ZOPFLI_MIN_MATCH};

// Cache used by ZopfliFindLongestMatch to remember previously found length/dist
// values.
// This is needed because the squeeze runs will ask these values multiple times for
// the same position.
// Uses large amounts of memory, since it has to remember the distance belonging
// to every possible shorter-than-the-best length (the so called "sublen" array).
pub struct ZopfliLongestMatchCache {
    length: Vec<u16>,
    dist: Vec<u16>,
    sublen: Vec<u8>,
}

impl ZopfliLongestMatchCache {
    pub fn new(blocksize: usize) -> ZopfliLongestMatchCache {
        ZopfliLongestMatchCache {
            /* length > 0 and dist 0 is invalid combination, which indicates on purpose
            that this cache value is not filled in yet. */
            length: vec![1; blocksize],
            dist: vec![0; blocksize],
            /* Rather large amount of memory. */
            sublen: vec![0; ZOPFLI_CACHE_LENGTH * blocksize * 3],
        }
    }

    pub fn length_at(&self, pos: usize) -> u16 {
        self.length[pos]
    }

    pub fn dist_at(&self, pos: usize) -> u16 {
        self.dist[pos]
    }

    pub fn store_length_at(&mut self, pos: usize, val: u16) {
        self.length[pos] = val;
    }

    pub fn store_dist_at(&mut self, pos: usize, val: u16) {
        self.dist[pos] = val;
    }

    /// Returns the length up to which could be stored in the cache.
    pub fn max_sublen(&self, pos: usize) -> u32 {
        let start = ZOPFLI_CACHE_LENGTH * pos * 3;
        if self.sublen[start + 1] == 0 && self.sublen[start + 2] == 0 {
            return 0;  // No sublen cached.
        }
        self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)] as u32 + 3
    }

    /// Stores sublen array in the cache.
    pub fn store_sublen(&mut self, sublen: &[u16], pos: usize, length: usize) {
        if length < 3 {
            return;
        }

        let start = ZOPFLI_CACHE_LENGTH * pos * 3;
        let mut i = 3;
        let mut j = 0;
        let mut bestlength = 0;
        while i <= length {
            if i == length || sublen[i] != sublen[i + 1] {
                self.sublen[start + (j * 3)] = (i - 3) as u8;
                self.sublen[start + (j * 3 + 1)] = sublen[i].wrapping_rem(256) as u8;
                self.sublen[start + (j * 3 + 2)] = ((sublen[i] >> 8)).wrapping_rem(256) as u8;
                bestlength = i as u32;
                j += 1;
                if j >= ZOPFLI_CACHE_LENGTH {
                    break;
                }
            }
            i += 1;
        }

        if j < ZOPFLI_CACHE_LENGTH {
            assert!(bestlength == length as u32);
            self.sublen[start + ((ZOPFLI_CACHE_LENGTH - 1) * 3)] = (bestlength - 3) as u8;
        } else {
            assert!(bestlength <= length as u32);
        }
        assert!(bestlength == self.max_sublen(pos));
    }

    /// Extracts sublen array from the cache.
    pub fn fetch_sublen(&self, pos: usize, length: usize, sublen: &mut [u16]) {
        if length < 3 {
            return;
        }

        let start = ZOPFLI_CACHE_LENGTH * pos * 3;
        let maxlength = self.max_sublen(pos) as usize;
        let mut prevlength = 0;

        for j in 0..ZOPFLI_CACHE_LENGTH {
            let length = self.sublen[start + (j * 3)] as usize + 3;
            let dist = self.sublen[start + (j * 3 + 1)] as u16 + 256 * self.sublen[start + (j * 3 + 2)] as u16;

            let mut i = prevlength;
            while i <= length {
                sublen[i] = dist;
                i += 1;
            }
            if length == maxlength {
                break;
            }
            prevlength = length + 1;
        }
    }
}

pub trait Cache {
    fn try_get(&self, pos: usize, limit: usize, sublen: &mut Option<&mut [u16]>, blockstart: usize) -> LongestMatch;
    fn store(&mut self, pos: usize, limit: usize, sublen: &mut Option<&mut [u16]>, distance: u16, length: u16, blockstart: usize);
}

pub struct NoCache;

impl Cache for NoCache {
    fn try_get(&self, _: usize, limit: usize, _: &mut Option<&mut [u16]>, _: usize) -> LongestMatch {
        LongestMatch::new(limit)
    }

    fn store(&mut self, _: usize, _: usize, _: &mut Option<&mut [u16]>, _: u16, _: u16, _: usize) {
        // Nowhere to store
    }
}

impl Cache for ZopfliLongestMatchCache {
    fn try_get(&self, pos: usize, mut limit: usize, sublen: &mut Option<&mut [u16]>, blockstart: usize) -> LongestMatch {
        let mut longest_match = LongestMatch::new(limit);

        /* The LMC cache starts at the beginning of the block rather than the
        beginning of the whole array. */
        let lmcpos = pos - blockstart;

        /* Length > 0 and dist 0 is invalid combination, which indicates on purpose
        that this cache value is not filled in yet. */
        let length_lmcpos = self.length_at(lmcpos);
        let dist_lmcpos = self.dist_at(lmcpos);
        let cache_available = length_lmcpos == 0 || dist_lmcpos != 0;
        let max_sublen = self.max_sublen(lmcpos);
        let limit_ok_for_cache = limit == ZOPFLI_MAX_MATCH || length_lmcpos <= limit as u16 || ( sublen.is_some() && max_sublen >= limit as u32);

        if limit_ok_for_cache && cache_available {
            if sublen.is_none() || length_lmcpos as u32 <= max_sublen {
                let length = cmp::min(length_lmcpos, limit as u16);
                let distance;
                if let &mut Some(ref mut subl) = sublen {
                    self.fetch_sublen(lmcpos, length as usize, subl);
                    distance = subl[length as usize];

                    if limit == ZOPFLI_MAX_MATCH && length >= ZOPFLI_MIN_MATCH as u16 {
                        assert!(subl[length as usize] == dist_lmcpos);
                    }
                } else {
                    distance = dist_lmcpos;
                }
                longest_match.distance = distance;
                longest_match.length = length;
                longest_match.from_cache = true;
                return longest_match;
            }
            /* Can't use much of the cache, since the "sublens" need to be calculated,
            but at least we already know when to stop. */
            limit = length_lmcpos as usize;
            longest_match.limit = limit;
        }

        longest_match
    }

    fn store(&mut self, pos: usize, limit: usize, sublen: &mut Option<&mut [u16]>, distance: u16, length: u16, blockstart: usize) {
        if let &mut Some(ref mut subl) = sublen {
            /* Length > 0 and dist 0 is invalid combination, which indicates on purpose
            that this cache value is not filled in yet. */
            let lmcpos = pos - blockstart;
            let cache_available = self.length_at(lmcpos) == 0 || self.dist_at(lmcpos) != 0;

            if limit == ZOPFLI_MAX_MATCH && !cache_available {
                assert!(self.length_at(lmcpos) == 1 && self.dist_at(lmcpos) == 0);
                if length < ZOPFLI_MIN_MATCH as u16 {
                    self.store_dist_at(lmcpos, 0);
                    self.store_length_at(lmcpos, 0);
                } else {
                    self.store_dist_at(lmcpos, distance);
                    self.store_length_at(lmcpos, length);
                }
                assert!(!(self.length_at(lmcpos) == 1 && self.dist_at(lmcpos) == 0));
                self.store_sublen(subl, lmcpos, length as usize);
            }
        }
    }
}
<|endoftext|>


<commit_before>fn main() {
     let rect1 = (30, 50);

    println!(
        "The area of the rectangle is {} square pixels.",
        area(rect1)
    );
}

fn area(dimensions: (u32, u32)) -> u32 {
    dimensions.0 * dimensions.1
}
<commit_msg>working code with struct<commit_after>struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let rect1 = Rectangle {  width: 30, height: 50 };

    println!(
        "The area of the rectangle is {} square pixels.",
        area(&rect1)
    );
}

fn area(rectangle: &Rectangle) -> u32 {
    rectangle.width * rectangle.height
}
<|endoftext|>


<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at https://mozilla.org/MPL/2.0/.

use common::is_whitespace;

use regex::Regex;
use std::borrow::Cow;

/// Removes unnecessary whitespaces from a String.
///
/// # Example
///
/// ```
/// use crowbook_text_processing::clean::remove_whitespaces;
/// let s = remove_whitespaces("  A  string   with   more   whitespaces  than  needed   ");
/// assert_eq!(&s, " A string with more whitespaces than needed ");
/// ```
pub fn remove_whitespaces<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> {
    lazy_static! {
        static ref REGEX: Regex = Regex::new(r"[  \x{202F}\x{2002}]{2,}?").unwrap();
    }
    let input = input.into();
    let first = REGEX.find(&input);
    if let Some((first, _)) = first {
        let mut new_s = String::with_capacity(input.len());
        new_s.push_str(&input[0..first]);
        let mut previous_space = false;
        for c in input[first..].chars() {
            if is_whitespace(c) {
                if previous_space {
                    // previous char already a space, don't copy it
                } else {
                    new_s.push(c);
                    previous_space = true;
                }
            } else {
                previous_space = false;
                new_s.push(c);
            }
        }
        Cow::Owned(new_s)
    } else {
        input
    }
}

/// Replace quotes with more typographic variants
///
/// # Example
///
/// ```
/// use crowbook_text_processing::typographic_quotes;
/// let s = typographic_quotes("\"foo\"");
/// assert_eq!(&s, "“foo”");
/// let s = typographic_quotes("'foo'");
/// assert_eq!(&s, "‘foo’");
/// ```
pub fn typographic_quotes<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> {
    lazy_static! {
        static ref REGEX: Regex = Regex::new("[\"\']").unwrap();
    }
    let input = input.into();
    let first = REGEX.find(&input);
    if let Some((mut first, _)) = first {
        let mut new_s = String::with_capacity(input.len());
        if first > 0 {
            // Move one step backward since we might need to know if previous char was
            // a letter or not
            first -= 1;
        }
        new_s.push_str(&input[0..first]);
        let chars = input[first..].chars().collect::<Vec<_>>();
        for i in 0..chars.len() {
            let c = chars[i];
            match c {
                '"' => {
                    if i > 0 && chars[i - 1].is_alphabetic() {
                        new_s.push('”');
                    } else if i < chars.len() - 1 && chars[i + 1].is_alphabetic() {
                        new_s.push('“');
                    } else {
                        new_s.push('"');
                    }
                },
                '\'' => {
                    if i > 0 && chars[i - 1].is_alphabetic() {
                        new_s.push('’');
                    } else if i < chars.len() - 1 && chars[i + 1].is_alphabetic() {
                        new_s.push('‘');
                    } else {
                        new_s.push('\'');
                    }
                },
                _ => new_s.push(c)
            }
        }
        Cow::Owned(new_s)
    } else {
        input
    }
}

#[test]
fn remove_whitespaces_1() {
    let s = "   Remove    supplementary   spaces    but    don't    trim     either   ";
    let res = remove_whitespaces(s);
    assert_eq!(&res, " Remove supplementary spaces but don't trim either ");
}
<commit_msg>Add tests to typographic quotes<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at https://mozilla.org/MPL/2.0/.

use common::is_whitespace;

use regex::Regex;
use std::borrow::Cow;

/// Removes unnecessary whitespaces from a String.
///
/// # Example
///
/// ```
/// use crowbook_text_processing::clean::remove_whitespaces;
/// let s = remove_whitespaces("  A  string   with   more   whitespaces  than  needed   ");
/// assert_eq!(&s, " A string with more whitespaces than needed ");
/// ```
pub fn remove_whitespaces<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> {
    lazy_static! {
        static ref REGEX: Regex = Regex::new(r"[  \x{202F}\x{2002}]{2,}?").unwrap();
    }
    let input = input.into();
    let first = REGEX.find(&input);
    if let Some((first, _)) = first {
        let mut new_s = String::with_capacity(input.len());
        new_s.push_str(&input[0..first]);
        let mut previous_space = false;
        for c in input[first..].chars() {
            if is_whitespace(c) {
                if previous_space {
                    // previous char already a space, don't copy it
                } else {
                    new_s.push(c);
                    previous_space = true;
                }
            } else {
                previous_space = false;
                new_s.push(c);
            }
        }
        Cow::Owned(new_s)
    } else {
        input
    }
}

/// Replace quotes with more typographic variants
///
/// # Example
///
/// ```
/// use crowbook_text_processing::typographic_quotes;
/// let s = typographic_quotes("\"foo\"");
/// assert_eq!(&s, "“foo”");
/// let s = typographic_quotes("'foo'");
/// assert_eq!(&s, "‘foo’");
/// ```
pub fn typographic_quotes<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> {
    lazy_static! {
        static ref REGEX: Regex = Regex::new("[\"\']").unwrap();
    }
    let input = input.into();
    let first = REGEX.find(&input);
    if let Some((mut first, _)) = first {
        let mut new_s = String::with_capacity(input.len());
        if first > 0 {
            // Move one step backward since we might need to know if previous char was
            // a letter or not
            first -= 1;
        }
        new_s.push_str(&input[0..first]);
        let chars = input[first..].chars().collect::<Vec<_>>();
        for i in 0..chars.len() {
            let c = chars[i];
            match c {
                '"' => {
                    if i > 0 && chars[i - 1].is_alphabetic() {
                        new_s.push('”');
                    } else if i < chars.len() - 1 && chars[i + 1].is_alphabetic() {
                        new_s.push('“');
                    } else {
                        new_s.push('"');
                    }
                },
                '\'' => {
                    if i > 0 && chars[i - 1].is_alphabetic() {
                        new_s.push('’');
                    } else if i < chars.len() - 1 && chars[i + 1].is_alphabetic() {
                        new_s.push('‘');
                    } else {
                        new_s.push('\'');
                    }
                },
                _ => new_s.push(c)
            }
        }
        Cow::Owned(new_s)
    } else {
        input
    }
}

#[test]
fn remove_whitespaces_1() {
    let s = "   Remove    supplementary   spaces    but    don't    trim     either   ";
    let res = remove_whitespaces(s);
    assert_eq!(&res, " Remove supplementary spaces but don't trim either ");
}

#[test]
fn typographic_quotes_1() {
    let s = "Some string without ' typographic ' quotes";
    let res = typographic_quotes(s);
    assert_eq!(&res, s);
}

#[test]
fn typographic_quotes_2() {
    let s = typographic_quotes("\"foo\"");
    assert_eq!(&s, "“foo”");
}

#[test]
fn typographic_quotes_3() {
    let s = typographic_quotes("'foo'");
    assert_eq!(&s, "‘foo’");
}
<|endoftext|>


<commit_before>use std::thread;
use std::time;

use chrono;
use float_duration::{FloatDuration, TimePoint};

use framerate::FrameCount;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TimeProgression {
    FixedStep,
    VariableStep,
}

#[derive(Debug, Clone)]
pub struct GameTime {
    frame_wall_time: chrono::DateTime<chrono::Local>,
    frame_game_time: time::Duration,
    elapsed_game_time: FloatDuration,
    elapsed_wall_time: FloatDuration,
    frame_number: u64,
}

#[derive(Debug, Clone)]
pub struct GameClock {
    last_frame_time: GameTime,
    start_wall_time: chrono::DateTime<chrono::Local>,
    total_game_time: time::Duration,
    current_frame: u64,
    clock_multiplier: f64,
    time_progression: TimeProgression,
}

impl GameClock {
    pub fn new() -> GameClock {
        let now = chrono::Local::now();
        let start_game_time = GameTime {
            frame_wall_time: now,
            frame_game_time: time::Duration::new(0, 0),
            elapsed_game_time: FloatDuration::zero(),
            elapsed_wall_time: FloatDuration::zero(),
            frame_number: 0,
        };

        GameClock {
            last_frame_time: start_game_time,
            start_wall_time: now,
            total_game_time: time::Duration::new(0, 0),
            current_frame: 0,
            clock_multiplier: 1.0,
            time_progression: TimeProgression::VariableStep,
        }
    }

    pub fn current_frame_index(&self) -> u64 {
        self.current_frame
    }
    pub fn start_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.start_wall_time
    }
    pub fn frame_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.last_frame_time.frame_wall_time
    }
    pub fn frame_elapsed_time(&self) -> FloatDuration {
        chrono::Local::now()
            .float_duration_since(self.frame_wall_time())
            .unwrap()
    }
    pub fn last_frame_time(&self) -> &GameTime {
        &self.last_frame_time
    }

    pub fn clock_multiplier(&self) -> f64 {
        self.clock_multiplier
    }
    pub fn with_clock_multiplier(&mut self, val: f64) -> &mut GameClock {
        self.clock_multiplier = val;
        self
    }

    pub fn tick<C>(&mut self, counter: &mut C) -> GameTime
        where C: FrameCount + ?Sized
    {
        let frame_start = chrono::Local::now();

        self.current_frame += 1;

        let elapsed_wall_time = frame_start
            .float_duration_since(self.frame_wall_time())
            .unwrap();

        let elapsed_game_time = self.elapsed_game_time_from_wall_time(counter, elapsed_wall_time);
        let total_game_time = self.total_game_time + elapsed_game_time.to_std().unwrap();

        self.total_game_time = total_game_time;

        let time = GameTime {
            frame_wall_time: frame_start,
            frame_game_time: total_game_time,
            elapsed_game_time,
            elapsed_wall_time,
            frame_number: self.current_frame,
        };

        self.last_frame_time = time.clone();

        counter.tick(&time);

        time
    }

    fn elapsed_game_time_from_wall_time<C>(&self,
                                           counter: &mut C,
                                           elapsed_wall_time: FloatDuration)
                                           -> FloatDuration
        where C: FrameCount + ?Sized
    {
        match self.time_progression {
            TimeProgression::FixedStep => counter.target_time_per_frame() * self.clock_multiplier,
            TimeProgression::VariableStep => elapsed_wall_time * self.clock_multiplier,
        }
    }

    pub fn sleep_remaining_via<C, F>(&mut self, counter: &C, f: F)
        where C: FrameCount + ?Sized,
              F: FnOnce(FloatDuration)
    {
        let remaining_time = counter.target_time_per_frame() -
                             chrono::Local::now()
                                 .float_duration_since(self.frame_wall_time())
                                 .unwrap();
        f(remaining_time)
    }

    pub fn sleep_remaining<C>(&mut self, counter: &C)
        where C: FrameCount + ?Sized
    {
        self.sleep_remaining_via(counter, |rem| thread::sleep(rem.to_std().unwrap()))
    }
}

impl Default for GameClock {
    fn default() -> GameClock {
        GameClock::new()
    }
}

impl GameTime {
    pub fn frame_game_time(&self) -> time::Duration {
        self.frame_game_time
    }
    pub fn frame_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.frame_wall_time
    }
    pub fn elapsed_game_time(&self) -> FloatDuration {
        self.elapsed_game_time
    }
    pub fn elapsed_wall_time(&self) -> FloatDuration {
        self.elapsed_wall_time
    }
    pub fn elapsed_time_since_frame_start(&self) -> FloatDuration {
        chrono::Local::now()
            .float_duration_since(self.frame_wall_time)
            .unwrap()
    }
    pub fn frame_number(&self) -> u64 {
        self.frame_number
    }
    pub fn instantaneous_frame_rate(&self) -> f64 {
        1.0 / self.elapsed_game_time.as_seconds()
    }
}
<commit_msg>Added `new` function to GameTime.<commit_after>use std::thread;
use std::time;

use chrono;
use float_duration::{FloatDuration, TimePoint};

use framerate::FrameCount;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TimeProgression {
    FixedStep,
    VariableStep,
}

#[derive(Debug, Clone)]
pub struct GameTime {
    frame_wall_time: chrono::DateTime<chrono::Local>,
    frame_game_time: time::Duration,
    elapsed_game_time: FloatDuration,
    elapsed_wall_time: FloatDuration,
    frame_number: u64,
}

#[derive(Debug, Clone)]
pub struct GameClock {
    last_frame_time: GameTime,
    start_wall_time: chrono::DateTime<chrono::Local>,
    total_game_time: time::Duration,
    current_frame: u64,
    clock_multiplier: f64,
    time_progression: TimeProgression,
}

impl GameClock {
    pub fn new() -> GameClock {
        let now = chrono::Local::now();
        let start_game_time = GameTime {
            frame_wall_time: now,
            frame_game_time: time::Duration::new(0, 0),
            elapsed_game_time: FloatDuration::zero(),
            elapsed_wall_time: FloatDuration::zero(),
            frame_number: 0,
        };

        GameClock {
            last_frame_time: start_game_time,
            start_wall_time: now,
            total_game_time: time::Duration::new(0, 0),
            current_frame: 0,
            clock_multiplier: 1.0,
            time_progression: TimeProgression::VariableStep,
        }
    }

    pub fn current_frame_index(&self) -> u64 {
        self.current_frame
    }
    pub fn start_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.start_wall_time
    }
    pub fn frame_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.last_frame_time.frame_wall_time
    }
    pub fn frame_elapsed_time(&self) -> FloatDuration {
        chrono::Local::now()
            .float_duration_since(self.frame_wall_time())
            .unwrap()
    }
    pub fn last_frame_time(&self) -> &GameTime {
        &self.last_frame_time
    }

    pub fn clock_multiplier(&self) -> f64 {
        self.clock_multiplier
    }
    pub fn with_clock_multiplier(&mut self, val: f64) -> &mut GameClock {
        self.clock_multiplier = val;
        self
    }

    pub fn tick<C>(&mut self, counter: &mut C) -> GameTime
        where C: FrameCount + ?Sized
    {
        let frame_start = chrono::Local::now();

        self.current_frame += 1;

        let elapsed_wall_time = frame_start
            .float_duration_since(self.frame_wall_time())
            .unwrap();

        let elapsed_game_time = self.elapsed_game_time_from_wall_time(counter, elapsed_wall_time);
        let total_game_time = self.total_game_time + elapsed_game_time.to_std().unwrap();

        self.total_game_time = total_game_time;

        let time = GameTime {
            frame_wall_time: frame_start,
            frame_game_time: total_game_time,
            elapsed_game_time,
            elapsed_wall_time,
            frame_number: self.current_frame,
        };

        self.last_frame_time = time.clone();

        counter.tick(&time);

        time
    }

    fn elapsed_game_time_from_wall_time<C>(&self,
                                           counter: &mut C,
                                           elapsed_wall_time: FloatDuration)
                                           -> FloatDuration
        where C: FrameCount + ?Sized
    {
        match self.time_progression {
            TimeProgression::FixedStep => counter.target_time_per_frame() * self.clock_multiplier,
            TimeProgression::VariableStep => elapsed_wall_time * self.clock_multiplier,
        }
    }

    pub fn sleep_remaining_via<C, F>(&mut self, counter: &C, f: F)
        where C: FrameCount + ?Sized,
              F: FnOnce(FloatDuration)
    {
        let remaining_time = counter.target_time_per_frame() -
                             chrono::Local::now()
                                 .float_duration_since(self.frame_wall_time())
                                 .unwrap();
        f(remaining_time)
    }

    pub fn sleep_remaining<C>(&mut self, counter: &C)
        where C: FrameCount + ?Sized
    {
        self.sleep_remaining_via(counter, |rem| thread::sleep(rem.to_std().unwrap()))
    }
}

impl Default for GameClock {
    fn default() -> GameClock {
        GameClock::new()
    }
}

impl GameTime {
    pub fn new(frame_wall_time: chrono::DateTime<chrono::Local>,
               frame_game_time: time::Duration,
               elapsed_game_time: FloatDuration,
               elapsed_wall_time: FloatDuration,
               frame_number: u64)
               -> GameTime {
        GameTime {
            frame_wall_time,
            frame_game_time,
            elapsed_game_time,
            elapsed_wall_time,
            frame_number,
        }
    }

    pub fn frame_game_time(&self) -> time::Duration {
        self.frame_game_time
    }
    pub fn frame_wall_time(&self) -> chrono::DateTime<chrono::Local> {
        self.frame_wall_time
    }
    pub fn elapsed_game_time(&self) -> FloatDuration {
        self.elapsed_game_time
    }
    pub fn elapsed_wall_time(&self) -> FloatDuration {
        self.elapsed_wall_time
    }
    pub fn elapsed_time_since_frame_start(&self) -> FloatDuration {
        chrono::Local::now()
            .float_duration_since(self.frame_wall_time)
            .unwrap()
    }
    pub fn frame_number(&self) -> u64 {
        self.frame_number
    }
    pub fn instantaneous_frame_rate(&self) -> f64 {
        1.0 / self.elapsed_game_time.as_seconds()
    }
}
<|endoftext|>


<commit_before>extern crate rand;
extern crate getopts;
extern crate timely;
extern crate timely_sort;
extern crate graph_map;
extern crate differential_dataflow;
extern crate vec_map;

use std::time::Instant;
use std::hash::Hash;
use std::mem;

use vec_map::VecMap;

use rand::{Rng, SeedableRng, StdRng};

use timely_sort::Unsigned;

use timely::dataflow::*;
use timely::dataflow::operators::*;

use differential_dataflow::{Collection, AsCollection};
use differential_dataflow::operators::*;
use differential_dataflow::operators::join::JoinArranged;
use differential_dataflow::collection::LeastUpperBound;

use graph_map::GraphMMap;

type Node = u32;
type Edge = (Node, Node);

fn main() {

    if std::env::args().find(|x| x == "pymk").is_some() {
        test_pymk();
    }
    else {
        test_graph();
    }

}

fn test_graph() {

    // snag a filename to use for the input graph.
    let filename = std::env::args().nth(1).unwrap();

    let reach = std::env::args().find(|x| x == "reach").is_some();
    let cc = std::env::args().find(|x| x == "cc").is_some();
    let bfs = std::env::args().find(|x| x == "bfs").is_some();

    timely::execute_from_args(std::env::args().skip(1), move |computation| {

        let peers = computation.peers();
        let index = computation.index();

        // // What you might do if you used GraphMMap:
        let graph = GraphMMap::new(&filename);

        let mut input = computation.scoped::<u64,_,_>(|scope| {

            let (input, stream) = scope.new_input();

            let roots = Collection::new(vec![(1,1)].into_iter().to_stream(scope));
            let graph = Collection::new(stream);

            if reach { _reach(&graph, &roots); }
            else if cc { _connected_components(&graph); }
            else if bfs { _bfs(&graph, &roots); }
            else {
                panic!("must specify one of 'reach', 'cc', 'bfs'.");
            }

            input
        });

        let timer = Instant::now();

        for node in 0..graph.nodes() {
            if node % peers == index {
                for &edge in graph.edges(node) {
                    input.send(((node as u32, edge), 1));
                }
            }
        }

        input.close();
        while computation.step() { }

        println!("loaded: {:?}", timer.elapsed());

    }).unwrap();
}

fn _test_trees() {

    let depth: usize = std::env::args().nth(1).unwrap().parse().unwrap();

    let timer = Instant::now();

    timely::execute_from_args(std::env::args().skip(1), move |computation| {

        computation.scoped::<(),_,_>(|scope| {

            let index = scope.index();
            let peers = scope.peers();

            let mut tree = vec![];
            let mut sum = 0usize;
            let mut cur_nodes = 0;
            let mut tot_nodes = 1;
            for _level in 0 .. depth {
                for node in cur_nodes .. tot_nodes {
                    cur_nodes += 1;
                    for _edge in 0 .. 3 {
                        if node % peers == index {
                            tree.push(((node as u32, tot_nodes as u32),1));
                        }
                        tot_nodes += 1;
                    }
                }
                sum += (tot_nodes - cur_nodes) * (tot_nodes - cur_nodes - 1) / 2;
            }

            println!("produced depth {} tree, {} nodes, {} edges", depth, tot_nodes, tree.len());
            println!("#(sg): {}", sum);

            let edges = tree.into_iter()
                            .to_stream(scope)
                            .as_collection();

            let dists = edges.map(|(p,c)| (p,(c,1)));

            dists.iterate(|tc| {
                    let edges = edges.enter(&tc.scope());
                    let dists = dists.enter(&tc.scope());
                    tc.map(|(a,(p,d))| (p,(a,d)))
                      .join_map_u(&edges, |_p,&(a,d),&c| (a,(c,d+1)))
                      .concat(&dists)
                      .group_u(|_,s,t| for (&x,_) in s { t.push((x,1)) })
                });
        });
    }).unwrap();

    println!("Elapsed: {:?}", timer.elapsed());
}

fn test_pymk() {

    // snag a filename to use for the input graph.
    let filename = std::env::args().nth(1).unwrap();

    timely::execute_from_args(std::env::args().skip(1), move |computation| {

        let peers = computation.peers();
        let index = computation.index();

        // // What you might do if you used GraphMMap:
        let graph = GraphMMap::new(&filename);

        let (mut input, mut roots, probe) = computation.scoped::<u64,_,_>(|scope| {

            let (input, stream1) = scope.new_input();
            let (rootz, stream2) = scope.new_input();

            let graph = stream1.as_collection();
            let roots = stream2.as_collection();

            let graph = graph.map_in_place(|x: &mut (u32, u32)| ::std::mem::swap(&mut x.0, &mut x.1))
                             .concat(&graph);

            let graph = graph.arrange_by_key(|k| k.clone(), |x| (VecMap::new(), x));
            let roots = roots.arrange_by_self(|k: &u32| k.as_u64(), |x| (VecMap::new(), x));

            let probe =
            graph.join(&roots, |k,v,_| (v.clone(), k.clone()))
                 .arrange_by_key(|k| k.clone(), |x| (VecMap::new(), x))
                 .join(&graph, |_,x,y| (x.clone(), y.clone()))
                 .group_u(|_,s,t| {
                    t.extend(s.map(|(x,y)| (*x,y)));
                    t.sort_by(|x,y| x.1.cmp(&y.1));
                    t.truncate(10);
                 })
                 .probe().0;

            (input, rootz, probe)
        });

        let timer = Instant::now();

        for node in 0..graph.nodes() {
            if node % peers == index {
                for &edge in graph.edges(node) {
                    input.send(((node as u32, edge), 1));
                }
            }
        }

        input.advance_to(1);
        roots.advance_to(1);
        while probe.lt(input.time()) { computation.step(); }

        println!("loaded: {:?}", timer.elapsed());

        let mut latencies = Vec::with_capacity(11);

        let seed: &[_] = &[1, 2, 3, 4];
        let mut rng: StdRng = SeedableRng::from_seed(seed);    // rng for edge additions

        for _count in 0..latencies.capacity() {
            let timer = Instant::now();
            if index == 0 {
                roots.send((rng.gen_range(0, graph.nodes() as u32), 1));
            }
            let next = input.epoch() + 1;
            input.advance_to(next);
            roots.advance_to(next);
            while probe.lt(input.time()) { computation.step(); }
            latencies.push(timer.elapsed());
        }

        if index == 0 {
            for lat in &latencies {
                println!("latency: {:?}", lat);
            }
        }

    }).unwrap();
}

// returns pairs (n, s) indicating node n can be reached from a root in s steps.
fn _pymk<G: Scope>(edges: &Collection<G, Edge>) -> Collection<G, (Node,Node)>
where G::Timestamp: LeastUpperBound {
    edges.join_map_u(&edges, |_x,&y,&z| (y,z))
         .consolidate_by(|x| x.0)
}

// returns pairs (n, s) indicating node n can be reached from a root in s steps.
fn _reach<G: Scope>(edges: &Collection<G, Edge>, roots: &Collection<G, Node>) -> Collection<G, Node>
where G::Timestamp: LeastUpperBound {

    // initialize roots as reaching themselves at distance 0
    // repeatedly update minimal distances each node can be reached from each root
    roots.iterate(|inner| {

        let edges = edges.enter(&inner.scope());
        let nodes = roots.enter(&inner.scope());

        edges.semijoin_u(&inner)
             .map(|(_,d)| d)
             .concat(&nodes)
             .distinct_u()
     })
}

fn _connected_components<G: Scope>(edges: &Collection<G, Edge>) -> Collection<G, (Node, Node)>
where G::Timestamp: LeastUpperBound+Hash {

    // each edge (x,y) means that we need at least a label for the min of x and y.
    let nodes = edges.map_in_place(|pair| {
                        let min = std::cmp::min(pair.0, pair.1);
                        *pair = (min, min);
                     })
                     .consolidate_by(|x| x.0);

    // each edge should exist in both directions.
    let edges = edges.map_in_place(|x| mem::swap(&mut x.0, &mut x.1))
                     .concat(&edges);

    // don't actually use these labels, just grab the type
    nodes.filter(|_| false)
         .iterate(|inner| {
             let edges = edges.enter(&inner.scope());
             let nodes = nodes.enter_at(&inner.scope(), |r| 256 * (64 - (r.0).0.leading_zeros() as u64));

            inner.join_map_u(&edges, |_k,l,d| (*d,*l))
                 .concat(&nodes)
                 .group_u(|_, mut s, t| { t.push((*s.peek().unwrap().0, 1)); } )
         })
}

// returns pairs (n, s) indicating node n can be reached from a root in s steps.
fn _bfs<G: Scope>(edges: &Collection<G, Edge>, roots: &Collection<G, Node>) -> Collection<G, (Node, u32)>
where G::Timestamp: LeastUpperBound {

    // initialize roots as reaching themselves at distance 0
    let nodes = roots.map(|x| (x, 0));

    // repeatedly update minimal distances each node can be reached from each root
    nodes.iterate(|inner| {

        let edges = edges.enter(&inner.scope());
        let nodes = nodes.enter(&inner.scope());

        inner.join_map_u(&edges, |_k,l,d| (*d, l+1))
             .concat(&nodes)
             .group_u(|_, s, t| t.push((*s.peek().unwrap().0, 1)))
     })
}
<commit_msg>updated for usability<commit_after>extern crate rand;
extern crate getopts;
extern crate timely;
extern crate timely_sort;
extern crate graph_map;
extern crate differential_dataflow;
extern crate vec_map;

use std::time::Instant;
use std::hash::Hash;
use std::mem;

use vec_map::VecMap;

use rand::{Rng, SeedableRng, StdRng};

use timely_sort::Unsigned;

use timely::dataflow::*;
use timely::dataflow::operators::*;

use differential_dataflow::Collection;
use differential_dataflow::operators::*;
use differential_dataflow::operators::join::JoinArranged;
use differential_dataflow::collection::LeastUpperBound;

use graph_map::GraphMMap;

type Node = u32;
type Edge = (Node, Node);

fn main() {

    // snag a filename to use for the input graph.
    let filename = std::env::args().nth(1).unwrap();
    let program = std::env::args().nth(2).unwrap();

    timely::execute_from_args(std::env::args().skip(1), move |computation| {

        let peers = computation.peers();
        let index = computation.index();

        // // What you might do if you used GraphMMap:
        let graph = GraphMMap::new(&filename);

        let (mut input, mut query, probe) = computation.scoped::<u64,_,_>(|scope| {

            let (input, stream1) = scope.new_input();
            let (rootz, stream2) = scope.new_input();

            let graph = Collection::new(stream1);
            let query = Collection::new(stream2);

            let probe = match program.as_str() {
                "reach" => _reach(&graph, &query).inner.probe().0,
                "cc"    => _connected_components(&graph).inner.probe().0,
                "bfs"   => _bfs(&graph, &query).inner.probe().0,
                "pymk"  => _pymk(&graph, &query, 10).inner.probe().0,
                _       => panic!("must specify one of 'reach', 'cc', 'bfs'.")
            };

            (input, rootz, probe)
        });

        let timer = Instant::now();

        // start loading up the graph
        for node in 0..graph.nodes() {
            if node % peers == index {
                for &edge in graph.edges(node) {
                    input.send(((node as u32, edge), 1));
                }
            }
        }

        // run until graph is loaded
        input.advance_to(1);
        query.advance_to(1);
        computation.step_while(|| probe.lt(query.time()));

        if index == 0 {
            println!("loaded: {:?}", timer.elapsed());
        }

        // conduct latencies.capacity() measurements.
        let mut latencies = Vec::with_capacity(11);

        let seed: &[_] = &[1, 2, 3, 4];
        let mut rng: StdRng = SeedableRng::from_seed(seed);    // rng for edge additions

        for _count in 0..latencies.capacity() {
            let timer = Instant::now();
            if index == 0 {
                query.send((rng.gen_range(0, graph.nodes() as u32), 1));
            }
            let next = query.epoch() + 1;
            input.advance_to(next);
            query.advance_to(next);
            while probe.lt(query.time()) { computation.step(); }
            latencies.push(timer.elapsed());
        }

        if index == 0 {
            for lat in &latencies {
                println!("latency: {:?}", lat);
            }
        }

    }).unwrap();
}

// returns pairs (root, friend-of-friend) for the top-k friends of friends by count.
fn _pymk<G: Scope>(edges: &Collection<G, Edge>, query: &Collection<G, Node>, k: usize) -> Collection<G, (Node,Node)>
where G::Timestamp: LeastUpperBound {

    // symmetrize the graph
    let edges = edges.map_in_place(|x: &mut (u32, u32)| ::std::mem::swap(&mut x.0, &mut x.1))
                     .concat(&edges);

    // "arrange" edges, because we'll want to use it twice the same way.
    let edges = edges.arrange_by_key(|k| k.clone(), |x| (VecMap::new(), x));
    let query = query.arrange_by_self(|k: &u32| k.as_u64(), |x| (VecMap::new(), x));

    // restrict attention to edges from query nodes
    edges.join(&query, |k,v,_| (v.clone(), k.clone()))
         .arrange_by_key(|k| k.clone(), |x| (VecMap::new(), x))
         .join(&edges, |_,x,y| (x.clone(), y.clone()))
         // the next thing is the "topk" computation. sorry!
         .group_u(move |_,s,t| {
             t.extend(s.map(|(x,y)| (*x,y)));       // propose all inputs as outputs
             t.sort_by(|x,y| (-x.1).cmp(&(-y.1)));  // sort by negative count (large numbers first)
             t.truncate(k)                          // keep at most k of these
         })
 }

// returns pairs (n, s) indicating node n can be reached from a root in s steps.
fn _reach<G: Scope>(edges: &Collection<G, Edge>, query: &Collection<G, Node>) -> Collection<G, Node>
where G::Timestamp: LeastUpperBound {

    // initialize query as reaching themselves at distance 0
    // repeatedly update minimal distances each node can be reached from each root
    query.iterate(|inner| {

        let edges = edges.enter(&inner.scope());
        let nodes = query.enter(&inner.scope());

        // edges from active sources activate their destinations
        edges.semijoin_u(&inner)
             .map(|(_,d)| d)
             .concat(&nodes)
             .distinct_u()
     })
}

// returns pairs (node, label) indicating the connected component containing each node
fn _connected_components<G: Scope>(edges: &Collection<G, Edge>) -> Collection<G, (Node, Node)>
where G::Timestamp: LeastUpperBound+Hash {

    // each edge (x,y) means that we need at least a label for the min of x and y.
    let nodes = edges.map_in_place(|pair| {
                        let min = std::cmp::min(pair.0, pair.1);
                        *pair = (min, min);
                     })
                     .consolidate_by(|x| x.0);

    // each edge should exist in both directions.
    let edges = edges.map_in_place(|x| mem::swap(&mut x.0, &mut x.1))
                     .concat(&edges);

    // don't actually use these labels, just grab the type
    nodes.filter(|_| false)
         .iterate(|inner| {
             let edges = edges.enter(&inner.scope());
             let nodes = nodes.enter_at(&inner.scope(), |r| 256 * (64 - (r.0).0.leading_zeros() as u64));

             inner.join_map_u(&edges, |_k,l,d| (*d,*l))
                  .concat(&nodes)
                  .group_u(|_, mut s, t| { t.push((*s.peek().unwrap().0, 1)); } )
         })
}

// returns pairs (n, s) indicating node n can be reached from a root in s steps.
fn _bfs<G: Scope>(edges: &Collection<G, Edge>, query: &Collection<G, Node>) -> Collection<G, (Node, u32)>
where G::Timestamp: LeastUpperBound {

    // initialize query as reaching themselves at distance 0
    let nodes = query.map(|x| (x, 0));

    // repeatedly update minimal distances each node can be reached from each root
    nodes.iterate(|inner| {

        let edges = edges.enter(&inner.scope());
        let nodes = nodes.enter(&inner.scope());

        inner.join_map_u(&edges, |_k,l,d| (*d, l+1))
             .concat(&nodes)
             .group_u(|_, s, t| t.push((*s.peek().unwrap().0, 1)))
     })
}<|endoftext|>


<commit_before>extern crate image;
extern crate nalgebra;

use std::f64::consts::{PI_2, FRAC_PI_2};
use std::io::File;
use std::num::Float;
use image::{GenericImage, Pixel, Rgba};
use nalgebra::{DVec, Mat2, Pnt2, Rot2, Vec1, Vec2, Vec3};

fn draw(v: &DVec<f64>, path: &Path) {
    let width = v.len() as u32;
    let height = 128u32;
    let white: Rgba<u8> = Pixel::from_channels(255, 255, 255, 255);
    let buffer = image::ImageBuffer::from_fn(width, height, box |&: _, _| white);
    let mut img = image::DynamicImage::ImageRgba8(buffer);
    let red = Pixel::from_channels(255, 0, 0, 255);
    for i in range(0u32, width) {
        let half = (height / 2) as f64;
        let y = half * (1.0 + v[i as uint]);
        let y = nalgebra::clamp(y as u32, 1u32, height);
        img.put_pixel(i, height - y, red);
    }
    let out = File::create(path).unwrap();
    let _ = img.save(out, image::PNG);
}

fn main() {
    println!("24 days of Rust - nalgebra (day 14)");
    let v = Vec2::new(0.0f64, 1.0);
    // 90 degrees clockwise
    //  0, 1
    // -1, 0
    let rot = Mat2::new(0.0f64, -1.0, 1.0, 0.0);
    println!("{:?}", rot * v);
    let angle = FRAC_PI_2;
    let rot = Rot2::new(Vec1::new(angle));
    println!("{:?}", rot * v);
    let point = Pnt2::new(4.0f64, 4.0);
    println!("Translate from {:?} to {:?}", point, nalgebra::translate(&v, &point));

    let v1 = Vec3::new(2.0f64, 2.0, 0.0);
    let v2 = Vec3::new(2.0f64, -2.0, 0.0);
    if nalgebra::approx_eq(&0.0f64, &nalgebra::dot(&v1, &v2)) {
        println!("v1 is orthogonal to v2");
    }

    println!("{:?}", nalgebra::cross(&v1, &v2));
    println!("{:?}", nalgebra::cross(&v2, &v1));

    const SIZE: uint = 512;
    let sine = DVec::from_fn(SIZE, |i: uint| {
        let t = i as f64 / 16.0f64;
        t.sin()
    });
    draw(&sine, &Path::new("out_sine.png"));

    let window = DVec::from_fn(SIZE, |i: uint| {
        0.54f64 - 0.46 * (PI_2 * (i as f64) / (SIZE - 1) as f64).cos()
    });
    draw(&window, &Path::new("out_window.png"));

    draw(&(sine * window), &Path::new("out_windowed.png"));
}
<commit_msg>Fixed box syntax.<commit_after>extern crate image;
extern crate nalgebra;

use std::f64::consts::{PI_2, FRAC_PI_2};
use std::io::File;
use std::num::Float;
use image::{GenericImage, Pixel, Rgba};
use nalgebra::{DVec, Mat2, Pnt2, Rot2, Vec1, Vec2, Vec3};

fn draw(v: &DVec<f64>, path: &Path) {
    let width = v.len() as u32;
    let height = 128u32;
    let white: Rgba<u8> = Pixel::from_channels(255, 255, 255, 255);
    let buffer = image::ImageBuffer::from_fn(width, height, Box::new(|&: _, _| white));
    let mut img = image::DynamicImage::ImageRgba8(buffer);
    let red = Pixel::from_channels(255, 0, 0, 255);
    for i in range(0u32, width) {
        let half = (height / 2) as f64;
        let y = half * (1.0 + v[i as uint]);
        let y = nalgebra::clamp(y as u32, 1u32, height);
        img.put_pixel(i, height - y, red);
    }
    let out = File::create(path).unwrap();
    let _ = img.save(out, image::PNG);
}

fn main() {
    println!("24 days of Rust - nalgebra (day 14)");
    let v = Vec2::new(0.0f64, 1.0);
    // 90 degrees clockwise
    //  0, 1
    // -1, 0
    let rot = Mat2::new(0.0f64, -1.0, 1.0, 0.0);
    println!("{:?}", rot * v);
    let angle = FRAC_PI_2;
    let rot = Rot2::new(Vec1::new(angle));
    println!("{:?}", rot * v);
    let point = Pnt2::new(4.0f64, 4.0);
    println!("Translate from {:?} to {:?}", point, nalgebra::translate(&v, &point));

    let v1 = Vec3::new(2.0f64, 2.0, 0.0);
    let v2 = Vec3::new(2.0f64, -2.0, 0.0);
    if nalgebra::approx_eq(&0.0f64, &nalgebra::dot(&v1, &v2)) {
        println!("v1 is orthogonal to v2");
    }

    println!("{:?}", nalgebra::cross(&v1, &v2));
    println!("{:?}", nalgebra::cross(&v2, &v1));

    const SIZE: uint = 512;
    let sine = DVec::from_fn(SIZE, |i: uint| {
        let t = i as f64 / 16.0f64;
        t.sin()
    });
    draw(&sine, &Path::new("out_sine.png"));

    let window = DVec::from_fn(SIZE, |i: uint| {
        0.54f64 - 0.46 * (PI_2 * (i as f64) / (SIZE - 1) as f64).cos()
    });
    draw(&window, &Path::new("out_window.png"));

    draw(&(sine * window), &Path::new("out_windowed.png"));
}
<|endoftext|>


<commit_before>use std::iter::Filter;
use entity::Entity;
use ecs::EntityIter;
use world;
use flags;
use dir6::Dir6;

/// Game update control.
#[deriving(PartialEq)]
pub enum ControlState {
    AwaitingInput,
    ReadyToUpdate,
}

/// Player input action.
#[deriving(Eq, PartialEq, Clone, Show, Encodable, Decodable)]
pub enum PlayerInput {
    /// Take a step in the given direction.
    Step(Dir6),
    // TODO: More
}

/// Return the player entity if one exists.
pub fn player() -> Option<Entity> {
    let mut iter = world::get().borrow().ecs.iter();
    for e in iter {
        if e.is_player() { return Some(e); }
    }
    None
}

/// Get the current control state.
pub fn control_state() -> ControlState {
    if world::get().borrow().flags.player_acted { return ReadyToUpdate; }
    match player() {
        Some(p) if p.acts_this_frame() => AwaitingInput,
        _ => ReadyToUpdate,
    }
}

/// Top-level game state update function. Only valid to call if
/// control_state() returned ReadyToUpdate.
pub fn update() {
    assert!(control_state() == ReadyToUpdate);

    ai_main();

    world::get().borrow_mut().flags.tick += 1;
    world::get().borrow_mut().flags.player_acted = false;
}

/// Give player input. Only valid to call if control_state() returned
/// AwaitingInput.
pub fn input(input: PlayerInput) {
    assert!(control_state() == AwaitingInput);
    let p = player().unwrap();
    match input {
        Step(d) => {
            p.step(d);
            flags::set_camera(p.location().unwrap());
        }
    }
    world::get().borrow_mut().flags.player_acted = true;
}

pub fn entities() -> EntityIter {
    world::get().borrow().ecs.iter()
}

pub fn mobs<'a>() -> Filter<'a, Entity, EntityIter> {
    world::get().borrow().ecs.iter().filter(|e| e.is_mob())
}

/// Run AI for all autonomous mobs.
fn ai_main() {
    for entity in entities() {
        entity.update();
    }
}
<commit_msg>Fix a glitch<commit_after>use std::iter::Filter;
use entity::Entity;
use ecs::EntityIter;
use world;
use flags;
use dir6::Dir6;

/// Game update control.
#[deriving(PartialEq)]
pub enum ControlState {
    AwaitingInput,
    ReadyToUpdate,
}

/// Player input action.
#[deriving(Eq, PartialEq, Clone, Show, Encodable, Decodable)]
pub enum PlayerInput {
    /// Take a step in the given direction.
    Step(Dir6),
    // TODO: More
}

/// Return the player entity if one exists.
pub fn player() -> Option<Entity> {
    let mut iter = world::get().borrow().ecs.iter();
    for e in iter {
        if e.is_player() { return Some(e); }
    }
    None
}

/// Get the current control state.
pub fn control_state() -> ControlState {
    if world::get().borrow().flags.player_acted { return ReadyToUpdate; }
    match player() {
        Some(p) if p.acts_this_frame() => AwaitingInput,
        _ => ReadyToUpdate,
    }
}

/// Top-level game state update function. Only valid to call if
/// control_state() returned ReadyToUpdate.
pub fn update() {
    assert!(control_state() == ReadyToUpdate);

    ai_main();

    world::get().borrow_mut().flags.tick += 1;
    world::get().borrow_mut().flags.player_acted = false;
}

/// Give player input. Only valid to call if control_state() returned
/// AwaitingInput.
pub fn input(input: PlayerInput) {
    assert!(control_state() == AwaitingInput);
    let p = player().unwrap();
    match input {
        Step(d) => {
            p.step(d);
            flags::set_camera(p.location().unwrap());
        }
    }
    world::get().borrow_mut().flags.player_acted = true;

    // Run one world update cycle right away, so that we don't get awkward
    // single frames rendered where the player has acted and the rest of the
    // world hasn't.
    if control_state() == ReadyToUpdate {
        update();
    }
}

pub fn entities() -> EntityIter {
    world::get().borrow().ecs.iter()
}

pub fn mobs<'a>() -> Filter<'a, Entity, EntityIter> {
    world::get().borrow().ecs.iter().filter(|e| e.is_mob())
}

/// Run AI for all autonomous mobs.
fn ai_main() {
    for entity in entities() {
        entity.update();
    }
}
<|endoftext|>


<commit_before>//! Input tokenizer

use std;
use std::rc::Rc;
use parser::tokens::{Token, SourceLocation, dummy_source};
use parser::util::{SharedString, rcstr, rcstring};

// --- Lexer: Error -------------------------------------------------------------

pub type LexerResult<T> = Result<T, LexerError>;

pub enum LexerError {
    UnknownToken {
        token: SharedString,  // result of curr_repr
        location: SourceLocation
    },
    InvalidInteger {
        input: SharedString,
        location: SourceLocation
    }
}

impl std::fmt::Show for LexerError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            LexerError::UnknownToken { ref token, ref location } => {
                write!(f, "unknown token: `{}` at {}", token, location)
            },
            LexerError::InvalidInteger { ref input, ref location } => {
                write!(f, "invalid integer: `{}` at {}", input, location)
            }
        }
    }
}

macro_rules! unknown_token(
    ($token:expr @ $location:expr) => (
        return Err(LexerError::UnknownToken {
            token: $token.clone(),
            location: $location
        })
    )
)

macro_rules! invalid_integer(
    ($input:expr @ $location:expr) => (
        return Err(LexerError::InvalidInteger {
            input: $input.clone(),
            location: $location
        })
    )
)

// --- Lexer --------------------------------------------------------------------

pub trait Lexer {
    /// Get the source of the current token
    fn get_source(&self) -> SourceLocation;

    /// Get the next token
    fn next_token(&mut self) -> LexerResult<Token>;

    /// Tokenize the input into a vector
    fn tokenize(&mut self) -> LexerResult<Vec<Token>>;
}

/// Lexer for tokenize a file from disk/memory.
pub struct FileLexer<'a> {
    source: &'a str,
    file: SharedString,
    len: uint,
    pos: uint,
    curr: Option<char>,
    lineno: uint
}

impl<'a> FileLexer<'a> {
    pub fn new(source: &'a str, file: &'a str) -> FileLexer<'a> {
        FileLexer {
            source: source,
            file: rcstr(file),
            len: source.len(),
            pos: 0,
            curr: Some(source.char_at(0)),
            lineno: 1
        }
    }

    /// --- Internal methods: Helpers -------------------------------------------

    /// Whether we've reached EOF
    fn is_eof(&self) -> bool {
        self.curr.is_none()
    }

    /// Move on to the next char
    fn bump(&mut self) {
        self.curr = self.nextch();
        self.pos += 1;

        debug!("Moved on to {}", self.curr)
    }

    /// Get the next char if possible
    fn nextch(&self) -> Option<char> {
        let mut new_pos = self.pos + 1;
        // When encountering multi-byte UTF-8, we may stop in the middle
        // of it. Fast forward till we see the next actual char or EOF
        while !self.source.is_char_boundary(new_pos)
            && self.pos < self.len {
            new_pos += 1;
        }

        if new_pos < self.len {
            Some(self.source.char_at(new_pos))
        } else {
            None
        }
    }

    //fn expect(&mut self, expect: char) {
    //    if self.curr != Some(expect) {
    //        let expect_str = match expect {
    //            '\'' => "quote".into_string(),
    //            c => format!("'{}'", c)
    //        };
    //        let found_str = match self.curr {
    //            Some(_) => format!("'{}'", self.curr_repr()),
    //            None => "EOF".into_string()
    //        };
    //
    //        self.fatal(format!("Expected `{}`, found `{}`",
    //                           expect_str, found_str))
    //    }
    //
    //   self.bump();
    //}

    /// Get a printable representation of the current char
    fn curr_repr(&self) -> SharedString {
        match self.curr {
            Some(c) => {
                let mut repr = vec![];
                c.escape_default(|r| repr.push(r));
                Rc::new(String::from_chars(repr[]))
            },
            None => rcstr("EOF")
        }
    }

    /// Collect a series of chars starting at the current character
    fn collect(&mut self, cond: |&char| -> bool) -> SharedString {
        let mut chars = vec![];

        debug!("start colleting")

        while let Some(c) = self.curr {
            if cond(&c) {
                chars.push(c);
                self.bump();
            } else {
                debug!("colleting finished")
                break;
            }
        }

        Rc::new(String::from_chars(chars[]))
    }

    //fn eat_all(&mut self, cond: |&char| -> bool) {
    //    while let Some(c) = self.curr {
    //        if cond(&c) { self.bump(); }
    //        else { break; }
    //    }
    //}

    // --- Internal methods: Tokenizers -----------------------------------------

    /// Tokenize a number
    fn tokenize_number(&mut self) -> LexerResult<Token> {
        let sign = if self.curr == Some('-') {
            self.bump();
            -1
        } else {
            1
        };
        let integer = self.collect(|c| c.is_digit());

        let integer = if let Some(m) = from_str(integer[]) { m }
                      else { invalid_integer!(integer @ self.get_source()) };

        Ok(Token::INTEGER(sign * integer))
    }

    /// Read the next token and return it
    fn read_token(&mut self) -> LexerResult<Option<Token>> {
        let c = match self.curr {
            Some(c) => c,
            None => return Ok(Some(Token::EOF))
        };

        let token = match c {
            c if c.is_digit() => {
                try!(self.tokenize_number())
            },
            '+' | '-' | '*' | '/' => {
                let is_num = c == '-' && match self.nextch() {
                    Some(c) => c.is_digit(),
                    None => false
                };

                if is_num {
                    // Tokenize number
                    try!(self.tokenize_number())
                } else {
                    self.bump();
                    Token::SYMBOL(rcstring(String::from_chars(&[c])))
                }
            },
            '(' => { self.bump(); Token::LPAREN },
            ')' => { self.bump(); Token::RPAREN },

            c if c.is_whitespace() => {
                if c == '\n' { self.lineno += 1; }

                self.bump();
                return Ok(None);
            },
            _ => {
                unknown_token!(self.curr_repr() @ self.get_source())
                // UNKNOWN(format!("{}", c).into_string())
            }
        };

        Ok(Some(token))
    }
}

impl<'a> Lexer for FileLexer<'a> {
    fn get_source(&self) -> SourceLocation {
        SourceLocation {
            filename: self.file.clone(),
            lineno: self.lineno
        }
    }

    fn next_token(&mut self) -> LexerResult<Token> {
        if self.is_eof() {
            Ok(Token::EOF)
        } else {
            let mut tok = try!(self.read_token());
            while tok.is_none() {
                // Token is to be ignored, try next one
                tok = try!(self.read_token());
            }

            Ok(tok.unwrap())  // Can't really be None any more
        }
    }

    #[allow(dead_code)]  // Used for tests
    fn tokenize(&mut self) -> LexerResult<Vec<Token>> {
        let mut tokens = vec![];

        // NOTE: We can't use `for c in self.iter` because then we can't
        //       access `self.iter` inside the body because it's borrowed.
        while !self.is_eof() {
            debug!("Processing {}", self.curr)

            if let Some(t) = try!(self.read_token()) {
                tokens.push(t);
            }

            debug!("So far: {}", tokens)
        }

        Ok(tokens)
    }
}

// A dummy lexer that takes it's tokens from a vector. Used for parser testing.
impl Lexer for Vec<Token> {
    fn get_source(&self) -> SourceLocation {
        dummy_source()
    }

    fn next_token(&mut self) -> LexerResult<Token> {
        match self.remove(0) {
            Some(tok) => Ok(tok),
            None => Ok(Token::EOF)
        }
    }

    fn tokenize(&mut self) -> LexerResult<Vec<Token>> {
        let mut v = vec![];
        v.push_all(self[]);

        Ok(v)
    }
}

#[cfg(test)]
mod tests {
    use parser::lexer::{Lexer, FileLexer};
    use parser::tokens::Token;
    use parser::tokens::Token::*;
    use parser::util::rcstr;

    fn tokenize(src: &'static str) -> Vec<Token> {
        FileLexer::new(src, "<test>").tokenize()
    }

    #[test]
    fn test_symbol() {
        assert_eq!(tokenize("+"),
                   vec![SYMBOL(rcstr("+"))]);
        assert_eq!(tokenize("-"),
                   vec![SYMBOL(rcstr("-"))]);
        assert_eq!(tokenize("*"),
                   vec![SYMBOL(rcstr("*"))]);
        assert_eq!(tokenize("/"),
                   vec![SYMBOL(rcstr("/"))]);
    }

    #[test]
    fn test_number() {
        assert_eq!(tokenize("123"),
                   vec![INTEGER(123)]);
    }

    #[test]
    fn test_number_neg() {
        assert_eq!(tokenize("-123"),
                   vec![INTEGER(-123)]);
    }

    #[test]
    fn test_parens() {
        assert_eq!(tokenize("("),
                   vec![LPAREN]);
        assert_eq!(tokenize(")"),
                   vec![RPAREN]);
    }
}<commit_msg>Don't crash on empty input<commit_after>//! Input tokenizer

use std;
use std::rc::Rc;
use parser::tokens::{Token, SourceLocation, dummy_source};
use parser::util::{SharedString, rcstr, rcstring};

// --- Lexer: Error -------------------------------------------------------------

pub type LexerResult<T> = Result<T, LexerError>;

pub enum LexerError {
    UnknownToken {
        token: SharedString,  // result of curr_repr
        location: SourceLocation
    },
    InvalidInteger {
        input: SharedString,
        location: SourceLocation
    }
}

impl std::fmt::Show for LexerError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            LexerError::UnknownToken { ref token, ref location } => {
                write!(f, "unknown token: `{}` at {}", token, location)
            },
            LexerError::InvalidInteger { ref input, ref location } => {
                write!(f, "invalid integer: `{}` at {}", input, location)
            }
        }
    }
}

macro_rules! unknown_token(
    ($token:expr @ $location:expr) => (
        return Err(LexerError::UnknownToken {
            token: $token.clone(),
            location: $location
        })
    )
)

macro_rules! invalid_integer(
    ($input:expr @ $location:expr) => (
        return Err(LexerError::InvalidInteger {
            input: $input.clone(),
            location: $location
        })
    )
)

// --- Lexer --------------------------------------------------------------------

pub trait Lexer {
    /// Get the source of the current token
    fn get_source(&self) -> SourceLocation;

    /// Get the next token
    fn next_token(&mut self) -> LexerResult<Token>;

    /// Tokenize the input into a vector
    fn tokenize(&mut self) -> LexerResult<Vec<Token>>;
}

/// Lexer for tokenize a file from disk/memory.
pub struct FileLexer<'a> {
    source: &'a str,
    file: SharedString,
    len: uint,
    pos: uint,
    curr: Option<char>,
    lineno: uint
}

impl<'a> FileLexer<'a> {
    pub fn new(source: &'a str, file: &'a str) -> FileLexer<'a> {
        FileLexer {
            source: source,
            file: rcstr(file),
            len: source.len(),
            pos: 0,
            curr: if source.len() > 0 { Some(source.char_at(0)) } else { None },
            lineno: 1
        }
    }

    /// --- Internal methods: Helpers -------------------------------------------

    /// Whether we've reached EOF
    fn is_eof(&self) -> bool {
        self.curr.is_none()
    }

    /// Move on to the next char
    fn bump(&mut self) {
        self.curr = self.nextch();
        self.pos += 1;

        debug!("Moved on to {}", self.curr)
    }

    /// Get the next char if possible
    fn nextch(&self) -> Option<char> {
        let mut new_pos = self.pos + 1;
        // When encountering multi-byte UTF-8, we may stop in the middle
        // of it. Fast forward till we see the next actual char or EOF
        while !self.source.is_char_boundary(new_pos)
            && self.pos < self.len {
            new_pos += 1;
        }

        if new_pos < self.len {
            Some(self.source.char_at(new_pos))
        } else {
            None
        }
    }

    //fn expect(&mut self, expect: char) {
    //    if self.curr != Some(expect) {
    //        let expect_str = match expect {
    //            '\'' => "quote".into_string(),
    //            c => format!("'{}'", c)
    //        };
    //        let found_str = match self.curr {
    //            Some(_) => format!("'{}'", self.curr_repr()),
    //            None => "EOF".into_string()
    //        };
    //
    //        self.fatal(format!("Expected `{}`, found `{}`",
    //                           expect_str, found_str))
    //    }
    //
    //   self.bump();
    //}

    /// Get a printable representation of the current char
    fn curr_repr(&self) -> SharedString {
        match self.curr {
            Some(c) => {
                let mut repr = vec![];
                c.escape_default(|r| repr.push(r));
                Rc::new(String::from_chars(repr[]))
            },
            None => rcstr("EOF")
        }
    }

    /// Collect a series of chars starting at the current character
    fn collect(&mut self, cond: |&char| -> bool) -> SharedString {
        let mut chars = vec![];

        debug!("start colleting")

        while let Some(c) = self.curr {
            if cond(&c) {
                chars.push(c);
                self.bump();
            } else {
                debug!("colleting finished")
                break;
            }
        }

        Rc::new(String::from_chars(chars[]))
    }

    //fn eat_all(&mut self, cond: |&char| -> bool) {
    //    while let Some(c) = self.curr {
    //        if cond(&c) { self.bump(); }
    //        else { break; }
    //    }
    //}

    // --- Internal methods: Tokenizers -----------------------------------------

    /// Tokenize a number
    fn tokenize_number(&mut self) -> LexerResult<Token> {
        let sign = if self.curr == Some('-') {
            self.bump();
            -1
        } else {
            1
        };
        let integer = self.collect(|c| c.is_digit());

        let integer = if let Some(m) = from_str(integer[]) { m }
                      else { invalid_integer!(integer @ self.get_source()) };

        Ok(Token::INTEGER(sign * integer))
    }

    /// Read the next token and return it
    fn read_token(&mut self) -> LexerResult<Option<Token>> {
        let c = match self.curr {
            Some(c) => c,
            None => return Ok(Some(Token::EOF))
        };

        let token = match c {
            c if c.is_digit() => {
                try!(self.tokenize_number())
            },
            '+' | '-' | '*' | '/' => {
                let is_num = c == '-' && match self.nextch() {
                    Some(c) => c.is_digit(),
                    None => false
                };

                if is_num {
                    // Tokenize number
                    try!(self.tokenize_number())
                } else {
                    self.bump();
                    Token::SYMBOL(rcstring(String::from_chars(&[c])))
                }
            },
            '(' => { self.bump(); Token::LPAREN },
            ')' => { self.bump(); Token::RPAREN },

            c if c.is_whitespace() => {
                if c == '\n' { self.lineno += 1; }

                self.bump();
                return Ok(None);
            },
            _ => {
                unknown_token!(self.curr_repr() @ self.get_source())
                // UNKNOWN(format!("{}", c).into_string())
            }
        };

        Ok(Some(token))
    }
}

impl<'a> Lexer for FileLexer<'a> {
    fn get_source(&self) -> SourceLocation {
        SourceLocation {
            filename: self.file.clone(),
            lineno: self.lineno
        }
    }

    fn next_token(&mut self) -> LexerResult<Token> {
        if self.is_eof() {
            Ok(Token::EOF)
        } else {
            let mut tok = try!(self.read_token());
            while tok.is_none() {
                // Token is to be ignored, try next one
                tok = try!(self.read_token());
            }

            Ok(tok.unwrap())  // Can't really be None any more
        }
    }

    #[allow(dead_code)]  // Used for tests
    fn tokenize(&mut self) -> LexerResult<Vec<Token>> {
        let mut tokens = vec![];

        // NOTE: We can't use `for c in self.iter` because then we can't
        //       access `self.iter` inside the body because it's borrowed.
        while !self.is_eof() {
            debug!("Processing {}", self.curr)

            if let Some(t) = try!(self.read_token()) {
                tokens.push(t);
            }

            debug!("So far: {}", tokens)
        }

        Ok(tokens)
    }
}

// A dummy lexer that takes it's tokens from a vector. Used for parser testing.
impl Lexer for Vec<Token> {
    fn get_source(&self) -> SourceLocation {
        dummy_source()
    }

    fn next_token(&mut self) -> LexerResult<Token> {
        match self.remove(0) {
            Some(tok) => Ok(tok),
            None => Ok(Token::EOF)
        }
    }

    fn tokenize(&mut self) -> LexerResult<Vec<Token>> {
        let mut v = vec![];
        v.push_all(self[]);

        Ok(v)
    }
}

#[cfg(test)]
mod tests {
    use parser::lexer::{Lexer, FileLexer};
    use parser::tokens::Token;
    use parser::tokens::Token::*;
    use parser::util::rcstr;

    fn tokenize(src: &'static str) -> Vec<Token> {
        FileLexer::new(src, "<test>").tokenize()
    }

    #[test]
    fn test_symbol() {
        assert_eq!(tokenize("+"),
                   vec![SYMBOL(rcstr("+"))]);
        assert_eq!(tokenize("-"),
                   vec![SYMBOL(rcstr("-"))]);
        assert_eq!(tokenize("*"),
                   vec![SYMBOL(rcstr("*"))]);
        assert_eq!(tokenize("/"),
                   vec![SYMBOL(rcstr("/"))]);
    }

    #[test]
    fn test_number() {
        assert_eq!(tokenize("123"),
                   vec![INTEGER(123)]);
    }

    #[test]
    fn test_number_neg() {
        assert_eq!(tokenize("-123"),
                   vec![INTEGER(-123)]);
    }

    #[test]
    fn test_parens() {
        assert_eq!(tokenize("("),
                   vec![LPAREN]);
        assert_eq!(tokenize(")"),
                   vec![RPAREN]);
    }
}<|endoftext|>


<commit_before>//! This module contains a trait representing the (real-valued) objective associated with a
//! discrete optimization problem. The greedy algorithm contained in this crate is implemented for
//! any struct implementing `Objective`.
use std::fmt::Debug;
use std::hash::Hash;
use rand::{Rng, thread_rng};
use rand::distributions::{Range, IndependentSample};
use errors::*;
use fnv::FnvHashSet;

#[derive(Copy, Clone, Debug)]
pub enum SampleElement<E> {
    Dependent(E),
    NonDependent,
}

use self::SampleElement::*;

pub type Set<E> = FnvHashSet<E>;

pub type ElementIterator<'a, O> = Box<Iterator<Item = <O as Objective>::Element> + 'a>;

pub mod curvature {
    //! Marker traits for Curvature bounds.
    //! These allow compile-time verification that an algorithm called on an `Objective` has its
    //! curvature constraints satisfied.
    //!
    //! As a concrete example, the `greedy::lazier_greedy` method requires a `Submodular`
    //! objective. Previously, this was done by checking an enum and panicking if it failed. Marker
    //! traits move the error from runtime to compilation.

    /// An objective with fixed bounds that are not the (0, 1) of `Submodular` or (1, ∞) of
    /// `Supermodular`.
    ///
    /// Ideally, this trait would be implemented automatically for any `Submodular` or
    /// `Supermodular` type. This may be done with an `avarice-derive` crate in the future.
    pub trait Bounded: super::Objective {
        fn bounds() -> (Option<f64>, Option<f64>);
    }

    /// A submodular objective, which satisfies `∀ S ⊆ T: f(S ∪ {e}) - f(S) ≥ f(T ∪ {e}) - f(T)` or (equivalently) has curvature bounded above by `1`.
    ///
    /// The `Bounded` implementation should return a fixed `(None, Some(1.0))`.
    pub trait Submodular: Bounded {}

    /// A supermodular objective, which satisfies `∀ S ⊆ T: f(S ∪ {e}) - f(S) ≤ f(T ∪ {e}) - f(T)` or (equivalently) has curvature bounded below by `1`.
    ///
    /// The `Bounded` implementation should return a fixed `(Some(1.0), None)`.
    pub trait Supermodular: Bounded {}

    /// A modular objective. The `Bounded` implementation should return a fixed `(Some(1.0),
    /// Some(1.0))`.
    ///
    /// This trait is the least common. The semantics of this type of objective are best understood
    /// by the equation `f(S) = Σf(e)`. That is, the objective is exactly the sum of the weights of
    /// the elements of the set `S`.
    pub trait Modular: Bounded {}
    impl<T: Modular> Submodular for T {}
    impl<T: Modular> Supermodular for T {}
}

/// An objective to be optimized.
///
/// The functions are assumed to be 'internally-stateless'. That is, they do not modify the problem
/// instance, and instead track any relevant information on the associated `State` type. This
/// allows snapshotting of the state and allows safe parallel computation.
///
/// Any implementation must at a minimum implement `elements`, `benefit`, and `insert_mut`, though
/// at least implementing `delta` is strongly encouraged.  `nabla` may be omitted if your
/// application does not need it.
pub trait Objective: Sized {
    type Element: Eq + Hash + Clone + Copy + Debug;
    /// Holds any state that is tracked either to assist in correctness or to provide e.g.
    /// cacheing.
    type State: Default + Clone;

    /// The domain of the solution.
    fn elements(&self) -> ElementIterator<Self>;

    /// The implementation of the function `f`. This is the eponymous `Objective`.
    fn benefit(&self, s: &Set<Self::Element>, state: &Self::State) -> Result<f64>;

    /// The marginal gain $\delta(u \mid S) = f(S \cup u) - f(S)$
    ///
    /// Should return `0` for $u \in S$.
    ///
    /// The default implementation is incredibly inefficient (two calls to `benefit`), so
    /// overriding this is *strongly* encouraged.
    fn delta(&self, u: Self::Element, s: &Set<Self::Element>, state: &Self::State) -> Result<f64> {
        let next_state = self.insert(u, state)?;
        let mut next_set = s.clone();
        next_set.insert(u);

        Ok(self.benefit(&next_set, &next_state)? - self.benefit(s, state)?)
    }

    /// The primal curvature $\nabla(u, v\mid S) = \frac{\delta(u \mid S \cup v)}{\delta(u \mid
    /// S)}$.
    ///
    /// Should return `1` for $u \in S$ or when `depends(u, S)` does not contain `v`.
    ///
    /// The default implementation should be avoided, as it computes this result directly by
    /// calling `delta` twice.
    fn nabla(&self,
             u: Self::Element,
             v: Self::Element,
             s: &Set<Self::Element>,
             state: &Self::State)
             -> Result<f64> {
        let next_state = self.insert(v, state)?;
        let mut next_set = s.clone();
        next_set.insert(v);

        let lower = self.delta(u, s, state)?;
        let upper = self.delta(u, &next_set, &next_state)?;
        if lower == 0.0 && upper == 0.0 {
            Ok(1.0)
        } else if lower == 0.0 {
            Err(ErrorKind::DivideByZero(upper, lower).into())
        } else {
            Ok(upper / lower)
        }
    }

    /// The elements on which the marginal gain of `u` depends. The technical definition I have
    /// been using is based on the primal curvature (`nabla`): if `u` depends on `v`, then
    /// `nabla(u, v, sol, state) == 1`.
    fn depends(&self, u: Self::Element, state: &Self::State) -> Result<ElementIterator<Self>>;

    /// Update the state tracked in `s` to reflect the insertion of `u` into the solution.
    fn insert_mut(&self, u: Self::Element, s: &mut Self::State) -> Result<()>;

    /// Produce a new state reflecting the insertion of `u` into the solution.
    fn insert(&self, u: Self::Element, s: &Self::State) -> Result<Self::State> {
        let mut state = s.clone();
        self.insert_mut(u, &mut state)?;
        Ok(state)
    }

    /// Uniformly sample a sequence of `k` elements from about element `u` consistent with initial
    /// state `state`. *The state is not updated, so if the dependent set is state-dependent,
    /// future elements may not be valid selections.*
    fn sample_sequence(&self,
                       u: Self::Element,
                       k: usize,
                       bias: Option<f64>,
                       sol: &Set<Self::Element>,
                       state: &Self::State)
                       -> Result<Vec<SampleElement<Self::Element>>> {
        let mut sample = Vec::with_capacity(k);
        let mut deps = self.depends(u, state)?
            .filter(|&v| !sol.contains(&v))
            .collect::<Vec<_>>();
        let mut rng = thread_rng();
        rng.shuffle(&mut deps);

        let bias = bias.unwrap_or_else(|| {
            deps.len() as f64 / (self.elements().count() - sol.len()) as f64
        });

        let uniform = Range::new(0.0, 1.0);

        for _ in 0..k {
            if uniform.ind_sample(&mut rng) <= bias && !deps.is_empty() {
                // take a dependent element
                sample.push(Dependent(deps.pop().unwrap()));
            } else {
                sample.push(NonDependent);
            }
        }

        Ok(sample)
    }

    /// Compute the total primal curvature of an element `u` after the first `k` elements of
    /// `sequence` have been added. If the `sequence` does not have at least `k` elements,
    /// `ErrorKind::SampleTooSmall` is returned.
    fn gamma(&self,
             u: Self::Element,
             k: usize,
             mut sol: Set<Self::Element>,
             sequence: Vec<SampleElement<Self::Element>>,
             mut state: Self::State)
             -> Result<(f64, Set<Self::Element>, Self::State)> {
        if sequence.len() < k {
            return Err(ErrorKind::SampleTooSmall(sequence.len(), k).into());
        }
        let mut prod = 1f64;
        for e in sequence.iter().take(k) {
            prod *= match e {
                &Dependent(v) => self.nabla(u, v, &sol, &state)?,
                // proof of this relation is in the 2017 notebook, pg. 8-9
                //
                // the gist of it is that the non-dependence of x implies a pair of f_u(S \cup {x})
                // = f_u(S)-style relations, which are used to show ∇(u, v | S) = ∇(u, v | S \cup
                // {x}).
                &NonDependent => 1f64,
            };

            if let &Dependent(v) = e {
                self.insert_mut(v, &mut state)?;
                sol.insert(v);
            }
        }

        Ok((prod, sol, state))
    }

    /// Compute the sequence of total primal curvature values for `1..k` elements of `sequence`.
    /// Unlike `gamma`, this does not return the final solution or state. `gamma(0|S)` is omitted
    /// as it is constant `1`.
    fn gamma_seq(&self,
                 u: Self::Element,
                 k: usize,
                 mut sol: Set<Self::Element>,
                 sequence: Vec<SampleElement<Self::Element>>,
                 mut state: Self::State)
                 -> Result<Vec<f64>> {
        if sequence.len() < k {
            return Err(ErrorKind::SampleTooSmall(sequence.len(), k).into());
        }
        let mut prod = 1f64;
        let mut seq = Vec::with_capacity(k);
        for e in sequence.iter().take(k) {
            prod *= match e {
                &Dependent(v) => self.nabla(u, v, &sol, &state)?,
                // proof of this relation is in the 2017 notebook, pg. 8-9
                //
                // the gist of it is that the non-dependence of x implies a pair of f_u(S \cup {x})
                // = f_u(S)-style relations, which are used to show ∇(u, v | S) = ∇(u, v | S \cup
                // {x}).
                &NonDependent => 1f64,
            };

            seq.push(prod);

            if let &Dependent(v) = e {
                self.insert_mut(v, &mut state)?;
                sol.insert(v);
            }
        }
        Ok(seq)
    }
}

/// Lazy extension of `Objective`. While the same state type is used, state updates are handled
/// independently. If you need two different kinds of state for each kind of update, consider using
/// an enum-struct. For example:
///
/// ```
/// #[derive(Clone, Debug)]
/// pub enum State {
///     Empty,
///     Strict {
///         foo: usize,
///         bar: Vec<usize>,
///     },
///     Lazy {
///         baz: f64,
///     }
/// }
///
/// impl Default for State {
///     fn default() -> Self {
///         State::Empty
///     }
/// }
/// ```
pub trait LazyObjective: Objective {
    /// Updates the marginal gain of `element`, returning it value after updating the state to
    /// reflect the insertion of `previous` elements.
    ///
    /// Returns `Ok(None)` if the marginal gain does not need to be updated.
    fn update_lazy_mut(&self,
                       element: Self::Element,
                       previous: &Set<Self::Element>,
                       state: &mut Self::State)
                       -> Result<Option<f64>>;

    /// Update the state to preserve invariants upon insertion of `element`.
    ///
    /// This should do the least amount of work possible without making `update_lazy_mut` too
    /// complex.
    fn insert_lazy_mut(&self, element: Self::Element, state: &mut Self::State) -> Result<()>;
}
<commit_msg>Cut old gamma code from objective.<commit_after>//! This module contains a trait representing the (real-valued) objective associated with a
//! discrete optimization problem. The greedy algorithm contained in this crate is implemented for
//! any struct implementing `Objective`.
use std::fmt::Debug;
use std::hash::Hash;
use errors::*;
use fnv::FnvHashSet;

pub type Set<E> = FnvHashSet<E>;

pub type ElementIterator<'a, O> = Box<Iterator<Item = <O as Objective>::Element> + 'a>;

pub mod curvature {
    //! Marker traits for Curvature bounds.
    //! These allow compile-time verification that an algorithm called on an `Objective` has its
    //! curvature constraints satisfied.
    //!
    //! As a concrete example, the `greedy::lazier_greedy` method requires a `Submodular`
    //! objective. Previously, this was done by checking an enum and panicking if it failed. Marker
    //! traits move the error from runtime to compilation.

    /// An objective with fixed bounds that are not the (0, 1) of `Submodular` or (1, ∞) of
    /// `Supermodular`.
    pub trait Bounded: super::Objective {
        fn bounds() -> (Option<f64>, Option<f64>);
    }

    /// A submodular objective, which satisfies `∀ S ⊆ T: f(S ∪ {e}) - f(S) ≥ f(T ∪ {e}) - f(T)` or (equivalently) has curvature bounded above by `1`.
    ///
    /// The `Bounded` implementation should return a fixed `(None, Some(1.0))`.
    pub trait Submodular: Bounded {}

    /// A supermodular objective, which satisfies `∀ S ⊆ T: f(S ∪ {e}) - f(S) ≤ f(T ∪ {e}) - f(T)` or (equivalently) has curvature bounded below by `1`.
    ///
    /// The `Bounded` implementation should return a fixed `(Some(1.0), None)`.
    pub trait Supermodular: Bounded {}

    /// A modular objective. The `Bounded` implementation should return a fixed `(Some(1.0),
    /// Some(1.0))`.
    ///
    /// This trait is the least common. The semantics of this type of objective are best understood
    /// by the equation `f(S) = Σf(e)`. That is, the objective is exactly the sum of the weights of
    /// the elements of the set `S`.
    pub trait Modular: Bounded {}
    impl<T: Modular> Submodular for T {}
    impl<T: Modular> Supermodular for T {}
}

/// An objective to be optimized.
///
/// The functions are assumed to be 'internally-stateless'. That is, they do not modify the problem
/// instance, and instead track any relevant information on the associated `State` type. This
/// allows snapshotting of the state and allows safe parallel computation.
///
/// Any implementation must at a minimum implement `elements`, `benefit`, and `insert_mut`, though
/// at least implementing `delta` is strongly encouraged.  `nabla` may be omitted if your
/// application does not need it.
pub trait Objective: Sized {
    type Element: Eq + Hash + Clone + Copy + Debug;
    /// Holds any state that is tracked either to assist in correctness or to provide e.g.
    /// cacheing.
    type State: Default + Clone;

    /// The domain of the solution.
    fn elements(&self) -> ElementIterator<Self>;

    /// The implementation of the function `f`. This is the eponymous `Objective`.
    fn benefit(&self, s: &Set<Self::Element>, state: &Self::State) -> Result<f64>;

    /// The marginal gain $\delta(u \mid S) = f(S \cup u) - f(S)$
    ///
    /// Should return `0` for $u \in S$.
    ///
    /// The default implementation is incredibly inefficient (two calls to `benefit`), so
    /// overriding this is *strongly* encouraged.
    fn delta(&self, u: Self::Element, s: &Set<Self::Element>, state: &Self::State) -> Result<f64> {
        let next_state = self.insert(u, state)?;
        let mut next_set = s.clone();
        next_set.insert(u);

        Ok(self.benefit(&next_set, &next_state)? - self.benefit(s, state)?)
    }

    /// The primal curvature $\nabla(u, v\mid S) = \frac{\delta(u \mid S \cup v)}{\delta(u \mid
    /// S)}$.
    ///
    /// Should return `1` for $u \in S$ or when `depends(u, S)` does not contain `v`.
    ///
    /// The default implementation should be avoided, as it computes this result directly by
    /// calling `delta` twice.
    fn nabla(&self,
             u: Self::Element,
             v: Self::Element,
             s: &Set<Self::Element>,
             state: &Self::State)
             -> Result<f64> {
        let next_state = self.insert(v, state)?;
        let mut next_set = s.clone();
        next_set.insert(v);

        let lower = self.delta(u, s, state)?;
        let upper = self.delta(u, &next_set, &next_state)?;
        if lower == 0.0 && upper == 0.0 {
            Ok(1.0)
        } else if lower == 0.0 {
            Err(ErrorKind::DivideByZero(upper, lower).into())
        } else {
            Ok(upper / lower)
        }
    }

    /// The elements on which the marginal gain of `u` depends. The technical definition I have
    /// been using is based on the primal curvature (`nabla`): if `u` depends on `v`, then
    /// `nabla(u, v, sol, state) != 1`.
    fn depends(&self, u: Self::Element, state: &Self::State) -> Result<ElementIterator<Self>>;

    /// Update the state tracked in `s` to reflect the insertion of `u` into the solution.
    fn insert_mut(&self, u: Self::Element, s: &mut Self::State) -> Result<()>;

    /// Produce a new state reflecting the insertion of `u` into the solution.
    fn insert(&self, u: Self::Element, s: &Self::State) -> Result<Self::State> {
        let mut state = s.clone();
        self.insert_mut(u, &mut state)?;
        Ok(state)
    }
}

/// Lazy extension of `Objective`. While the same state type is used, state updates are handled
/// independently. If you need two different kinds of state for each kind of update, consider using
/// an enum-struct. For example:
///
/// ```
/// #[derive(Clone, Debug)]
/// pub enum State {
///     Empty,
///     Strict {
///         foo: usize,
///         bar: Vec<usize>,
///     },
///     Lazy {
///         baz: f64,
///     }
/// }
///
/// impl Default for State {
///     fn default() -> Self {
///         State::Empty
///     }
/// }
/// ```
pub trait LazyObjective: Objective {
    /// Updates the marginal gain of `element`, returning it value after updating the state to
    /// reflect the insertion of `previous` elements.
    ///
    /// Returns `Ok(None)` if the marginal gain does not need to be updated.
    fn update_lazy_mut(&self,
                       element: Self::Element,
                       previous: &Set<Self::Element>,
                       state: &mut Self::State)
                       -> Result<Option<f64>>;

    /// Update the state to preserve invariants upon insertion of `element`.
    ///
    /// This should do the least amount of work possible without making `update_lazy_mut` too
    /// complex.
    fn insert_lazy_mut(&self, element: Self::Element, state: &mut Self::State) -> Result<()>;
}
<|endoftext|>