Skip to content

Instantly share code, notes, and snippets.

@duckie
duckie / list_intersection_2.cpp
Created June 3, 2014 16:14
Compute intersection of two lists - Method 2
#include <iostream>
#include <string>
#include <set>
#include <unordered_set>
#include <list>
#include <algorithm>
using namespace std;
template <typename T> using intersection_set = set<reference_wrapper<T const>, less<T>>;
@duckie
duckie / list_intersection_1.cpp
Created June 3, 2014 16:07
Compute intersection of two lists - Method 1
#include <iostream>
#include <string>
#include <unordered_set>
#include <list>
#include <algorithm>
using namespace std;
list<string> compare_list_warning(list<string> const& list_one, list<string> const& list_two)
{
list<string> const& small_list = list_one.size() < list_two.size() ? list_one : list_two;
@duckie
duckie / list_intersection_performance.cpp
Created June 4, 2014 13:08
Compare performances between different intersection algorithms
#include <iostream>
#include <string>
#include <unordered_set>
#include <set>
#include <list>
#include <algorithm>
#include <random>
#include <chrono>
#include <sstream>
@duckie
duckie / readonly_member.cpp
Created June 10, 2014 01:33
Private data member with automatic public read-only through an accessor.
#include <memory>
template <class Parent, typename T> class readable final {
friend Parent;
T value_;
operator T& () { return value_; }
public:
readable(T const& v) : value_(v) {}
readable(T&& v) : value_(std::move(v)) {}
T const& operator () () { return value_; }
#include <iostream>
#include <chrono>
#include <vector>
#include <set>
#include <list>
#include <unordered_set>
#include <random>
#include <string>
#include <sstream>
@duckie
duckie / virtual_variadic.cpp
Created July 26, 2014 14:36
Virtuality with a variadic interface.
#include <string>
#include <sstream>
#include <iostream>
#include <memory>
struct Serializer {
virtual ~Serializer() {}
virtual void StartRecord() = 0;
virtual void EndRecord() = 0;
@duckie
duckie / write_bmp_image.cc
Last active August 29, 2015 14:15
Generate an image in Microsoft BMP format
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdint>
#include <array>
using UChar = uint8_t;
using Color = std::array<UChar, 3>;
using Image = std::vector<Color>;
@duckie
duckie / cpp_file_simulatneous_read_write.cpp
Created June 27, 2012 12:51
C++ : How to write and read with only one file open for both operations
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <string>
std::ostream & format_2_l0(std::ostream & iStream)
{
return iStream << std::setw(2) << std::setfill('0');
}
@duckie
duckie / french_number_writer.cpp
Created August 2, 2012 18:28
Converts a number into its litteral french translation
#include <iostream>
#include <map>
#include <sstream>
/**
* Converts a number into its french representation
*
* @see http://fr.wikipedia.org/wiki/Adjectif_num%C3%A9ral
*/
class Convertisseur
@duckie
duckie / errors.h
Created August 3, 2012 10:24
Utility to parse a header, extract error codes and store them into a *.map re-usable from C++ code.
#ifndef IBM_ERRORS
#define IBM_ERRORS
/**
* C'est un peu la fête du slip les codes d'erreurs en macros
*/
#define ERREUR_MARCEL_A_TROP_BU 1
#define ERREUR_ROBERT_A_TROP_MANGE 2
# define ERREUR_Y_A_PLUS_DE_PASTIS 3