Skip to content

Instantly share code, notes, and snippets.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char* ht_key_t;
typedef int ht_value_t;
// key => value plus pointer to next item for hash collisions
typedef struct HashTableItem HashTableItem;
#include <Adafruit_NeoPixel.h>
#include <DS3231_Simple.h>
// Which pin on the Arduino is connected to the NeoPixels?
const int LEDCLOCK_PIN = 6;
const int LEDDOWNLIGHT_PIN = 5;
// How many NeoPixels are attached to the Arduino?
const int LEDCLOCK_COUNT = 207;
const int LEDDOWNLIGHT_COUNT = 12;
@oschonrock
oschonrock / txt_file_parsing.cpp
Last active January 8, 2020 21:50
High performance txt file parsing
// START include/flat_hash_map/flat_hash_map.hpp
// Copyright Malte Skarupke 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See http://www.boost.org/LICENSE_1_0.txt)
#include <cstdint>
#include <cstddef>
@oschonrock
oschonrock / timer.h
Created December 4, 2019 20:04
C++ Basic profiling timer class
class Timer
{
public:
Timer(std::string label_) : start{std::chrono::high_resolution_clock::now()}, label{label_} {};
~Timer() { print(); }
void print() {
finish = std::chrono::high_resolution_clock::now();
auto elapsed_ms = std::chrono::duration_cast<std::chrono::duration<double>>(finish - start).count() * 1000;
@oschonrock
oschonrock / profile_tests.cpp
Last active December 4, 2019 20:07
SequencedMap a C++ container like std::map which retains insertion order
int main() {
using Key = std::string;
using Value = int;
SequencedMap<Key, Value> smap;
// arbitrary ad-hoc temporary structure for the data (for demo purposes only)
for (auto p: std::vector<std::pair<Key, Value>>{
{"Mary", 10}, {"Alex", 20}, {"Johnny", 40}, {"Roman", 40}, {"Johnny", 50},
}) {
@oschonrock
oschonrock / fparse.cpp
Last active January 5, 2021 19:23
High speed parsing of floats in CSV format - C++
// refers to this question of SO:
// https://stackoverflow.com/questions/17465061/how-to-parse-space-separated-floats-in-c-quickly/59013147
#include <iomanip>
#include <iostream>
// for mmap:
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
@oschonrock
oschonrock / crack_atof.cpp
Created November 23, 2019 23:03
crack_atof() a blisteringly fast alternative to the teriibly slow C++ std::stod
// the C++ STL is a mess for parsing numbers from strings, FAST. from_chars looked good but is not implemented in clang/gcc
// crack_atof is a very very fast alternative
// Original crack_atof version is at http://crackprogramming.blogspot.sg/2012/10/implement-atof.html
// But it cannot convert floating point with high +/- exponent.
// The version below by Tian Bo fixes that problem and improves performance by 10%
// http://coliru.stacked-crooked.com/a/2e28f0d71f47ca5e
// Oliver Schonrock: I picked this code up from
// https://www.codeproject.com/Articles/1130262/Cplusplus-string-view-Conversion-to-Integral-Types
<?php
/**
* intention of this class is not to do anything too fancy
* it basically just runs one query, but it does so with ease and in a scalable and safe way
* ie it won't block, you don't have to worry about setting up a new connection
* it will reuse the connection and the prepared statement if you make multiple calls
* been tested and refined in high volume production for 1 year.
*
* to use it just customise the __contrsuct below with your DB details and call:
@oschonrock
oschonrock / leach.php
Last active December 7, 2017 10:44
leach() = "legacy each": php72 compat for deprecated each(). Use to refactor tired third party libs, when not performance critical.
<?php
/**
* leach() = "legacy each"
*
* replacement for deprecated each() for php7.2
* use this if refactoring would be too painful and performance is not relevant
*
*/
function leach(&$arr)
@oschonrock
oschonrock / generate_sodium_compat.php
Last active December 7, 2017 11:22
pecl-libsodium to php72-sodium compat lib
#!/usr/bin/env php
<?php
/*
* generates the code for sodium_compat.php
*/
define('BASE', dirname(dirname(__FILE__)) . '/');
$lib = '<?php