Skip to content

Instantly share code, notes, and snippets.

@arrieta
arrieta / node-coloring-greedy.cpp
Created November 8, 2021 14:27
Graph Coloring Algorithms
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <vector>
using Graph = std::vector<std::set<int>>;
using Coloring = std::vector<int>;
@arrieta
arrieta / kdtree.cpp
Created November 8, 2021 14:22
Trivial k-d tree implementation in C++
// Educational implementation of a k-d tree.
#include <algorithm>
#include <array>
#include <iostream>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
@arrieta
arrieta / fsm.cpp
Created November 2, 2021 16:38
Basic Table-Driven State Machine in C++
// clang-format off
/*
Basic example showing how could one implement a table-driven state machine in
C++.
Suppose one has a lamp which can be in one of three states: "Off", "On",
"Blink". At any time and via some mechanism, the lamp can trigger two events:
"Switch Off" or "Switch On".
@arrieta
arrieta / slurp-test.cpp
Created October 18, 2021 15:04
Read whole file in C++ std::string
// slurp-test.cpp.
//
// Benchmark several methods to read a file into a C++ std::string. Partly based
// on the answers to my question:
//
// https://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring
//
// asked on April 08, 2010.
//
// Tests on several files with different sizes (from a few bytes to a couple of
@arrieta
arrieta / nzl-coding-conventions-virtual-functions.cpp
Created December 26, 2020 14:53
Coding Conventions for Virtual Functions in C++
// Example that shows some coding conventions regarding the use of virtual
// functions in C++.
// (C) 2020 Nabla Zero Labs
// MIT License
// Contents of "config.hpp" or similar -- a header containing some build
// configuration compile-time constants.
namespace nzl {
namespace config {
static constexpr const auto debug = true; // whether to build in debug mode
@arrieta
arrieta / pmr_example.cpp
Created August 18, 2020 21:04
Example Polymorphic Memory Resource (std::pmr) Monotonic Buffer Resource
// Example Polymorphic Memory Resource (std::pmr) Monotonic Buffer Resource.
// J. Arrieta, Nabla Zero Labs
//
// * Pre-allocate two buffers, one with space for 2 chars and one with space for 256 chars.
// * Filled pre-allocated buffers with underscores to peek into them later and see where memory was touched.
// * Create two monotonic_buffer_resources, one for each buffer.
// * Notice that the main pool receives a pointer to the "backup" pool.
// * Create a vector of char that uses the main memory resource.
// * Push back on the vector all chars from 'a' to 'z'.
// * See how the first char was allocated using the first memory resource (pool).
@arrieta
arrieta / make_canonical.hpp
Created April 25, 2019 18:48
Transform strings into a canonical representation
/// @file make_canonical.hpp
/// @brief Transform strings into a canonical representation.
/// @author J. Arrieta <[email protected]>
/// @copyright (C) 2019 Nabla Zero Labs
/// @license MIT
#pragma once
#include <algorithm>
#include <cctype>
@arrieta
arrieta / timestamp.hpp
Created April 22, 2019 20:04
UNIX Timestamp in C++ (Guaranteed in C++20; de facto standard pre-C++20)
/// @file timestamp.hpp
/// @brief Return UNIX timestamp.
/// @author J. Arrieta <[email protected]>
/// @copyright (C) 2019 Nabla Zero Labs
/// @license MIT
#pragma once
#include <chrono>
#include <cstdint>
@arrieta
arrieta / heartbeat.cpp
Last active September 1, 2023 03:49
Dirt Simple Heart Beat Server in C++
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <chrono>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
@arrieta
arrieta / AWSLambdaGenerateKeyPair.go
Last active January 28, 2021 03:06
AWS Lambda Generate Key Pair (Go)
// Sample code used to generate a cryptographic key pair via an AWS Lambda function.
// (C) 2019 Nabla Zero Labs
// MIT License
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"