<commit_before>use std::ops::{Index, IndexMut};
use id::{Id, IdHash};

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Color {
    Red,
    Black,
}

struct NodeData<Node: Id, T> {
    value: T,
    next: Option<Node>,
    prev: Option<Node>,
    parent: Option<Node>,
    left: Option<Node>,
    right: Option<Node>,
    color: Color,
}

pub struct RedBlackTree<Node: Id, T> {
    root: Option<Node>,
    nodes: IdHash<Node, NodeData<Node, T>>,
}

impl<Node: Id, T> Default for RedBlackTree<Node, T> {
    fn default() -> Self {
        RedBlackTree {
            root: None,
            nodes: IdHash::default(),
        }
    }
}

impl<Node: Id, T> RedBlackTree<Node, T> {
    pub fn root(&self) -> Option<Node> {
        self.root
    }

    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    pub fn next(&self, node: Node) -> Option<Node> {
        self.nodes[node].next
    }

    pub fn prev(&self, node: Node) -> Option<Node> {
        self.nodes[node].prev
    }

    pub fn left(&self, node: Node) -> Option<Node> {
        self.nodes[node].left
    }

    pub fn right(&self, node: Node) -> Option<Node> {
        self.nodes[node].right
    }

    pub fn parent(&self, node: Node) -> Option<Node> {
        self.nodes[node].parent
    }

    pub fn color(&self, node: Node) -> Color {
        self.nodes[node].color
    }

    fn set_next(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].next = value;
    }

    fn set_prev(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].prev = value;
    }

    fn set_left(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].left = value;
    }

    fn set_right(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].right = value;
    }

    fn set_parent(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].parent = value;
    }

    fn set_color(&mut self, node: Node, value: Color) {
        self.nodes[node].color = value;
    }

    fn if_red(&self, node: Option<Node>) -> Option<Node> {
        match node {
            Some(node) if self.color(node) == Color::Red => Some(node),
            _ => None,
        }
    }

    pub fn insert_after(&mut self, node: Option<Node>, value: T) -> Node {
	    let successor = self.nodes.insert(NodeData {
	        value: value,
	        next: None,
	        prev: None,
	        parent: None,
	        left: None,
	        right: None,
	        color: Color::Red,
	    });
	    let mut parent_ = if let Some(node) = node {
	        self.set_prev(successor, Some(node));
	        let next = self.next(node);
	        self.set_next(successor, next);
	        if let Some(next) = next {
	            self.set_prev(next, Some(successor));
            }
            self.set_next(node, Some(successor));
            if let Some(right) = self.right(node) {
		        let node = self.first(right);
		        self.set_left(node, Some(successor));
		        Some(node)
		    } else {
			    self.set_right(node, Some(successor));
			    Some(node)
			}
		} else {
		    if let Some(root) = self.root {
		        let node = self.first(root);
		        self.set_prev(successor, None);
		        self.set_next(successor, Some(node));
		        self.set_prev(node, Some(successor));
		        self.set_left(node, Some(successor));
		        Some(node)
		    } else {
		        self.root = Some(successor);
		        None
		    }
		};
	    self.set_parent(successor, parent_);
	    let mut node = successor;
	    while let Some(mut parent) = parent_ {
	        if self.color(parent) == Color::Black {
	            break;
	        }
	        let grandpa = self.parent(parent).unwrap();
		    if Some(parent) == self.left(grandpa) {
			    if let Some(uncle) = self.if_red(self.right(grandpa)) {
			        self.set_color(parent, Color::Black);
			        self.set_color(uncle, Color::Black);
				    self.set_color(grandpa, Color::Red);
				    node = grandpa;
			    } else {
				    if Some(node) == self.right(parent) {
					    let p = self.rotate_left(parent);
					    node = parent;
					    parent = p;
				    }
				    self.set_color(parent, Color::Black);
				    self.set_color(grandpa, Color::Red);
				    self.rotate_right(grandpa);
			    }
			} else {
			    if let Some(uncle) = self.if_red(self.left(grandpa)) {
				    self.set_color(parent, Color::Black);
				    self.set_color(uncle, Color::Black);
				    self.set_color(grandpa, Color::Red);
				    node = grandpa;
				} else {
				    if Some(node) == self.left(parent) {
					    let p = self.rotate_right(parent);
					    node = parent;
					    parent = p;
				    }
    				self.set_color(parent, Color::Black);
    				self.set_color(grandpa, Color::Red);
    				self.rotate_left(grandpa);
				}
			}
		    parent_ = self.parent(node);
	    }
	    let root = self.root.unwrap();
	    self.set_color(root, Color::Black);
	    successor
	}

    pub fn remove(&mut self, node: Node) {
	    let prev = self.prev(node);
	    let next = self.next(node);
	    if let Some(next) = next {
		    self.set_prev(next, prev);
		}
		if let Some(prev) = prev {
		    self.set_next(prev, next);
		}
		//////println!("{:?}, {:?}", prev, next);
		//self.set_prev(node, None);
		//self.set_next(node, None);
		let parent = self.parent(node);
		let left = self.left(node);
		let right = self.right(node);
		let node_color = self.color(node);
		self.nodes.remove(node);
		let next = if let Some(left) = left {
		    if let Some(right) = right {
		        Some(self.first(right))
		    } else {
		        Some(left)
		    }
		} else {
		    right
		};
		//////println!("{:?}", next);
		if let Some(parent) = parent {
		    if Some(node) == self.left(parent) {
		        self.set_left(parent, next);
		    } else {
		        self.set_right(parent, next);
		    }
		} else {
		    self.root = next;
		}
		//////println!("{:?}", self.root);
        let node_parent = parent;
		let color;
	    let (node, parent) = if let (Some(left), Some(right)) = (left, right) {
	        let next = next.unwrap();
		    color = self.color(next);
		    self.set_color(next, node_color);
		    self.set_left(next, Some(left));
		    self.set_parent(left, Some(next));
		    if next != right {
    			let parent = self.parent(next).unwrap();
    			self.set_parent(next, node_parent);
    			let node = self.right(next);
    			self.set_left(parent, node);
    			self.set_right(next, Some(right));
    			self.set_parent(right, Some(next));
    			(node, Some(parent))
    		} else {
    			self.set_parent(next, parent);
    			(self.right(next), Some(next))
    		}
	    } else {
	        color = node_color;
	        (next, parent)
        };
		////println!("{:?}, {:?}, {:?}, {:?}", node, parent, self.left(parent.unwrap()), self.right(parent.unwrap()));
    	if let Some(node) = node {
    	    self.set_parent(node, parent);
    	}
    	if color == Color::Red {
    	    return;
    	}
    	if let Some(node) = self.if_red(node) {
    	    self.set_color(node, Color::Black);
    	    return;
    	}
    	let mut parent_ = parent;
    	let mut node = node;
        loop {
			if node == self.root {
				break;
			}
			let parent = parent_.unwrap();
    		let mut sibling: Node;
		    if node == self.left(parent) {
			    sibling = self.right(parent).unwrap();
			    if self.color(sibling) == Color::Red {
				    self.set_color(sibling, Color::Black);
				    self.set_color(parent, Color::Red);
				    self.rotate_left(parent);
					sibling = self.right(parent).unwrap();
				}
    			let left = self.if_red(self.left(sibling));
    			let right = self.if_red(self.right(sibling));
    			if left.is_some() || right.is_some() {
    				if let Some(left) = left {
    				    self.set_color(left, Color::Black);
    				    self.set_color(sibling, Color::Red);
    					self.rotate_right(sibling);
    					sibling = self.right(parent).unwrap()
    				}
    				let parent_color = self.color(parent);
    				self.set_color(sibling, parent_color);
    				self.set_color(parent, Color::Black);
    				let right = self.right(sibling).unwrap();
    				self.set_color(right, Color::Black);
    				self.rotate_left(parent);
    				node = self.root;
    				break;
    			}
		    } else {
    			sibling = self.left(parent).unwrap();
    			if self.color(sibling) == Color::Red {
				    self.set_color(sibling, Color::Black);
				    self.set_color(parent, Color::Red);
				    self.rotate_right(parent);
					sibling = self.left(parent).unwrap();
				}
    			let left = self.if_red(self.left(sibling));
    			let right = self.if_red(self.right(sibling));
    			if left.is_some() || right.is_some() {
    				if let Some(right) = right {
    				    self.set_color(right, Color::Black);
    				    self.set_color(sibling, Color::Red);
    				    self.rotate_left(sibling);
    				    sibling = self.left(parent).unwrap();
    				}
    				let parent_color = self.color(parent);
    				self.set_color(sibling, parent_color);
    				self.set_color(parent, Color::Black);
    				let left = self.left(sibling).unwrap();
    				self.set_color(left, Color::Black);
    				self.rotate_right(parent);
    				node = self.root;
    				break;
    			}
		    }
		    self.set_color(sibling, Color::Red);
		    node = Some(parent);
		    parent_ = self.parent(parent);
	        if self.if_red(node).is_some() {
    	        break;
    	    }
    	}
	    if let Some(node) = node {
	        self.set_color(node, Color::Black);
	    }
    }

    pub fn first(&self, node: Node) -> Node {
        let mut result = node;
        while let Some(left) = self.left(result) {
            result = left;
        }
        result
    }

    pub fn last(&self, node: Node) -> Node {
        let mut result = node;
        while let Some(left) = self.right(result) {
            result = left;
        }
        result
    }

    fn rotate_left(&mut self, node: Node) -> Node {
		////println!("LEFT {:?}", self.nodes[&node].value);
	    let child = self.right(node).unwrap(); //can't be None
	    let parent = self.parent(node);
	    if let Some(parent) = parent {
		    if Some(node) == self.left(parent) {
			    self.set_left(parent, Some(child));
		    } else {
			    self.set_right(parent, Some(child));
		    }
		} else {
		    self.root = Some(child);
	    }
	    self.set_parent(child, parent);
	    self.set_parent(node, Some(child));
	    let left = self.left(child);
	    self.set_right(node, left);
	    if let Some(left) = left {
		    self.set_parent(left, Some(node))
	    }
	    self.set_left(child, Some(node));
	    child
    }

    fn rotate_right(&mut self, node: Node) -> Node {
		////println!("RIGHT {:?}", self.nodes[&node].value);
	    let child = self.left(node).unwrap(); //can't be None
	    let parent = self.parent(node);
	    if let Some(parent) = parent {
		    if Some(node) == self.left(parent) {
			    self.set_left(parent, Some(child));
		    } else {
			    self.set_right(parent, Some(child));
		    }
		} else {
		    self.root = Some(child);
	    }
	    self.set_parent(child, parent);
	    self.set_parent(node, Some(child));
	    let right = self.right(child);
	    self.set_left(node, right);
	    if let Some(right) = right {
		    self.set_parent(right, Some(node))
	    }
	    self.set_right(child, Some(node));
	    child
    }
}

impl<Node: Id, T> Index<Node> for RedBlackTree<Node, T> {
    type Output = T;

    fn index(&self, index: Node) -> &Self::Output {
        &self.nodes[index].value
    }
}

impl<Node: Id, T> IndexMut<Node> for RedBlackTree<Node, T> {
    fn index_mut(&mut self, index: Node) -> &mut Self::Output {
        &mut self.nodes[index].value
    }
}

impl<Node: Id, T> RedBlackTree<Node, T> {
	pub fn dump(&self) {
		self.dump_node(self.root, 0);
	}

	fn dump_node(&self, node: Option<Node>, indent: usize) {
		for _ in 0..indent {
			print!("-");
		}
		if let Some(node) = node {
			println!("{:?} - {:?}", node, self.color(node));
			self.dump_node(self.left(node), indent + 1);
			self.dump_node(self.right(node), indent + 1);
		} else {
			println!("Nil");
		}
	}
}

<commit_msg>IdVec<commit_after>use std::ops::{Index, IndexMut};
use id::{Id, IdHash};

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Color {
    Red,
    Black,
}

struct NodeData<Node: Id, T> {
    value: T,
    next: Option<Node>,
    prev: Option<Node>,
    parent: Option<Node>,
    left: Option<Node>,
    right: Option<Node>,
    color: Color,
}

pub struct RedBlackTree<Node: Id, T> {
    root: Option<Node>,
    nodes: IdVec<Node, NodeData<Node, T>>,
}

impl<Node: Id, T> Default for RedBlackTree<Node, T> {
    fn default() -> Self {
        RedBlackTree {
            root: None,
            nodes: IdVec::default(),
        }
    }
}

impl<Node: Id, T> RedBlackTree<Node, T> {
    pub fn root(&self) -> Option<Node> {
        self.root
    }

    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    pub fn next(&self, node: Node) -> Option<Node> {
        self.nodes[node].next
    }

    pub fn prev(&self, node: Node) -> Option<Node> {
        self.nodes[node].prev
    }

    pub fn left(&self, node: Node) -> Option<Node> {
        self.nodes[node].left
    }

    pub fn right(&self, node: Node) -> Option<Node> {
        self.nodes[node].right
    }

    pub fn parent(&self, node: Node) -> Option<Node> {
        self.nodes[node].parent
    }

    pub fn color(&self, node: Node) -> Color {
        self.nodes[node].color
    }

    fn set_next(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].next = value;
    }

    fn set_prev(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].prev = value;
    }

    fn set_left(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].left = value;
    }

    fn set_right(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].right = value;
    }

    fn set_parent(&mut self, node: Node, value: Option<Node>) {
        self.nodes[node].parent = value;
    }

    fn set_color(&mut self, node: Node, value: Color) {
        self.nodes[node].color = value;
    }

    fn if_red(&self, node: Option<Node>) -> Option<Node> {
        match node {
            Some(node) if self.color(node) == Color::Red => Some(node),
            _ => None,
        }
    }

    pub fn insert_after(&mut self, node: Option<Node>, value: T) -> Node {
	    let successor = self.nodes.push(NodeData {
	        value: value,
	        next: None,
	        prev: None,
	        parent: None,
	        left: None,
	        right: None,
	        color: Color::Red,
	    });
	    let mut parent_ = if let Some(node) = node {
	        self.set_prev(successor, Some(node));
	        let next = self.next(node);
	        self.set_next(successor, next);
	        if let Some(next) = next {
	            self.set_prev(next, Some(successor));
            }
            self.set_next(node, Some(successor));
            if let Some(right) = self.right(node) {
		        let node = self.first(right);
		        self.set_left(node, Some(successor));
		        Some(node)
		    } else {
			    self.set_right(node, Some(successor));
			    Some(node)
			}
		} else {
		    if let Some(root) = self.root {
		        let node = self.first(root);
		        self.set_prev(successor, None);
		        self.set_next(successor, Some(node));
		        self.set_prev(node, Some(successor));
		        self.set_left(node, Some(successor));
		        Some(node)
		    } else {
		        self.root = Some(successor);
		        None
		    }
		};
	    self.set_parent(successor, parent_);
	    let mut node = successor;
	    while let Some(mut parent) = parent_ {
	        if self.color(parent) == Color::Black {
	            break;
	        }
	        let grandpa = self.parent(parent).unwrap();
		    if Some(parent) == self.left(grandpa) {
			    if let Some(uncle) = self.if_red(self.right(grandpa)) {
			        self.set_color(parent, Color::Black);
			        self.set_color(uncle, Color::Black);
				    self.set_color(grandpa, Color::Red);
				    node = grandpa;
			    } else {
				    if Some(node) == self.right(parent) {
					    let p = self.rotate_left(parent);
					    node = parent;
					    parent = p;
				    }
				    self.set_color(parent, Color::Black);
				    self.set_color(grandpa, Color::Red);
				    self.rotate_right(grandpa);
			    }
			} else {
			    if let Some(uncle) = self.if_red(self.left(grandpa)) {
				    self.set_color(parent, Color::Black);
				    self.set_color(uncle, Color::Black);
				    self.set_color(grandpa, Color::Red);
				    node = grandpa;
				} else {
				    if Some(node) == self.left(parent) {
					    let p = self.rotate_right(parent);
					    node = parent;
					    parent = p;
				    }
    				self.set_color(parent, Color::Black);
    				self.set_color(grandpa, Color::Red);
    				self.rotate_left(grandpa);
				}
			}
		    parent_ = self.parent(node);
	    }
	    let root = self.root.unwrap();
	    self.set_color(root, Color::Black);
	    successor
	}

    pub fn remove(&mut self, node: Node) {
	    let prev = self.prev(node);
	    let next = self.next(node);
	    if let Some(next) = next {
		    self.set_prev(next, prev);
		}
		if let Some(prev) = prev {
		    self.set_next(prev, next);
		}
		//////println!("{:?}, {:?}", prev, next);
		//self.set_prev(node, None);
		//self.set_next(node, None);
		let parent = self.parent(node);
		let left = self.left(node);
		let right = self.right(node);
		let node_color = self.color(node);
		//self.nodes.remove(node);
		let next = if let Some(left) = left {
		    if let Some(right) = right {
		        Some(self.first(right))
		    } else {
		        Some(left)
		    }
		} else {
		    right
		};
		//////println!("{:?}", next);
		if let Some(parent) = parent {
		    if Some(node) == self.left(parent) {
		        self.set_left(parent, next);
		    } else {
		        self.set_right(parent, next);
		    }
		} else {
		    self.root = next;
		}
		//////println!("{:?}", self.root);
        let node_parent = parent;
		let color;
	    let (node, parent) = if let (Some(left), Some(right)) = (left, right) {
	        let next = next.unwrap();
		    color = self.color(next);
		    self.set_color(next, node_color);
		    self.set_left(next, Some(left));
		    self.set_parent(left, Some(next));
		    if next != right {
    			let parent = self.parent(next).unwrap();
    			self.set_parent(next, node_parent);
    			let node = self.right(next);
    			self.set_left(parent, node);
    			self.set_right(next, Some(right));
    			self.set_parent(right, Some(next));
    			(node, Some(parent))
    		} else {
    			self.set_parent(next, parent);
    			(self.right(next), Some(next))
    		}
	    } else {
	        color = node_color;
	        (next, parent)
        };
		////println!("{:?}, {:?}, {:?}, {:?}", node, parent, self.left(parent.unwrap()), self.right(parent.unwrap()));
    	if let Some(node) = node {
    	    self.set_parent(node, parent);
    	}
    	if color == Color::Red {
    	    return;
    	}
    	if let Some(node) = self.if_red(node) {
    	    self.set_color(node, Color::Black);
    	    return;
    	}
    	let mut parent_ = parent;
    	let mut node = node;
        loop {
			if node == self.root {
				break;
			}
			let parent = parent_.unwrap();
    		let mut sibling: Node;
		    if node == self.left(parent) {
			    sibling = self.right(parent).unwrap();
			    if self.color(sibling) == Color::Red {
				    self.set_color(sibling, Color::Black);
				    self.set_color(parent, Color::Red);
				    self.rotate_left(parent);
					sibling = self.right(parent).unwrap();
				}
    			let left = self.if_red(self.left(sibling));
    			let right = self.if_red(self.right(sibling));
    			if left.is_some() || right.is_some() {
    				if let Some(left) = left {
    				    self.set_color(left, Color::Black);
    				    self.set_color(sibling, Color::Red);
    					self.rotate_right(sibling);
    					sibling = self.right(parent).unwrap()
    				}
    				let parent_color = self.color(parent);
    				self.set_color(sibling, parent_color);
    				self.set_color(parent, Color::Black);
    				let right = self.right(sibling).unwrap();
    				self.set_color(right, Color::Black);
    				self.rotate_left(parent);
    				node = self.root;
    				break;
    			}
		    } else {
    			sibling = self.left(parent).unwrap();
    			if self.color(sibling) == Color::Red {
				    self.set_color(sibling, Color::Black);
				    self.set_color(parent, Color::Red);
				    self.rotate_right(parent);
					sibling = self.left(parent).unwrap();
				}
    			let left = self.if_red(self.left(sibling));
    			let right = self.if_red(self.right(sibling));
    			if left.is_some() || right.is_some() {
    				if let Some(right) = right {
    				    self.set_color(right, Color::Black);
    				    self.set_color(sibling, Color::Red);
    				    self.rotate_left(sibling);
    				    sibling = self.left(parent).unwrap();
    				}
    				let parent_color = self.color(parent);
    				self.set_color(sibling, parent_color);
    				self.set_color(parent, Color::Black);
    				let left = self.left(sibling).unwrap();
    				self.set_color(left, Color::Black);
    				self.rotate_right(parent);
    				node = self.root;
    				break;
    			}
		    }
		    self.set_color(sibling, Color::Red);
		    node = Some(parent);
		    parent_ = self.parent(parent);
	        if self.if_red(node).is_some() {
    	        break;
    	    }
    	}
	    if let Some(node) = node {
	        self.set_color(node, Color::Black);
	    }
    }

    pub fn first(&self, node: Node) -> Node {
        let mut result = node;
        while let Some(left) = self.left(result) {
            result = left;
        }
        result
    }

    pub fn last(&self, node: Node) -> Node {
        let mut result = node;
        while let Some(left) = self.right(result) {
            result = left;
        }
        result
    }

    fn rotate_left(&mut self, node: Node) -> Node {
		////println!("LEFT {:?}", self.nodes[&node].value);
	    let child = self.right(node).unwrap(); //can't be None
	    let parent = self.parent(node);
	    if let Some(parent) = parent {
		    if Some(node) == self.left(parent) {
			    self.set_left(parent, Some(child));
		    } else {
			    self.set_right(parent, Some(child));
		    }
		} else {
		    self.root = Some(child);
	    }
	    self.set_parent(child, parent);
	    self.set_parent(node, Some(child));
	    let left = self.left(child);
	    self.set_right(node, left);
	    if let Some(left) = left {
		    self.set_parent(left, Some(node))
	    }
	    self.set_left(child, Some(node));
	    child
    }

    fn rotate_right(&mut self, node: Node) -> Node {
		////println!("RIGHT {:?}", self.nodes[&node].value);
	    let child = self.left(node).unwrap(); //can't be None
	    let parent = self.parent(node);
	    if let Some(parent) = parent {
		    if Some(node) == self.left(parent) {
			    self.set_left(parent, Some(child));
		    } else {
			    self.set_right(parent, Some(child));
		    }
		} else {
		    self.root = Some(child);
	    }
	    self.set_parent(child, parent);
	    self.set_parent(node, Some(child));
	    let right = self.right(child);
	    self.set_left(node, right);
	    if let Some(right) = right {
		    self.set_parent(right, Some(node))
	    }
	    self.set_right(child, Some(node));
	    child
    }
}

impl<Node: Id, T> Index<Node> for RedBlackTree<Node, T> {
    type Output = T;

    fn index(&self, index: Node) -> &Self::Output {
        &self.nodes[index].value
    }
}

impl<Node: Id, T> IndexMut<Node> for RedBlackTree<Node, T> {
    fn index_mut(&mut self, index: Node) -> &mut Self::Output {
        &mut self.nodes[index].value
    }
}

impl<Node: Id, T> RedBlackTree<Node, T> {
	pub fn dump(&self) {
		self.dump_node(self.root, 0);
	}

	fn dump_node(&self, node: Option<Node>, indent: usize) {
		for _ in 0..indent {
			print!("-");
		}
		if let Some(node) = node {
			println!("{:?} - {:?}", node, self.color(node));
			self.dump_node(self.left(node), indent + 1);
			self.dump_node(self.right(node), indent + 1);
		} else {
			println!("Nil");
		}
	}
}

<|endoftext|>


<commit_before>// while a WIP
#![allow(unused_variables)]
use rutorch::*;
use std::ops::{Index, IndexMut};
use std::convert::From;
use std::cmp::max;
use std::hash::{Hash, Hasher};

use storage::*;
use ::*;
pub use tensor::tensor_ops::*;
use rand;
use {Ixs, RcMut};

pub enum TensorType {
    Float,
    Double,
    Byte,
    Char,
    Short,
    Int,
    Long,
}

#[derive(Hash)]
pub enum TensorKind {
    FloatTensor(Tensor<f32>),
    LongTensor(Tensor<i64>),
}
pub enum NumKind {
    Float(f32),
    Double(f64),
    Long(i64),
}

impl NumKind {
    pub fn intof32(&self) -> f32 {
        use self::NumKind::{Float, Double, Long};
        match *self {
            Float(v) => v,
            Double(v) => unimplemented!(),
            Long(v) => unimplemented!(),
        }
    }
    pub fn intoi64(&self) -> i64 {
        use self::NumKind::{Float, Double, Long};
        match *self {
            Float(v) => unimplemented!(),
            Double(v) => unimplemented!(),
            Long(v) => v,
        }
    }
}

impl<T: Copy> From<T> for NumKind {
    #[allow(unused_variables)]
    default fn from(input: T) -> Self {
        unreachable!()
    }
}
impl From<f32> for NumKind {
    fn from(input: f32) -> Self {
        NumKind::Float(input)
    }
}
impl From<f64> for NumKind {
    fn from(input: f64) -> Self {
        NumKind::Double(input)
    }
}
impl From<i64> for NumKind {
    fn from(input: i64) -> Self {
        NumKind::Long(input)
    }
}

pub type TensorList<T> = Vec<Tensor<T>>;
pub type TensorKindList = Vec<TensorKind>;
pub type OptTensorKindList = Vec<Option<TensorKind>>;
pub type RefTensorList<'a, T> = Vec<&'a mut Tensor<T>>;
pub type RefTensorKindList<'a> = Vec<&'a TensorKind>;
pub type TensorId = usize;

impl TensorKind {
    pub fn in_thft(&self) -> *mut THFloatTensor {
        unimplemented!();
    }
    pub fn in_thlt(&self) -> *mut THLongTensor {
        unimplemented!();
    }
    pub fn backend(&self) -> Box<nn::BackendIntf> {
        unimplemented!()
    }
    pub fn inner(&self) -> *mut ::std::os::raw::c_void {
        unimplemented!()
    }
}

impl Index<usize> for TensorKind {
    type Output = NumKind;
    fn index(&self, idx: usize) -> &Self::Output {
        unimplemented!()
    }
}
impl PartialEq for TensorKind {
    fn eq(&self, other: &Self) -> bool {
        use self::TensorKind::{FloatTensor, LongTensor};
        match (self, other) {
            (&FloatTensor(ref t1), &FloatTensor(ref t2)) => t1.id() == t2.id(),
            (&LongTensor(ref t1), &LongTensor(ref t2)) => t1.id() == t2.id(),
            _ => false,
        }
    }
}
impl Eq for TensorKind {}
impl Clone for TensorKind {
    fn clone(&self) -> Self {
        use self::TensorKind::{FloatTensor, LongTensor};
        match *self {
            FloatTensor(ref t) => FloatTensor(t.clone()),
            LongTensor(ref t) => LongTensor(t.clone()),
        }
    }
}

impl<T> From<Tensor<T>> for TensorKind {
    #[allow(unused_variables)]
    default fn from(input: Tensor<T>) -> Self {
        unreachable!()
    }
}

impl From<Tensor<f32>> for TensorKind {
    fn from(input: Tensor<f32>) -> Self {
        TensorKind::FloatTensor(input)
    }
}

impl From<Tensor<i64>> for TensorKind {
    fn from(input: Tensor<i64>) -> Self {
        TensorKind::LongTensor(input)
    }
}

impl<T> From<TensorKind> for Tensor<T> {
    #[allow(unused_variables)]
    default fn from(input: TensorKind) -> Self {
        panic!("bad cast")
    }
}

impl<'a, T> From<&'a TensorKind> for &'a Tensor<T> {
    #[allow(unused_variables)]
    default fn from(input: &'a TensorKind) -> Self {
        panic!("bad cast")
    }
}

impl<'a> From<&'a TensorKind> for &'a Tensor<f32> {
    fn from(input: &'a TensorKind) -> Self {
        match *input {
            TensorKind::FloatTensor(ref t) => t,
            _ => unreachable!(),
        }
    }
}

impl<'a> From<&'a TensorKind> for &'a Tensor<i64> {
    fn from(input: &'a TensorKind) -> Self {
        match *input {
            TensorKind::LongTensor(ref t) => t,
            _ => unreachable!(),
        }
    }
}

impl From<TensorKind> for Tensor<f32> {
    fn from(input: TensorKind) -> Self {
        match input {
            TensorKind::FloatTensor(v) => v,
            _ => unimplemented!(),
        }
    }
}

impl From<TensorKind> for Tensor<i64> {
    fn from(input: TensorKind) -> Self {
        match input {
            TensorKind::LongTensor(v) => v,
            _ => unimplemented!(),
        }
    }
}
pub struct Tensor<T> {
    pub value: RcMut<TensorImpl<T, Output = T>>,
}
impl<T: Copy> Hash for Tensor<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id().hash(state)
    }
}

impl<T: Copy> Index<usize> for Tensor<T> {
    type Output = T;
    fn index(&self, idx: usize) -> &Self::Output {

        //        self.value.borrow_mut().index(idx as isize)
        unimplemented!()
    }
}

impl<T: Copy> Index<i32> for Tensor<T> {
    type Output = T;
    fn index(&self, idx: i32) -> &Self::Output {

        //        self.value.borrow_mut().index(idx as isize)
        unimplemented!()
    }
}

pub trait New<D, T> {
    fn new(args: D) -> T;
}
pub trait NewSelf<D, T> {
    fn new(&self, args: D) -> T;
}

impl<T> NewSelf<usize, Tensor<T>> for Tensor<T> {
    fn new(&self, args: usize) -> Self {
        unimplemented!()
    }
}
impl<T> NewSelf<(), Tensor<T>> for Tensor<T> {
    fn new(&self, args: ()) -> Self {
        unimplemented!()
    }
}
impl<T> NewSelf<Vec<usize>, Tensor<T>> for Tensor<T> {
    fn new(&self, args: Vec<usize>) -> Self {
        unimplemented!()
    }
}
impl NewSelf<usize, TensorKind> for TensorKind {
    fn new(&self, args: usize) -> Self {
        unimplemented!()
    }
}
impl NewSelf<(), TensorKind> for TensorKind {
    fn new(&self, args: ()) -> Self {
        unimplemented!()
    }
}
impl NewSelf<Vec<usize>, TensorKind> for TensorKind {
    fn new(&self, args: Vec<usize>) -> Self {
        unimplemented!()
    }
}
impl NewSelf<[usize; 2], TensorKind> for TensorKind {
    fn new(&self, args: [usize; 2]) -> Self {
        unimplemented!()
    }
}

impl<T> Tensor<T> {
    pub fn len(&self) -> usize {
        unimplemented!()
    }
    pub fn s(&self, dim: usize) -> Self {
        unimplemented!()
    }
}

impl<T> Default for Tensor<T> {
    fn default() -> Self {
        unimplemented!()
    }
}

impl<T> Clone for Tensor<T> {
    fn clone(&self) -> Self {
        Tensor { value: self.value.clone() }
    }
}

impl<T: Copy> Index<isize> for Tensor<T> {
    type Output = T;

    fn index(&self, idx: isize) -> &Self::Output {
        unimplemented!()
    }
}

type RefTI<T> = RcMut<TensorImpl<T, Output = T>>;
pub type TIArg<T> = TensorImpl<T, Output = T>;
pub trait TensorImpl<T>: Index<Ixs, Output = T> {
    //fn view<'a>(&self, dims: &[i32]) -> Tensor<'a>;
    fn new(&self) -> RefTI<T>;
    fn add(&self, value: T, output: &TIArg<T>);
    fn addt(&self, value: T, rhs: &TIArg<T>, output: &TIArg<T>);
    fn inner(&self) -> *mut ::std::os::raw::c_void;
}

impl TensorImpl<f32> for FloatTensor {
    fn new(&self) -> RefTI<f32> {
        // XXX place holder implementation
        RcMutNew(FloatTensor::new())
    }
    fn add(&self, value: f32, output: &TIArg<f32>) {
        let out = typecast!(output.inner(), THFloatTensor);
        unsafe {
            THFloatTensor_add(out, self.t, value);
        };
    }
    fn inner(&self) -> *mut ::std::os::raw::c_void {
        self.t as *mut ::std::os::raw::c_void
    }
    fn addt(&self, value: f32, rhs: &TIArg<f32>, output: &TIArg<f32>) {
        let rhsp = typecast!(rhs.inner(), THFloatTensor);
    }
}

impl Index<isize> for FloatTensor {
    type Output = f32;
    fn index(&self, idx: isize) -> &Self::Output {
        unimplemented!()
    }
}

pub struct FloatTensor {
    t: *mut THFloatTensor,
    storage: FloatStorage,
    dims: Vec<isize>,
}

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

impl FloatTensor {
    pub fn new() -> Self {
        unsafe {
            FloatTensor {
                t: THFloatTensor_new(),
                storage: FloatStorage::new(),
                dims: Vec::new(),
            }
        }
    }
    pub fn with_capacity(dims: &[isize]) -> Self {
        let size = dims.iter().product();
        let storage = FloatStorage::with_capacity(size);
        let strides = vec![1; dims.len()];
        let mut t = THFloatTensor {
            size: dims.clone().as_ptr() as *mut ::std::os::raw::c_long,
            stride: strides.as_ptr() as *mut ::std::os::raw::c_long,
            nDimension: dims.len() as i32,
            storage: storage.t,
            storageOffset: 0,
            refcount: 1,
            flag: TH_TENSOR_REFCOUNTED as i8,
        };
        FloatTensor {
            t: &mut t,
            storage: storage,
            dims: Vec::from(dims),
        }
    }
    pub fn randn(dims: &[isize]) -> Self {
        /* XXX */
        let mut t = FloatTensor::with_capacity(dims);
        for x in t.storage.iter_mut() {
            *x = rand::random::<f32>()
        }
        t
    }
}

impl<'a> Index<&'a [isize]> for FloatTensor {
    type Output = f32;

    fn index(&self, idx: &'a [isize]) -> &Self::Output {
        let mut index: isize = 0;
        let lastidx = max(0, idx.len() as isize - 1) as usize;
        if idx.len() != self.dims.len() {
            panic!("bad dimlen")
        }
        for i in 0..lastidx {
            if idx[i] >= self.dims[i] {
                panic!("bad dimlen")
            }
            index += idx[i] * self.dims[i]
        }
        if idx[lastidx] >= self.dims[lastidx] {
            panic!("bad dimlen")
        }
        index += idx[lastidx];
        &self.storage[index]
    }
}

impl<'a> IndexMut<&'a [isize]> for FloatTensor {
    fn index_mut(&mut self, idx: &'a [isize]) -> &mut Self::Output {
        let mut index: isize = 0;
        let lastidx = max(0, idx.len() as isize - 1) as usize;
        if idx.len() != self.dims.len() {
            panic!("bad dimlen")
        }
        for i in 0..lastidx {
            if idx[i] >= self.dims[i] {
                panic!("bad dimlen")
            }
            index += idx[i] * self.dims[i]
        }
        if idx[lastidx] >= self.dims[lastidx] {
            panic!("bad dimlen")
        }
        index += idx[lastidx];
        &mut self.storage[index]
    }
}
<commit_msg>remove in_thft / in_thlt<commit_after>// while a WIP
#![allow(unused_variables)]
use rutorch::*;
use std::ops::{Index, IndexMut};
use std::convert::From;
use std::cmp::max;
use std::hash::{Hash, Hasher};

use storage::*;
use ::*;
pub use tensor::tensor_ops::*;
use rand;
use {Ixs, RcMut};

pub enum TensorType {
    Float,
    Double,
    Byte,
    Char,
    Short,
    Int,
    Long,
}

#[derive(Hash)]
pub enum TensorKind {
    FloatTensor(Tensor<f32>),
    LongTensor(Tensor<i64>),
}
pub enum NumKind {
    Float(f32),
    Double(f64),
    Long(i64),
}

impl NumKind {
    pub fn intof32(&self) -> f32 {
        use self::NumKind::{Float, Double, Long};
        match *self {
            Float(v) => v,
            Double(v) => unimplemented!(),
            Long(v) => unimplemented!(),
        }
    }
    pub fn intoi64(&self) -> i64 {
        use self::NumKind::{Float, Double, Long};
        match *self {
            Float(v) => unimplemented!(),
            Double(v) => unimplemented!(),
            Long(v) => v,
        }
    }
}

impl<T: Copy> From<T> for NumKind {
    #[allow(unused_variables)]
    default fn from(input: T) -> Self {
        unreachable!()
    }
}
impl From<f32> for NumKind {
    fn from(input: f32) -> Self {
        NumKind::Float(input)
    }
}
impl From<f64> for NumKind {
    fn from(input: f64) -> Self {
        NumKind::Double(input)
    }
}
impl From<i64> for NumKind {
    fn from(input: i64) -> Self {
        NumKind::Long(input)
    }
}

pub type TensorList<T> = Vec<Tensor<T>>;
pub type TensorKindList = Vec<TensorKind>;
pub type OptTensorKindList = Vec<Option<TensorKind>>;
pub type RefTensorList<'a, T> = Vec<&'a mut Tensor<T>>;
pub type RefTensorKindList<'a> = Vec<&'a TensorKind>;
pub type TensorId = usize;

impl TensorKind {
    pub fn backend(&self) -> Box<nn::BackendIntf> {
        unimplemented!()
    }
    pub fn inner(&self) -> *mut ::std::os::raw::c_void {
        unimplemented!()
    }
}

impl Index<usize> for TensorKind {
    type Output = NumKind;
    fn index(&self, idx: usize) -> &Self::Output {
        unimplemented!()
    }
}
impl PartialEq for TensorKind {
    fn eq(&self, other: &Self) -> bool {
        use self::TensorKind::{FloatTensor, LongTensor};
        match (self, other) {
            (&FloatTensor(ref t1), &FloatTensor(ref t2)) => t1.id() == t2.id(),
            (&LongTensor(ref t1), &LongTensor(ref t2)) => t1.id() == t2.id(),
            _ => false,
        }
    }
}
impl Eq for TensorKind {}
impl Clone for TensorKind {
    fn clone(&self) -> Self {
        use self::TensorKind::{FloatTensor, LongTensor};
        match *self {
            FloatTensor(ref t) => FloatTensor(t.clone()),
            LongTensor(ref t) => LongTensor(t.clone()),
        }
    }
}

impl<T> From<Tensor<T>> for TensorKind {
    #[allow(unused_variables)]
    default fn from(input: Tensor<T>) -> Self {
        unreachable!()
    }
}

impl From<Tensor<f32>> for TensorKind {
    fn from(input: Tensor<f32>) -> Self {
        TensorKind::FloatTensor(input)
    }
}

impl From<Tensor<i64>> for TensorKind {
    fn from(input: Tensor<i64>) -> Self {
        TensorKind::LongTensor(input)
    }
}

impl<T> From<TensorKind> for Tensor<T> {
    #[allow(unused_variables)]
    default fn from(input: TensorKind) -> Self {
        panic!("bad cast")
    }
}

impl<'a, T> From<&'a TensorKind> for &'a Tensor<T> {
    #[allow(unused_variables)]
    default fn from(input: &'a TensorKind) -> Self {
        panic!("bad cast")
    }
}

impl<'a> From<&'a TensorKind> for &'a Tensor<f32> {
    fn from(input: &'a TensorKind) -> Self {
        match *input {
            TensorKind::FloatTensor(ref t) => t,
            _ => unreachable!(),
        }
    }
}

impl<'a> From<&'a TensorKind> for &'a Tensor<i64> {
    fn from(input: &'a TensorKind) -> Self {
        match *input {
            TensorKind::LongTensor(ref t) => t,
            _ => unreachable!(),
        }
    }
}

impl From<TensorKind> for Tensor<f32> {
    fn from(input: TensorKind) -> Self {
        match input {
            TensorKind::FloatTensor(v) => v,
            _ => unimplemented!(),
        }
    }
}

impl From<TensorKind> for Tensor<i64> {
    fn from(input: TensorKind) -> Self {
        match input {
            TensorKind::LongTensor(v) => v,
            _ => unimplemented!(),
        }
    }
}
pub struct Tensor<T> {
    pub value: RcMut<TensorImpl<T, Output = T>>,
}
impl<T: Copy> Hash for Tensor<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id().hash(state)
    }
}

impl<T: Copy> Index<usize> for Tensor<T> {
    type Output = T;
    fn index(&self, idx: usize) -> &Self::Output {

        //        self.value.borrow_mut().index(idx as isize)
        unimplemented!()
    }
}

impl<T: Copy> Index<i32> for Tensor<T> {
    type Output = T;
    fn index(&self, idx: i32) -> &Self::Output {

        //        self.value.borrow_mut().index(idx as isize)
        unimplemented!()
    }
}

pub trait New<D, T> {
    fn new(args: D) -> T;
}
pub trait NewSelf<D, T> {
    fn new(&self, args: D) -> T;
}

impl<T> NewSelf<usize, Tensor<T>> for Tensor<T> {
    fn new(&self, args: usize) -> Self {
        unimplemented!()
    }
}
impl<T> NewSelf<(), Tensor<T>> for Tensor<T> {
    fn new(&self, args: ()) -> Self {
        unimplemented!()
    }
}
impl<T> NewSelf<Vec<usize>, Tensor<T>> for Tensor<T> {
    fn new(&self, args: Vec<usize>) -> Self {
        unimplemented!()
    }
}
impl NewSelf<usize, TensorKind> for TensorKind {
    fn new(&self, args: usize) -> Self {
        unimplemented!()
    }
}
impl NewSelf<(), TensorKind> for TensorKind {
    fn new(&self, args: ()) -> Self {
        unimplemented!()
    }
}
impl NewSelf<Vec<usize>, TensorKind> for TensorKind {
    fn new(&self, args: Vec<usize>) -> Self {
        unimplemented!()
    }
}
impl NewSelf<[usize; 2], TensorKind> for TensorKind {
    fn new(&self, args: [usize; 2]) -> Self {
        unimplemented!()
    }
}

impl<T> Tensor<T> {
    pub fn len(&self) -> usize {
        unimplemented!()
    }
    pub fn s(&self, dim: usize) -> Self {
        unimplemented!()
    }
}

impl<T> Default for Tensor<T> {
    fn default() -> Self {
        unimplemented!()
    }
}

impl<T> Clone for Tensor<T> {
    fn clone(&self) -> Self {
        Tensor { value: self.value.clone() }
    }
}

impl<T: Copy> Index<isize> for Tensor<T> {
    type Output = T;

    fn index(&self, idx: isize) -> &Self::Output {
        unimplemented!()
    }
}

type RefTI<T> = RcMut<TensorImpl<T, Output = T>>;
pub type TIArg<T> = TensorImpl<T, Output = T>;
pub trait TensorImpl<T>: Index<Ixs, Output = T> {
    //fn view<'a>(&self, dims: &[i32]) -> Tensor<'a>;
    fn new(&self) -> RefTI<T>;
    fn add(&self, value: T, output: &TIArg<T>);
    fn addt(&self, value: T, rhs: &TIArg<T>, output: &TIArg<T>);
    fn inner(&self) -> *mut ::std::os::raw::c_void;
}

impl TensorImpl<f32> for FloatTensor {
    fn new(&self) -> RefTI<f32> {
        // XXX place holder implementation
        RcMutNew(FloatTensor::new())
    }
    fn add(&self, value: f32, output: &TIArg<f32>) {
        let out = typecast!(output.inner(), THFloatTensor);
        unsafe {
            THFloatTensor_add(out, self.t, value);
        };
    }
    fn inner(&self) -> *mut ::std::os::raw::c_void {
        self.t as *mut ::std::os::raw::c_void
    }
    fn addt(&self, value: f32, rhs: &TIArg<f32>, output: &TIArg<f32>) {
        let rhsp = typecast!(rhs.inner(), THFloatTensor);
    }
}

impl Index<isize> for FloatTensor {
    type Output = f32;
    fn index(&self, idx: isize) -> &Self::Output {
        unimplemented!()
    }
}

pub struct FloatTensor {
    t: *mut THFloatTensor,
    storage: FloatStorage,
    dims: Vec<isize>,
}

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

impl FloatTensor {
    pub fn new() -> Self {
        unsafe {
            FloatTensor {
                t: THFloatTensor_new(),
                storage: FloatStorage::new(),
                dims: Vec::new(),
            }
        }
    }
    pub fn with_capacity(dims: &[isize]) -> Self {
        let size = dims.iter().product();
        let storage = FloatStorage::with_capacity(size);
        let strides = vec![1; dims.len()];
        let mut t = THFloatTensor {
            size: dims.clone().as_ptr() as *mut ::std::os::raw::c_long,
            stride: strides.as_ptr() as *mut ::std::os::raw::c_long,
            nDimension: dims.len() as i32,
            storage: storage.t,
            storageOffset: 0,
            refcount: 1,
            flag: TH_TENSOR_REFCOUNTED as i8,
        };
        FloatTensor {
            t: &mut t,
            storage: storage,
            dims: Vec::from(dims),
        }
    }
    pub fn randn(dims: &[isize]) -> Self {
        /* XXX */
        let mut t = FloatTensor::with_capacity(dims);
        for x in t.storage.iter_mut() {
            *x = rand::random::<f32>()
        }
        t
    }
}

impl<'a> Index<&'a [isize]> for FloatTensor {
    type Output = f32;

    fn index(&self, idx: &'a [isize]) -> &Self::Output {
        let mut index: isize = 0;
        let lastidx = max(0, idx.len() as isize - 1) as usize;
        if idx.len() != self.dims.len() {
            panic!("bad dimlen")
        }
        for i in 0..lastidx {
            if idx[i] >= self.dims[i] {
                panic!("bad dimlen")
            }
            index += idx[i] * self.dims[i]
        }
        if idx[lastidx] >= self.dims[lastidx] {
            panic!("bad dimlen")
        }
        index += idx[lastidx];
        &self.storage[index]
    }
}

impl<'a> IndexMut<&'a [isize]> for FloatTensor {
    fn index_mut(&mut self, idx: &'a [isize]) -> &mut Self::Output {
        let mut index: isize = 0;
        let lastidx = max(0, idx.len() as isize - 1) as usize;
        if idx.len() != self.dims.len() {
            panic!("bad dimlen")
        }
        for i in 0..lastidx {
            if idx[i] >= self.dims[i] {
                panic!("bad dimlen")
            }
            index += idx[i] * self.dims[i]
        }
        if idx[lastidx] >= self.dims[lastidx] {
            panic!("bad dimlen")
        }
        index += idx[lastidx];
        &mut self.storage[index]
    }
}
<|endoftext|>


<commit_before>use nom::{Err, IResult, Needed};

use std::io;
use std::io::Read;
use std::ptr;
use std::cmp;

use super::StreamProducer;

#[derive(Debug)]
pub enum ErrorKind {
  IO(io::Error),
  Incomplete(usize),
  Consumed(usize),
  EndOfInput,
  Unknown,
}

// Structure that hold a slice of bytes.
pub struct ByteStream<'a> {
  offset: usize,
  bytes: &'a [u8],
}

impl<'a> ByteStream<'a> {
  pub fn new(bytes: &'a [u8]) -> Self {
    ByteStream {
      offset: 0,
      bytes: bytes,
    }
  }

  #[inline]
  pub fn len(&self) -> usize {
    self.bytes.len() - self.offset
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.len() == 0
  }
}

impl<'a> StreamProducer for ByteStream<'a> {
  fn parse<F, T>(&mut self, f: F) -> Result<T, ErrorKind>
   where F: FnOnce(&[u8]) -> IResult<&[u8], T> {
    if self.is_empty() {
      return Err(ErrorKind::EndOfInput);
    }

    match f(&self.bytes[self.offset..]) {
      IResult::Done(i, o)    => {
        self.offset += self.len() - i.len();

        Ok(o)
      }
      IResult::Incomplete(n) => {
        let mut needed = self.len();

        if let Needed::Size(size) = n {
          needed = size;
        }

        Err(ErrorKind::Incomplete(needed))
      }
      IResult::Error(e)      => {
        if let Err::Position(_, i) = e {
          let consumed = self.len() - i.len();

          self.offset += consumed;

          Err(ErrorKind::Consumed(consumed))
        } else {
          Err(ErrorKind::Unknown)
        }
      }
    }
  }
}

pub struct Buffer {
  data: Vec<u8>,
  filled: usize,
  offset: usize,
}

impl Buffer {
  pub fn new() -> Self {
    Self::with_capacity(1024)
  }

  pub fn with_capacity(capacity: usize) -> Self {
    let mut buffer = Vec::with_capacity(capacity);

    unsafe {
      buffer.set_len(capacity);
    }

    Buffer {
      data: buffer,
      filled: 0,
      offset: 0,
    }
  }

  #[inline]
  pub fn len(&self) -> usize {
    self.filled - self.offset
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.len() == 0
  }

  #[inline]
  pub fn capacity(&self) -> usize {
    self.data.len()
  }

  pub fn as_slice(&self) -> &[u8] {
    &self.data[self.offset..self.filled]
  }

  pub fn fill<R: Read>(&mut self, reader: &mut R) -> io::Result<usize> {
    reader.read(&mut self.data[self.filled..]).map(|consumed| {
      self.filled += consumed;

      consumed
    })
  }

  pub fn resize(&mut self, size: usize) {
    if size > self.data.capacity() {
      self.data.reserve(size);

      let capacity = self.data.capacity();

      unsafe {
        self.data.set_len(capacity);
      }
    }

    if self.data.len() - self.filled < size  {
      let length  = self.filled - self.offset;
      let mut_ptr = self.data.as_mut_ptr();

      unsafe {
        let offset_ptr  = self.data.as_ptr().offset(self.offset as isize);

        ptr::copy(offset_ptr, mut_ptr, length);
      }

      self.filled -= self.offset;
      self.offset  = 0;
    }
  }

  pub fn consume(&mut self, consumed: usize) {
    self.offset += consumed;
  }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParserState {
  Incomplete,
  EndOfInput,
}

fn fill<R: Read>(buffer: &mut Buffer, reader: &mut R, needed: usize)
                 -> io::Result<usize> {
  let mut read = 0;

  if buffer.len() < needed {
    buffer.resize(needed);

    while buffer.len() < needed {
      let size_read = try!(buffer.fill(reader));

      if size_read > 0 {
        read += size_read;
      } else {
        break;
      }
    }
  }

  Ok(read)
}

// Structure that hold a reader for a source of bytes.
pub struct ReadStream<R: Read> {
  reader: R,
  buffer: Buffer,
  needed: usize,
  state: ParserState,
}

impl<R> ReadStream<R> where R: Read {
  pub fn new(reader: R) -> Self {
    ReadStream {
      reader: reader,
      buffer: Buffer::new(),
      needed: 0,
      state: ParserState::Incomplete,
    }
  }

  fn fill(&mut self) -> io::Result<usize> {
    let needed = cmp::max(1, self.needed);

    fill(&mut self.buffer, &mut self.reader, needed).map(|consumed| {
      if self.buffer.len() < needed {
        self.state = ParserState::EndOfInput;
      }

      consumed
    })
  }
}

fn from_iresult<T>(buffer: &Buffer, result: IResult<&[u8], T>)
                   -> Result<(usize, T), ErrorKind> {
  match result {
    IResult::Done(i, o)    => Ok((buffer.len() - i.len(), o)),
    IResult::Incomplete(n) => {
      let mut needed = buffer.capacity();

      if let Needed::Size(size) = n {
        needed = size;
      }

      Err(ErrorKind::Incomplete(needed))
    }
    IResult::Error(e)      => {
      if let Err::Position(_, i) = e {
        Err(ErrorKind::Consumed(buffer.len() - i.len()))
      } else {
        Err(ErrorKind::Unknown)
      }
    }
  }
}

impl<R> StreamProducer for ReadStream<R> where R: Read {
  fn parse<F, T>(&mut self, f: F) -> Result<T, ErrorKind>
   where F: FnOnce(&[u8]) -> IResult<&[u8], T> {
    if self.state != ParserState::EndOfInput {
      try!(self.fill().map_err(ErrorKind::IO));
    }

    let mut buffer = &mut self.buffer;

    if buffer.is_empty() {
      self.state = ParserState::EndOfInput;

      return Err(ErrorKind::EndOfInput);
    }

    let result = {
      let iresult = f(buffer.as_slice());

      from_iresult(&buffer, iresult)
    };

    match result {
      Ok((consumed, o)) => {
        buffer.consume(consumed);

        Ok(o)
      }
      Err(kind)         => {
        match kind {
          ErrorKind::Incomplete(needed) => self.needed = needed,
          ErrorKind::Consumed(consumed) => buffer.consume(consumed),
          _                             => (),
        }

        Err(kind)
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use utility::StreamProducer;
  use nom::be_u32;

  #[test]
  fn test_buffer() {
    let mut buffer = Buffer::new();
    let bytes      = b"Hello World";
    let mut reader = &bytes[..];

    assert!(buffer.is_empty());
    assert_eq!(buffer.capacity(), 1024);

    let bytes_read = buffer.fill(&mut reader).unwrap_or(0);
    let bytes_len  = bytes.len();

    assert_eq!(bytes_read, bytes_len);
    assert_eq!(buffer.len(), bytes_len);
    assert_eq!(buffer.as_slice(), bytes);

    buffer.resize(512);
    assert_eq!(buffer.capacity(), 1024);
  }

  #[test]
  fn test_byte_stream() {
    let bytes      = b"Hello World";
    let mut stream = ByteStream::new(bytes);

    assert_eq!(stream.len(), bytes.len());

    let result = stream.parse(be_u32).unwrap_or(0);

    assert_eq!(result, 1214606444);
    assert_eq!(stream.len(), 7);
  }

  #[test]
  fn test_read_stream() {
    let bytes      = b"Hello World";
    let mut stream = ReadStream::new(&bytes[..]);

    let result = stream.parse(be_u32).unwrap_or(0);

    assert_eq!(result, 1214606444)
  }
}
<commit_msg>Add documentation for `Buffer`<commit_after>use nom::{Err, IResult, Needed};

use std::io;
use std::io::Read;
use std::ptr;
use std::cmp;

use super::StreamProducer;

#[derive(Debug)]
pub enum ErrorKind {
  IO(io::Error),
  Incomplete(usize),
  Consumed(usize),
  EndOfInput,
  Unknown,
}

// Structure that hold a slice of bytes.
pub struct ByteStream<'a> {
  offset: usize,
  bytes: &'a [u8],
}

impl<'a> ByteStream<'a> {
  pub fn new(bytes: &'a [u8]) -> Self {
    ByteStream {
      offset: 0,
      bytes: bytes,
    }
  }

  #[inline]
  pub fn len(&self) -> usize {
    self.bytes.len() - self.offset
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.len() == 0
  }
}

impl<'a> StreamProducer for ByteStream<'a> {
  fn parse<F, T>(&mut self, f: F) -> Result<T, ErrorKind>
   where F: FnOnce(&[u8]) -> IResult<&[u8], T> {
    if self.is_empty() {
      return Err(ErrorKind::EndOfInput);
    }

    match f(&self.bytes[self.offset..]) {
      IResult::Done(i, o)    => {
        self.offset += self.len() - i.len();

        Ok(o)
      }
      IResult::Incomplete(n) => {
        let mut needed = self.len();

        if let Needed::Size(size) = n {
          needed = size;
        }

        Err(ErrorKind::Incomplete(needed))
      }
      IResult::Error(e)      => {
        if let Err::Position(_, i) = e {
          let consumed = self.len() - i.len();

          self.offset += consumed;

          Err(ErrorKind::Consumed(consumed))
        } else {
          Err(ErrorKind::Unknown)
        }
      }
    }
  }
}

// Growable buffer of bytes.
//
// Mainly used to the `ReadStream` structure but can be used seperately for
// manually filling with some `Read` source.
pub struct Buffer {
  data: Vec<u8>,
  filled: usize,
  offset: usize,
}

impl Buffer {
  pub fn new() -> Self {
    Self::with_capacity(1024)
  }

  pub fn with_capacity(capacity: usize) -> Self {
    let mut buffer = Vec::with_capacity(capacity);

    unsafe {
      buffer.set_len(capacity);
    }

    Buffer {
      data: buffer,
      filled: 0,
      offset: 0,
    }
  }

  #[inline]
  pub fn len(&self) -> usize {
    self.filled - self.offset
  }

  #[inline]
  pub fn is_empty(&self) -> bool {
    self.len() == 0
  }

  #[inline]
  pub fn capacity(&self) -> usize {
    self.data.len()
  }

  pub fn as_slice(&self) -> &[u8] {
    &self.data[self.offset..self.filled]
  }

  pub fn fill<R: Read>(&mut self, reader: &mut R) -> io::Result<usize> {
    reader.read(&mut self.data[self.filled..]).map(|consumed| {
      self.filled += consumed;

      consumed
    })
  }

  pub fn resize(&mut self, size: usize) {
    if size > self.data.capacity() {
      self.data.reserve(size);

      let capacity = self.data.capacity();

      unsafe {
        self.data.set_len(capacity);
      }
    }

    if self.data.len() - self.filled < size  {
      let length  = self.filled - self.offset;
      let mut_ptr = self.data.as_mut_ptr();

      unsafe {
        let offset_ptr  = self.data.as_ptr().offset(self.offset as isize);

        ptr::copy(offset_ptr, mut_ptr, length);
      }

      self.filled -= self.offset;
      self.offset  = 0;
    }
  }

  pub fn consume(&mut self, consumed: usize) {
    self.offset += consumed;
  }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParserState {
  Incomplete,
  EndOfInput,
}

fn fill<R: Read>(buffer: &mut Buffer, reader: &mut R, needed: usize)
                 -> io::Result<usize> {
  let mut read = 0;

  if buffer.len() < needed {
    buffer.resize(needed);

    while buffer.len() < needed {
      let size_read = try!(buffer.fill(reader));

      if size_read > 0 {
        read += size_read;
      } else {
        break;
      }
    }
  }

  Ok(read)
}

// Structure that hold a reader for a source of bytes.
pub struct ReadStream<R: Read> {
  reader: R,
  buffer: Buffer,
  needed: usize,
  state: ParserState,
}

impl<R> ReadStream<R> where R: Read {
  pub fn new(reader: R) -> Self {
    ReadStream {
      reader: reader,
      buffer: Buffer::new(),
      needed: 0,
      state: ParserState::Incomplete,
    }
  }

  fn fill(&mut self) -> io::Result<usize> {
    let needed = cmp::max(1, self.needed);

    fill(&mut self.buffer, &mut self.reader, needed).map(|consumed| {
      if self.buffer.len() < needed {
        self.state = ParserState::EndOfInput;
      }

      consumed
    })
  }
}

fn from_iresult<T>(buffer: &Buffer, result: IResult<&[u8], T>)
                   -> Result<(usize, T), ErrorKind> {
  match result {
    IResult::Done(i, o)    => Ok((buffer.len() - i.len(), o)),
    IResult::Incomplete(n) => {
      let mut needed = buffer.capacity();

      if let Needed::Size(size) = n {
        needed = size;
      }

      Err(ErrorKind::Incomplete(needed))
    }
    IResult::Error(e)      => {
      if let Err::Position(_, i) = e {
        Err(ErrorKind::Consumed(buffer.len() - i.len()))
      } else {
        Err(ErrorKind::Unknown)
      }
    }
  }
}

impl<R> StreamProducer for ReadStream<R> where R: Read {
  fn parse<F, T>(&mut self, f: F) -> Result<T, ErrorKind>
   where F: FnOnce(&[u8]) -> IResult<&[u8], T> {
    if self.state != ParserState::EndOfInput {
      try!(self.fill().map_err(ErrorKind::IO));
    }

    let mut buffer = &mut self.buffer;

    if buffer.is_empty() {
      self.state = ParserState::EndOfInput;

      return Err(ErrorKind::EndOfInput);
    }

    let result = {
      let iresult = f(buffer.as_slice());

      from_iresult(&buffer, iresult)
    };

    match result {
      Ok((consumed, o)) => {
        buffer.consume(consumed);

        Ok(o)
      }
      Err(kind)         => {
        match kind {
          ErrorKind::Incomplete(needed) => self.needed = needed,
          ErrorKind::Consumed(consumed) => buffer.consume(consumed),
          _                             => (),
        }

        Err(kind)
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use utility::StreamProducer;
  use nom::be_u32;

  #[test]
  fn test_buffer() {
    let mut buffer = Buffer::new();
    let bytes      = b"Hello World";
    let mut reader = &bytes[..];

    assert!(buffer.is_empty());
    assert_eq!(buffer.capacity(), 1024);

    let bytes_read = buffer.fill(&mut reader).unwrap_or(0);
    let bytes_len  = bytes.len();

    assert_eq!(bytes_read, bytes_len);
    assert_eq!(buffer.len(), bytes_len);
    assert_eq!(buffer.as_slice(), bytes);

    buffer.resize(512);
    assert_eq!(buffer.capacity(), 1024);
  }

  #[test]
  fn test_byte_stream() {
    let bytes      = b"Hello World";
    let mut stream = ByteStream::new(bytes);

    assert_eq!(stream.len(), bytes.len());

    let result = stream.parse(be_u32).unwrap_or(0);

    assert_eq!(result, 1214606444);
    assert_eq!(stream.len(), 7);
  }

  #[test]
  fn test_read_stream() {
    let bytes      = b"Hello World";
    let mut stream = ReadStream::new(&bytes[..]);

    let result = stream.parse(be_u32).unwrap_or(0);

    assert_eq!(result, 1214606444)
  }
}
<|endoftext|>


<commit_before>use super::TimerUpdateWidget;
use text::{Attributes, Text};

use std::time::Duration;

use chrono::prelude::*;
use xcb;

pub struct Clock {
    conn: xcb::Connection,
    update_interval: Duration,
    attr: Attributes,
}

impl Clock {
    pub fn new(attr: Attributes) -> Clock {
        let (conn, screen_idx) = xcb::Connection::connect_with_xlib_display().unwrap();

        Clock {
            conn: conn,
            update_interval: Duration::from_secs(1),
            attr: attr,
        }
    }
}

impl TimerUpdateWidget for Clock {
    fn update_interval(&self) -> Duration {
        self.update_interval
    }

    fn tick(&self) -> Vec<Text> {
        let current_time = Local::now().format("%Y-%m-%d %a %I:%M %p").to_string();
        vec![Text {
                 attr: self.attr.clone(),
                 text: current_time,
                 stretch: false,
             }]
    }
}
<commit_msg>Clock widget doesn't need xcb connection.<commit_after>use super::TimerUpdateWidget;
use text::{Attributes, Text};

use std::time::Duration;

use chrono::prelude::*;

pub struct Clock {
    update_interval: Duration,
    attr: Attributes,
}

impl Clock {
    pub fn new(attr: Attributes) -> Clock {
        Clock {
            update_interval: Duration::from_secs(1),
            attr: attr,
        }
    }
}

impl TimerUpdateWidget for Clock {
    fn update_interval(&self) -> Duration {
        self.update_interval
    }

    fn tick(&self) -> Vec<Text> {
        let current_time = Local::now().format("%Y-%m-%d %a %I:%M %p").to_string();
        vec![Text {
                 attr: self.attr.clone(),
                 text: current_time,
                 stretch: false,
             }]
    }
}
<|endoftext|>


<commit_before>use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops::{Index, IndexMut};
use std::str::FromStr;

use self::cell::Cell;
use self::error::ParseMapError;

mod cell;
mod error;

pub struct Map {
    data: Vec<Cell>,
    nrow: usize,
    ncol: usize,
}

impl FromStr for Map {
    type Err = ParseMapError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use self::error::ParseMapError::*;

        if s.is_empty() {
            return Err(Empty);
        }
        let lines = s.lines().collect::<Vec<_>>();
        let (nrow, ncol) = (
            lines.len(),
            lines[0].chars().filter(|c| !c.is_whitespace()).count(),
        );
        let mut cells = Vec::with_capacity(nrow * ncol);
        for line in lines {
            let mut nchar = 0;
            for c in line.chars().filter(|c| !c.is_whitespace()) {
                nchar += 1;
                cells.push(match c {
                    '·' => Cell::new_dead(),
                    _ => Cell::new_alive(),
                });
            }
            if nchar != ncol {
                return Err(NotRectangular);
            }
        }
        Ok(Map::from_parts_unchecked(cells, (nrow, ncol)))
    }
}

impl Index<(usize, usize)> for Map {
    type Output = Cell;

    fn index(&self, (i, j): (usize, usize)) -> &Self::Output {
        &self.data[i * self.ncol + j]
    }
}

impl IndexMut<(usize, usize)> for Map {
    fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut Self::Output {
        &mut self.data[i * self.ncol + j]
    }
}

impl Display for Map {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        let mut map_str = String::with_capacity(self.nrow * (self.ncol * 3 + 1));
        for row in self.data.chunks(self.ncol) {
            for cell in row {
                if cell.alive {
                    map_str.push_str(" X");
                } else {
                    map_str.push_str(" ·");
                }
            }
            map_str.push('\n');
        }
        f.write_str(&map_str)
    }
}

impl Map {
    pub fn dim(&self) -> (usize, usize) {
        (self.nrow, self.ncol)
    }

    pub fn next(&mut self) {
        use std::cmp::min;

        for i in 0..self.nrow {
            for j in 0..self.ncol {
                let mut live_neighbours = 0u8;
                for m in i.saturating_sub(1)..min(i + 2, self.nrow) {
                    for n in j.saturating_sub(1)..min(j + 2, self.ncol) {
                        if self[(m, n)].alive && (m != i || n != j) {
                            live_neighbours += 1;
                        }
                    }
                }
                let cell = &mut self[(i, j)];
                if !cell.alive {
                    if live_neighbours == 3 {
                        cell.lives = true;
                    }
                } else if live_neighbours < 2 || 3 < live_neighbours {
                    cell.lives = false;
                }
            }
        }
        for cell in &mut self.data {
            cell.alive = cell.lives;
        }
    }
}

impl Map {
    fn from_parts_unchecked(cells: Vec<Cell>, (nrow, ncol): (usize, usize)) -> Self {
        assert_eq!(cells.len(), nrow * ncol);
        Map { data: cells, nrow, ncol }
    }
}
<commit_msg>use boxed slice instead of vec<commit_after>use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops::{Index, IndexMut};
use std::str::FromStr;

use self::cell::Cell;
use self::error::ParseMapError;

mod cell;
mod error;

pub struct Map {
    data: Box<[Cell]>,
    nrow: usize,
    ncol: usize,
}

impl FromStr for Map {
    type Err = ParseMapError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use self::error::ParseMapError::*;

        if s.is_empty() {
            return Err(Empty);
        }
        let lines = s.lines().collect::<Vec<_>>();
        let (nrow, ncol) = (
            lines.len(),
            lines[0].chars().filter(|c| !c.is_whitespace()).count(),
        );
        let mut cells = Vec::with_capacity(nrow * ncol);
        for line in lines {
            let mut nchar = 0;
            for c in line.chars().filter(|c| !c.is_whitespace()) {
                nchar += 1;
                cells.push(match c {
                    '·' => Cell::new_dead(),
                    _ => Cell::new_alive(),
                });
            }
            if nchar != ncol {
                return Err(NotRectangular);
            }
        }
        Ok(Map::from_parts_unchecked(cells, (nrow, ncol)))
    }
}

impl Index<(usize, usize)> for Map {
    type Output = Cell;

    fn index(&self, (i, j): (usize, usize)) -> &Self::Output {
        &self.data[i * self.ncol + j]
    }
}

impl IndexMut<(usize, usize)> for Map {
    fn index_mut(&mut self, (i, j): (usize, usize)) -> &mut Self::Output {
        &mut self.data[i * self.ncol + j]
    }
}

impl Display for Map {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        let mut map_str = String::with_capacity(self.nrow * (self.ncol * 3 + 1));
        for row in self.data.chunks(self.ncol) {
            for cell in row {
                if cell.alive {
                    map_str.push_str(" X");
                } else {
                    map_str.push_str(" ·");
                }
            }
            map_str.push('\n');
        }
        f.write_str(&map_str)
    }
}

impl Map {
    pub fn dim(&self) -> (usize, usize) {
        (self.nrow, self.ncol)
    }

    pub fn next(&mut self) {
        use std::cmp::min;

        for i in 0..self.nrow {
            for j in 0..self.ncol {
                let mut live_neighbours = 0u8;
                for m in i.saturating_sub(1)..min(i + 2, self.nrow) {
                    for n in j.saturating_sub(1)..min(j + 2, self.ncol) {
                        if self[(m, n)].alive && (m != i || n != j) {
                            live_neighbours += 1;
                        }
                    }
                }
                let cell = &mut self[(i, j)];
                if !cell.alive {
                    if live_neighbours == 3 {
                        cell.lives = true;
                    }
                } else if live_neighbours < 2 || 3 < live_neighbours {
                    cell.lives = false;
                }
            }
        }
        for cell in &mut *self.data {
            cell.alive = cell.lives;
        }
    }
}

impl Map {
    fn from_parts_unchecked(cells: Vec<Cell>, (nrow, ncol): (usize, usize)) -> Self {
        assert_eq!(cells.len(), nrow * ncol);
        Map { data: cells.into_boxed_slice(), nrow, ncol }
    }
}
<|endoftext|>


<commit_before>use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

use crossbeam_deque::{Injector, Steal, Stealer, Worker};
use rand::distributions::{Distribution, Uniform};
use rand::thread_rng;
use scoped_threadpool::Pool;

use gc::root::Slot;
use gc::{Address, Region};

pub fn start(rootset: &[Slot], heap: Region, perm: Region, threadpool: &mut Pool) {
    let number_workers = threadpool.thread_count() as usize;
    let mut workers = Vec::with_capacity(number_workers);
    let mut stealers = Vec::with_capacity(number_workers);
    let injector = Injector::new();

    for _ in 0..number_workers {
        let w = Worker::new_lifo();
        let s = w.stealer();
        workers.push(w);
        stealers.push(s);
    }

    for root in rootset {
        let root_ptr = root.get();

        if heap.contains(root_ptr) {
            let root_obj = root_ptr.to_mut_obj();

            if !root_obj.header().is_marked_non_atomic() {
                root_obj.header_mut().mark_non_atomic();
                injector.push(root_ptr);
            }
        } else {
            debug_assert!(root_ptr.is_null() || perm.contains(root_ptr));
        }
    }

    let terminator = Terminator::new(number_workers);

    threadpool.scoped(|scoped| {
        for (task_id, worker) in workers.into_iter().enumerate() {
            let heap_region = heap.clone();
            let perm_region = perm.clone();

            let injector = &injector;
            let stealers = &stealers;
            let terminator = &terminator;

            scoped.execute(move || {
                let mut task = MarkingTask {
                    task_id: task_id,
                    local: Segment::new(),
                    worker: worker,
                    injector: injector,
                    stealers: stealers,
                    terminator: terminator,
                    heap_region: heap_region,
                    perm_region: perm_region,
                    marked: 0,
                };

                task.run();
            });
        }
    });
}

pub struct Terminator {
    nworkers: AtomicUsize,
}

impl Terminator {
    pub fn new(number_workers: usize) -> Terminator {
        Terminator {
            nworkers: AtomicUsize::new(number_workers),
        }
    }

    pub fn try_terminate(&self) -> bool {
        self.decrease_workers();
        thread::sleep(Duration::from_micros(1));
        self.zero_or_increase_workers()
    }

    fn decrease_workers(&self) -> bool {
        self.nworkers.fetch_sub(1, Ordering::SeqCst) == 1
    }

    fn zero_or_increase_workers(&self) -> bool {
        let mut nworkers = self.nworkers.load(Ordering::Relaxed);

        loop {
            if nworkers == 0 {
                return true;
            }

            let prev_nworkers =
                self.nworkers
                    .compare_and_swap(nworkers, nworkers + 1, Ordering::SeqCst);

            if nworkers == prev_nworkers {
                return false;
            }

            nworkers = prev_nworkers;
        }
    }
}

struct MarkingTask<'a> {
    task_id: usize,
    local: Segment,
    worker: Worker<Address>,
    injector: &'a Injector<Address>,
    stealers: &'a [Stealer<Address>],
    terminator: &'a Terminator,
    heap_region: Region,
    perm_region: Region,
    marked: usize,
}

impl<'a> MarkingTask<'a> {
    fn pop(&mut self) -> Option<Address> {
        self.pop_local()
            .or_else(|| self.pop_worker())
            .or_else(|| self.pop_global())
            .or_else(|| self.steal())
    }

    fn pop_local(&mut self) -> Option<Address> {
        if self.local.is_empty() {
            return None;
        }

        let obj = self.local.pop().expect("should be non-empty");
        Some(obj)
    }

    fn pop_worker(&mut self) -> Option<Address> {
        self.worker.pop()
    }

    fn pop_global(&mut self) -> Option<Address> {
        loop {
            let result = self.injector.steal_batch_and_pop(&mut self.worker);

            match result {
                Steal::Empty => break,
                Steal::Success(value) => return Some(value),
                Steal::Retry => continue,
            }
        }

        None
    }

    fn steal(&self) -> Option<Address> {
        if self.stealers.len() == 1 {
            return None;
        }

        let mut rng = thread_rng();
        let range = Uniform::new(0, self.stealers.len());

        for _ in 0..2 * self.stealers.len() {
            let mut stealer_id = self.task_id;

            while stealer_id == self.task_id {
                stealer_id = range.sample(&mut rng);
            }

            let stealer = &self.stealers[stealer_id];

            loop {
                match stealer.steal_batch_and_pop(&self.worker) {
                    Steal::Empty => break,
                    Steal::Success(address) => return Some(address),
                    Steal::Retry => continue,
                }
            }
        }

        None
    }

    fn run(&mut self) {
        loop {
            let object_addr = if let Some(object_addr) = self.pop() {
                object_addr
            } else if self.terminator.try_terminate() {
                break;
            } else {
                continue;
            };

            let object = object_addr.to_mut_obj();

            object.visit_reference_fields(|field| {
                self.trace(field);
            });
        }
    }

    fn trace(&mut self, slot: Slot) {
        let field_addr = slot.get();

        if self.heap_region.contains(field_addr) {
            let field_obj = field_addr.to_mut_obj();

            if field_obj.header().try_mark_non_atomic() {
                if self.local.has_capacity() {
                    self.local.push(field_addr);
                    self.defensive_push();
                } else {
                    self.worker.push(field_addr);
                }
            }
        } else {
            debug_assert!(field_addr.is_null() || self.perm_region.contains(field_addr));
        }
    }

    fn defensive_push(&mut self) {
        self.marked += 1;

        if self.marked > 256 {
            if self.local.len() > 4 {
                let target_len = self.local.len() / 2;

                while self.local.len() > target_len {
                    let val = self.local.pop().unwrap();
                    self.injector.push(val);
                }
            }

            self.marked = 0;
        }
    }
}

const SEGMENT_SIZE: usize = 64;

struct Segment {
    data: Vec<Address>,
}

impl Segment {
    fn new() -> Segment {
        Segment {
            data: Vec::with_capacity(SEGMENT_SIZE),
        }
    }

    fn empty() -> Segment {
        Segment { data: Vec::new() }
    }

    fn with(addr: Address) -> Segment {
        let mut segment = Segment::new();
        segment.data.push(addr);

        segment
    }

    fn has_capacity(&self) -> bool {
        self.data.len() < SEGMENT_SIZE
    }

    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    fn push(&mut self, addr: Address) {
        debug_assert!(self.has_capacity());
        self.data.push(addr);
    }

    fn pop(&mut self) -> Option<Address> {
        self.data.pop()
    }

    fn len(&mut self) -> usize {
        self.data.len()
    }
}
<commit_msg>swiper: fast-path for single worker termination<commit_after>use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

use crossbeam_deque::{Injector, Steal, Stealer, Worker};
use rand::distributions::{Distribution, Uniform};
use rand::thread_rng;
use scoped_threadpool::Pool;

use gc::root::Slot;
use gc::{Address, Region};

pub fn start(rootset: &[Slot], heap: Region, perm: Region, threadpool: &mut Pool) {
    let number_workers = threadpool.thread_count() as usize;
    let mut workers = Vec::with_capacity(number_workers);
    let mut stealers = Vec::with_capacity(number_workers);
    let injector = Injector::new();

    for _ in 0..number_workers {
        let w = Worker::new_lifo();
        let s = w.stealer();
        workers.push(w);
        stealers.push(s);
    }

    for root in rootset {
        let root_ptr = root.get();

        if heap.contains(root_ptr) {
            let root_obj = root_ptr.to_mut_obj();

            if !root_obj.header().is_marked_non_atomic() {
                root_obj.header_mut().mark_non_atomic();
                injector.push(root_ptr);
            }
        } else {
            debug_assert!(root_ptr.is_null() || perm.contains(root_ptr));
        }
    }

    let terminator = Terminator::new(number_workers);

    threadpool.scoped(|scoped| {
        for (task_id, worker) in workers.into_iter().enumerate() {
            let heap_region = heap.clone();
            let perm_region = perm.clone();

            let injector = &injector;
            let stealers = &stealers;
            let terminator = &terminator;

            scoped.execute(move || {
                let mut task = MarkingTask {
                    task_id: task_id,
                    local: Segment::new(),
                    worker: worker,
                    injector: injector,
                    stealers: stealers,
                    terminator: terminator,
                    heap_region: heap_region,
                    perm_region: perm_region,
                    marked: 0,
                };

                task.run();
            });
        }
    });
}

pub struct Terminator {
    const_nworkers: usize,
    nworkers: AtomicUsize,
}

impl Terminator {
    pub fn new(number_workers: usize) -> Terminator {
        Terminator {
            const_nworkers: number_workers,
            nworkers: AtomicUsize::new(number_workers),
        }
    }

    pub fn try_terminate(&self) -> bool {
        if self.const_nworkers == 1 {
            return;
        }

        self.decrease_workers();
        thread::sleep(Duration::from_micros(1));
        self.zero_or_increase_workers()
    }

    fn decrease_workers(&self) -> bool {
        self.nworkers.fetch_sub(1, Ordering::SeqCst) == 1
    }

    fn zero_or_increase_workers(&self) -> bool {
        let mut nworkers = self.nworkers.load(Ordering::Relaxed);

        loop {
            if nworkers == 0 {
                return true;
            }

            let prev_nworkers =
                self.nworkers
                    .compare_and_swap(nworkers, nworkers + 1, Ordering::SeqCst);

            if nworkers == prev_nworkers {
                return false;
            }

            nworkers = prev_nworkers;
        }
    }
}

struct MarkingTask<'a> {
    task_id: usize,
    local: Segment,
    worker: Worker<Address>,
    injector: &'a Injector<Address>,
    stealers: &'a [Stealer<Address>],
    terminator: &'a Terminator,
    heap_region: Region,
    perm_region: Region,
    marked: usize,
}

impl<'a> MarkingTask<'a> {
    fn pop(&mut self) -> Option<Address> {
        self.pop_local()
            .or_else(|| self.pop_worker())
            .or_else(|| self.pop_global())
            .or_else(|| self.steal())
    }

    fn pop_local(&mut self) -> Option<Address> {
        if self.local.is_empty() {
            return None;
        }

        let obj = self.local.pop().expect("should be non-empty");
        Some(obj)
    }

    fn pop_worker(&mut self) -> Option<Address> {
        self.worker.pop()
    }

    fn pop_global(&mut self) -> Option<Address> {
        loop {
            let result = self.injector.steal_batch_and_pop(&mut self.worker);

            match result {
                Steal::Empty => break,
                Steal::Success(value) => return Some(value),
                Steal::Retry => continue,
            }
        }

        None
    }

    fn steal(&self) -> Option<Address> {
        if self.stealers.len() == 1 {
            return None;
        }

        let mut rng = thread_rng();
        let range = Uniform::new(0, self.stealers.len());

        for _ in 0..2 * self.stealers.len() {
            let mut stealer_id = self.task_id;

            while stealer_id == self.task_id {
                stealer_id = range.sample(&mut rng);
            }

            let stealer = &self.stealers[stealer_id];

            loop {
                match stealer.steal_batch_and_pop(&self.worker) {
                    Steal::Empty => break,
                    Steal::Success(address) => return Some(address),
                    Steal::Retry => continue,
                }
            }
        }

        None
    }

    fn run(&mut self) {
        loop {
            let object_addr = if let Some(object_addr) = self.pop() {
                object_addr
            } else if self.terminator.try_terminate() {
                break;
            } else {
                continue;
            };

            let object = object_addr.to_mut_obj();

            object.visit_reference_fields(|field| {
                self.trace(field);
            });
        }
    }

    fn trace(&mut self, slot: Slot) {
        let field_addr = slot.get();

        if self.heap_region.contains(field_addr) {
            let field_obj = field_addr.to_mut_obj();

            if field_obj.header().try_mark_non_atomic() {
                if self.local.has_capacity() {
                    self.local.push(field_addr);
                    self.defensive_push();
                } else {
                    self.worker.push(field_addr);
                }
            }
        } else {
            debug_assert!(field_addr.is_null() || self.perm_region.contains(field_addr));
        }
    }

    fn defensive_push(&mut self) {
        self.marked += 1;

        if self.marked > 256 {
            if self.local.len() > 4 {
                let target_len = self.local.len() / 2;

                while self.local.len() > target_len {
                    let val = self.local.pop().unwrap();
                    self.injector.push(val);
                }
            }

            self.marked = 0;
        }
    }
}

const SEGMENT_SIZE: usize = 64;

struct Segment {
    data: Vec<Address>,
}

impl Segment {
    fn new() -> Segment {
        Segment {
            data: Vec::with_capacity(SEGMENT_SIZE),
        }
    }

    fn empty() -> Segment {
        Segment { data: Vec::new() }
    }

    fn with(addr: Address) -> Segment {
        let mut segment = Segment::new();
        segment.data.push(addr);

        segment
    }

    fn has_capacity(&self) -> bool {
        self.data.len() < SEGMENT_SIZE
    }

    fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    fn push(&mut self, addr: Address) {
        debug_assert!(self.has_capacity());
        self.data.push(addr);
    }

    fn pop(&mut self) -> Option<Address> {
        self.data.pop()
    }

    fn len(&mut self) -> usize {
        self.data.len()
    }
}
<|endoftext|>


<commit_before>#[cfg(feature = "curl-easy")]
pub use easy::CurlEasyClient;

#[cfg(feature = "curl-easy")]
mod easy {
    use super::super::{
        FromRequest,
        Request,
    };
    use curl::easy::{
        Easy,
        List,
        Transfer,
    };
    use http_types::{
        header::HeaderValue,
        Method,
    };

    fn headermap_to_curl_list(headermap: &http_types::HeaderMap<HeaderValue>) -> List {
        let mut list = List::new();
        headermap.iter().for_each(|(k, v)| {
                            // this will scan for printable US-ASCII only bytes
                            let header = v.to_str()
                                          .map(|hval| [k.as_str(), ": ", hval].concat())
                                          .expect("found header value without a displayable US-ASCII string");
                            list.append(header.as_str())
                                .expect("failed to allocate node for curl list of headers");
                        });
        list
    }

    #[derive(Debug)]
    pub enum CurlEasyClient<'easy, 'data> {
        Easy(&'easy Easy),
        Transfer(Transfer<'easy, 'data>),
    }

    impl<'easy, 'data> From<Transfer<'easy, 'data>> for CurlEasyClient<'easy, 'data> {
        fn from(t: Transfer<'easy, 'data>) -> Self {
            Self::Transfer(t)
        }
    }

    impl<'easy> From<&'easy Easy> for CurlEasyClient<'easy, '_> {
        fn from(e: &'easy Easy) -> Self {
            Self::Easy(e)
        }
    }

    impl<'easy, 'data> CurlEasyClient<'easy, 'data> {
        pub fn new(easy: &'easy Easy) -> Self {
            Self::Easy(easy)
        }

        pub fn perform(&self) -> Result<(), curl::Error> {
            match self {
                Self::Easy(e) => e.perform(),
                Self::Transfer(t) => t.perform(),
            }
        }

        pub fn into_transfer(self) -> Option<Transfer<'easy, 'data>> {
            match self {
                Self::Transfer(t) => Some(t),
                Self::Easy(_) => None,
            }
        }

        pub fn easy(&self) -> Option<&'easy Easy> {
            if let Self::Easy(e) = self {
                Some(e)
            } else {
                None
            }
        }
    }

    impl<'easy, 'data, URI: ToString> FromRequest<(&'easy mut Easy, URI)> for CurlEasyClient<'easy, 'data> {
        fn from_request(r: Request, params: (&'easy mut Easy, URI)) -> Self {
            let (uri, body) = r.parameters.uri_and_body(r.path);
            let (client, uri_base) = params;
            let uri = uri_base.to_string() + uri.as_ref();

            match r.method {
                Method::GET => client.get(true),
                Method::POST => client.post(true),
                Method::PUT => client.put(true),
                // any other verb needs to use custom_request()
                m => client.custom_request(m.as_str()),
            }.expect("failed to set up the request's method");

            client.url(uri.as_str()).expect("error setting up url for curl");
            let mut headerlist = headermap_to_curl_list(&r.headers);
            // libcurl by default adds "Expect: 100-continue" to send bodies, which would break us
            headerlist.append("Expect:")
                      .expect("failed to allocate node for curl list of headers");
            // don't specify Content-Type for this request (similar to other clients)
            headerlist.append("Content-Type:")
                      .expect("failed to allocate node for curl list of headers");
            client.http_headers(headerlist)
                  .expect("error setting up headers for curl");

            match body {
                Some(_) => {
                    use std::io::Read;

                    let body = r.parameters.into_inner();
                    // this sets the Content-Length - some servers will misbehave without this
                    client.post_field_size(body.len() as u64)
                          .expect("failed to set post size");
                    let mut transfer = client.transfer();

                    let mut count = 0usize;
                    transfer.read_function(move |buf| {
                                let mut bytes = &body.as_bytes()[count..];
                                let newcount =
                                    bytes.read(buf).expect("error while copying body data to buffer");
                                count += newcount;
                                Ok(newcount)
                            })
                            .expect("failed to set up read function for curl");

                    transfer.into()
                }
                None => (client as &Easy).into(),
            }
        }
    }
}
<commit_msg>request/curl: move to new SetupRequest trait impl<commit_after>#[cfg(feature = "curl-easy")]
pub use easy::CurlEasyClient;

#[cfg(feature = "curl-easy")]
mod easy {
    use super::super::{
        Request,
        SetupRequest,
    };
    use curl::easy::{
        Easy,
        List,
        Transfer,
    };
    use http_types::{
        header::HeaderValue,
        Method,
    };

    fn headermap_to_curl_list(headermap: &http_types::HeaderMap<HeaderValue>) -> List {
        let mut list = List::new();
        headermap.iter().for_each(|(k, v)| {
                            // this will scan for printable US-ASCII only bytes
                            let header = v.to_str()
                                          .map(|hval| [k.as_str(), ": ", hval].concat())
                                          .expect("found header value without a displayable US-ASCII string");
                            list.append(header.as_str())
                                .expect("failed to allocate node for curl list of headers");
                        });
        list
    }

    #[derive(Debug)]
    pub enum CurlEasyClient<'easy, 'data> {
        Easy(&'easy Easy),
        Transfer(Transfer<'easy, 'data>),
    }

    impl<'easy, 'data> From<Transfer<'easy, 'data>> for CurlEasyClient<'easy, 'data> {
        fn from(t: Transfer<'easy, 'data>) -> Self {
            Self::Transfer(t)
        }
    }

    impl<'easy> From<&'easy Easy> for CurlEasyClient<'easy, '_> {
        fn from(e: &'easy Easy) -> Self {
            Self::Easy(e)
        }
    }

    impl<'easy, 'data> CurlEasyClient<'easy, 'data> {
        pub fn new(easy: &'easy Easy) -> Self {
            Self::Easy(easy)
        }

        pub fn perform(&self) -> Result<(), curl::Error> {
            match self {
                Self::Easy(e) => e.perform(),
                Self::Transfer(t) => t.perform(),
            }
        }

        pub fn into_transfer(self) -> Option<Transfer<'easy, 'data>> {
            match self {
                Self::Transfer(t) => Some(t),
                Self::Easy(_) => None,
            }
        }

        pub fn easy(&self) -> Option<&'easy Easy> {
            if let Self::Easy(e) = self {
                Some(e)
            } else {
                None
            }
        }
    }

    impl<'easy, 'data, URI: ToString> SetupRequest<'easy, URI, CurlEasyClient<'easy, 'data>> for Easy {
        fn setup_request(&'easy mut self, r: Request, params: URI) -> CurlEasyClient<'easy, 'data> {
            let (uri, body) = r.parameters.uri_and_body(r.path);
            let uri_base = params;
            let uri = uri_base.to_string() + uri.as_ref();

            match r.method {
                Method::GET => self.get(true),
                Method::POST => self.post(true),
                Method::PUT => self.put(true),
                // any other verb needs to use custom_request()
                m => self.custom_request(m.as_str()),
            }.expect("failed to set up the request's method");

            self.url(uri.as_str()).expect("error setting up url for curl");
            let mut headerlist = headermap_to_curl_list(&r.headers);
            // libcurl by default adds "Expect: 100-continue" to send bodies, which would break us
            headerlist.append("Expect:")
                      .expect("failed to allocate node for curl list of headers");
            // don't specify Content-Type for this request (similar to other clients)
            headerlist.append("Content-Type:")
                      .expect("failed to allocate node for curl list of headers");
            self.http_headers(headerlist)
                .expect("error setting up headers for curl");

            match body {
                Some(_) => {
                    use std::io::Read;

                    let body = r.parameters.into_inner();
                    // this sets the Content-Length - some servers will misbehave without this
                    self.post_field_size(body.len() as u64)
                        .expect("failed to set post size");
                    let mut transfer = self.transfer();

                    let mut count = 0usize;
                    transfer.read_function(move |buf| {
                                let mut bytes = &body.as_bytes()[count..];
                                let newcount =
                                    bytes.read(buf).expect("error while copying body data to buffer");
                                count += newcount;
                                Ok(newcount)
                            })
                            .expect("failed to set up read function for curl");

                    transfer.into()
                }
                None => (self as &Easy).into(),
            }
        }
    }
}
<|endoftext|>


<commit_before><commit_msg>Declarative Ptr variance (#119)<commit_after><|endoftext|>


<commit_before><commit_msg>Add rss to psq<commit_after><|endoftext|>


<commit_before>use ra_db::{FileId, LocalSyntaxPtr, Cancelable};
use ra_syntax::{
    SyntaxNode, AstNode, SmolStr,
    ast
};
use hir::{Def, ModuleSource};

use crate::{
    NavigationTarget,
    FileSymbol,
    db::RootDatabase,
};

impl NavigationTarget {
    pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
        NavigationTarget {
            file_id: symbol.file_id,
            name: symbol.name.clone(),
            kind: symbol.ptr.kind(),
            range: symbol.ptr.range(),
            ptr: Some(symbol.ptr.clone()),
        }
    }

    // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
    pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
        Ok(match def {
            Def::Struct(s) => {
                let (file_id, node) = s.source(db)?;
                Some(NavigationTarget::from_named(
                    file_id.original_file(db),
                    &*node,
                ))
            }
            Def::Enum(e) => {
                let (file_id, node) = e.source(db)?;
                Some(NavigationTarget::from_named(
                    file_id.original_file(db),
                    &*node,
                ))
            }
            Def::EnumVariant(ev) => {
                let (file_id, node) = ev.source(db)?;
                Some(NavigationTarget::from_named(
                    file_id.original_file(db),
                    &*node,
                ))
            }
            Def::Function(f) => {
                let (file_id, node) = f.source(db)?;
                Some(NavigationTarget::from_named(
                    file_id.original_file(db),
                    &*node,
                ))
            }
            Def::Module(m) => {
                let (file_id, source) = m.definition_source(db)?;
                let name = m
                    .name(db)?
                    .map(|it| it.to_string().into())
                    .unwrap_or_else(|| SmolStr::new(""));
                match source {
                    ModuleSource::SourceFile(node) => {
                        Some(NavigationTarget::from_syntax(file_id, name, node.syntax()))
                    }
                    ModuleSource::Module(node) => {
                        Some(NavigationTarget::from_syntax(file_id, name, node.syntax()))
                    }
                }
            }
            Def::Item => None,
        })
    }

    fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
        let name = node
            .name()
            .map(|it| it.text().clone())
            .unwrap_or_else(|| SmolStr::new(""));
        NavigationTarget::from_syntax(file_id, name, node.syntax())
    }

    fn from_syntax(file_id: FileId, name: SmolStr, node: &SyntaxNode) -> NavigationTarget {
        NavigationTarget {
            file_id,
            name,
            kind: node.kind(),
            range: node.range(),
            ptr: Some(LocalSyntaxPtr::new(node)),
        }
    }
}
<commit_msg>reshuffle nones<commit_after>use ra_db::{FileId, LocalSyntaxPtr, Cancelable};
use ra_syntax::{
    SyntaxNode, AstNode, SmolStr,
    ast
};
use hir::{Def, ModuleSource};

use crate::{
    NavigationTarget,
    FileSymbol,
    db::RootDatabase,
};

impl NavigationTarget {
    pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget {
        NavigationTarget {
            file_id: symbol.file_id,
            name: symbol.name.clone(),
            kind: symbol.ptr.kind(),
            range: symbol.ptr.range(),
            ptr: Some(symbol.ptr.clone()),
        }
    }

    // TODO once Def::Item is gone, this should be able to always return a NavigationTarget
    pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> {
        let res = match def {
            Def::Struct(s) => {
                let (file_id, node) = s.source(db)?;
                NavigationTarget::from_named(file_id.original_file(db), &*node)
            }
            Def::Enum(e) => {
                let (file_id, node) = e.source(db)?;
                NavigationTarget::from_named(file_id.original_file(db), &*node)
            }
            Def::EnumVariant(ev) => {
                let (file_id, node) = ev.source(db)?;
                NavigationTarget::from_named(file_id.original_file(db), &*node)
            }
            Def::Function(f) => {
                let (file_id, node) = f.source(db)?;
                NavigationTarget::from_named(file_id.original_file(db), &*node)
            }
            Def::Module(m) => {
                let (file_id, source) = m.definition_source(db)?;
                let name = m
                    .name(db)?
                    .map(|it| it.to_string().into())
                    .unwrap_or_else(|| SmolStr::new(""));
                match source {
                    ModuleSource::SourceFile(node) => {
                        NavigationTarget::from_syntax(file_id, name, node.syntax())
                    }
                    ModuleSource::Module(node) => {
                        NavigationTarget::from_syntax(file_id, name, node.syntax())
                    }
                }
            }
            Def::Item => return Ok(None),
        };
        Ok(Some(res))
    }

    fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget {
        let name = node
            .name()
            .map(|it| it.text().clone())
            .unwrap_or_else(|| SmolStr::new(""));
        NavigationTarget::from_syntax(file_id, name, node.syntax())
    }

    fn from_syntax(file_id: FileId, name: SmolStr, node: &SyntaxNode) -> NavigationTarget {
        NavigationTarget {
            file_id,
            name,
            kind: node.kind(),
            range: node.range(),
            ptr: Some(LocalSyntaxPtr::new(node)),
        }
    }
}
<|endoftext|>


<commit_before><commit_msg>Parse cxx_name and rust_name attributes on enum variants<commit_after><|endoftext|>


<commit_before><commit_msg>Added new test.<commit_after><|endoftext|>


<commit_before><commit_msg>Define Transfrom struct<commit_after><|endoftext|>


<commit_before><commit_msg>types: support the money type<commit_after><|endoftext|>