Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / deque.cpp
Created September 22, 2014 23:44
A simple deque implementation
#include <iostream>
#include <stdexcept>
template <typename Item>
class Deque
{
class iterator;
class node;
public:
Deque() = default;
@dgodfrey206
dgodfrey206 / has_type.cpp
Last active August 29, 2015 14:06
Checks if there is a type within a tuple without repeatedly instantiating the template to check each type.
#include <type_traits>
#include <tuple>
// Sequence of integers to index the elements of the tuple
template <std::size_t... Indices>
struct index_sequence {
typedef index_sequence<Indices..., sizeof...(Indices)> next;
};
@dgodfrey206
dgodfrey206 / has_type_v2.cpp
Last active August 29, 2015 14:07
The second improved version of has_type. It's shorter, does away with recursion and uses the bool_sequence idiom.
#include <type_traits>
#include <tuple>
template <bool...>
struct bool_sequence { };
template <bool... Bs>
using bool_and = std::is_same<bool_sequence<Bs...>,
bool_sequence<(Bs || true)...>>;
@dgodfrey206
dgodfrey206 / has_type_v3.cpp
Last active August 29, 2015 14:07
The third improved version of has_type. This version exhibits behavior equivalent to short circuiting with no recursion whatsoever.
#include <type_traits>
#include <tuple>
namespace detail
{
template <typename value, typename tuple>
struct has_type;
template <typename value, typename... xs>
struct has_type<value, std::tuple<xs...>>
@dgodfrey206
dgodfrey206 / tuple_fold.cpp
Last active August 29, 2015 14:07
A right heterogeneous fold on variadic tuples.
#include <tuple>
#include <type_traits>
#include <utility>
#include <integer_sequence>
template <class F, class Tuple, size_t... I>
auto tuple_map_impl(F f, Tuple&& t, index_sequence<I...>)
{
return std::make_tuple(f(std::get<I>(std::forward<Tuple>(t)))...);
}
@dgodfrey206
dgodfrey206 / compose.cpp
Last active August 29, 2015 14:07
Allows a functional style syntax for composing functions
#include <functional>
template<class T>
struct wrap_impl
{
wrap_impl(T&& x) : data(std::forward<T>(x)) { }
T get() const & { return data; }
T&& get() && { return std::move(data); }
private:
@dgodfrey206
dgodfrey206 / two-stack.cpp
Last active April 15, 2022 15:40
Using Dijkstra's two-stack algorithm to parse simple arithmetic expressions
#include <iostream>
#include <string>
#include <stack>
void calculate(std::stack<int>& numbers, std::stack<char>& operators);
int main()
{
std::stack<int> numbers;
std::stack<char> operators;
@dgodfrey206
dgodfrey206 / filename.cpp
Created November 6, 2014 17:05
A facet that holds a stream's filename
#include <iostream>
#include <fstream>
struct filename_facet : std::locale::facet
{
private:
std::string m_name;
public:
filename_facet(std::string const& name, std::size_t refs = 0)
: std::locale::facet(refs)
@dgodfrey206
dgodfrey206 / Employee.cpp
Created November 6, 2014 18:56
Employee class with I/O
#include <iostream>
#include <sstream>
#include <string>
#include <boost/io/ios_state.hpp>
template <class cT>
class whitespace_fmt;
template <>
class whitespace_fmt<char> : public std::ctype<char>
@dgodfrey206
dgodfrey206 / alphafmt.cpp
Created November 14, 2014 00:45
Facet that ignores all alphabetical characters
#include <iostream>
#include <sstream>
class whitespace_fmt : public std::ctype<char>
{
mask table[table_size];
public:
whitespace_fmt(size_t refs = 0)
: std::ctype<char>(table, false, refs)
{