Skip to content

Instantly share code, notes, and snippets.

View airglow923's full-sized avatar
🪖
National Service

Hyundeok Park airglow923

🪖
National Service
  • South Korea
  • 07:40 (UTC +09:00)
View GitHub Profile
@airglow923
airglow923 / entropy.cpp
Last active September 4, 2020 18:11
Calculate entropy using modern C++
#include <cassert>
#include <cmath>
#include <concepts>
namespace phd {
template<typename T>
concept numeric = std::integral<T> || std::floating_point<T>;
template<numeric Arg, std::integral Base>
@airglow923
airglow923 / AnyMap.cpp
Created October 9, 2020 07:39
Map wrapper using class template argument deduction (CTAD).
#include <map>
#include <unordered_map>
template<
template<typename, typename> typename Map,
typename K,
typename V
>
class AnyMap {
public:
@airglow923
airglow923 / convert_each_line_to_hash.sh
Created October 25, 2020 08:30
Convert each line to hash
#!/bin/bash
usage() {
echo "Usage: $0 -i [INPUT] -o [OUTPUT] -a [HASH_ALGORITHM]"
1>&2;
exit 1;
}
input=""
output=""
@airglow923
airglow923 / read_random_line_from_file.py
Created October 25, 2020 08:31
Read random lines from a file
#!/usr/bin/python3
import sys
import getopt
import codecs
import secrets
import hashlib
from typing import List
def usage():
@airglow923
airglow923 / clean-up-latex.sh
Created December 3, 2020 02:22
Clean up the LaTeX related files other than .pdf and .tex.
#!/bin/bash
usage() {
echo "Usage: $0 [FILENAME]"
1>&2;
exit 1;
}
if [ "$#" -ne 1 ]; then
echo "Only a filename is needed."
@airglow923
airglow923 / tmux-kill-unattached.sh
Last active December 18, 2020 08:28
Shell commands to kill detached (unattached) tmux sessions
#!/bin/bash
TMUX_UNATTACHED=$(tmux ls | grep -E -v '\(attached\)$')
while read session; do
tmux kill-session -t "${line%%:*}"
done <<< $TMUX_UNATTACHED
@airglow923
airglow923 / constexpr-pow.cpp
Created December 18, 2020 15:07
Exponents at compile time
namespace {
using LL = signed long long int;
template <LL N, LL X>
struct PowerOf {
static constexpr LL value_ = N * PowerOf<N, X - 1>::value_;
};
template <LL N>
@airglow923
airglow923 / constexpr-factorial.cpp
Last active December 19, 2020 07:20
Factorial at compile time
#include <concepts>
using ULL = unsigned long long int;
template <std::integral auto I>
struct Factorial {
static constexpr ULL value = I * Factorial<I - 1>::value;
};
template <>
@airglow923
airglow923 / swap-file.sh
Created December 24, 2020 05:02
Swap file using mv
#!/bin/sh
FILE1=$1
FILE2=$2
output_error() {
echo "$1"
1>&2;
exit 1;
}
@airglow923
airglow923 / jq-inplace.sh
Last active January 2, 2021 15:12
jq but edit json inplace
#!/bin/sh
# The first argument should be json string that would be fed into jq.
# The second argument should be json filename.
# create temporary file
TEMP=$(mktemp)
# remove temoorary file in case the program terminates unexpectedly
trap "{ rm -f $TEMP; }" EXIT