Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / awareness.cpp
Created September 19, 2018 14:57
C++ Translation of Go Code
// I am interested in practical comparisons between C++ and Go. To this end, I
// find "good" Go code (by some definition of "good") and translate it into C++.
//
// It is mostly unimportant what the code does (in fact, it does not do anything
// at all). What matters is to attempt to capture the same API and semantics.
//
// This file implements a class called "Awareness" found in the hashicorp repo:
//
// https://github.com/hashicorp/memberlist/blob/master/awareness.go
//
@arrieta
arrieta / mt-log.cpp
Created March 4, 2018 00:17
An approach to multithreaded logging to an existing buffer
#include <algorithm>
#include <atomic>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <iterator>
#include <chrono>
@arrieta
arrieta / async_server.py
Created March 3, 2018 14:18
Asynchronous Server Basic Example
"""
The server code is based entirely on D. Beazley's talk "Topics of Interest (Python Asyncio)"
given in 2015 at the Python Brazil conference.
http://pyvideo.org/python-brasil-2015/keynote-david-beazley-topics-of-interest-python-asyncio.html
The server can handle 60,000 requests in 2.020 seconds (29,696 requests/second) in a ~2015 macOS.
"""
@arrieta
arrieta / wsgi_middleware.py
Created March 2, 2018 20:54
Brief explanation of WSGI middleware
"""
A brief explanation of WSGI middleware.
Nabla Zero Labs
"""
# The main WSGI application returns the string "Hello, {ip_address}!", where
# `ip_address` is the IP address of the host making the request.
def application(environ, start_response):
print("Main application")
@arrieta
arrieta / contiguous-slice-mt-access.cpp
Created February 24, 2018 22:30
Simple pattern for thread-safe, read-only access to an existing, fixed, contiguous memory slice.
/*
A simple pattern for thread-safe, read-only access to an existing, fixed,
contiguous memory slice.
This pattern is useful when simple chunking would be undesirable. For example:
A job may consist of three hard (H) tasks, each lasting two seconds, and three
easy (E) tasks, each lasting one second. If we simply pass the first three to
one thread, and the last three to a second thread, the second thread will
finish after three seconds, and leave the first thread to finish six seconds
worth of work. (total runtime: six seconds). The proposed approach would take
@arrieta
arrieta / pds.js
Last active February 21, 2018 16:24
Lexer for the Planetary Data System (PDS) Object Description Language (ODL)
/*
This program implements a lexer for the Object Description Language (ODL), a legacy metadata format
used by NASA's Planetary Data System (PDS).
As of this writing, the ODL version is 2.1, and the specification can be found in:
https://pds.jpl.nasa.gov/documents/sr/Chapter12.pdf
This lexer simply emits the tokens needed by an ODL parser, which is provided as a separate program.
@arrieta
arrieta / knapsack.py
Created December 6, 2017 21:48
Solve small knapsack problems by different methods
"""
knapsack.py
Solve small knapsack problems by different methods.
(C) 2017 J. Arrieta, Nabla Zero Labs
MIT License
"""
@arrieta
arrieta / mintrans.py
Created December 6, 2017 15:38
Minimum transformations from N to 1
def T(n):
memo = {
1 : (0, "<Base>"),
2 : (1, " / 2"),
3 : (1, " / 3")
}
def build_memo(n):