Skip to content

Instantly share code, notes, and snippets.

#!/bin/sh
# usage: ./tabless INPUT_FILE
#
# INPUT_FILE is expected to be a text file containing lines like the following:
#
# 23 column-1-name -554.23 column-2-name ...
#
# The output will be a `less` session of the numbers only, tabulated, but with
# a header of column names pinned to the top line, e.g.
@dgoffredo
dgoffredo / lock-free-stack.cpp
Created January 17, 2025 02:35
lock-free stack
#include <atomic>
#include <memory>
#include <utility>
template <typename T>
class LockFreeStack {
private:
struct Node {
T value;
Node *next;
@dgoffredo
dgoffredo / 1-just-use-a-function.cpp
Last active January 9, 2025 18:32
recursive function with closure
int count(
std::unordered_map<std::int64_t, int>& memo,
const std::vector<int>& denominations,
int coin_index,
int amount) {
if (coin_index < 0 || amount < 0) {
return 0;
}
if (amount == 0) {
@dgoffredo
dgoffredo / hash.cpp
Last active January 9, 2025 01:05
hash a tuple; treat optional specially
#include <optional>
#include <tuple>
struct TupleHasher {
template <typename... Values>
std::size_t operator()(const std::tuple<Values...>& key) const {
std::size_t seed = 0;
std::apply(HashAppend{seed}, key);
return seed;
}
#include <string>
#include <vector>
std::string longest_palindromic_substring(const std::string& text) {
const int n = text.size();
if (n < 2) {
return text;
}
std::vector<bool> grandparent(n, true);
std::vector<bool> parent(n - 1, true);
@dgoffredo
dgoffredo / shared_ptr.cpp
Created December 3, 2024 17:23
copy constructor gotcha
#include <iostream>
template <typename T>
class shared_ptr {
public:
shared_ptr() {
std::cout << "Default constructor.\n";
}
// If you comment out this _real_ copy constructor, then the compiler
@dgoffredo
dgoffredo / re_match.py
Last active August 21, 2024 18:51
re_match in sqlite3
"""re_match - sqlite3 regular expression pattern extraction function
The SQL function `re_match(pattern, subject)` returns the first subgroup match in
`subject` if `subject` matches `pattern`. It returns `subject` if it matches but
has no subgroups. It returns null if it doesn't match.
`re_match(pattern, subject, group)` allows for the extraction of some other
subgroup, which can be a 1-based index or a name.
`re_matchi` is `re_match` but case-insensitive.
@dgoffredo
dgoffredo / path_ends_with.cpp
Created May 10, 2024 23:52
std::filesystem::path ends with
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
namespace fs = std::filesystem;
// Return whether the specified `path` ends with the specified `suffix`.
@dgoffredo
dgoffredo / up
Created August 2, 2023 00:26
wrapper around "docker compose up"
#!/bin/sh
# Forward command line arguments to "docker compose up",
# but also prevent ctrl+c from being handled by "docker compose up".
# Instead, handle it here by calling "docker compose down".
# This way, containers and networks created by docker compose will be
# cleaned up in workflows where you bring down services via ctrl+c.
# The default handling by docker compose does bring down services
# but does not remove networks and containers.
// Based off of a Go example in <https://research.swtch.com/glob> accessed
// February 3, 2022.
function match(pattern, subject) {
px = 0 // [p]attern inde[x]
sx = 0 // [s]ubject inde[x]
nextPx = 0
nextSx = 0
while (px < pattern.length || sx < subject.length) {
if (px < pattern.length) {