Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / infix2postfix.cpp
Last active February 6, 2016 18:53
Infix to Postfix (Reverse Polish Notation)
#include <iostream>
#include <cstring>
#include <stack>
uint8_t precedence[256];
bool HasHigherPrecedence(char a, char b) {
return precedence[(uint8_t)a] < precedence[(uint8_t)b];
}
#include <iostream>
#include <utility>
#include <tuple>
namespace detail {
template<class... Args>
struct stream_bump {
std::tuple<Args...> args;
template<class F>
void operator->*(F&& f) && {
@dgodfrey206
dgodfrey206 / rc.cpp
Created June 21, 2015 21:16
Reference collapsing
#include <utility>
template<class T> struct TD;
template<class T> void f(T&& x) { TD<decltype(x)>(); }
// T & = T&
// T && = T&&
// T& & = T&
// T& && = T&
@dgodfrey206
dgodfrey206 / word_inserter.cpp
Last active March 26, 2022 16:40
Word inserter
#include <iostream>
#include <sstream>
#include <algorithm>
template<class charT>
struct word_inserter_impl {
word_inserter_impl(int words, std::basic_string<charT>& str, charT delim)
: str_(str)
, delim_(delim)
, words_(words)
@dgodfrey206
dgodfrey206 / colornode.cpp
Last active March 26, 2022 16:41
HackerRank
#include <string>
#include <iostream>
#include <initializer_list>
enum struct Color {
red,blue,orange,purple,green,black,yellow,invalid=-1
};
Color FromStringToColor(std::string const& str) {
return ((str == "red") ? Color::red :
@dgodfrey206
dgodfrey206 / pair.cpp
Last active March 26, 2022 16:41
HackerRank2
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>
namespace detail {
bool isMatchingPair(char c1, char c2) {
return std::isupper(c1) && std::tolower(c1) == c2;
}
}
@dgodfrey206
dgodfrey206 / spartan.cpp
Last active March 26, 2022 16:42
HackRank
#include <string>
#include <algorithm>
#include <map>
#include <cassert>
namespace detail {
std::map<char, unsigned int> numerals = { {'I', 1}, {'E', 3}, {'V', 5}, {'X', 10}, {'L', 50} };
unsigned int numeral_rank(char c) {
return numerals[c];
@dgodfrey206
dgodfrey206 / tools.cpp
Last active August 29, 2015 14:21
C++14 Metafunction tools
#include <utility>
#include <tuple>
#include <cassert>
struct type_erasure { };
template<class T>
struct wrapper : type_erasure {
wrapper(T&& w) : w_(std::forward<T>(w)) { }
T&& w_;
@dgodfrey206
dgodfrey206 / ToRBGA.cpp
Last active August 29, 2015 14:21
Converts ARGB to RGBA
#include <iostream>
#include <bitset>
#include <vector>
#include <algorithm>
template<class T>
T Rotate(T i) {
return (i << 1) | (i >> sizeof(i));
//return ((i & (1 << (sizeof(T) - 1)) >> sizeof(T)) | (i << 1));
}
@dgodfrey206
dgodfrey206 / tostring.cpp
Last active March 26, 2022 16:43
ToString for arbitrary types
#include <iostream>
#include <cstring>
#define TO_STRING_FOR_CLASS(X)\
template<class T>\
const char* ToString();\
\
template<>\
const char* ToString<X>() {\
static const char* str = #X;\