Skip to content

Instantly share code, notes, and snippets.

View thynson's full-sized avatar

Xingcan LAN thynson

  • Li Auto
  • Beijing, China
View GitHub Profile
// C++11 sugar for use of ranged-based for over iterator
// e.g.
// for (auto &x : range(begin, end)) { ... }
//
constexpr struct
{
template<typename ForwardIterator>
class range_helper
{
#include <string>
struct A
{
// You need to explicitly define constructor and destructor
union
{
@thynson
thynson / goagent.service
Last active August 29, 2015 13:57
systemd goagent user session
#
# Save this file to $HOME/.local/share/systemd/user/goagent.service
# Install goagent to $HOME/.local/share/goagent
#
# Enable goagent by
# systemctl --user enable goagent
# systemctl --user start goagent
#
[Unit]
Description=GoAgent
@thynson
thynson / k_from_n.js
Last active August 29, 2015 13:57
Get random k numbers from an iterator of arbitrary N length array where N is unknown. (pseudo code)
function(iter, k) {
var array = [];
var count = 0;
for (var val = iter(); val !== undefined; val = iter()) {
count ++;
if (array.length < k)
array.push_back(val);
else {
var luck = rand() % count;
if (luck < k) {
@thynson
thynson / epoll-mt.c
Created April 28, 2014 15:04
epoll behaviour test on multriple threads environment
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This program explains the following behaviour of epoll:
* When multriple threads are waiting on same epoll instance simultaneously,
* I/O event raised from a file registered on the epoll instance with edge
* trigger mode cause exactly one thread to wake up; while with level trigger
* mode cause at least one thread to wake up, depends on the event-consuming
* time and the schedule of threads.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Compile this file with -DUSE_EDGE_TRIGGER to see the behaviour of edge
@thynson
thynson / themes.xml
Created July 29, 2014 13:22
Android themes
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
_ = require 'lodash'
###
aggr = aggregate (['a', 'b'])
aggr ([{ a: 1, b:1, c:1 },
{ a: 1, b:1, c:1 },
{ a: 1, b:1, c:2 },
{ a: 1, b:2, c:1 },
@thynson
thynson / type_enumerator.hpp
Created November 13, 2014 11:08
Type enumerator
/*
* Copyright (C) 2014 LAN Xingcan
* All right reserved
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
@thynson
thynson / version.gradle
Last active August 29, 2015 14:20
Automated Android version code & name
// Git tag must in format of vA.B.CrcD, e.g. v1.0 or v1.0.2rc4,
// where A, B, C, D is major version code, minor version code,
// fix version code and RC number respectively.
// Minor, fix and rc number are optional, and rc number is simply
// ignored when calculating version code
//
// In build.gradle:
//
// apply from: './path/to/version.gradle'
//
@thynson
thynson / mersenne_twister_engine.hpp
Last active July 22, 2016 08:07
Mersenne Twister Randome Number Genrator
/*
* Copyright (C) 2016 LAN Xingcan
* All right reserved
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF