Skip to content

Instantly share code, notes, and snippets.

View haudan's full-sized avatar

Daniel Hauser haudan

  • Fronius International GmbH
  • Austria
  • 07:56 (UTC +01:00)
View GitHub Profile
@haudan
haudan / 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],
@haudan
haudan / 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 {
@haudan
haudan / quake2_fade_dithering.c
Last active April 5, 2018 08:15
Dithering based fading in Quake 2
// https://github.com/id-Software/Quake-2/blob/372afde46e7defc9dd2d719a1732b8ace1fa096e/ref_soft/r_draw.c#L428
void Draw_FadeScreen (void)
{
int x,y;
byte *pbuf;
int t;
for (y=0 ; y<vid.height ; y++)
{
pbuf = (byte *)(vid.buffer + vid.rowbytes*y);
@haudan
haudan / obj_bytes_obj.rs
Created April 5, 2018 08:12
A struct which can store an object to raw memory and restore from it.
use std::marker::PhantomData;
#[derive(Clone)]
struct App {
data: Vec<String>,
data2: Tester,
}
#[derive(Clone)]
struct Tester;
@haudan
haudan / stacked_coroutine.rs
Created April 5, 2018 12:20
Coroutine implementation similar to Unity's, in nightly Rust.
#![feature(generators, generator_trait)]
use std::ops::{Generator, GeneratorState};
use std::thread;
use std::time::{Duration, Instant};
struct CoStep;
impl CoGenerator for CoStep {
fn resume(&mut self) -> CoGenState {
@haudan
haudan / alloc_locality.rs
Created April 6, 2018 10:21
Rust allocator locality tester. Analyses whether the current allocator allocates successive allocations sequentially in memory.
const PAYLOAD_SIZE: usize = 128;
const TESTS: usize = 20;
fn main() {
let mut items: [Box<[u8; PAYLOAD_SIZE]>; TESTS] = unsafe { std::mem::uninitialized() };
for item in items.iter_mut() {
unsafe {
std::ptr::write(item as *mut _, Box::new([0u8; PAYLOAD_SIZE]));
}
@haudan
haudan / zig1.zig
Created August 6, 2018 21:55
Some fun with the Zig programming language
const std = @import("std");
const Controller = struct {
up: u1,
down: u1,
left: u1,
right: u1,
};
pub fn main() void {
@haudan
haudan / cpp17_tokens.cpp
Created August 31, 2018 11:49
C++17 Token struct
#include <cstdint>
#include <cstddef>
#include <string>
#include <iostream>
struct TokIdentifier {
std::string name;
TokIdentifier() = delete;
TokIdentifier(TokIdentifier const &) = default;
@haudan
haudan / generic_mut_event.rs
Created September 14, 2018 10:13
Generic event container inspired by delegate / event in C#
use std::cell::UnsafeCell;
use std::marker::PhantomData;
type EventReceiver<'a, A, R> = Box<dyn (FnMut(A) -> R + 'a)>;
struct Event<'a, A, R> {
receivers: Vec<UnsafeCell<EventReceiver<'a, A, R>>>,
_m: PhantomData<&'a ()>,
}
@haudan
haudan / zig_matrix.zig
Created September 26, 2018 06:26
Zig generic Matrix struct
pub fn main() void {
const foo = Matrix(f32, 4, 4).identity();
}
fn Matrix(comptime Scalar: type, comptime Width: usize, comptime Height: usize) type {
return struct {
const Self = @This();
pub elems: [Width * Height]Scalar,