Skip to content

Instantly share code, notes, and snippets.

View apainintheneck's full-sized avatar

Kevin apainintheneck

View GitHub Profile
@apainintheneck
apainintheneck / shell-lib.hpp
Last active April 20, 2022 16:40
Shell-lib: A small wrapper for standard C++ library functions that makes them behave like shell commands.
/*
Shell-lib
----------
Mimic familiar shell commands inside of C++.
--------------------------------------------
MIT License
Copyright (c) 2022 Kevin Robell
Permission is hereby granted, free of charge, to any person obtaining a copy
@apainintheneck
apainintheneck / word-pos-trie.cpp
Created April 22, 2022 08:48
A trie used to find the positions of words in a given text file.
//A trie used to find the positions of words in a text file.
#include <vector>
#include <string>
#include <cctype>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <memory>
@apainintheneck
apainintheneck / permutations.js
Created May 11, 2022 23:21
A stock implementation of Heap's algorithm as a JS generator.
export function* permutations(array) {
let mutArray = array.slice();
if(mutArray.length > 0) yield mutArray.slice();
const stack = Array(mutArray.length).fill(0);
for(let i = 0; i < mutArray.length;) {
if(stack[i] < i) {
if(i % 2 === 0) {
[mutArray[0], mutArray[i]] = [mutArray[i], mutArray[0]];
} else {
@apainintheneck
apainintheneck / bit-manipulation-tips.md
Last active May 15, 2022 05:18
A bit manipulation refresher

Bit Manipulation Tips & Tricks

We all know that bit masking is occasionally necessary if you're dealing with data at the binary level but, if you're anything like me, you use it infrequently enough that you constantly have to lookup anything beyond the basics. This is just a spot to record all of the little tricks that I've come across in one place.

Basics

The three most common operations are to get, set and clear single bits. Those are usually pretty easy to remember but for thoroughness I'll note them down here.

# Get a bit
mask = (1<<bit)
num &amp; mask
@apainintheneck
apainintheneck / old.cpp
Created June 4, 2022 22:19
A simple cli app that allows you to search for files older than x.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <sys/stat.h>
#include <glob.h>
#include <ctime>
@apainintheneck
apainintheneck / csv_cpp.h
Created July 3, 2022 16:36
A simple CSV reader/writer library
#pragma once
#include <fstream>
#include <string>
#include <vector>
#include <cctype>
/*
csv_cpp is complaint with the RFC 4180 (https://datatracker.ietf.org/doc/html/rfc4180)
with a few exceptions.
@apainintheneck
apainintheneck / agram.cpp
Created July 10, 2022 16:54
A simple program that finds partial anagrams of a given word.
#include <array>
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
/*
Usage: agram [base word] [min length]
Find partial anagrams using the /usr/share/dict/words list on mac.
@apainintheneck
apainintheneck / tocgen.awk
Created September 10, 2022 19:11
A small program that generates a table of contents for a markdown file.
#!/usr/bin/env awk -f
# A program that produces a table of contents from a markdown file.
# It assumes that the start title depth is 2 (## or h2) but that can
# be modified by passing in the START variable.
#
# Ex. cat readme.md | ./tocgen.awk
# Ex. cat readme.md | ./tocgen.awk -v START=1
BEGIN {
if(!START) START = 2
@apainintheneck
apainintheneck / yamlpack.awk
Created September 11, 2022 22:49
A program that packs yaml docs into a more greppable format.
#!/usr/bin/env awk -f
# A program that packs yaml into strings representing the location
# of elements that makes yaml more greppable. Inspired by Gron
# which does something similar with JSON (https://github.com/tomnomnom/gron).
# This currently only supports a subset of YAML.
# - Simple keys
# - (alphanumeric + dash + underscore) + (optional spaces) + colon
# - [A-Za-z_-]+[ ]*:
# - Colon must be followed by a space
@apainintheneck
apainintheneck / igit-trim
Last active October 8, 2023 23:21
An interactive way to trim unused or unwanted git branches in the current directory.
#!/usr/bin/env awk -f
# An interactive way to trim unused or unwanted git branches in the current directory.
#
# 1. Go through the branches from oldest to newest.
# 2. Autoremove already merged-in branches.
# 3. Wait for user command on other branches.
#
# User commands:
# h: Help page
# l: Commit log