Skip to content

Instantly share code, notes, and snippets.

View cleoold's full-sized avatar
🤔
thonking

midori cleoold

🤔
thonking
View GitHub Profile
@cleoold
cleoold / sformat.hpp
Last active May 24, 2021 07:50
a dirty enough string.format() function for pre-c++20 for copy pasta
// string format
// by cos 2020. licensed under MIT
#pragma once
#include <string>
#include <sstream>
namespace detail {
template<class CharT>
void format_impl(std::basic_ostringstream<CharT> &oss, const CharT *s) {
@cleoold
cleoold / logiccircuit.cpp
Last active July 4, 2020 10:24
simple publisher-observer logic gate simulation
#include <iostream>
#include <vector>
#include <string>
// observer-based logic gate simulation
// things are done in a naive way. one may have stack overflow
// if events invoke each other
// interfaces
@cleoold
cleoold / vector_inserter.hpp
Last active August 26, 2020 00:10
dirty convinient std::vector inserter
#pragma once
#include <vector>
// inspiration: boost/assign/std/vector.hpp
template<class VT> struct vector_inserter {
VT &vec;
template<class Arg> auto &operator,(Arg &&v) {
vec.emplace_back(std::forward<Arg>(v));
return *this;
}
@cleoold
cleoold / lambda_traits.hpp
Last active April 24, 2021 14:04
detect return, param type of a C++ functor (or lambda)
/* Lambda traits (c) cos Licensed under BSD 2-Clause */
#pragma once
#include <tuple>
namespace detail {
template<typename R, typename ...Args>
struct lambda_traits_base_noconst {
using result_type = R;
using args_type = std::tuple<Args...>;
template<size_t idx> using arg_type_at = std::tuple_element_t<idx, args_type>;
@cleoold
cleoold / treewithuniqptr.cpp
Created May 30, 2020 03:46
tree implementation with strict ownership
#include <functional>
#include <iostream>
#include <memory>
#include <queue>
#include <vector>
template<typename V>
class Tree;
template<typename V>
@cleoold
cleoold / acc.cpp
Last active April 7, 2020 08:59
C++ accumulating template arguments
// -std=c++17 (auto parameter, inline variable)
// x64msvc>=v19.20, x64clang>=5.0.0, x64gcc>=8.1
#include <utility> // integer_sequence
namespace num {
template <typename T, T n>
struct _pack {
using type = _pack;
using value_type = T;
static constexpr T value = n;
@cleoold
cleoold / test.cpp
Last active October 12, 2023 08:51
quicksort in C++ template metaprogramming
// -std=c++11
// licensed under MIT
// 2020 cos https://opensource.org/licenses/MIT
#include <iostream>
namespace li {
template <int n> struct int32 {
using type = int32;
static constexpr int value = n;
};
@cleoold
cleoold / advancedtype.ts
Last active May 18, 2020 21:36
remember type knowledge
/** https://www.typescriptlang.org/docs/handbook/advanced-types.html */
/** intersection type */
/** returns a new object with properties coming from two given objects */
const merge = <T extends object, S extends object>(first: T, second: S): T & S => {
const res: Partial<T & S> = {};
for (const prop in first)
if (first.hasOwnProperty(prop))
@cleoold
cleoold / index.html
Last active February 9, 2020 23:53
Two plain CSS buttons
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Button</title>
<link href="https://fonts.googleapis.com/css?family=Raleway:600&display=swap" rel="stylesheet">
</head>
<style>
@cleoold
cleoold / index.html
Created February 6, 2020 06:37
React Counter example 2
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Button</title>
<link href="https://fonts.googleapis.com/css?family=Raleway&display=swap" rel="stylesheet">
</head>