Skip to content

Instantly share code, notes, and snippets.

@arrieta
arrieta / check-rsa-signature.go
Created September 22, 2018 02:07
Check RSA Signature (golang)
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"fmt"
@arrieta
arrieta / aliasing_constructor.cpp
Created October 6, 2018 21:02
C++ std::shared_ptr<T> Aliasing Constructor
// About std::shared_ptr<T> aliasing constructor.
// J. Arrieta, Nabla Zero Labs.
// October 06, 2018.
//
// The std::shared_ptr<T> aliasing constructor has the signature
//
// template< class Y >
// shared_ptr(const shared_ptr<Y>& r, element_type* ptr) noexcept;
//
// According to cppreference.com, it "constructs a shared_ptr which shares
@arrieta
arrieta / non-owning-ts-cache.cpp
Created October 6, 2018 22:12
C++ Non-Owning Thread-Safe Object Cache
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
// In practice these would be actual types instead of aliases.
using Id = int;
@arrieta
arrieta / string_view_and_arguments.cpp
Created October 23, 2018 16:24
Experiments with std::string_view, argument dispatching, and stuff like that.
/// Experiments with string view, dispatching, and stuff like that.
/// J. Arrieta
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>
// POSIX only
#include <sys/stat.h>
@arrieta
arrieta / pds.cpp
Created October 27, 2018 15:56
PDS OBJ Parser Excercise
#include <cctype>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
inline bool semantic_compare(std::size_t n, const char *value,
const std::string &s) {
@arrieta
arrieta / pool_of_T.cpp
Created November 3, 2018 18:21
Aligned Object Pool
#include <cassert>
#include <iostream>
#include <memory>
#include <type_traits>
#include <vector>
template <typename T>
class Pool {
public:
using Block = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
@arrieta
arrieta / lexer.cpp
Last active June 5, 2024 17:22
Simple C++ Lexer
// A simple Lexer meant to demonstrate a few theoretical concepts. It can
// support several parser concepts and is very fast (though speed is not its
// design goal).
//
// J. Arrieta, Nabla Zero Labs
//
// This code is released under the MIT License.
//
// Copyright 2018 Nabla Zero Labs
//
@arrieta
arrieta / memory_map.hpp
Created November 23, 2018 21:21
Simple Memory Mapping in C++
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file nzl/memory_map.hpp
/// @brief A mapped memory region.
/// @author J. Arrieta <juan.arrieta@nablazerolabs.com>
/// @date November 23, 2018
///
/// Copyright 2018 Nabla Zero Labs
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
@arrieta
arrieta / benchmark-poly.py
Created November 27, 2018 23:15
Basic benchmark polynomial evaluation using Horner's Method
"""Simple re-arrangement can yield significant performance improvements in
naive, low-order polynomial evaluation. The interested reader may wish to read
about "Horner's method."
(C) 2018 Nabla Zero Labs
MIT License
"""
import time
def p0(a, x):
@arrieta
arrieta / slurp.hpp
Created January 13, 2019 00:06
Read file into std::string
#pragma once
#include <fstream>
#include <string>
#include <system_error>
#include <iterator>
inline std::string slurp(const std::string& path) {
constexpr auto flags = std::ios::in | std::ios::binary;
if (auto fp = std::ifstream(path, flags); fp) {