Skip to content

Instantly share code, notes, and snippets.

View Lisoph's full-sized avatar

Daniel Hauser Lisoph

  • Fronius International GmbH
  • Austria
  • 12:39 (UTC +02:00)
View GitHub Profile
@Lisoph
Lisoph / dynamic_bitset_128.rs
Created March 21, 2018 11:14
BitSet with up to 32 dynamic 128bit chunks. Can store values 0..4096. Sparse memory representation.
#![feature(i128_type)]
struct BitSet {
offsets: u32,
chunks: Vec<u128>,
}
impl BitSet {
fn new() -> Self {
Self {
@Lisoph
Lisoph / bitset_128.rs
Created March 20, 2018 22:12
BitSet (aka BitMap) container based on 128bit integers. Can store values 0..1024
#![feature(i128_type)]
struct BitSet {
ints: [u128; 8],
}
impl BitSet {
fn new() -> Self {
Self {
ints: [0; 8],
@Lisoph
Lisoph / main.rs
Created December 6, 2017 10:01
Advent of Code 2017 #1.2
fn main() {
let digits: Vec<_> = INPUT.chars().map(|c| char::to_digit(c, 10).unwrap()).collect();
let step = digits.len() / 2;
let mut acc = 0;
for (i, d) in digits.iter().enumerate() {
if let Some(x) = digits.iter().cycle().skip(i).nth(step) {
if d == x {
acc += d;
}
@Lisoph
Lisoph / main.rs
Created December 6, 2017 09:12
Advent of Code 2017 #4
fn main() {
let input = r#"pphsv ojtou brvhsj cer ntfhlra udeh ccgtyzc zoyzmh jum lugbnk
vxjnf fzqitnj uyfck blnl impo kxoow nngd worcm bdesehw
caibh nfuk kfnu llfdbz uxjty yxjut jcea
qiho qif eupwww avyglnj nxzotsu hio lws
xjty usocjsh pivk qnknunc yjcgh bwya djw zpyr
ycfmfe mgq sjiomg nfzjul bjwkmgu yvsnvgj dcjupu wzz blmn
rdowgbt vpwfdoi blzl laghnk gsa vhnpo cztxzlb rtz hvwonhb eciju pfjtbo
bqs bqs dbutvgf mmzb izpyud rap izpyud xlzeb mnj hjncs
xpu vwp nujcos piu irindir tpmfd umtvlm gznu
@Lisoph
Lisoph / main.rs
Created December 6, 2017 07:40
Advent of Code 2017 #3
#[derive(Debug, PartialEq, Copy, Clone)]
enum Dir {
Up,
Down,
Left,
Right,
}
#[derive(Clone, Copy, PartialEq)]
struct Point {
@Lisoph
Lisoph / parser_combinators2.rs
Created October 4, 2017 09:19
Parser Combinators 2
#![feature(conservative_impl_trait)]
use std::cmp::PartialEq;
type ParseResult<T, I: Iterator<Item = T>> = Result<ParseStatus<T, I>, ParseError>;
#[derive(Debug)]
enum ParseStatus<T, I: Iterator<Item = T>> {
Ok(Vec<T>, I),
NeedMoreData(usize), // # of units needed
@Lisoph
Lisoph / shared_widgets.rs
Last active June 7, 2017 09:03
Rust shared widgets
use std::collections::HashSet;
use std::cell::RefCell;
use std::rc::Rc;
struct WidgetRef(Rc<RefCell<Box<Widget>>>);
impl WidgetRef {
fn new(inner: Rc<RefCell<Box<Widget>>>) -> Self {
WidgetRef(inner)
}
@Lisoph
Lisoph / event.rs
Created June 1, 2017 09:31
A Rust data structure that can store closures and invoke them. A poor mans version of `event` from C#.
struct Event<'a> {
handlers: Vec<Box<FnMut() + 'a>>,
}
impl<'a> Event<'a> {
fn new() -> Self {
Self {
handlers: Vec::new(),
}
}
@Lisoph
Lisoph / gui.rs
Created June 1, 2017 08:47
Rust GUI core concept
enum MouseButton {
Left,
Middle,
Right,
}
enum UiEvent {
MouseMove(i32, i32),
MouseClicked(i32, i32, MouseButton),
}
#![feature(conservative_impl_trait)]
fn take<'a>(take: &'a str) -> impl Fn(&'a str) -> (&'a str, &'a str) {
move |input| {
let len = input.chars().zip(take.chars()).take_while(|&(i, t)| i == t).count();
(&input[0..len], &input[len..])
}
}
fn chain<'a, F1, F2>(first: F1, second: F2) -> impl Fn(&'a str) -> ((&'a str, &'a str), &'a str)