Skip to content

Instantly share code, notes, and snippets.

View lichray's full-sized avatar

Zhihao Yuan lichray

View GitHub Profile
@lichray
lichray / make_array.cc
Last active August 29, 2023 16:37
Factory function of std::array
#include <array>
#include <functional>
template <typename... T>
using common_type_t = typename std::common_type<T...>::type;
template <typename T>
using remove_cv_t = typename std::remove_cv<T>::type;
template <bool, typename T, typename... U>
@lichray
lichray / visit_at.cc
Last active January 15, 2023 10:45
Access tuple by runtime index (C++20)
#include <functional>
#include <stdexcept>
#include <tuple>
#include <type_traits>
template <int Low, int High, int Mid = (Low + High) / 2>
inline constexpr auto _visit_at = nullptr;
template <int Low, int High, int Mid>
requires(Low > High)
@lichray
lichray / echo_test.c
Created July 12, 2012 19:06
A kqueue(2) example, read from stdin and echo.
/* Copyright (C) 2002 by Jilles Tjoelker */
/* revised by Zhihao Yuan, 2012 */
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <err.h>
#include <fcntl.h>
#include <signal.h>
@lichray
lichray / update-clouddns.py
Created August 23, 2013 21:08
(alpha) Dynamic DNS using Rackspace Cloud DNS
#!/usr/bin/env python
INTERVAL = 300
import clouddns
import requests
try:
from gi.repository import GObject as gobject
except ImportError:
@lichray
lichray / binary_literal.cc
Created November 27, 2012 12:23
User-defined binary literal in C++11
#include <iostream>
// http://stackoverflow.com/questions/537303/binary-literals
template<char... digits>
struct conv2bin;
template <char... digits>
constexpr int operator"" _b() {
return conv2bin<digits...>::value;
}
@lichray
lichray / fraction.cc
Last active November 2, 2018 06:12
Fraction class (MSVC support is experimental)
#include <limits>
#include <ostream>
#include <stdexcept>
#include <system_error>
#include <type_traits>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
@lichray
lichray / iota_n.cc
Last active September 9, 2018 13:18
generate_n is a better iota_n.
#include <algorithm>
#include <vector>
#include <iterator>
#include <iostream>
int main()
{
std::vector<int> v;
// instead of asking for iota_n(std::back_inserter(v), 10, 1);
std::generate_n(back_inserter(v), 10, [n = 0]() mutable { return ++n; });
@lichray
lichray / csv.cc
Created August 10, 2018 05:01
A tiny CSV data reader for online coding tests
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <errno.h>
#include <iterator>
#include <stdlib.h>
namespace csv
{
@lichray
lichray / patch-mylogo
Created September 26, 2012 11:05
Customize xscreensaver's unlock dialog image.
@lichray
lichray / .gitconfig
Created February 7, 2018 20:49
Common git aliases
[alias]
st = status
fix = commit --amend -C HEAD
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative
discard = reset HEAD --