Skip to content

Instantly share code, notes, and snippets.

View jweinst1's full-sized avatar
🎯
Focusing

Josh Weinstein jweinst1

🎯
Focusing
View GitHub Profile
@jweinst1
jweinst1 / bit_field_set_ordered.c
Last active June 17, 2025 00:27
uses continuous bit fields to get first set or unset to an arbitrary size
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
// allows a set for a number over field of 64 bit blocks of bits
void set_bit_for_number(size_t* nums, size_t key) {
const size_t tabs = key / 64;
const size_t rem = key % 64;
printf("Setting block %zu, bit %zu, mask %zu\n", tabs, rem, 1 << rem);
@jweinst1
jweinst1 / 32_bit_bench.cpp
Last active June 12, 2025 21:12
sorted trie for fast integer lookups
#include <iostream>
#include <set>
#include <vector>
#include <thread>
#include <mutex>
#include <random>
#include <chrono>
#include <atomic>
constexpr int BITS_PER_LEVEL = 6;
@jweinst1
jweinst1 / rust_indexing.rs
Last active May 23, 2025 22:22
indexing a string in rust.
use std::fs::File;
use std::io::prelude::*;
use std::io::SeekFrom;
use std::fs::OpenOptions;
#[derive(Debug)]
struct Seg(usize, usize);
impl Seg {
fn contains(&self, text:&[u8], term:&[u8]) -> bool {
@jweinst1
jweinst1 / mqueue.c
Created May 12, 2025 07:27
a disk backed message queue in C with kqueue
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include <inttypes.h>
@jweinst1
jweinst1 / hostwide.c
Created May 7, 2025 01:15
host wide cpu usage on a mac
#include <stdio.h>
#include <mach/mach.h>
#include <mach/mach_host.h>
int main() {
// Get the host's CPU statistics
host_cpu_load_info_data_t cpu_info;
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
kern_return_t kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpu_info, &count);
@jweinst1
jweinst1 / kqueue_tail.c
Last active May 11, 2025 23:08
Tailing implemented with Kqueue
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include <limits.h>
#include <assert.h>
#include <sys/event.h>
#include <sys/time.h>
@jweinst1
jweinst1 / fsevents.c
Last active May 2, 2025 00:31
Monitor FSEvents in C with apple library core services
#include <CoreServices/CoreServices.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void fsevent_callback(
ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
@jweinst1
jweinst1 / atlas_based_game.swift
Last active May 1, 2025 00:09
img atlas based game swift
//
// GameScene.swift
// FunScape
//
// Created by Joshua Weinstein on 4/26/25.
//
import SpriteKit
import GameplayKit
@jweinst1
jweinst1 / game.swift
Last active April 22, 2025 05:44
playground game of blocks in swift with contacts and jumps and joints
//: A SpriteKit based Playground
import PlaygroundSupport
import SpriteKit
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
class GameOverScene : SKScene {
var label = SKLabelNode(fontNamed: "Chalkduster")
@jweinst1
jweinst1 / match_multi_group.cpp
Created April 3, 2025 23:07
match multiple regex groups in C++
#include <regex>
int main(int argc, char const *argv[])
{
const std::string foo = "hello \n sir \n";
std::vector<EventPos> pairs;
generateEvents(foo, '\n', pairs);
std::printf("%zu\n", pairs.size());
const std::string sample = "remote= bar foo=4 six=bar fhhfghfgdhgdf do=555";
const std::regex sampleRex("([a-zA-Z0-9_]+)\\s*=\\s*([a-zA-Z0-9_]+)");