Skip to content

Instantly share code, notes, and snippets.

View Liam0205's full-sized avatar
🎯
Focusing

Liam Huang Liam0205

🎯
Focusing
View GitHub Profile
@Liam0205
Liam0205 / install_vim.sh
Created November 2, 2018 03:17
Compile and install VIM on CentOS 6.x without root.
#!/bin/bash
git clone https://github.com/vim/vim.git --depth=1
cd vim
make -p $HOME/local/bin
./configure --prefix=$HOME/local/vim --enable-multibyte --with-tlib=tinfo --enable-pythoninterp --enable-rubyinterp --with-ruby-command=/usr/bin/ruby --with-features=huge
make -j24
make install
cd $HOME/local/bin
for file in $(ls $HOME/local/vim/bin); do ln -sf $HOME/local/vim/bin/$file $file; done
@Liam0205
Liam0205 / cstring_switch.cc
Last active July 29, 2021 02:15
Apply C style string in switch-case clause in C++. (C++11 or above required)
#include <iostream>
#include <string>
using hash_t = size_t;
constexpr hash_t prime = 0x100000001B3ull;
constexpr hash_t basis = 0xCBF29CE484222325ull;
hash_t hash_run_time(const char* str) {
hash_t ret = basis;
@Liam0205
Liam0205 / displayfile
Created April 23, 2018 15:14
displayfile (Skim)
#!/bin/bash
# displayfile (Skim)
#
# Usage: displayfile [-r] [-g] PDFFILE
#
# Modified from "displayline" to only revert the file, not jump to a given line
#
if [ $# == 0 -o "$1" == "-h" -o "$1" == "-help" ]; then
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{lfonts}{
basicstyle = \footnotesize\ttfamily,
stringstyle = \color{purple},
keywordstyle = \color{blue!60!black}\bfseries,
commentstyle = \color{olive}\scshape,
}
\lstdefinestyle{lnumbers}{
@Liam0205
Liam0205 / distinct_random.cc
Last active February 2, 2018 12:01
generate distinct random numbers in C++.
#include <iostream>
#include <unordered_set>
#include <random>
template <typename IntType = int>
class distinct_uniform_int_distribution {
public:
using result_type = IntType;
private:
@Liam0205
Liam0205 / MersenneTwister.py
Created January 25, 2018 04:40
A demo implementation of Mesenne Twister PRNG in Python.
class MersenneTwister:
__n = 624
__m = 397
__a = 0x9908b0df
__b = 0x9d2c5680
__c = 0xefc60000
__kInitOperand = 0x6c078965
__kMaxBits = 0xffffffff
__kUpperBits = 0x80000000
__kLowerBits = 0x7fffffff
\documentclass{article}
\usepackage{amsmath}
\newcommand{\arbbv}[3][0]{\ensuremath{#2_{#1}, #2_{#1}, \ldots, #2_{#3}}}
\begin{document}
\[
\arbbv{a}{n}.
\]
\end{document}
@Liam0205
Liam0205 / socket_client.c
Created January 19, 2018 01:34
A "hello world"-demo for Inter-Process Communication by the use of socket. Borrowed code from https://users.cs.cf.ac.uk/Dave.Marshall/C/node28.html.
/* borrowed from https://users.cs.cf.ac.uk/Dave.Marshall/C/node28.html */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NSTRS 3 /* no. of strings */
@Liam0205
Liam0205 / crack_private.cc
Created January 15, 2018 11:06
Crack the access control of C++ class by the use of virtual table.
#include <stddef.h>
#include <iostream>
class Base {
public:
virtual void f() {
std::cout << "Your are calling Base::f (public)." << std::endl;
}
private:
@Liam0205
Liam0205 / offsetof.cc
Created January 10, 2018 10:43
A customized `offsetof` implement, in modern C++.
#include <stdio.h>
#define offsetof(s, m) reinterpret_cast<size_t>(&reinterpret_cast<const volatile char&>(\
static_cast<s*>(nullptr)->m))
struct S {
char c;
double d;
char cc;
};