Skip to content

Instantly share code, notes, and snippets.

View lichray's full-sized avatar

Zhihao Yuan lichray

View GitHub Profile
@lichray
lichray / version.py
Last active November 18, 2017 03:51 — forked from ludwigschwardt/version.py
Produce a setuptools-compatible package version number based on git tags
"""Calculate the current package version number based on git tags.
If possible, use the output of `git describe` modified to conform to the
versioning scheme that setuptools uses (see PEP 386). Releases must be
labelled with annotated tags (signed tags are annotated) of the following
format:
v<num>(.<num>)+ [ {a|b|c|rc} <num> (.<num>)* ]
If `git describe` returns an error (likely because we're in an unpacked copy
@lichray
lichray / static_if.cc
Created September 13, 2014 02:19
Implement static_if using C11 generic selection
#include <type_traits>
#include <tuple>
#include <iostream>
// Link: https://github.com/aeyakovenko/notes
//count arguments
//COUNT_ARGS :: ... -> Int
#define COUNT_ARGS(...) COUNT_ARGS_(,##__VA_ARGS__,6,5,4,3,2,1,0)
#define COUNT_ARGS_(z,a,b,c,d,e,f,cnt,...) cnt
@lichray
lichray / 203.cc
Created March 24, 2017 22:35
"Two star programming" with no star
#include <functional>
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
auto ls = std::ref(head);
while (ls != nullptr) {
if (ls.get()->val == val) {
auto p = ls.get();
@lichray
lichray / generic_literals.cc
Created February 13, 2016 11:30
Produce type-generic character and string literals
#include <type_traits>
#include <utility>
namespace etude {
template<class... Fs>
struct overloaded_function_impl_;
template<>
struct overloaded_function_impl_<> {
template<class... Args,
@lichray
lichray / kindle.tcsh
Created October 18, 2012 02:55
A tcsh alias to control Kindle on FreeBSD
# usage: kindle eject|start
# The 'start' command works for Kindle, but I saw an error message.
# However, I'm not silencing it here.
alias kindle 'camcontrol devlist | grep Kindle |'\
'sed -nE '\''s/^.*(pass[[:digit:]]+).*$/\1/p'\'' |'\
'xargs -J% camcontrol \!:1 % -E'
# Any brief command of camcontrol(8) is supported actually; 'eject'
# and 'start' are enough.
complete kindle 'p/1/(eject start)/'
@lichray
lichray / tie_from.cc
Created September 14, 2013 15:37
Simulate std::tie's functionality for the input iterators.
#include <tuple>
template <typename Iter, typename T1>
inline auto tie_from(Iter it, T1& t1)
-> std::tuple<T1&>
{
t1 = *it;
return std::tuple<T1&>(t1);
}
@lichray
lichray / make_vector.cc
Created August 16, 2013 02:37
Use variadic template instead of initializer_list to handle movable objects.
#include <type_traits>
#include <vector>
template <typename V, typename T1>
inline void back_pusher(V& v, T1 t1)
{
v.push_back(std::move(t1));
}
template <typename V, typename T1, typename... T2>
@lichray
lichray / less.in
Created June 25, 2013 21:26
Less configuration file; compile it with lesskey.
#line-edit
^A home
^E end
\ef word-right
\eb word-left
\ed word-delete
^W word-backspace
#env
LESS = -i
#include <limits>
#include <climits>
#include <type_traits>
template <int bit>
struct fill_bits
{
template <typename Int>
static constexpr auto apply(Int n) -> Int
{
@lichray
lichray / pow2_roundup.cc
Last active December 18, 2015 06:08
Round an integer up to the closest 2's power.
#include <limits>
#include <type_traits>
template <int bit>
struct or_shift
{
template <typename Int>
static constexpr auto apply(Int n) -> Int
{
return or_shift<bit / 2>::apply(n | (n >> bit));