/** Solve the connections between HexBlocks in a list, given boundary conditions

We solve from the left, to the right. For each node, we look at what possible
bridges exist between the current node and the next one. We remove those
that would form a loop with the node's left chain.

The bridges are solved level by level until we have solved it down to size 0.
*/
Solve the connections between HexBlocks in a list, given boundary conditions
We solve from the left, to the right. For each node, we look at what possible
bridges exist between the current node and the next one. We remove those
that would form a loop with the node's left chain.

The bridges are solved level by level until we have solved it down to size 0.
Solve the connections between HexBlocks in a list given boundary conditions We solve from the left to the right . For each node we look at what possible bridges exist between the current node and the next one . We remove those that would form a loop with the node ' s left chain . The bridges are solved level by level until we have solved it down to size 0 .
fn solve_links(path: Vec<HexBlock>, leftmost: HexChain, rightmost: HexChain) -> Vec<HexNode> {
	let mut rng = thread_rng();
	// Convert $path to a chain of HexNodes
	let mut chain: Vec<HexNode> = path.into_iter().map(HexNode::from_block).collect();
	// Try until we find a chain that doesn't form a loop at the end
	'found: loop {
		// Expand the sides of each HexNode
		let mut left = leftmost.clone();
		let mut chain_iter = chain.iter_mut().peekable();
		while let Some(node) = chain_iter.next() {
			if node.block.is_atom() {
				continue;
			}
			let mut next_left = HexChain::new();
			let mut right = HexChain::new();
			if let Some(next_node) = chain_iter.peek() {
				// Given the left side find the right side: loop until we solved the node down to size 0
				loop {
					if right.last().map(HexBlock::is_atom).unwrap_or(false) {
						break;
					}
					let right_point = right.last().unwrap_or_else(|| &node.block);
					let next_left_point = next_left.last().unwrap_or_else(|| &next_node.block);
					let (mut bridge_left, mut bridge_right) = find_link(right_point, next_left_point).into_iter()
						.filter(|bridge| !chains_form_loop(&left, &bridge.left))
						.choose(&mut rng).expect("There is always a way to bridge across adjacent HexBlocks")
						.into_chains();
					right.append(&mut bridge_left);
					next_left.append(&mut bridge_right);
				}
			} else {
				// This is the last node
				right = rightmost.clone();
				if !chains_form_loop(&left, &right) {
					/* Copied from the loop tail, because we break the loop to prove
					that `right = rightmost` happens only once */
					node.left = left;
					node.right = right;
					break 'found;
				} else {
					// Retry to find one without a loop
					continue 'found;
				}
			}
			node.left = left;
			node.right = right;
			left = next_left;
		}
		break;
	}
	chain
}
fn solve_links ( path : Vec < HexBlock > , leftmost : HexChain , rightmost : HexChain ) -> Vec < HexNode > { let mut rng = thread_rng ( ) ; // Convert $path to a chain of HexNodes let mut chain : Vec < HexNode > = path . into_iter ( ) . map ( HexNode :: from_block ) . collect ( ) ; // Try until we find a chain that doesn't form a loop at the end ' found : loop { // Expand the sides of each HexNode let mut left = leftmost . clone ( ) ; let mut chain_iter = chain . iter_mut ( ) . peekable ( ) ; while let Some ( node ) = chain_iter . next ( ) { if node . block . is_atom ( ) { continue ; } let mut next_left = HexChain :: new ( ) ; let mut right = HexChain :: new ( ) ; if let Some ( next_node ) = chain_iter . peek ( ) { // Given the left side find the right side: loop until we solved the node down to size 0 loop { if right . last ( ) . map ( HexBlock :: is_atom ) . unwrap_or ( false ) { break ; } let right_point = right . last ( ) . unwrap_or_else ( | | & node . block ) ; let next_left_point = next_left . last ( ) . unwrap_or_else ( | | & next_node . block ) ; let ( mut bridge_left , mut bridge_right ) = find_link ( right_point , next_left_point ) . into_iter ( ) . filter ( | bridge | ! chains_form_loop ( & left , & bridge . left ) ) . choose ( & mut rng ) . expect ( " " ) . into_chains ( ) ; right . append ( & mut bridge_left ) ; next_left . append ( & mut bridge_right ) ; } } else { // This is the last node right = rightmost . clone ( ) ; if ! chains_form_loop ( & left , & right ) { /* Copied from the loop tail, because we break the loop to prove
					that `right = rightmost` happens only once */ node . left = left ; node . right = right ; break ' found ; } else { // Retry to find one without a loop continue ' found ; } } node . left = left ; node . right = right ; left = next_left ; } break ; } chain }
Solve the connections between HexBlocks in a list, given boundary conditions
We solve from the left, to the right.
Solve the connections between HexBlocks in a list given boundary conditions We solve from the left to the right .
path
Vec<HexBlock>
leftmost
HexChain
rightmost
HexChain