Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 28, 2019 17:07
Show Gist options
  • Save rust-play/217ca6628468e07c9892945e2c1b9c02 to your computer and use it in GitHub Desktop.
Save rust-play/217ca6628468e07c9892945e2c1b9c02 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// from bluss dlx solver
type Index = usize;
#[derive(Copy, Clone, Debug)]
struct Node<T> {
/// Prev, Next, Up, Down
link: [usize; 4],
value: T,
}
impl<T> Node<T> {
fn new(value: T) -> Self
{
Node {
value: value,
link: [!0; 4],
}
}
fn prev(&self) -> Index { self.get(Prev) }
fn next(&self) -> Index { self.get(Next) }
fn set_prev(&mut self, index: Index) -> &mut Self { self.set(Prev, index) }
fn set_next(&mut self, index: Index) -> &mut Self { self.set(Next, index) }
fn up(&self) -> Index { self.get(Up) }
fn down(&self) -> Index { self.get(Down) }
fn set_up(&mut self, index: Index) -> &mut Self { self.set(Up, index) }
fn set_down(&mut self, index: Index) -> &mut Self { self.set(Down, index) }
fn get(&self, dir: Direction) -> Index {
self.link[dir as usize]
}
fn set(&mut self, dir: Direction, index: Index) -> &mut Self {
self.link[dir as usize] = index;
self
}
fn assign(&mut self, dir: Direction) -> &mut usize {
&mut self.link[dir as usize]
}
}
// Direction of list link
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Direction {
Prev,
Next,
Up,
Down,
}
use Direction::*;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment