<commit_before>use std::io;
use std::io::Cursor;
use std::io::prelude::*;
use std::net::TcpStream;
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};

const RECV_BUF_SIZE: usize = 8192;
const GQTP_HEADER_SIZE: usize = 24;

#[derive(Debug)]
pub enum GQTPError {
    InvalidProtocol,
    InvalidBodySize,
    StatusError(u16),
    IO(io::Error)
}

impl From<io::Error> for GQTPError {
    fn from(err: io::Error) -> GQTPError {
        GQTPError::IO(err)
    }
}

/// Request [GQTP protocol](http://groonga.org/docs/spec/gqtp.html) over TcpStream
pub struct GQTPRequest<'a> {
    addr: &'a str,
}

impl<'a> Default for GQTPRequest<'a> {
    fn default() -> GQTPRequest<'a> {
        GQTPRequest { addr: "127.0.0.1:10043" }
    }
}

impl<'a> GQTPRequest<'a> {
    /// Create a GQTP client.
    pub fn new() -> GQTPRequest<'a> {
        GQTPRequest::default()
    }

    /// Set host address for GQTP server.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate ruroonga_client as groonga;
    ///
    /// groonga::GQTPRequest::new().with_addr("127.0.0.1:20043");
    /// ```
    pub fn with_addr(mut self, addr: &'a str) -> GQTPRequest<'a> {
        self.addr = addr;
        self
    }

    /// Send request and Receive response.
    pub fn call<C>(&self, command: C) -> Result<String, GQTPError>
        where C: AsRef<str>
    {
        // send
        let mut stream = TcpStream::connect(self.addr).unwrap();
        let mut send_buf = vec![];
        try!(send_buf.write_u8(0xc7));
        try!(send_buf.write_u8(0));
        try!(send_buf.write_i16::<BigEndian>(0));
        try!(send_buf.write_u8(0));
        try!(send_buf.write_u8(0x02));   // flags
        try!(send_buf.write_u16::<BigEndian>(0));
        try!(send_buf.write_u32::<BigEndian>(command.as_ref().len() as u32));
        try!(send_buf.write_u32::<BigEndian>(0));
        try!(send_buf.write_u64::<BigEndian>(0));
        send_buf.extend_from_slice(command.as_ref().as_bytes());
        let _ = stream.write_all(send_buf.as_slice());

        // receive and check protocol header value
        let mut read_buf = vec![0; RECV_BUF_SIZE];
        let _ = stream.read(&mut read_buf);
        let mut buf = Cursor::new(read_buf);

        let protocol = buf.read_u8().unwrap();
        let query_type = buf.read_u8().unwrap();
        if protocol != 0xc7 || query_type > 5 {
            return Err(GQTPError::InvalidProtocol);
        }
        let _ = buf.read_i16::<BigEndian>().unwrap();
        let _ = buf.read_u8().unwrap();

        let flags = buf.read_u8().unwrap();
        if !((flags & 0x01) == 0x01 || (flags & 0x02) == 0x02) {
            return Err(GQTPError::InvalidProtocol);
        }

        let status = buf.read_u16::<BigEndian>().unwrap();
        if status != 0 && status != 1 {
            return Err(GQTPError::StatusError(status));
        }
        let size = buf.read_i32::<BigEndian>().unwrap();
        let _ = buf.read_i32::<BigEndian>().unwrap();    // opaque
        let _ = buf.read_i64::<BigEndian>().unwrap();    // cas

        // read body
        let mut msg_buf_len = if (size as usize + GQTP_HEADER_SIZE) > RECV_BUF_SIZE {
            RECV_BUF_SIZE - GQTP_HEADER_SIZE
        } else {
            size as usize
        };
        let mut msg = vec![0; msg_buf_len];
        let _ = buf.read(&mut msg).unwrap();
        if (size as usize + GQTP_HEADER_SIZE) > RECV_BUF_SIZE {
            loop {
                let mut read_buf = vec![0; RECV_BUF_SIZE];
                let rsize = stream.read(&mut read_buf).unwrap();
                msg.extend_from_slice(read_buf.as_ref());
                msg_buf_len += rsize;
                if msg_buf_len >= size as usize {
                    break;
                }
            }
        }

        Ok(String::from_utf8(msg).unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn smoke_gqtp() {
        let req = GQTPRequest::new();
        assert_eq!("127.0.0.1:10043", req.addr)
    }

    #[test]
    fn smoke_gqtp_with_addr() {
        let req = GQTPRequest::new().with_addr("127.0.0.1:20043");
        assert_eq!("127.0.0.1:20043", req.addr)
    }
}
<commit_msg>gqtp_request: Use try! when reading<commit_after>use std::io;
use std::io::Cursor;
use std::io::prelude::*;
use std::net::TcpStream;
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};

const RECV_BUF_SIZE: usize = 8192;
const GQTP_HEADER_SIZE: usize = 24;

#[derive(Debug)]
pub enum GQTPError {
    InvalidProtocol,
    InvalidBodySize,
    StatusError(u16),
    IO(io::Error)
}

impl From<io::Error> for GQTPError {
    fn from(err: io::Error) -> GQTPError {
        GQTPError::IO(err)
    }
}

/// Request [GQTP protocol](http://groonga.org/docs/spec/gqtp.html) over TcpStream
pub struct GQTPRequest<'a> {
    addr: &'a str,
}

impl<'a> Default for GQTPRequest<'a> {
    fn default() -> GQTPRequest<'a> {
        GQTPRequest { addr: "127.0.0.1:10043" }
    }
}

impl<'a> GQTPRequest<'a> {
    /// Create a GQTP client.
    pub fn new() -> GQTPRequest<'a> {
        GQTPRequest::default()
    }

    /// Set host address for GQTP server.
    ///
    /// # Examples
    ///
    /// ```
    /// extern crate ruroonga_client as groonga;
    ///
    /// groonga::GQTPRequest::new().with_addr("127.0.0.1:20043");
    /// ```
    pub fn with_addr(mut self, addr: &'a str) -> GQTPRequest<'a> {
        self.addr = addr;
        self
    }

    /// Send request and Receive response.
    pub fn call<C>(&self, command: C) -> Result<String, GQTPError>
        where C: AsRef<str>
    {
        // send
        let mut stream = TcpStream::connect(self.addr).unwrap();
        let mut send_buf = vec![];
        try!(send_buf.write_u8(0xc7));
        try!(send_buf.write_u8(0));
        try!(send_buf.write_i16::<BigEndian>(0));
        try!(send_buf.write_u8(0));
        try!(send_buf.write_u8(0x02));   // flags
        try!(send_buf.write_u16::<BigEndian>(0));
        try!(send_buf.write_u32::<BigEndian>(command.as_ref().len() as u32));
        try!(send_buf.write_u32::<BigEndian>(0));
        try!(send_buf.write_u64::<BigEndian>(0));
        send_buf.extend_from_slice(command.as_ref().as_bytes());
        let _ = stream.write_all(send_buf.as_slice());

        // receive and check protocol header value
        let mut read_buf = vec![0; RECV_BUF_SIZE];
        let _ = stream.read(&mut read_buf);
        let mut buf = Cursor::new(read_buf);

        let protocol = try!(buf.read_u8());
        let query_type = try!(buf.read_u8());
        if protocol != 0xc7 || query_type > 5 {
            return Err(GQTPError::InvalidProtocol);
        }
        let _ = try!(buf.read_i16::<BigEndian>());
        let _ = try!(buf.read_u8());

        let flags = try!(buf.read_u8());
        if !((flags & 0x01) == 0x01 || (flags & 0x02) == 0x02) {
            return Err(GQTPError::InvalidProtocol);
        }

        let status = try!(buf.read_u16::<BigEndian>());
        if status != 0 && status != 1 {
            return Err(GQTPError::StatusError(status));
        }
        let size = try!(buf.read_i32::<BigEndian>());
        let _ = try!(buf.read_i32::<BigEndian>());    // opaque
        let _ = try!(buf.read_i64::<BigEndian>());    // cas

        // read body
        let mut msg_buf_len = if (size as usize + GQTP_HEADER_SIZE) > RECV_BUF_SIZE {
            RECV_BUF_SIZE - GQTP_HEADER_SIZE
        } else {
            size as usize
        };
        let mut msg = vec![0; msg_buf_len];
        let _ = try!(buf.read(&mut msg));
        if (size as usize + GQTP_HEADER_SIZE) > RECV_BUF_SIZE {
            loop {
                let mut read_buf = vec![0; RECV_BUF_SIZE];
                let rsize = try!(stream.read(&mut read_buf));
                msg.extend_from_slice(read_buf.as_ref());
                msg_buf_len += rsize;
                if msg_buf_len >= size as usize {
                    break;
                }
            }
        }

        Ok(String::from_utf8(msg).unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn smoke_gqtp() {
        let req = GQTPRequest::new();
        assert_eq!("127.0.0.1:10043", req.addr)
    }

    #[test]
    fn smoke_gqtp_with_addr() {
        let req = GQTPRequest::new().with_addr("127.0.0.1:20043");
        assert_eq!("127.0.0.1:20043", req.addr)
    }
}
<|endoftext|>


<commit_before>//! Definition of various functions, constants and types useful only for
//! x86 architectures

use core::ops::{BitAnd, BitOr};

pub const PAGE_SHIFT: usize = 12;

/// Size of a page
pub const PAGE_SIZE: usize = 1 << PAGE_SHIFT;

/// Mask to apply to a page table entry to isolate the flags
pub const PTE_FLAGS_MASK: u64 = (PAGE_SIZE - 1) as u64;

// Special case: Xen uses PAE for x86 which changes some constants related to
// paging. We are in the arch part of the hal, and this is Xen related,
// but it would not make much sense to define so constants for every single
// platform because of one 'exception'

#[cfg(target_arch = "x86_64")]
pub const PML4_OFFSET_SHIFT: usize = 39;

#[cfg(target_arch = "x86")]
pub const PML4_OFFSET_SHIFT: usize = 0;

pub const PDP_OFFSET_SHIFT: usize = 30;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const PD_OFFSET_SHIFT: usize = 21;

#[cfg(all(not(feature = "xen"), target_arch = "x86"))]
pub const PD_OFFSET_SHIFT: usize = 22;

pub const PT_OFFSET_SHIFT: usize = 12;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const PTE_PER_TABLE: usize = 512;

#[cfg(all(not(feature = "xen"), target_arch = "x86"))]
pub const PTE_PER_TABLE: usize = 1024;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const PTE_MASK: u64 = (1 << 44) - 1;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const OFFSET_MASK: usize = 0x1FF;

#[cfg(all(not(feature = "xen"), target_arch = "x86"))]
pub const OFFSET_MASK: usize = 0x3FF;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PageEntry<T = usize> {
    entry: T,
}

// XXX: Is there a better solution than that ?
impl<T> PageEntry<T> where T: BitOr<T> + From<<T as BitOr>::Output>,
                           T: BitAnd<T> + From<<T as BitAnd>::Output>,
                           T: PartialEq<T> + From<PageFlags> + Copy {
    #[inline]
    pub fn new(entry: T) -> Self {
        PageEntry {
            entry: entry
        }
    }

    #[inline]
    pub fn value(&self) -> T {
        self.entry
    }

    #[inline]
    pub fn mask(mut self, mask: T) -> Self {
        self.entry = T::from(self.entry & mask);
        self
    }

    #[inline]
    pub fn set(mut self, flag: PageFlags) -> Self {
        self.entry = T::from(self.entry | T::from(flag));
        self
    }

    #[inline]
    pub fn has(&self, flags: PageFlags) -> bool {
        let flags = T::from(flags);

        T::from(self.entry & flags) == flags
    }
}

#[allow(dead_code)]
pub enum PageFlags {
    Present = 0x1,
    Writable = 0x2,
    User = 0x4,
    WriteThrough = 0x8,
    CacheDisabled = 0x10,
}

impl From<PageFlags> for usize {
    #[inline]
    fn from(flags: PageFlags) -> Self {
        flags as usize
    }
}

impl From<PageFlags> for u32 {
    #[inline]
    fn from(flags: PageFlags) -> Self {
        flags as u32
    }
}

impl From<PageFlags> for u64 {
    #[inline]
    fn from(flags: PageFlags) -> Self {
        flags as u64
    }
}

pub unsafe fn atomic_set_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock bts $1, $0"
         :: "=*m" (addr as *mut u32), "Ir" (nr)
         : "memory"
         : "volatile");
}

pub unsafe fn atomic_clear_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock btr $1, $0"
         :: "=*m" (addr as *mut u32), "Ir" (nr)
         : "memory"
         : "volatile");
}

pub fn first_bit(data: usize) -> usize {
    unsafe {
        let ret;

        asm!("bsf $1, $0\n\t\
              jnz 1f\n\t
              mov $$0, $0\n\t
              1:\n\t"
             : "=r" (ret)
             : "r" (data)
             :: "volatile");

        ret
    }
}

pub fn wmb() {
    unsafe {
        asm!("sfence" ::: "memory" : "volatile");
    }
}

#[test]
pub fn test_set_and_clear() {
    let mut array = [0u32; 4];

    unsafe {
        atomic_set_bit(1, &mut array[0] as *mut u32);
        atomic_set_bit(2, &mut array[0] as *mut u32);
        atomic_set_bit(3, &mut array[0] as *mut u32);
        atomic_set_bit(32, &mut array[0] as *mut u32);
        atomic_set_bit(33, &mut array[0] as *mut u32);
        atomic_set_bit(34, &mut array[0] as *mut u32);
        atomic_set_bit(127, &mut array[0] as *mut u32);

        assert_eq!(array[0], 0xE);
        assert_eq!(array[1], 0x7);
        assert_eq!(array[2], 0x0);
        assert_eq!(array[3], 0x80000000);

        atomic_clear_bit(1, &mut array[0] as *mut u32);
        atomic_clear_bit(2, &mut array[0] as *mut u32);
        atomic_clear_bit(3, &mut array[0] as *mut u32);
        atomic_clear_bit(32, &mut array[0] as *mut u32);
        atomic_clear_bit(33, &mut array[0] as *mut u32);
        atomic_clear_bit(34, &mut array[0] as *mut u32);
        atomic_clear_bit(127, &mut array[0] as *mut u32);

        assert_eq!(array[0], 0x0);
        assert_eq!(array[1], 0x0);
        assert_eq!(array[2], 0x0);
        assert_eq!(array[3], 0x0);
    }
}

#[test]
pub fn test_first_bit() {
    assert_eq!(first_bit(0x0), 0);
    assert_eq!(first_bit(0x1), 0);
    assert_eq!(first_bit(0x2), 1);
    assert_eq!(first_bit(0x3), 0);
    assert_eq!(first_bit(0x8000), 15);
    assert_eq!(first_bit(0xFF80), 7);
    assert_eq!(first_bit(0x80000000), 31);
}
<commit_msg>misc: Fix build issues with latest nightly<commit_after>//! Definition of various functions, constants and types useful only for
//! x86 architectures

use core::ops::{BitAnd, BitOr};

pub const PAGE_SHIFT: usize = 12;

/// Size of a page
pub const PAGE_SIZE: usize = 1 << PAGE_SHIFT;

/// Mask to apply to a page table entry to isolate the flags
pub const PTE_FLAGS_MASK: u64 = (PAGE_SIZE - 1) as u64;

// Special case: Xen uses PAE for x86 which changes some constants related to
// paging. We are in the arch part of the hal, and this is Xen related,
// but it would not make much sense to define so constants for every single
// platform because of one 'exception'

#[cfg(target_arch = "x86_64")]
pub const PML4_OFFSET_SHIFT: usize = 39;

#[cfg(target_arch = "x86")]
pub const PML4_OFFSET_SHIFT: usize = 0;

pub const PDP_OFFSET_SHIFT: usize = 30;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const PD_OFFSET_SHIFT: usize = 21;

#[cfg(all(not(feature = "xen"), target_arch = "x86"))]
pub const PD_OFFSET_SHIFT: usize = 22;

pub const PT_OFFSET_SHIFT: usize = 12;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const PTE_PER_TABLE: usize = 512;

#[cfg(all(not(feature = "xen"), target_arch = "x86"))]
pub const PTE_PER_TABLE: usize = 1024;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const PTE_MASK: u64 = (1 << 44) - 1;

#[cfg(any(feature = "xen", target_arch = "x86_64"))]
pub const OFFSET_MASK: usize = 0x1FF;

#[cfg(all(not(feature = "xen"), target_arch = "x86"))]
pub const OFFSET_MASK: usize = 0x3FF;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct PageEntry<T = usize> {
    entry: T,
}

// XXX: Is there a better solution than that ?
impl<T> PageEntry<T> where T: BitOr<T> + From<<T as BitOr>::Output>,
                           T: BitAnd<T> + From<<T as BitAnd>::Output>,
                           T: PartialEq<T> + From<PageFlags> + Copy {
    #[inline]
    pub fn new(entry: T) -> Self {
        PageEntry {
            entry: entry
        }
    }

    #[inline]
    pub fn value(&self) -> T {
        self.entry
    }

    #[inline]
    pub fn mask(mut self, mask: T) -> Self {
        self.entry = T::from(self.entry & mask);
        self
    }

    #[inline]
    pub fn set(mut self, flag: PageFlags) -> Self {
        self.entry = T::from(self.entry | T::from(flag));
        self
    }

    #[inline]
    pub fn has(&self, flags: PageFlags) -> bool {
        let flags = T::from(flags);

        T::from(self.entry & flags) == flags
    }
}

#[allow(dead_code)]
pub enum PageFlags {
    Present = 0x1,
    Writable = 0x2,
    User = 0x4,
    WriteThrough = 0x8,
    CacheDisabled = 0x10,
}

impl From<PageFlags> for usize {
    #[inline]
    fn from(flags: PageFlags) -> Self {
        flags as usize
    }
}

impl From<PageFlags> for u32 {
    #[inline]
    fn from(flags: PageFlags) -> Self {
        flags as u32
    }
}

impl From<PageFlags> for u64 {
    #[inline]
    fn from(flags: PageFlags) -> Self {
        flags as u64
    }
}

pub unsafe fn atomic_set_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock bts $1, $0"
         : "=*m" (addr as *mut u32)
         : "Ir" (nr)
         : "memory"
         : "volatile");
}

pub unsafe fn atomic_clear_bit<T>(nr: usize, addr: *mut T) {
    asm!("lock btr $1, $0"
         : "=*m" (addr as *mut u32)
         : "Ir" (nr)
         : "memory"
         : "volatile");
}

pub fn first_bit(data: usize) -> usize {
    unsafe {
        let ret;

        asm!("bsf $1, $0\n\t\
              jnz 1f\n\t
              mov $$0, $0\n\t
              1:\n\t"
             : "=r" (ret)
             : "r" (data)
             :: "volatile");

        ret
    }
}

pub fn wmb() {
    unsafe {
        asm!("sfence" ::: "memory" : "volatile");
    }
}

#[test]
pub fn test_set_and_clear() {
    let mut array = [0u32; 4];

    unsafe {
        atomic_set_bit(1, &mut array[0] as *mut u32);
        atomic_set_bit(2, &mut array[0] as *mut u32);
        atomic_set_bit(3, &mut array[0] as *mut u32);
        atomic_set_bit(32, &mut array[0] as *mut u32);
        atomic_set_bit(33, &mut array[0] as *mut u32);
        atomic_set_bit(34, &mut array[0] as *mut u32);
        atomic_set_bit(127, &mut array[0] as *mut u32);

        assert_eq!(array[0], 0xE);
        assert_eq!(array[1], 0x7);
        assert_eq!(array[2], 0x0);
        assert_eq!(array[3], 0x80000000);

        atomic_clear_bit(1, &mut array[0] as *mut u32);
        atomic_clear_bit(2, &mut array[0] as *mut u32);
        atomic_clear_bit(3, &mut array[0] as *mut u32);
        atomic_clear_bit(32, &mut array[0] as *mut u32);
        atomic_clear_bit(33, &mut array[0] as *mut u32);
        atomic_clear_bit(34, &mut array[0] as *mut u32);
        atomic_clear_bit(127, &mut array[0] as *mut u32);

        assert_eq!(array[0], 0x0);
        assert_eq!(array[1], 0x0);
        assert_eq!(array[2], 0x0);
        assert_eq!(array[3], 0x0);
    }
}

#[test]
pub fn test_first_bit() {
    assert_eq!(first_bit(0x0), 0);
    assert_eq!(first_bit(0x1), 0);
    assert_eq!(first_bit(0x2), 1);
    assert_eq!(first_bit(0x3), 0);
    assert_eq!(first_bit(0x8000), 15);
    assert_eq!(first_bit(0xFF80), 7);
    assert_eq!(first_bit(0x80000000), 31);
}
<|endoftext|>


<commit_before>use super::super::{ Network, NodeId };

pub fn pagerank<N: Network>(network: &N, beta: f64, eps: f64) -> Vec<f64> {
  let init_value = 1.0 / (network.num_nodes() as f64);
  let mut ranks = vec![0.0; network.num_nodes()];
  let mut new_ranks = vec![init_value; network.num_nodes()];
  let adj_lists = build_adj_list(network);
  let inv_out_deg = inv_out_deg(network);
  while !is_converged(&ranks, &new_ranks, eps) {
    ranks = new_ranks;
    new_ranks = mult_matrix_vec(&adj_lists, &inv_out_deg, beta, &ranks);
    normalize(&mut new_ranks);
  }
  ranks
}

fn inv_out_deg<N: Network>(network: &N) -> Vec<f64> {
  let mut inv_out_deg = Vec::with_capacity(network.num_nodes());
  for i in 0..network.num_nodes() {
    let out_deg = network.adjacent(i as NodeId).len() as f64;
    if out_deg > 0.0 {
      inv_out_deg.push(1.0 / out_deg);
    } else {
      inv_out_deg.push(0.0);
    }
  }
  inv_out_deg
}

fn build_adj_list<N: Network>(network: &N) -> Vec<Vec<usize>> {
  let mut adj_list = Vec::with_capacity(network.num_nodes());
  for i in 0..network.num_nodes() {
    let adj_nodes = network.adjacent(i as NodeId);
    let mut i_th_adj_nodes = Vec::with_capacity(adj_nodes.len());
    for j in adj_nodes {
      i_th_adj_nodes.push(j as usize);
    }
    adj_list.push(i_th_adj_nodes);
  }
  adj_list
}

fn normalize(vector: &mut Vec<f64>) {
  let mut sum = 0.0;
  for i in 0..vector.len() {
    sum += vector[i];
  }

  assert!(sum <= 1.0);
  let corrective_value = (1.0 - sum)/(vector.len() as f64);
  for i in 0..vector.len() {
    vector[i] += corrective_value;
  }
}

fn mult_matrix_vec(adj_list: &Vec<Vec<usize>>, inv_out_degs: &Vec<f64>, beta: f64, current: &Vec<f64>) -> Vec<f64> {
  let mut new_ranks = vec![0.0; current.len()];
  for source_node in 0..current.len() {
    let inv_out_deg = inv_out_degs[source_node];
    for target_node in &adj_list[source_node] {
      new_ranks[*target_node] += (1.0-beta) * inv_out_deg * current[source_node];
    }
  }
  new_ranks
}

fn is_converged(old: &Vec<f64>, new: &Vec<f64>, eps: f64) -> bool {
  assert!(old.len() == new.len());
  let mut sum = 0.0;
  for i in 0..old.len() {
    sum += (old[i] - new[i]).abs();
  }
  sum <= eps
}

#[test]
fn test_inv_out_deg() {
  use super::super::compact_star::compact_star_from_edge_vec;
  let mut edges = vec![
      (0,1,0.0,0.0),
      (0,2,0.0,0.0),
      (0,3,0.0,0.0),
      (1,2,0.0,0.0),
      (1,3,0.0,0.0),
      (2,0,0.0,0.0),
      (3,0,0.0,0.0),
      (3,2,0.0,0.0)];
  let compact_star = compact_star_from_edge_vec(4, &mut edges);
  assert_eq!(vec![1.0/3.0, 1.0/2.0, 1.0/1.0, 1.0/2.0], inv_out_deg(&compact_star));
}

#[test]
fn test_build_adj_list() {
  use super::super::compact_star::compact_star_from_edge_vec;
  let mut edges = vec![
      (0,1,0.0,0.0),
      (0,2,0.0,0.0),
      (0,3,0.0,0.0),
      (1,2,0.0,0.0),
      (1,3,0.0,0.0),
      (2,0,0.0,0.0),
      (3,0,0.0,0.0),
      (3,2,0.0,0.0)];
  let compact_star = compact_star_from_edge_vec(4, &mut edges);
  let adj_list = vec![vec![1,2,3], vec![2,3], vec![0], vec![0,2]];
  assert_eq!(adj_list, build_adj_list(&compact_star));
}

#[test]
fn test_normalize() {
  let mut to_normalize = vec![0.125, 0.125, 0.125, 0.125];
  normalize(&mut to_normalize);
  assert_eq!(vec![0.25, 0.25, 0.25, 0.25], to_normalize);
}

#[test]
fn test_is_converged() {
  let v1 = vec![0.0; 5];
  let v2 = vec![1.0; 5];
  let v3 = vec![1.0, 1.0, 1.0, 1.0, 1.00000001];
  assert!(is_converged(&v1, &v1, 1e-6));
  assert!(!is_converged(&v1, &v2, 1e-6));
  assert!(is_converged(&v2, &v3, 1e-4));
}

#[test]
fn test_pagerank() {
  use super::super::compact_star::compact_star_from_edge_vec;
  let mut edges = vec![
      (0,1,0.0,0.0),
      (0,2,0.0,0.0),
      (0,3,0.0,0.0),
      (1,2,0.0,0.0),
      (1,3,0.0,0.0),
      (2,0,0.0,0.0),
      (3,0,0.0,0.0),
      (3,2,0.0,0.0)];
  let compact_star = compact_star_from_edge_vec(4, &mut edges);
  let ranks = pagerank(&compact_star, 0.2, 1e-3);
  assert_eq!(vec![0.38,0.12,0.29,0.19], ranks);
}
<commit_msg>Finalized pagerank implementation.<commit_after>use super::super::{ Network, NodeId };

/// Runs pagerank algorithm on a graph until convergence.
/// Convergence is reached, when the last ranks vector and the new one
/// differ by less than `eps` in their L1-norm.
/// `beta` is the teleport probability. CAUTION: Never use a teleport
/// probability of `beta == 0.0`!!! Due to precision errors in the double
/// values, the sum of the ranks vector elements can exceed `1.0` which
/// will be caught by an assertion and the algorithm will panic.
/// The result will be the pagerank for each node in the network.
pub fn pagerank<N: Network>(network: &N, beta: f64, eps: f64) -> Vec<f64> {
  let init_value = 1.0 / (network.num_nodes() as f64);
  let mut ranks = vec![0.0; network.num_nodes()];
  let mut new_ranks = vec![init_value; network.num_nodes()];
  let adj_lists = build_adj_list(network);
  let inv_out_deg = inv_out_deg(network);
  while !is_converged(&ranks, &new_ranks, eps) {
    ranks = new_ranks;
    new_ranks = mult_matrix_vec(&adj_lists, &inv_out_deg, beta, &ranks);
    normalize(&mut new_ranks);
  }
  ranks
}
/// Calculates the inverse of the out degree for each node in the network.
/// For out degree `0`, the inverse will also be `0`, guaranteeing that we
/// add `0.0` to the pagerank of the respective node.
fn inv_out_deg<N: Network>(network: &N) -> Vec<f64> {
  let mut inv_out_deg = Vec::with_capacity(network.num_nodes());
  for i in 0..network.num_nodes() {
    let out_deg = network.adjacent(i as NodeId).len() as f64;
    if out_deg > 0.0 {
      inv_out_deg.push(1.0 / out_deg);
    } else {
      inv_out_deg.push(0.0);
    }
  }
  inv_out_deg
}

/// Converts the network in a slightly faster traversable adjacency list.
fn build_adj_list<N: Network>(network: &N) -> Vec<Vec<usize>> {
  let mut adj_list = Vec::with_capacity(network.num_nodes());
  for i in 0..network.num_nodes() {
    let adj_nodes = network.adjacent(i as NodeId);
    let mut i_th_adj_nodes = Vec::with_capacity(adj_nodes.len());
    for j in adj_nodes {
      i_th_adj_nodes.push(j as usize);
    }
    adj_list.push(i_th_adj_nodes);
  }
  adj_list
}

/// Normalize the vector to \sum_i v_i = 1. Remaining mass is distributed
/// evenly over all nodes. (Also known as smoothing.)
fn normalize(vector: &mut Vec<f64>) {
  let mut sum = 0.0;
  for i in 0..vector.len() {
    sum += vector[i];
  }

  assert!(sum <= 1.0);
  let corrective_value = (1.0 - sum)/(vector.len() as f64);
  for i in 0..vector.len() {
    vector[i] += corrective_value;
  }
}

/// Multiply the ranks vector with the adjacency matrix. Every entry is
/// damped by `1.0 - beta`. The vector is multiplied from the left!
fn mult_matrix_vec(adj_list: &Vec<Vec<usize>>, inv_out_degs: &Vec<f64>, beta: f64, current: &Vec<f64>) -> Vec<f64> {
  let mut new_ranks = vec![0.0; current.len()];
  for source_node in 0..current.len() {
    let inv_out_deg = inv_out_degs[source_node];
    for target_node in &adj_list[source_node] {
      new_ranks[*target_node] += (1.0-beta) * inv_out_deg * current[source_node];
    }
  }
  new_ranks
}

/// Determines convergence for two vectors with respect to the tolerance.
fn is_converged(old: &Vec<f64>, new: &Vec<f64>, eps: f64) -> bool {
  assert!(old.len() == new.len());
  let mut sum = 0.0;
  for i in 0..old.len() {
    sum += (old[i] - new[i]).abs();
  }
  sum <= eps
}

#[test]
fn test_inv_out_deg() {
  use super::super::compact_star::compact_star_from_edge_vec;
  let mut edges = vec![
      (0,1,0.0,0.0),
      (0,2,0.0,0.0),
      (0,3,0.0,0.0),
      (1,2,0.0,0.0),
      (1,3,0.0,0.0),
      (2,0,0.0,0.0),
      (3,0,0.0,0.0),
      (3,2,0.0,0.0)];
  let compact_star = compact_star_from_edge_vec(4, &mut edges);
  assert_eq!(vec![1.0/3.0, 1.0/2.0, 1.0/1.0, 1.0/2.0], inv_out_deg(&compact_star));
}

#[test]
fn test_build_adj_list() {
  use super::super::compact_star::compact_star_from_edge_vec;
  let mut edges = vec![
      (0,1,0.0,0.0),
      (0,2,0.0,0.0),
      (0,3,0.0,0.0),
      (1,2,0.0,0.0),
      (1,3,0.0,0.0),
      (2,0,0.0,0.0),
      (3,0,0.0,0.0),
      (3,2,0.0,0.0)];
  let compact_star = compact_star_from_edge_vec(4, &mut edges);
  let adj_list = vec![vec![1,2,3], vec![2,3], vec![0], vec![0,2]];
  assert_eq!(adj_list, build_adj_list(&compact_star));
}

#[test]
fn test_normalize() {
  let mut to_normalize = vec![0.125, 0.125, 0.125, 0.125];
  normalize(&mut to_normalize);
  assert_eq!(vec![0.25, 0.25, 0.25, 0.25], to_normalize);
}

#[test]
fn test_is_converged() {
  let v1 = vec![0.0; 5];
  let v2 = vec![1.0; 5];
  let v3 = vec![1.0, 1.0, 1.0, 1.0, 1.00000001];
  assert!(is_converged(&v1, &v1, 1e-6));
  assert!(!is_converged(&v1, &v2, 1e-6));
  assert!(is_converged(&v2, &v3, 1e-4));
}

#[test]
fn test_pagerank() {
  use super::super::compact_star::compact_star_from_edge_vec;
  let mut edges = vec![
      (0,1,0.0,0.0),
      (0,2,0.0,0.0),
      (0,3,0.0,0.0),
      (1,2,0.0,0.0),
      (1,3,0.0,0.0),
      (2,0,0.0,0.0),
      (3,0,0.0,0.0),
      (3,2,0.0,0.0)];
  let compact_star = compact_star_from_edge_vec(4, &mut edges);
  let ranks = pagerank(&compact_star, 0.2, 1e-3);
  assert_eq!(vec![0.38,0.12,0.29,0.19], ranks);
}
<|endoftext|>


<commit_before>use std::io::{Error, Read, BufRead, Result};
use std::io::ErrorKind::*;

use bitstream::byteread::*;

#[doc = /**
 * Safe bytereader abstraction
 */]
#[allow(dead_code)]
fn get_buffer<R: Read + ?Sized>(reader: &mut R, buf: &mut [u8]) -> Result<()> {
    let mut nread = 0usize;
    while nread < buf.len() {
        match reader.read(&mut buf[nread..]) {
            Ok(0) => return Err(Error::new(UnexpectedEof, "Empty")),
            Ok(n) => nread += n,
            Err(e) => return Err(e),
        }
    }
    Ok(())
}

macro_rules! get {
    ($s: ident, $name: ident, $size: expr) => ({
        let mut buf = [0; $size];
        try!(get_buffer($s, &mut buf));
        Ok($name(&buf))
    })
}

pub trait ByteRead: Read {
    fn get_u8(&mut self) -> Result<u8> {
        get!(self, get_u8, 1)
    }
    fn get_i8(&mut self) -> Result<i8> {
        get!(self, get_i8, 1)
    }
    fn get_u16l(&mut self) -> Result<u16> {
        get!(self, get_u16l, 2)
    }
    fn get_i16l(&mut self) -> Result<i16> {
        get!(self, get_i16l, 2)
    }
    fn get_u32l(&mut self) -> Result<u32> {
        get!(self, get_u32l, 4)
    }
    fn get_i32l(&mut self) -> Result<i32> {
        get!(self, get_i32l, 4)
    }
    fn get_u64l(&mut self) -> Result<u64> {
        get!(self, get_u64l, 8)
    }
    fn get_i64l(&mut self) -> Result<i64> {
        get!(self, get_i64l, 8)
    }
    fn get_f32l(&mut self) -> Result<f32> {
        get!(self, get_f32l, 4)
    }
    fn get_f64l(&mut self) -> Result<f64> {
        get!(self, get_f64l, 8)
    }
    fn get_u16b(&mut self) -> Result<u16> {
        get!(self, get_u16b, 2)
    }
    fn get_i16b(&mut self) -> Result<i16> {
        get!(self, get_i16b, 2)
    }
    fn get_u32b(&mut self) -> Result<u32> {
        get!(self, get_u32b, 4)
    }
    fn get_i32b(&mut self) -> Result<i32> {
        get!(self, get_i32b, 4)
    }
    fn get_u64b(&mut self) -> Result<u64> {
        get!(self, get_u64b, 8)
    }
    fn get_i64b(&mut self) -> Result<i64> {
        get!(self, get_i64b, 8)
    }
    fn get_f32b(&mut self) -> Result<f32> {
        get!(self, get_f32b, 4)
    }
    fn get_f64b(&mut self) -> Result<f64> {
        get!(self, get_f64b, 8)
    }
}

impl<R: Read + ?Sized> ByteRead for R {}

macro_rules! peek {
   ($s: ident, $name: ident, $size: expr) => ({
        let buf = try!($s.fill_buf());
        if buf.len() < $size {
            Err(Error::new(UnexpectedEof, "Empty"))
        } else {
            Ok($name(&buf))
        }
    })
}

// TODO: bind to a trait that is a bit more strict
pub trait BytePeek: BufRead {
    fn peek_u8(&mut self) -> Result<u8> {
        peek!(self, get_u8, 1)
    }
    fn peek_i8(&mut self) -> Result<i8> {
        peek!(self, get_i8, 1)
    }
    fn peek_u16l(&mut self) -> Result<u16> {
        peek!(self, get_u16l, 2)
    }
    fn peek_i16l(&mut self) -> Result<i16> {
        peek!(self, get_i16l, 2)
    }
    fn peek_u32l(&mut self) -> Result<u32> {
        peek!(self, get_u32l, 4)
    }
    fn peek_i32l(&mut self) -> Result<i32> {
        peek!(self, get_i32l, 4)
    }
    fn peek_u64l(&mut self) -> Result<u64> {
        peek!(self, get_u64l, 8)
    }
    fn peek_i64l(&mut self) -> Result<i64> {
        peek!(self, get_i64l, 8)
    }
    fn peek_f32l(&mut self) -> Result<f32> {
        peek!(self, get_f32l, 4)
    }
    fn peek_f64l(&mut self) -> Result<f64> {
        peek!(self, get_f64l, 8)
    }
    fn peek_u16b(&mut self) -> Result<u16> {
        peek!(self, get_u16b, 2)
    }
    fn peek_i16b(&mut self) -> Result<i16> {
        peek!(self, get_i16b, 2)
    }
    fn peek_u32b(&mut self) -> Result<u32> {
        peek!(self, get_u32b, 4)
    }
    fn peek_i32b(&mut self) -> Result<i32> {
        peek!(self, get_i32b, 4)
    }
    fn peek_u64b(&mut self) -> Result<u64> {
        peek!(self, get_u64b, 8)
    }
    fn peek_i64b(&mut self) -> Result<i64> {
        peek!(self, get_i64b, 8)
    }
    fn peek_f32b(&mut self) -> Result<f32> {
        peek!(self, get_f32b, 4)
    }
    fn peek_f64b(&mut self) -> Result<f64> {
        peek!(self, get_f64b, 8)
    }
}

impl<R: BufRead + ?Sized> BytePeek for R {}

#[cfg(test)]
mod test {
    use std::io::{Cursor, BufReader};
    use io::byteread::*;

    macro_rules! test_read {
        {$fun: ident, $val: expr, $len: expr} => {
            #[test]
            fn $fun() {
                let mut buf = Cursor::new(vec![1; 17]);

                for _ in 0..$len {
                    let v = buf.$fun().unwrap();
                    assert!(v == $val);
                }
                match buf.$fun() {
                    Ok(_) => assert!(false),
                    Err(e) => assert!(e.kind() == UnexpectedEof)
                }
            }
        }
    }

    test_read! { get_u8, 1, 17 }
    test_read! { get_i8, 1, 17 }
    test_read! { get_u16l, 257, 8 }
    test_read! { get_i16l, 257, 8 }
    test_read! { get_u32l, 16843009, 4 }
    test_read! { get_i32l, 16843009, 4 }
    test_read! { get_u64l, 72340172838076673u64, 2 }
    test_read! { get_i64l, 72340172838076673i64, 2 }

    macro_rules! test_peek {
        {$fun: ident, $val: expr, $len: expr} => {
            #[test]
            fn $fun() {
                let cur = Cursor::new(vec![1; 17]);
                let mut buf = BufReader::with_capacity(16, cur);

                for _ in 0..$len {
                    let v = buf.$fun().unwrap();
                    assert!(v == $val);
                }
            }
        }
    }

    test_peek! { peek_u8, 1, 18 }
    test_peek! { peek_i8, 1, 17 }
    test_peek! { peek_u16l, 257, 9 }
    test_peek! { peek_i16l, 257, 9 }
    test_peek! { peek_u32l, 16843009, 5 }
    test_peek! { peek_i32l, 16843009, 5 }
    test_peek! { peek_u64l, 72340172838076673u64, 3 }
    test_peek! { peek_i64l, 72340172838076673i64, 3 }
}
<commit_msg>drop a fmt mangled comment<commit_after>use std::io::{Error, Read, BufRead, Result};
use std::io::ErrorKind::*;

use bitstream::byteread::*;

#[allow(dead_code)]
fn get_buffer<R: Read + ?Sized>(reader: &mut R, buf: &mut [u8]) -> Result<()> {
    let mut nread = 0usize;
    while nread < buf.len() {
        match reader.read(&mut buf[nread..]) {
            Ok(0) => return Err(Error::new(UnexpectedEof, "Empty")),
            Ok(n) => nread += n,
            Err(e) => return Err(e),
        }
    }
    Ok(())
}

macro_rules! get {
    ($s: ident, $name: ident, $size: expr) => ({
        let mut buf = [0; $size];
        try!(get_buffer($s, &mut buf));
        Ok($name(&buf))
    })
}

pub trait ByteRead: Read {
    fn get_u8(&mut self) -> Result<u8> {
        get!(self, get_u8, 1)
    }
    fn get_i8(&mut self) -> Result<i8> {
        get!(self, get_i8, 1)
    }
    fn get_u16l(&mut self) -> Result<u16> {
        get!(self, get_u16l, 2)
    }
    fn get_i16l(&mut self) -> Result<i16> {
        get!(self, get_i16l, 2)
    }
    fn get_u32l(&mut self) -> Result<u32> {
        get!(self, get_u32l, 4)
    }
    fn get_i32l(&mut self) -> Result<i32> {
        get!(self, get_i32l, 4)
    }
    fn get_u64l(&mut self) -> Result<u64> {
        get!(self, get_u64l, 8)
    }
    fn get_i64l(&mut self) -> Result<i64> {
        get!(self, get_i64l, 8)
    }
    fn get_f32l(&mut self) -> Result<f32> {
        get!(self, get_f32l, 4)
    }
    fn get_f64l(&mut self) -> Result<f64> {
        get!(self, get_f64l, 8)
    }
    fn get_u16b(&mut self) -> Result<u16> {
        get!(self, get_u16b, 2)
    }
    fn get_i16b(&mut self) -> Result<i16> {
        get!(self, get_i16b, 2)
    }
    fn get_u32b(&mut self) -> Result<u32> {
        get!(self, get_u32b, 4)
    }
    fn get_i32b(&mut self) -> Result<i32> {
        get!(self, get_i32b, 4)
    }
    fn get_u64b(&mut self) -> Result<u64> {
        get!(self, get_u64b, 8)
    }
    fn get_i64b(&mut self) -> Result<i64> {
        get!(self, get_i64b, 8)
    }
    fn get_f32b(&mut self) -> Result<f32> {
        get!(self, get_f32b, 4)
    }
    fn get_f64b(&mut self) -> Result<f64> {
        get!(self, get_f64b, 8)
    }
}

impl<R: Read + ?Sized> ByteRead for R {}

macro_rules! peek {
   ($s: ident, $name: ident, $size: expr) => ({
        let buf = try!($s.fill_buf());
        if buf.len() < $size {
            Err(Error::new(UnexpectedEof, "Empty"))
        } else {
            Ok($name(&buf))
        }
    })
}

// TODO: bind to a trait that is a bit more strict
pub trait BytePeek: BufRead {
    fn peek_u8(&mut self) -> Result<u8> {
        peek!(self, get_u8, 1)
    }
    fn peek_i8(&mut self) -> Result<i8> {
        peek!(self, get_i8, 1)
    }
    fn peek_u16l(&mut self) -> Result<u16> {
        peek!(self, get_u16l, 2)
    }
    fn peek_i16l(&mut self) -> Result<i16> {
        peek!(self, get_i16l, 2)
    }
    fn peek_u32l(&mut self) -> Result<u32> {
        peek!(self, get_u32l, 4)
    }
    fn peek_i32l(&mut self) -> Result<i32> {
        peek!(self, get_i32l, 4)
    }
    fn peek_u64l(&mut self) -> Result<u64> {
        peek!(self, get_u64l, 8)
    }
    fn peek_i64l(&mut self) -> Result<i64> {
        peek!(self, get_i64l, 8)
    }
    fn peek_f32l(&mut self) -> Result<f32> {
        peek!(self, get_f32l, 4)
    }
    fn peek_f64l(&mut self) -> Result<f64> {
        peek!(self, get_f64l, 8)
    }
    fn peek_u16b(&mut self) -> Result<u16> {
        peek!(self, get_u16b, 2)
    }
    fn peek_i16b(&mut self) -> Result<i16> {
        peek!(self, get_i16b, 2)
    }
    fn peek_u32b(&mut self) -> Result<u32> {
        peek!(self, get_u32b, 4)
    }
    fn peek_i32b(&mut self) -> Result<i32> {
        peek!(self, get_i32b, 4)
    }
    fn peek_u64b(&mut self) -> Result<u64> {
        peek!(self, get_u64b, 8)
    }
    fn peek_i64b(&mut self) -> Result<i64> {
        peek!(self, get_i64b, 8)
    }
    fn peek_f32b(&mut self) -> Result<f32> {
        peek!(self, get_f32b, 4)
    }
    fn peek_f64b(&mut self) -> Result<f64> {
        peek!(self, get_f64b, 8)
    }
}

impl<R: BufRead + ?Sized> BytePeek for R {}

#[cfg(test)]
mod test {
    use std::io::{Cursor, BufReader};
    use io::byteread::*;

    macro_rules! test_read {
        {$fun: ident, $val: expr, $len: expr} => {
            #[test]
            fn $fun() {
                let mut buf = Cursor::new(vec![1; 17]);

                for _ in 0..$len {
                    let v = buf.$fun().unwrap();
                    assert!(v == $val);
                }
                match buf.$fun() {
                    Ok(_) => assert!(false),
                    Err(e) => assert!(e.kind() == UnexpectedEof)
                }
            }
        }
    }

    test_read! { get_u8, 1, 17 }
    test_read! { get_i8, 1, 17 }
    test_read! { get_u16l, 257, 8 }
    test_read! { get_i16l, 257, 8 }
    test_read! { get_u32l, 16843009, 4 }
    test_read! { get_i32l, 16843009, 4 }
    test_read! { get_u64l, 72340172838076673u64, 2 }
    test_read! { get_i64l, 72340172838076673i64, 2 }

    macro_rules! test_peek {
        {$fun: ident, $val: expr, $len: expr} => {
            #[test]
            fn $fun() {
                let cur = Cursor::new(vec![1; 17]);
                let mut buf = BufReader::with_capacity(16, cur);

                for _ in 0..$len {
                    let v = buf.$fun().unwrap();
                    assert!(v == $val);
                }
            }
        }
    }

    test_peek! { peek_u8, 1, 18 }
    test_peek! { peek_i8, 1, 17 }
    test_peek! { peek_u16l, 257, 9 }
    test_peek! { peek_i16l, 257, 9 }
    test_peek! { peek_u32l, 16843009, 5 }
    test_peek! { peek_i32l, 16843009, 5 }
    test_peek! { peek_u64l, 72340172838076673u64, 3 }
    test_peek! { peek_i64l, 72340172838076673i64, 3 }
}
<|endoftext|>


<commit_before>use super::{
    EdgeType,
    Incoming,
};
use super::graph::{
    Graph,
    IndexType,
    NodeIndex,
};

#[derive(Debug)]
struct Vf2State<Ix, Ty> {
    /// The current mapping M(s) of nodes from G0 → G1 and G1 → G0,
    /// NodeIndex::end() for no mapping.
    mapping: Vec<NodeIndex<Ix>>,
    /// out[i] is non-zero if i is in either M_0(s) or Tout_0(s)
    /// These are all the next vertices that are not mapped yet, but
    /// have an outgoing edge from the mapping.
    out: Vec<usize>,
    /// ins[i] is non-zero if i is in either M_0(s) or Tin_0(s)
    /// These are all the incoming vertices, those not mapped yet, but
    /// have an edge from them into the mapping.
    /// Unused if graph is undirected -- it's identical with out in that case.
    ins: Vec<usize>,
    out_size: usize,
    ins_size: usize,
    generation: usize,
}

impl<Ix, Ty> Vf2State<Ix, Ty> where Ix: IndexType, Ty: EdgeType,
{
    pub fn new(c0: usize) -> Self
    {
        let mut state = Vf2State {
            mapping: Vec::with_capacity(c0),
            out: Vec::with_capacity(c0),
            ins: Vec::with_capacity(c0 * <Ty as EdgeType>::is_directed() as usize),
            out_size: 0,
            ins_size: 0,
            generation: 0,
        };
        for _ in (0..c0) {
            state.mapping.push(NodeIndex::end());
            state.out.push(0);
            if <Ty as EdgeType>::is_directed() {
                state.ins.push(0);
            }
        }
        state
    }

    /// Return **true** if we have a complete mapping
    pub fn is_complete(&self) -> bool
    {
        self.generation == self.mapping.len()
    }

    /// Add mapping **from** <-> **to** to the state.
    pub fn push_mapping<N, E>(&mut self, from: NodeIndex<Ix>, to: NodeIndex<Ix>,
                              g: &Graph<N, E, Ty, Ix>)
    {
        self.generation += 1;
        let s = self.generation;
        self.mapping[from.index()] = to;
        // update T0 & T1 ins/outs
        // T0out: Node in G0 not in M0 but successor of a node in M0.
        // st.out[0]: Node either in M0 or successor of M0
        for ix in g.neighbors(from) {
            if self.out[ix.index()] == 0 {
                self.out[ix.index()] = s;
            }
            self.out_size += 1;
        }
        if g.is_directed() {
            for ix in g.neighbors_directed(from, Incoming) {
                if self.ins[ix.index()] == 0 {
                    self.ins[ix.index()] = s;
                }
            }
            self.ins_size += 1;
        }
    }

    /// Restore the state to before the last added mapping
    pub fn pop_mapping<N, E>(&mut self, from: NodeIndex<Ix>,
                             g: &Graph<N, E, Ty, Ix>)
    {
        let s = self.generation;
        self.generation -= 1;

        // undo (n, m) mapping
        self.mapping[from.index()] = NodeIndex::end();

        // unmark in ins and outs
        for ix in g.neighbors(from) {
            if self.out[ix.index()] == s {
                self.out[ix.index()] = 0;
            }
            self.out_size -= 1;
        }
        if g.is_directed() {
            for ix in g.neighbors_directed(from, Incoming) {
                if self.ins[ix.index()] == s {
                    self.ins[ix.index()] = 0;
                }
            }
            self.ins_size -= 1;
        }
    }

    /// Find the next (least) node in the Tout set.
    pub fn next_out_index(&self, from_index: usize) -> Option<usize>
    {
        self.out[from_index..].iter()
                    .enumerate()
                    .filter(|&(index, elt)| *elt > 0 && self.mapping[from_index + index] == NodeIndex::end())
                    .next()
                    .map(|(index, _)| index)
    }

    /// Find the next (least) node in the Tin set.
    pub fn next_in_index(&self, from_index: usize) -> Option<usize>
    {
        if !<Ty as EdgeType>::is_directed() {
            return None
        }
        self.ins[from_index..].iter()
                    .enumerate()
                    .filter(|&(index, elt)| *elt > 0 && self.mapping[from_index + index] == NodeIndex::end())
                    .next()
                    .map(|(index, _)| index)
    }

    /// Find the next (least) node in the N - M set.
    pub fn next_rest_index(&self, from_index: usize) -> Option<usize>
    {
        self.mapping[from_index..].iter()
               .enumerate()
               .filter(|&(_, elt)| *elt == NodeIndex::end())
               .next()
               .map(|(index, _)| index)
    }
}

/// Return **true** if the graphs **g0** and **g1** are isomorphic.
///
/// Using the VF2 algorithm.
///
/// Not implemented: Vertex or edge weight matching.
///
/// ## References
///
/// * A (Sub)Graph Isomorphism Algorithm for Matching Large Graphs
///   Luigi P. Cordella, Pasquale Foggia, Carlo Sansone,
///   and Mario Vento
pub fn is_isomorphic<N, E, Ix, Ty>(g0: &Graph<N, E, Ty, Ix>,
                                   g1: &Graph<N, E, Ty, Ix>) -> bool where
    Ix: IndexType,
    Ty: EdgeType,
{
    if g0.node_count() != g1.node_count() || g0.edge_count() != g1.edge_count() {
        return false
    }

    /// Return Some(bool) if isomorphism is decided, else None.
    fn try_match<N, E, Ix, Ty>(st: &mut [Vf2State<Ix, Ty>; 2],
                               g0: &Graph<N, E, Ty, Ix>,
                               g1: &Graph<N, E, Ty, Ix>) -> Option<bool> where
        Ix: IndexType,
        Ty: EdgeType,
    {
        let g = [g0, g1];
        let graph_indices = 0..2us;
        let end = NodeIndex::end();

        // if all are mapped -- we are done and have an iso
        if st[0].is_complete() {
            return Some(true)
        }

        // A "depth first" search of a valid mapping from graph 1 to graph 2

        // F(s, n, m) -- evaluate state s and add mapping n <-> m

        // Find least T1out node (in st.out[1] but not in M[1])
        #[derive(Copy, Clone, PartialEq, Debug)]
        enum OpenList {
            Out,
            In,
            Other,
        }
        let mut open_list = OpenList::Out;

        let mut to_index;
        let mut from_index = None;
        // Try the out list
        to_index = st[1].next_out_index(0);

        if to_index.is_some() {
            from_index = st[0].next_out_index(0);
            open_list = OpenList::Out;
        }

        // Try the in list
        if to_index.is_none() || from_index.is_none() {
            to_index = st[1].next_in_index(0);

            if to_index.is_some() {
                from_index = st[0].next_in_index(0);
                open_list = OpenList::In;
            }
        }

        // Try the other list -- disconnected graph
        if to_index.is_none() || from_index.is_none() {
            to_index = st[1].next_rest_index(0);
            if to_index.is_some() {
                from_index = st[0].next_rest_index(0);
                open_list = OpenList::Other;
            }
        }

        let (cand0, cand1) = match (from_index, to_index) {
            (Some(n), Some(m)) => (n, m),
            // No more candidates
            _ => return None,
        };

        let mut nx = NodeIndex::new(cand0);
        let mx = NodeIndex::new(cand1);

        let mut first = true;

        'candidates: loop {
            if !first {
                // Find the next node index to try on the `from` side of the mapping
                let start = nx.index() + 1;
                let cand0 = match open_list {
                    OpenList::Out => st[0].next_out_index(start),
                    OpenList::In => st[0].next_in_index(start),
                    OpenList::Other => st[0].next_rest_index(start),
                }.map(|c| c + start); // compensate for start offset.
                nx = match cand0 {
                    None => break, // no more candidates
                    Some(ix) => NodeIndex::new(ix),
                };
                debug_assert!(nx.index() >= start);
            }
            first = false;

            let nodes = [nx, mx];

            /*
            print!("Mapping state: ");
            for (index, nmap) in st.mapping[0].iter().enumerate() {
                if *nmap == end {
                    continue;
                }
                print!("{} => {}, ", index, nmap.index());
            }
            println!("{} => {}", nx.index(), mx.index());
            */

            // Check syntactic feasibility of mapping by ensuring adjacencies
            // of nx map to adjacencies of mx.
            //
            // nx == map to => mx
            //
            // R_succ
            //
            // Check that every neighbor of nx is mapped to a neighbor of mx,
            // then check the reverse, from mx to nx. Check that they have the same
            // count of edges.
            let mut succ_count = [0, 0];
            for j in graph_indices.clone() {
                for n_neigh in g[j].neighbors(nodes[j]) {
                    succ_count[j] += 1;
                    let m_neigh = st[j].mapping[n_neigh.index()];
                    if m_neigh == end {
                        continue;
                    }
                    let has_edge = g[1-j].find_edge(nodes[1-j], m_neigh).is_some();
                    if !has_edge {
                        continue 'candidates;
                    }
                }
            }
            if succ_count[0] != succ_count[1] {
                continue 'candidates;
            }

            // R_pred
            if g[0].is_directed() {
                let mut pred_count = [0, 0];
                for j in graph_indices.clone() {
                    for n_neigh in g[j].neighbors_directed(nodes[j], Incoming) {
                        pred_count[j] += 1;
                        let m_neigh = st[j].mapping[n_neigh.index()];
                        if m_neigh == end {
                            continue;
                        }
                        let has_edge = g[1-j].find_edge(m_neigh, nodes[1-j]).is_some();
                        if !has_edge {
                            continue 'candidates;
                        }
                    }
                }
                if pred_count[0] != pred_count[1] {
                    continue 'candidates;
                }
            }

            // Check cardinalities of open sets, important to prune
            // the search tree.
            //
            // counts in Tin/Tout
            if st[0].out_size != st[0].out_size ||
               st[1].ins_size != st[1].ins_size {
                continue 'candidates;
            }

            // counts in N0 - M0 - Tin - Tout
            // equal to count in N - ins - outs
            let x0 = st[0].ins.iter().zip(st[0].out.iter())
                        .filter(|&(&a, &b)| a == 0 && b == 0)
                        .count();
            let x1 = st[1].ins.iter().zip(st[1].out.iter())
                        .filter(|&(&a, &b)| a == 0 && b == 0)
                        .count();

            if x0 != x1 {
                continue 'candidates;
            }

            // Add mapping nx <-> mx to the state
            for j in graph_indices.clone() {
                st[j].push_mapping(nodes[j], nodes[1-j], g[j]);
            }

            // Recurse
            match try_match(st, g0, g1) {
                None => {}
                result => return result,
            }

            // Restore state.
            for j in graph_indices.clone() {
                st[j].pop_mapping(nodes[j], g[j]);
            }
        }
        None
    }
    let mut st = [Vf2State::new(g0.node_count()),
                  Vf2State::new(g1.node_count())];
    try_match(&mut st, g0, g1).unwrap_or(false)
}

<commit_msg>Move Tin/Tout cardinality check to a saner place<commit_after>use super::{
    EdgeType,
    Incoming,
};
use super::graph::{
    Graph,
    IndexType,
    NodeIndex,
};

#[derive(Debug)]
struct Vf2State<Ix, Ty> {
    /// The current mapping M(s) of nodes from G0 → G1 and G1 → G0,
    /// NodeIndex::end() for no mapping.
    mapping: Vec<NodeIndex<Ix>>,
    /// out[i] is non-zero if i is in either M_0(s) or Tout_0(s)
    /// These are all the next vertices that are not mapped yet, but
    /// have an outgoing edge from the mapping.
    out: Vec<usize>,
    /// ins[i] is non-zero if i is in either M_0(s) or Tin_0(s)
    /// These are all the incoming vertices, those not mapped yet, but
    /// have an edge from them into the mapping.
    /// Unused if graph is undirected -- it's identical with out in that case.
    ins: Vec<usize>,
    out_size: usize,
    ins_size: usize,
    generation: usize,
}

impl<Ix, Ty> Vf2State<Ix, Ty> where Ix: IndexType, Ty: EdgeType,
{
    pub fn new(c0: usize) -> Self
    {
        let mut state = Vf2State {
            mapping: Vec::with_capacity(c0),
            out: Vec::with_capacity(c0),
            ins: Vec::with_capacity(c0 * <Ty as EdgeType>::is_directed() as usize),
            out_size: 0,
            ins_size: 0,
            generation: 0,
        };
        for _ in (0..c0) {
            state.mapping.push(NodeIndex::end());
            state.out.push(0);
            if <Ty as EdgeType>::is_directed() {
                state.ins.push(0);
            }
        }
        state
    }

    /// Return **true** if we have a complete mapping
    pub fn is_complete(&self) -> bool
    {
        self.generation == self.mapping.len()
    }

    /// Add mapping **from** <-> **to** to the state.
    pub fn push_mapping<N, E>(&mut self, from: NodeIndex<Ix>, to: NodeIndex<Ix>,
                              g: &Graph<N, E, Ty, Ix>)
    {
        self.generation += 1;
        let s = self.generation;
        self.mapping[from.index()] = to;
        // update T0 & T1 ins/outs
        // T0out: Node in G0 not in M0 but successor of a node in M0.
        // st.out[0]: Node either in M0 or successor of M0
        for ix in g.neighbors(from) {
            if self.out[ix.index()] == 0 {
                self.out[ix.index()] = s;
            }
            self.out_size += 1;
        }
        if g.is_directed() {
            for ix in g.neighbors_directed(from, Incoming) {
                if self.ins[ix.index()] == 0 {
                    self.ins[ix.index()] = s;
                }
            }
            self.ins_size += 1;
        }
    }

    /// Restore the state to before the last added mapping
    pub fn pop_mapping<N, E>(&mut self, from: NodeIndex<Ix>,
                             g: &Graph<N, E, Ty, Ix>)
    {
        let s = self.generation;
        self.generation -= 1;

        // undo (n, m) mapping
        self.mapping[from.index()] = NodeIndex::end();

        // unmark in ins and outs
        for ix in g.neighbors(from) {
            if self.out[ix.index()] == s {
                self.out[ix.index()] = 0;
            }
            self.out_size -= 1;
        }
        if g.is_directed() {
            for ix in g.neighbors_directed(from, Incoming) {
                if self.ins[ix.index()] == s {
                    self.ins[ix.index()] = 0;
                }
            }
            self.ins_size -= 1;
        }
    }

    /// Find the next (least) node in the Tout set.
    pub fn next_out_index(&self, from_index: usize) -> Option<usize>
    {
        self.out[from_index..].iter()
                    .enumerate()
                    .filter(|&(index, elt)| *elt > 0 && self.mapping[from_index + index] == NodeIndex::end())
                    .next()
                    .map(|(index, _)| index)
    }

    /// Find the next (least) node in the Tin set.
    pub fn next_in_index(&self, from_index: usize) -> Option<usize>
    {
        if !<Ty as EdgeType>::is_directed() {
            return None
        }
        self.ins[from_index..].iter()
                    .enumerate()
                    .filter(|&(index, elt)| *elt > 0 && self.mapping[from_index + index] == NodeIndex::end())
                    .next()
                    .map(|(index, _)| index)
    }

    /// Find the next (least) node in the N - M set.
    pub fn next_rest_index(&self, from_index: usize) -> Option<usize>
    {
        self.mapping[from_index..].iter()
               .enumerate()
               .filter(|&(_, elt)| *elt == NodeIndex::end())
               .next()
               .map(|(index, _)| index)
    }
}

/// Return **true** if the graphs **g0** and **g1** are isomorphic.
///
/// Using the VF2 algorithm.
///
/// Not implemented: Vertex or edge weight matching.
///
/// ## References
///
/// * A (Sub)Graph Isomorphism Algorithm for Matching Large Graphs
///   Luigi P. Cordella, Pasquale Foggia, Carlo Sansone,
///   and Mario Vento
pub fn is_isomorphic<N, E, Ix, Ty>(g0: &Graph<N, E, Ty, Ix>,
                                   g1: &Graph<N, E, Ty, Ix>) -> bool where
    Ix: IndexType,
    Ty: EdgeType,
{
    if g0.node_count() != g1.node_count() || g0.edge_count() != g1.edge_count() {
        return false
    }

    /// Return Some(bool) if isomorphism is decided, else None.
    fn try_match<N, E, Ix, Ty>(st: &mut [Vf2State<Ix, Ty>; 2],
                               g0: &Graph<N, E, Ty, Ix>,
                               g1: &Graph<N, E, Ty, Ix>) -> Option<bool> where
        Ix: IndexType,
        Ty: EdgeType,
    {
        let g = [g0, g1];
        let graph_indices = 0..2us;
        let end = NodeIndex::end();

        // if all are mapped -- we are done and have an iso
        if st[0].is_complete() {
            return Some(true)
        }

        // A "depth first" search of a valid mapping from graph 1 to graph 2

        // F(s, n, m) -- evaluate state s and add mapping n <-> m

        // Find least T1out node (in st.out[1] but not in M[1])
        #[derive(Copy, Clone, PartialEq, Debug)]
        enum OpenList {
            Out,
            In,
            Other,
        }
        let mut open_list = OpenList::Out;

        let mut to_index;
        let mut from_index = None;
        // Try the out list
        to_index = st[1].next_out_index(0);

        if to_index.is_some() {
            from_index = st[0].next_out_index(0);
            open_list = OpenList::Out;
        }

        // Try the in list
        if to_index.is_none() || from_index.is_none() {
            to_index = st[1].next_in_index(0);

            if to_index.is_some() {
                from_index = st[0].next_in_index(0);
                open_list = OpenList::In;
            }
        }

        // Try the other list -- disconnected graph
        if to_index.is_none() || from_index.is_none() {
            to_index = st[1].next_rest_index(0);
            if to_index.is_some() {
                from_index = st[0].next_rest_index(0);
                open_list = OpenList::Other;
            }
        }

        let (cand0, cand1) = match (from_index, to_index) {
            (Some(n), Some(m)) => (n, m),
            // No more candidates
            _ => return None,
        };

        let mut nx = NodeIndex::new(cand0);
        let mx = NodeIndex::new(cand1);

        let mut first = true;

        'candidates: loop {
            if !first {
                // Find the next node index to try on the `from` side of the mapping
                let start = nx.index() + 1;
                let cand0 = match open_list {
                    OpenList::Out => st[0].next_out_index(start),
                    OpenList::In => st[0].next_in_index(start),
                    OpenList::Other => st[0].next_rest_index(start),
                }.map(|c| c + start); // compensate for start offset.
                nx = match cand0 {
                    None => break, // no more candidates
                    Some(ix) => NodeIndex::new(ix),
                };
                debug_assert!(nx.index() >= start);
            }
            first = false;

            let nodes = [nx, mx];

            /*
            print!("Mapping state: ");
            for (index, nmap) in st.mapping[0].iter().enumerate() {
                if *nmap == end {
                    continue;
                }
                print!("{} => {}, ", index, nmap.index());
            }
            println!("{} => {}", nx.index(), mx.index());
            */

            // Check syntactic feasibility of mapping by ensuring adjacencies
            // of nx map to adjacencies of mx.
            //
            // nx == map to => mx
            //
            // R_succ
            //
            // Check that every neighbor of nx is mapped to a neighbor of mx,
            // then check the reverse, from mx to nx. Check that they have the same
            // count of edges.
            let mut succ_count = [0, 0];
            for j in graph_indices.clone() {
                for n_neigh in g[j].neighbors(nodes[j]) {
                    succ_count[j] += 1;
                    let m_neigh = st[j].mapping[n_neigh.index()];
                    if m_neigh == end {
                        continue;
                    }
                    let has_edge = g[1-j].find_edge(nodes[1-j], m_neigh).is_some();
                    if !has_edge {
                        continue 'candidates;
                    }
                }
            }
            if succ_count[0] != succ_count[1] {
                continue 'candidates;
            }

            // R_pred
            if g[0].is_directed() {
                let mut pred_count = [0, 0];
                for j in graph_indices.clone() {
                    for n_neigh in g[j].neighbors_directed(nodes[j], Incoming) {
                        pred_count[j] += 1;
                        let m_neigh = st[j].mapping[n_neigh.index()];
                        if m_neigh == end {
                            continue;
                        }
                        let has_edge = g[1-j].find_edge(m_neigh, nodes[1-j]).is_some();
                        if !has_edge {
                            continue 'candidates;
                        }
                    }
                }
                if pred_count[0] != pred_count[1] {
                    continue 'candidates;
                }
            }

            // Check cardinalities of open sets, important to prune
            // the search tree.
            //
            // counts in N0 - M0 - Tin - Tout
            // equal to count in N - ins - outs
            let x0 = st[0].ins.iter().zip(st[0].out.iter())
                        .filter(|&(&a, &b)| a == 0 && b == 0)
                        .count();
            let x1 = st[1].ins.iter().zip(st[1].out.iter())
                        .filter(|&(&a, &b)| a == 0 && b == 0)
                        .count();

            if x0 != x1 {
                continue 'candidates;
            }

            // Add mapping nx <-> mx to the state
            for j in graph_indices.clone() {
                st[j].push_mapping(nodes[j], nodes[1-j], g[j]);
            }

            // Check counts in Tin/Tout
            if st[0].out_size == st[0].out_size ||
               st[1].ins_size == st[1].ins_size
            {

                // Recurse
                match try_match(st, g0, g1) {
                    None => {}
                    result => return result,
                }
            }

            // Restore state.
            for j in graph_indices.clone() {
                st[j].pop_mapping(nodes[j], g[j]);
            }
        }
        None
    }
    let mut st = [Vf2State::new(g0.node_count()),
                  Vf2State::new(g1.node_count())];
    try_match(&mut st, g0, g1).unwrap_or(false)
}

<|endoftext|>


<commit_before>use std::io::{Read, Take};
use std::mem;
use std::vec;
use serde::de;
use serde::de::{DeserializeSeed, SeqVisitor, Visitor};

use super::error::{Error, ResultE};
use super::osc_reader::OscReader;
use super::maybeskipcomma::MaybeSkipComma;

/// Deserializes a single message, within a packet.
pub struct MsgVisitor<'a, R: Read + 'a> {
    read: &'a mut Take<R>,
    state: State,
}

/// Which part of the OSC message is being parsed
enum State {
    /// Deserializing the address pattern.
    Address(String),
    /// Deserializing the typestring.
    Typestring,
    /// Deserializing the argument data.
    /// Each entry in the Vec is the typecode we parsed earlier
    /// We store this as an iterator to avoid tracking the index of the current arg.
    Arguments(MaybeSkipComma<vec::IntoIter<u8>>),
}

/// Struct to deserialize a single element from the OSC message sequence.
/// (e.g. just the address, or the first argument, etc).
enum OscType {
    I32(i32),
    F32(f32),
    String(String),
    Blob(Vec<u8>),
    // TODO: Bundle
}

impl<'a, R> MsgVisitor<'a, R>
    where R: Read + 'a
{
    pub fn new(read: &'a mut Take<R>, address: String) -> Self {
        Self {
            read: read,
            state: State::Address(address),
        }
    }

    fn parse_next(&mut self) -> ResultE<Option<OscType>> {
        let (new_state, osc_type) = match mem::replace(&mut self.state, State::Typestring) {
            State::Address(address) => {
                // yield the address component; advance to the typestring.
                (State::Typestring, Ok(Some(OscType::String(address.clone()))))
            },
            State::Typestring => {
                let empty_typestr = MaybeSkipComma::new(Vec::with_capacity(0).into_iter());
                // If we are at the end of our buffer, then there won't be a typestring
                // and any attempt to parse it would error.
                // But the 1.0 specs recommend typestrings be optional, so return Ok(..)
                if self.read.limit() == 0 {
                    (State::Arguments(empty_typestr), Ok(None))
                } else {
                    // parse the typestring
                    let tags = self.read.parse_typetag();
                    match tags {
                        // Unable to parse type tag
                        Err(err) => (State::Arguments(empty_typestr), Err(err)),
                        // Parsed: yield first argument, if it exists, else None.
                        Ok(mut tags) => {
                            let result = match tags.next() {
                                None => Ok(None),
                                Some(tag) => self.parse_arg(tag).map(|arg| Some(arg)),
                            };
                            (State::Arguments(tags), result)
                        }
                    }
                }
            },
            State::Arguments(mut tags) => {
                let result = match tags.next() {
                    None => Ok(None),
                    Some(tag) => self.parse_arg(tag).map(|arg| Some(arg)),
                };
                (State::Arguments(tags), result)
            },
        };
        self.state = new_state;
        osc_type
    }
    fn parse_arg(&mut self, typecode: u8) -> ResultE<OscType> {
        match typecode {
            b'i' => self.read.parse_i32().map(|i| { OscType::I32(i) }),
            b'f' => self.read.parse_f32().map(|f| { OscType::F32(f) }),
            b's' => self.read.parse_str().map(|s| { OscType::String(s) }),
            b'b' => self.read.parse_blob().map(|b| { OscType::Blob(b) }),
            c => Err(Error::UnknownType(c)),
        }
    }
}

impl de::Deserializer for OscType {
    type Error = Error;
    // deserializes a single item from the message, consuming self.
    fn deserialize<V>(self, visitor: V) -> ResultE<V::Value>
    where
        V: Visitor
    {
        match self {
            OscType::I32(i) => visitor.visit_i32(i),
            OscType::F32(f) => visitor.visit_f32(f),
            OscType::String(s) => visitor.visit_string(s),
            // TODO: If the user is attempting to deserialize a Vec<u8>, this
            //   will error! We should make use of the deserialize_seq function
            //   in this case.
            OscType::Blob(b) => visitor.visit_byte_buf(b),
        }
    }

    // OSC messages are strongly typed, so we don't make use of any type hints.
    // More info: https://github.com/serde-rs/serde/blob/b7d6c5d9f7b3085a4d40a446eeb95976d2337e07/serde/src/macros.rs#L106
    forward_to_deserialize! {
        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
        seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
        tuple_struct struct struct_field tuple enum ignored_any
    }
}

impl<'a, R> SeqVisitor for MsgVisitor<'a, R>
    where R: Read + 'a
{
    type Error = Error;
    fn visit_seed<T>(&mut self, seed: T) -> ResultE<Option<T::Value>>
        where T: DeserializeSeed
    {
        // Return None when the message has been fully parsed,
        // else call seed.deserialize to deserialize the next item.
        let value = self.parse_next()?;
        match value {
            // end of sequence
            None => Ok(None),
            Some(osc_arg) => seed.deserialize(osc_arg)
                .map(|value| { Some(value) }),
        }
    }
}
<commit_msg>Move redundant code in parse_next into a subroutine<commit_after>use std::io::{Read, Take};
use std::mem;
use std::vec;
use serde::de;
use serde::de::{DeserializeSeed, SeqVisitor, Visitor};

use super::error::{Error, ResultE};
use super::osc_reader::OscReader;
use super::maybeskipcomma::MaybeSkipComma;

/// Deserializes a single message, within a packet.
pub struct MsgVisitor<'a, R: Read + 'a> {
    read: &'a mut Take<R>,
    state: State,
}

/// Which part of the OSC message is being parsed
enum State {
    /// Deserializing the address pattern.
    Address(String),
    /// Deserializing the typestring.
    Typestring,
    /// Deserializing the argument data.
    /// Each entry in the Vec is the typecode we parsed earlier
    /// We store this as an iterator to avoid tracking the index of the current arg.
    Arguments(MaybeSkipComma<vec::IntoIter<u8>>),
}

/// Struct to deserialize a single element from the OSC message sequence.
/// (e.g. just the address, or the first argument, etc).
enum OscType {
    I32(i32),
    F32(f32),
    String(String),
    Blob(Vec<u8>),
    // TODO: Bundle
}

impl<'a, R> MsgVisitor<'a, R>
    where R: Read + 'a
{
    pub fn new(read: &'a mut Take<R>, address: String) -> Self {
        Self {
            read: read,
            state: State::Address(address),
        }
    }

    fn parse_next(&mut self) -> ResultE<Option<OscType>> {
        let (new_state, osc_type) = match mem::replace(&mut self.state, State::Typestring) {
            State::Address(address) => {
                // yield the address component; advance to the typestring.
                (State::Typestring, Ok(Some(OscType::String(address.clone()))))
            },
            State::Typestring => {
                let empty_typestr = MaybeSkipComma::new(Vec::with_capacity(0).into_iter());
                // If we are at the end of our buffer, then there won't be a typestring
                // and any attempt to parse it would error.
                // But the 1.0 specs recommend typestrings be optional, so return Ok(..)
                if self.read.limit() == 0 {
                    (State::Arguments(empty_typestr), Ok(None))
                } else {
                    // parse the typestring
                    let tags = self.read.parse_typetag();
                    match tags {
                        // Unable to parse type tag
                        Err(err) => (State::Arguments(empty_typestr), Err(err)),
                        // Parsed: yield first argument, if it exists, else None.
                        Ok(tags) => self.pop_tag(tags),
                    }
                }
            },
            State::Arguments(tags) => self.pop_tag(tags),
        };
        self.state = new_state;
        osc_type
    }
    /// Helper for parse_next function.
    /// Pops an argument tag & attempts to parse an argument of the corresponding type.
    /// Returns both the parsed argument & the new state of the parser.
    fn pop_tag(&mut self, mut tags: MaybeSkipComma<vec::IntoIter<u8>>) -> (State, ResultE<Option<OscType>>) {
        let result = match tags.next() {
            None => Ok(None),
            Some(tag) => self.parse_arg(tag).map(|arg| Some(arg)),
        };
        (State::Arguments(tags), result)
    }
    fn parse_arg(&mut self, typecode: u8) -> ResultE<OscType> {
        match typecode {
            b'i' => self.read.parse_i32().map(|i| { OscType::I32(i) }),
            b'f' => self.read.parse_f32().map(|f| { OscType::F32(f) }),
            b's' => self.read.parse_str().map(|s| { OscType::String(s) }),
            b'b' => self.read.parse_blob().map(|b| { OscType::Blob(b) }),
            c => Err(Error::UnknownType(c)),
        }
    }
}

impl de::Deserializer for OscType {
    type Error = Error;
    // deserializes a single item from the message, consuming self.
    fn deserialize<V>(self, visitor: V) -> ResultE<V::Value>
    where
        V: Visitor
    {
        match self {
            OscType::I32(i) => visitor.visit_i32(i),
            OscType::F32(f) => visitor.visit_f32(f),
            OscType::String(s) => visitor.visit_string(s),
            // TODO: If the user is attempting to deserialize a Vec<u8>, this
            //   will error! We should make use of the deserialize_seq function
            //   in this case.
            OscType::Blob(b) => visitor.visit_byte_buf(b),
        }
    }

    // OSC messages are strongly typed, so we don't make use of any type hints.
    // More info: https://github.com/serde-rs/serde/blob/b7d6c5d9f7b3085a4d40a446eeb95976d2337e07/serde/src/macros.rs#L106
    forward_to_deserialize! {
        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit option
        seq seq_fixed_size bytes byte_buf map unit_struct newtype_struct
        tuple_struct struct struct_field tuple enum ignored_any
    }
}

impl<'a, R> SeqVisitor for MsgVisitor<'a, R>
    where R: Read + 'a
{
    type Error = Error;
    fn visit_seed<T>(&mut self, seed: T) -> ResultE<Option<T::Value>>
        where T: DeserializeSeed
    {
        // Return None when the message has been fully parsed,
        // else call seed.deserialize to deserialize the next item.
        let value = self.parse_next()?;
        match value {
            // end of sequence
            None => Ok(None),
            Some(osc_arg) => seed.deserialize(osc_arg)
                .map(|value| { Some(value) }),
        }
    }
}
<|endoftext|>


<commit_before>#![feature(test)]

extern crate test;

use bio::stats::pairhmm::*;
use bio::stats::{LogProb, Prob};
use test::Bencher;

static TEXT: &'static [u8] = b"GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGC\
ATTTGGTATTTTCGTCTGGGGGGTATGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTAT\
CTGTCTTTGATTCCTGCCTCATCCTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACTTACTAAAGTGT";

static PATTERN: &'static [u8] = b"GGGTATGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGC";

// Single base insertion and deletion rates for R1 according to Schirmer et al.
// BMC Bioinformatics 2016, 10.1186/s12859-016-0976-y
static PROB_ILLUMINA_INS: Prob = Prob(2.8e-6);
static PROB_ILLUMINA_DEL: Prob = Prob(5.1e-6);
static PROB_ILLUMINA_SUBST: Prob = Prob(0.0021);

fn prob_emit_x_or_y() -> LogProb {
    LogProb::from(Prob(1.0) - PROB_ILLUMINA_SUBST)
}

pub struct TestEmissionParams {
    x: &'static [u8],
    y: &'static [u8],
}

impl EmissionParameters for TestEmissionParams {
    fn prob_emit_xy(&self, i: usize, j: usize) -> XYEmission {
        if self.x[i] == self.y[j] {
            XYEmission::Match(LogProb::from(Prob(1.0) - PROB_ILLUMINA_SUBST))
        } else {
            XYEmission::Mismatch(LogProb::from(PROB_ILLUMINA_SUBST / Prob(3.0)))
        }
    }

    fn prob_emit_x(&self, _: usize) -> LogProb {
        prob_emit_x_or_y()
    }

    fn prob_emit_y(&self, _: usize) -> LogProb {
        prob_emit_x_or_y()
    }

    fn len_x(&self) -> usize {
        self.x.len()
    }

    fn len_y(&self) -> usize {
        self.y.len()
    }
}

pub struct SemiglobalGapParams;

impl GapParameters for SemiglobalGapParams {
    fn prob_gap_x(&self) -> LogProb {
        LogProb::from(PROB_ILLUMINA_INS)
    }

    fn prob_gap_y(&self) -> LogProb {
        LogProb::from(PROB_ILLUMINA_DEL)
    }

    fn prob_gap_x_extend(&self) -> LogProb {
        LogProb::ln_zero()
    }

    fn prob_gap_y_extend(&self) -> LogProb {
        LogProb::ln_zero()
    }
}

impl StartEndGapParameters for SemiglobalGapParams {
    fn free_start_gap_x(&self) -> bool {
        true
    }

    fn free_end_gap_x(&self) -> bool {
        true
    }
}

#[bench]
fn pairhmm_semiglobal(b: &mut Bencher) {
    let emission_params = TestEmissionParams {
        x: TEXT,
        y: PATTERN,
    };
    let gap_params = SemiglobalGapParams;

    let mut pair_hmm = PairHMM::new();
    pair_hmm.prob_related(&gap_params, &emission_params, Some(4));

    b.iter(|| {
        let p = pair_hmm.prob_related(&gap_params, &emission_params, Some(4));
        assert!(*p <= 0.0);
    });
}
<commit_msg>Fix pairhmm bench compilation (#328)<commit_after>#![feature(test)]

extern crate test;

use bio::stats::pairhmm::*;
use bio::stats::{LogProb, Prob};
use test::Bencher;

static TEXT: &'static [u8] = b"GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGC\
ATTTGGTATTTTCGTCTGGGGGGTATGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTAT\
CTGTCTTTGATTCCTGCCTCATCCTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACTTACTAAAGTGT";

static PATTERN: &'static [u8] = b"GGGTATGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGC";

// Single base insertion and deletion rates for R1 according to Schirmer et al.
// BMC Bioinformatics 2016, 10.1186/s12859-016-0976-y
static PROB_ILLUMINA_INS: Prob = Prob(2.8e-6);
static PROB_ILLUMINA_DEL: Prob = Prob(5.1e-6);
static PROB_ILLUMINA_SUBST: Prob = Prob(0.0021);

fn prob_emit_x_or_y() -> LogProb {
    LogProb::from(Prob(1.0) - PROB_ILLUMINA_SUBST)
}

pub struct TestEmissionParams {
    x: &'static [u8],
    y: &'static [u8],
}

impl EmissionParameters for TestEmissionParams {
    fn prob_emit_xy(&self, i: usize, j: usize) -> XYEmission {
        if self.x[i] == self.y[j] {
            XYEmission::Match(LogProb::from(Prob(1.0) - PROB_ILLUMINA_SUBST))
        } else {
            XYEmission::Mismatch(LogProb::from(PROB_ILLUMINA_SUBST / Prob(3.0)))
        }
    }

    fn prob_emit_x(&self, _: usize) -> LogProb {
        prob_emit_x_or_y()
    }

    fn prob_emit_y(&self, _: usize) -> LogProb {
        prob_emit_x_or_y()
    }

    fn len_x(&self) -> usize {
        self.x.len()
    }

    fn len_y(&self) -> usize {
        self.y.len()
    }
}

pub struct SemiglobalGapParams;

impl GapParameters for SemiglobalGapParams {
    fn prob_gap_x(&self) -> LogProb {
        LogProb::from(PROB_ILLUMINA_INS)
    }

    fn prob_gap_y(&self) -> LogProb {
        LogProb::from(PROB_ILLUMINA_DEL)
    }

    fn prob_gap_x_extend(&self) -> LogProb {
        LogProb::ln_zero()
    }

    fn prob_gap_y_extend(&self) -> LogProb {
        LogProb::ln_zero()
    }
}

pub struct SemiglobalAlignment;

impl StartEndGapParameters for SemiglobalAlignment {
    fn free_start_gap_x(&self) -> bool {
        true
    }

    fn free_end_gap_x(&self) -> bool {
        true
    }
}

#[bench]
fn pairhmm_semiglobal(b: &mut Bencher) {
    let emission_params = TestEmissionParams {
        x: TEXT,
        y: PATTERN,
    };
    let gap_params = SemiglobalGapParams;

    let mut pair_hmm = PairHMM::new(&gap_params);
    pair_hmm.prob_related(&emission_params, &SemiglobalAlignment, Some(4));

    b.iter(|| {
        let p = pair_hmm.prob_related(&emission_params, &SemiglobalAlignment, Some(4));
        assert!(*p <= 0.0);
    });
}
<|endoftext|>


<commit_before>//! Windows console handling

// FIXME (#13400): this is only a tiny fraction of the Windows console api

extern crate libc;

use std::io;
use std::io::prelude::*;

use crate::Attr;
use crate::color;
use crate::Terminal;

/// A Terminal implementation that uses the Win32 Console API.
pub struct WinConsole<T> {
    buf: T,
    def_foreground: color::Color,
    def_background: color::Color,
    foreground: color::Color,
    background: color::Color,
}

type WORD = u16;
type DWORD = u32;
type BOOL = i32;
type HANDLE = *mut u8;

#[allow(non_snake_case)]
#[repr(C)]
struct CONSOLE_SCREEN_BUFFER_INFO {
    dwSize: [libc::c_short; 2],
    dwCursorPosition: [libc::c_short; 2],
    wAttributes: WORD,
    srWindow: [libc::c_short; 4],
    dwMaximumWindowSize: [libc::c_short; 2],
}

#[allow(non_snake_case)]
#[link(name = "kernel32")]
extern "system" {
    fn SetConsoleTextAttribute(handle: HANDLE, attr: WORD) -> BOOL;
    fn GetStdHandle(which: DWORD) -> HANDLE;
    fn GetConsoleScreenBufferInfo(handle: HANDLE, info: *mut CONSOLE_SCREEN_BUFFER_INFO) -> BOOL;
}

fn color_to_bits(color: color::Color) -> u16 {
    // magic numbers from mingw-w64's wincon.h

    let bits = match color % 8 {
        color::BLACK => 0,
        color::BLUE => 0x1,
        color::GREEN => 0x2,
        color::RED => 0x4,
        color::YELLOW => 0x2 | 0x4,
        color::MAGENTA => 0x1 | 0x4,
        color::CYAN => 0x1 | 0x2,
        color::WHITE => 0x1 | 0x2 | 0x4,
        _ => unreachable!(),
    };

    if color >= 8 {
        bits | 0x8
    } else {
        bits
    }
}

fn bits_to_color(bits: u16) -> color::Color {
    let color = match bits & 0x7 {
        0 => color::BLACK,
        0x1 => color::BLUE,
        0x2 => color::GREEN,
        0x4 => color::RED,
        0x6 => color::YELLOW,
        0x5 => color::MAGENTA,
        0x3 => color::CYAN,
        0x7 => color::WHITE,
        _ => unreachable!(),
    };

    color | (bits & 0x8) // copy the hi-intensity bit
}

impl<T: Write + Send + 'static> WinConsole<T> {
    fn apply(&mut self) {
        let _unused = self.buf.flush();
        let mut accum: WORD = 0;
        accum |= color_to_bits(self.foreground);
        accum |= color_to_bits(self.background) << 4;

        unsafe {
            // Magic -11 means stdout, from
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
            //
            // You may be wondering, "but what about stderr?", and the answer
            // to that is that setting terminal attributes on the stdout
            // handle also sets them for stderr, since they go to the same
            // terminal! Admittedly, this is fragile, since stderr could be
            // redirected to a different console. This is good enough for
            // rustc though. See #13400.
            let out = GetStdHandle(-11i32 as DWORD);
            SetConsoleTextAttribute(out, accum);
        }
    }

    /// Returns `None` whenever the terminal cannot be created for some reason.
    pub fn new(out: T) -> io::Result<WinConsole<T>> {
        let fg;
        let bg;
        unsafe {
            #[allow(deprecated)]
            let mut buffer_info = ::std::mem::uninitialized();
            if GetConsoleScreenBufferInfo(GetStdHandle(-11i32 as DWORD), &mut buffer_info) != 0 {
                fg = bits_to_color(buffer_info.wAttributes);
                bg = bits_to_color(buffer_info.wAttributes >> 4);
            } else {
                fg = color::WHITE;
                bg = color::BLACK;
            }
        }
        Ok(WinConsole {
            buf: out,
            def_foreground: fg,
            def_background: bg,
            foreground: fg,
            background: bg,
        })
    }
}

impl<T: Write> Write for WinConsole<T> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.buf.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.buf.flush()
    }
}

impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
    type Output = T;

    fn fg(&mut self, color: color::Color) -> io::Result<bool> {
        self.foreground = color;
        self.apply();

        Ok(true)
    }

    fn bg(&mut self, color: color::Color) -> io::Result<bool> {
        self.background = color;
        self.apply();

        Ok(true)
    }

    fn attr(&mut self, attr: Attr) -> io::Result<bool> {
        match attr {
            Attr::ForegroundColor(f) => {
                self.foreground = f;
                self.apply();
                Ok(true)
            }
            Attr::BackgroundColor(b) => {
                self.background = b;
                self.apply();
                Ok(true)
            }
            _ => Ok(false),
        }
    }

    fn supports_attr(&self, attr: Attr) -> bool {
        // it claims support for underscore and reverse video, but I can't get
        // it to do anything -cmr
        match attr {
            Attr::ForegroundColor(_) | Attr::BackgroundColor(_) => true,
            _ => false,
        }
    }

    fn reset(&mut self) -> io::Result<bool> {
        self.foreground = self.def_foreground;
        self.background = self.def_background;
        self.apply();

        Ok(true)
    }

    fn get_ref(&self) -> &T {
        &self.buf
    }

    fn get_mut(&mut self) -> &mut T {
        &mut self.buf
    }

    fn into_inner(self) -> T
        where Self: Sized
    {
        self.buf
    }
}
<commit_msg>Correct definition of CONSOLE_SCREEN_BUFFER_INFO<commit_after>//! Windows console handling

// FIXME (#13400): this is only a tiny fraction of the Windows console api

extern crate libc;

use std::io;
use std::io::prelude::*;

use crate::Attr;
use crate::color;
use crate::Terminal;

/// A Terminal implementation that uses the Win32 Console API.
pub struct WinConsole<T> {
    buf: T,
    def_foreground: color::Color,
    def_background: color::Color,
    foreground: color::Color,
    background: color::Color,
}

type SHORT = i16;
type WORD = u16;
type DWORD = u32;
type BOOL = i32;
type HANDLE = *mut u8;

#[allow(non_snake_case)]
#[repr(C)]
struct SMALL_RECT {
    Left: SHORT,
    Top: SHORT,
    Right: SHORT,
    Bottom: SHORT,
}

#[allow(non_snake_case)]
#[repr(C)]
struct COORD {
    X: SHORT,
    Y: SHORT,
}

#[allow(non_snake_case)]
#[repr(C)]
struct CONSOLE_SCREEN_BUFFER_INFO {
    dwSize: COORD,
    dwCursorPosition: COORD,
    wAttributes: WORD,
    srWindow: SMALL_RECT,
    dwMaximumWindowSize: COORD,
}

#[allow(non_snake_case)]
#[link(name = "kernel32")]
extern "system" {
    fn SetConsoleTextAttribute(handle: HANDLE, attr: WORD) -> BOOL;
    fn GetStdHandle(which: DWORD) -> HANDLE;
    fn GetConsoleScreenBufferInfo(handle: HANDLE, info: *mut CONSOLE_SCREEN_BUFFER_INFO) -> BOOL;
}

fn color_to_bits(color: color::Color) -> u16 {
    // magic numbers from mingw-w64's wincon.h

    let bits = match color % 8 {
        color::BLACK => 0,
        color::BLUE => 0x1,
        color::GREEN => 0x2,
        color::RED => 0x4,
        color::YELLOW => 0x2 | 0x4,
        color::MAGENTA => 0x1 | 0x4,
        color::CYAN => 0x1 | 0x2,
        color::WHITE => 0x1 | 0x2 | 0x4,
        _ => unreachable!(),
    };

    if color >= 8 {
        bits | 0x8
    } else {
        bits
    }
}

fn bits_to_color(bits: u16) -> color::Color {
    let color = match bits & 0x7 {
        0 => color::BLACK,
        0x1 => color::BLUE,
        0x2 => color::GREEN,
        0x4 => color::RED,
        0x6 => color::YELLOW,
        0x5 => color::MAGENTA,
        0x3 => color::CYAN,
        0x7 => color::WHITE,
        _ => unreachable!(),
    };

    color | (bits & 0x8) // copy the hi-intensity bit
}

impl<T: Write + Send + 'static> WinConsole<T> {
    fn apply(&mut self) {
        let _unused = self.buf.flush();
        let mut accum: WORD = 0;
        accum |= color_to_bits(self.foreground);
        accum |= color_to_bits(self.background) << 4;

        unsafe {
            // Magic -11 means stdout, from
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231%28v=vs.85%29.aspx
            //
            // You may be wondering, "but what about stderr?", and the answer
            // to that is that setting terminal attributes on the stdout
            // handle also sets them for stderr, since they go to the same
            // terminal! Admittedly, this is fragile, since stderr could be
            // redirected to a different console. This is good enough for
            // rustc though. See #13400.
            let out = GetStdHandle(-11i32 as DWORD);
            SetConsoleTextAttribute(out, accum);
        }
    }

    /// Returns `None` whenever the terminal cannot be created for some reason.
    pub fn new(out: T) -> io::Result<WinConsole<T>> {
        let fg;
        let bg;
        unsafe {
            #[allow(deprecated)]
            let mut buffer_info = ::std::mem::uninitialized();
            if GetConsoleScreenBufferInfo(GetStdHandle(-11i32 as DWORD), &mut buffer_info) != 0 {
                fg = bits_to_color(buffer_info.wAttributes);
                bg = bits_to_color(buffer_info.wAttributes >> 4);
            } else {
                fg = color::WHITE;
                bg = color::BLACK;
            }
        }
        Ok(WinConsole {
            buf: out,
            def_foreground: fg,
            def_background: bg,
            foreground: fg,
            background: bg,
        })
    }
}

impl<T: Write> Write for WinConsole<T> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.buf.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.buf.flush()
    }
}

impl<T: Write + Send + 'static> Terminal for WinConsole<T> {
    type Output = T;

    fn fg(&mut self, color: color::Color) -> io::Result<bool> {
        self.foreground = color;
        self.apply();

        Ok(true)
    }

    fn bg(&mut self, color: color::Color) -> io::Result<bool> {
        self.background = color;
        self.apply();

        Ok(true)
    }

    fn attr(&mut self, attr: Attr) -> io::Result<bool> {
        match attr {
            Attr::ForegroundColor(f) => {
                self.foreground = f;
                self.apply();
                Ok(true)
            }
            Attr::BackgroundColor(b) => {
                self.background = b;
                self.apply();
                Ok(true)
            }
            _ => Ok(false),
        }
    }

    fn supports_attr(&self, attr: Attr) -> bool {
        // it claims support for underscore and reverse video, but I can't get
        // it to do anything -cmr
        match attr {
            Attr::ForegroundColor(_) | Attr::BackgroundColor(_) => true,
            _ => false,
        }
    }

    fn reset(&mut self) -> io::Result<bool> {
        self.foreground = self.def_foreground;
        self.background = self.def_background;
        self.apply();

        Ok(true)
    }

    fn get_ref(&self) -> &T {
        &self.buf
    }

    fn get_mut(&mut self) -> &mut T {
        &mut self.buf
    }

    fn into_inner(self) -> T
        where Self: Sized
    {
        self.buf
    }
}
<|endoftext|>


<commit_before>use serde_test::Token;
use std::iter;

macro_rules! btreeset {
    () => {
        BTreeSet::new()
    };
    ($($value:expr),+) => {{
        let mut set = BTreeSet::new();
        $(set.insert($value);)+
        set
    }};
}

macro_rules! btreemap {
    () => {
        BTreeMap::new()
    };
    ($($key:expr => $value:expr),+) => {{
        let mut map = BTreeMap::new();
        $(map.insert($key, $value);)+
        map
    }};
}

macro_rules! hashset {
    () => {
        HashSet::new()
    };
    ($($value:expr),+) => {{
        let mut set = HashSet::new();
        $(set.insert($value);)+
        set
    }};
    ($hasher:ident @ $($value:expr),+) => {{
        use std::hash::BuildHasherDefault;
        let mut set = HashSet::with_hasher(BuildHasherDefault::<$hasher>::default());
        $(set.insert($value);)+
        set
    }};
}

macro_rules! hashmap {
    () => {
        HashMap::new()
    };
    ($($key:expr => $value:expr),+) => {{
        let mut map = HashMap::new();
        $(map.insert($key, $value);)+
        map
    }};
    ($hasher:ident @ $($key:expr => $value:expr),+) => {{
        use std::hash::BuildHasherDefault;
        let mut map = HashMap::with_hasher(BuildHasherDefault::<$hasher>::default());
        $(map.insert($key, $value);)+
        map
    }};
}

pub trait SingleTokenIntoIterator {
    fn into_iter(self) -> iter::Once<Token>;
}

impl SingleTokenIntoIterator for Token {
    fn into_iter(self) -> iter::Once<Token> {
        iter::once(self)
    }
}

macro_rules! seq {
    ($($elem:expr),* $(,)?) => {{
        use crate::macros::SingleTokenIntoIterator;
        let mut vec = Vec::new();
        $(<Vec<Token> as Extend<Token>>::extend(&mut vec, $elem.into_iter());)*
        vec
    }};
}
<commit_msg>Ignore unused_macro_rules warning in test macros<commit_after>#![allow(unused_macro_rules)]

use serde_test::Token;
use std::iter;

macro_rules! btreeset {
    () => {
        BTreeSet::new()
    };
    ($($value:expr),+) => {{
        let mut set = BTreeSet::new();
        $(set.insert($value);)+
        set
    }};
}

macro_rules! btreemap {
    () => {
        BTreeMap::new()
    };
    ($($key:expr => $value:expr),+) => {{
        let mut map = BTreeMap::new();
        $(map.insert($key, $value);)+
        map
    }};
}

macro_rules! hashset {
    () => {
        HashSet::new()
    };
    ($($value:expr),+) => {{
        let mut set = HashSet::new();
        $(set.insert($value);)+
        set
    }};
    ($hasher:ident @ $($value:expr),+) => {{
        use std::hash::BuildHasherDefault;
        let mut set = HashSet::with_hasher(BuildHasherDefault::<$hasher>::default());
        $(set.insert($value);)+
        set
    }};
}

macro_rules! hashmap {
    () => {
        HashMap::new()
    };
    ($($key:expr => $value:expr),+) => {{
        let mut map = HashMap::new();
        $(map.insert($key, $value);)+
        map
    }};
    ($hasher:ident @ $($key:expr => $value:expr),+) => {{
        use std::hash::BuildHasherDefault;
        let mut map = HashMap::with_hasher(BuildHasherDefault::<$hasher>::default());
        $(map.insert($key, $value);)+
        map
    }};
}

pub trait SingleTokenIntoIterator {
    fn into_iter(self) -> iter::Once<Token>;
}

impl SingleTokenIntoIterator for Token {
    fn into_iter(self) -> iter::Once<Token> {
        iter::once(self)
    }
}

macro_rules! seq {
    ($($elem:expr),* $(,)?) => {{
        use crate::macros::SingleTokenIntoIterator;
        let mut vec = Vec::new();
        $(<Vec<Token> as Extend<Token>>::extend(&mut vec, $elem.into_iter());)*
        vec
    }};
}
<|endoftext|>