Skip to content

Instantly share code, notes, and snippets.

View nikhedonia's full-sized avatar
💭
actively open-sourcing

Gaetano Checinski nikhedonia

💭
actively open-sourcing
View GitHub Profile
// Type Erasure
// https://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil
// https://akrzemi1.wordpress.com/2013/11/18/type-erasure-part-i/
#include<memory>
#include<iostream>
struct Printable {
#include<iostream>
#include<vector>
#include<string>
#include<set>
auto numberOfUniqueChars(std::string const& str) {
std::set<char> s;
for(auto c : str) {
s.insert(c);
}
#include<vector>
#include<iostream>
template<class T>
struct Matrix {
Matrix(std::size_t n=1, std::size_t m=1)
: n{n}, m{m}, data(n*m)
{}
Matrix(std::size_t n, std::size_t m, std::vector<T> const& data)
#include<functional>
template<class T>
struct AsFunction
: public AsFunction<decltype(&T::operator())>
{};
template<class ReturnType, class... Args>
struct AsFunction<ReturnType(Args...)> {
using type = std::function<ReturnType(Args...)>;
#include<functional>
template<class T>
struct AsFunction
: public AsFunction<decltype(&T::operator())>
{};
template<class ReturnType, class... Args>
struct AsFunction<ReturnType(Args...)> {
using type = std::function<ReturnType(Args...)>;
@nikhedonia
nikhedonia / Duals.cpp
Created February 23, 2017 20:01
example showing the beauty of duals
// Example program
#include <iostream>
#include <string>
struct Dual {
float real;
float img;
Dual(float x, float y=0)
:real{x}, img{y}
@nikhedonia
nikhedonia / EitherAsmAnalysis.cpp
Created May 16, 2017 11:39
analysing Assembly generated by Eithers
#include <type_traits>
template<class L>
struct Left {
L const value;
};
template<class R>
struct Right {
R const value;
struct Texture {
  Texture(string const& path) { cout << "Texture loaded from " << path << endl; }
 ~Texture() { cout<< "Texture destroyed" << endl; }
 
  static shared_ptr<Texture> load(string const& path) {
  return make_shared<Texture>(path)
  }
};
// this functions will take a copy and increment the counter
template<int D>
struct Vector {
static constexpr unsigned N = D;
int data[N];
Vector (Vector<D> const& v) {
for(int i=0; i<N ; ++i) {
data[i] = v[i];
}
}
#define CREATE_VECTOR(D) \
struct Vector_##D {\
static constexpr int N = D;\
int data[N];\
\
Vector_##D (Vector_##D const& v) {\
for(int i=0; i<N ; ++i) {\
data[i] = v[i];\
}\