Skip to content

Instantly share code, notes, and snippets.

View lichray's full-sized avatar

Zhihao Yuan lichray

View GitHub Profile
@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 / 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 / .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 --
@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 / 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 / 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 / rsynclib.h
Last active August 29, 2015 14:16
librsync job reader & writer
/*
* Copyright 2015 Rackspace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@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 / 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; });