Skip to content

Instantly share code, notes, and snippets.

View hare1039's full-sized avatar
:shipit:
わかります

Ryan Yang (hare1039) hare1039

:shipit:
わかります
View GitHub Profile
#include <iostream>
// Just A CRTP pratices
template<typename Derived>
class enable_compare_operators
{
public:
bool operator == (enable_compare_operators<Derived> const & rhs) const
{
@hare1039
hare1039 / calc_e.cpp
Created July 1, 2018 17:25
Compile time calcuating "e (mathematical constant: 2.71828)" in C++17
// largely inspire by https://monoinfinito.wordpress.com/series/introduction-to-c-template-metaprogramming/
#include <iostream>
// represent a fraction (upper/downer)
template <int upper, int downer>
struct frac
{
static constexpr int up = upper;
static constexpr int down = downer;
#include <iostream>
#include <type_traits>
namespace functional_list
{
struct NIL
{
using head = NIL;
#include <iostream>
#include <vector>
#include <type_traits>
template<typename T>
struct can_string_cat_impl
{
template <typename Operand>
static auto test(Operand o) -> decltype(std::declval<std::string>() += o);
template <typename...>
@hare1039
hare1039 / map.cpp
Created November 3, 2018 17:33
An mat impl without using for loops
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
template<typename T = int>
class mat
{
T width = 0, height = 0;
std::vector<std::vector<T>> hold;
@hare1039
hare1039 / c++labmda_example.cpp
Created December 4, 2018 03:36
lambda example
#include <iostream>
#include <functional>
#include <memory>
std::function<int(void)> create_counter(int start)
{
std::shared_ptr<int> ptr = std::make_shared<int>(start);
std::function<int(void)> inc_one = [ptr] () {
return (*ptr)++;
};
@hare1039
hare1039 / RAP.cpp
Last active December 23, 2018 05:48
dp recuesive example (Resource Allocation Problem)
#include <array>
#include <map>
#include <limits>
#include <iostream>
using course = int;
using days = int;
using score_table = std::array<std::array<int, 5>, 4>;
inline
@hare1039
hare1039 / 030.cpp
Created December 31, 2018 04:25
leetcode 030
#include <vector>
#include <algorithm>
#include <iostream>
#include <string_view>
std::vector<int> findSubstring(std::string s, std::vector<std::string>& words)
{
std::string::size_type word_length = words.front().length();
std::sort(words.begin(), words.end());
@hare1039
hare1039 / beautiful_switch.cpp
Last active October 8, 2019 14:41
A string switch impl in c++
#include <iostream>
// based on https://stackoverflow.com/a/46711735/5921729
constexpr inline
long long int hash(char const * str, int h = 0)
{
return (!str[h] ? 5381 : (hash(str, h+1)*33) ^ str[h] );
}
constexpr inline
@hare1039
hare1039 / cpp_multiple_argument_max.cpp
Created March 1, 2019 11:36
A gist of doing multiple argument max
template<typename T>
T max (T&& t) {return t;}
template<typename T, typename ... Types>
auto max(T && t, Types && ...args)
{
return std::max(t, max(args...));
}