Skip to content

Instantly share code, notes, and snippets.

View PhDP's full-sized avatar
🏠
Working from home

Philippe Desjardins-Proulx PhDP

🏠
Working from home
View GitHub Profile
@PhDP
PhDP / fol.js
Created March 1, 2018 19:57
fol.js
// tslint:disable
/*
* Generated by PEG.js 0.10.0.
*
* http://pegjs.org/
*/
"use strict";
function peg$subclass(child, parent) {
@PhDP
PhDP / quantum_demux_va.cc
Last active January 27, 2018 18:37
C++17 sum-types version of a quantum circuit model for a 2-bit demultiplexer. It's like the object-oriented version, but it doesn't suck.
// With gcc 7+ or clang 5+, compiles with -std=c++17 flag
#include <iostream>
#include <variant>
#include <vector>
struct toffoli_gate {
uint32_t c0, c1, x;
toffoli_gate(uint32_t c0_, uint32_t c1_, uint32_t x_) noexcept
: c0(c0_), c1(c1_), x(x_) {
@PhDP
PhDP / quantum_demux_oo.cc
Last active January 27, 2018 18:37
C++17 object-oriented version of a quantum circuit model for a 2-bit demultiplexer.
// With gcc 7+ or clang 5+, compiles with -std=c++17 flag
#include <iostream>
#include <memory>
#include <vector>
// Base class for gates:
struct gate {
virtual void apply(std::vector<bool>& bits) const = 0;
virtual std::ostream& print(std::ostream&) const = 0;
#include <iostream>
#include <map>
#include <unordered_map>
template<typename Key, typename Value>
using stdmap = std::map<Key, Value>;
template<typename Key, typename Value>
using stdumap = std::unordered_map<Key, Value>;
@PhDP
PhDP / rh_hash_table.hpp
Created April 27, 2017 20:04 — forked from ssylvan/rh_hash_table.hpp
Quick'n'dirty Robin Hood hash table implementation. Note, I have implemented this algorithm before, with tons of tests etc. But *this* code was written specifically for the blog post at http://sebastiansylvan.com/post/robin-hood-hashing-should-be-your-default-hash-table-implementation/, it has not been extensively tested so there may be bugs (an…
#define USE_ROBIN_HOOD_HASH 1
#define USE_SEPARATE_HASH_ARRAY 1
template<class Key, class Value>
class hash_table
{
static const int INITIAL_SIZE = 256;
static const int LOAD_FACTOR_PERCENT = 90;
struct elem
@PhDP
PhDP / fol.pegjs
Created October 1, 2016 03:04
PEGJs Logic + Math parser
{
function getTerms(t, ts) {
if (t) {
var arr = [];
for (var i = 0; i < ts.length; ++i) {
arr.push(ts[i][2]);
}
return [t].concat(arr);
}
return [];
@PhDP
PhDP / expression.hh
Created March 12, 2016 19:09
C++11 template for math expressions
#ifndef EXPRESSION_HH__
#define EXPRESSION_HH__
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/lexical_cast.hpp>
namespace reasoning {
@PhDP
PhDP / permutations.cc
Created February 24, 2016 04:59
List all unique sets for a range of integers.
// clang++ -std=c++11 permutations.cc -o permutations
#include <iostream>
#include <ostream>
#include <set>
#include <vector>
template<typename T>
auto operator<<(std::ostream &os, const std::set<T> &xs) -> std::ostream& {
os << '{';
@PhDP
PhDP / bond.fsx
Created February 5, 2016 01:13 — forked from evelinag/bond.fsx
Analysing box office success of James Bond films using HTML type provider
#load "packages/FsLab/FsLab.fsx"
open FSharp.Data
open XPlot.GoogleCharts
let bondUrl = "https://en.wikipedia.org/w/index.php?title=List_of_James_Bond_films&oldid=688916363"
type BondProvider = HtmlProvider<"https://en.wikipedia.org/w/index.php?title=List_of_James_Bond_films&oldid=688916363">
let bondWiki = BondProvider.Load(bondUrl)
@PhDP
PhDP / safe_sqrt.cc
Created January 28, 2016 06:46
Example of boost::optional to write a safe sqrt function.
// clang++ -std=c++11 safe_sqrt.cc
#include <iostream>
#include <cmath>
#include <boost/optional.hpp>
auto safe_sqrt(double x) -> boost::optional<double> {
if (x >= 0.0)
return std::sqrt(x);
return boost::none;