Skip to content

Instantly share code, notes, and snippets.

View edingroot's full-sized avatar

Chi Chang edingroot

  • Yelp
  • London, United Kingdom
View GitHub Profile
@edingroot
edingroot / ImageScalingTest.kt
Last active June 14, 2024 11:55
Test scaling images with different libraries
package org.example
import com.sksamuel.scrimage.ImmutableImage
import com.sksamuel.scrimage.ScaleMethod
import com.sksamuel.scrimage.nio.ImageIOReader
import com.sksamuel.scrimage.nio.JpegWriter
import net.coobird.thumbnailator.Thumbnails
import net.coobird.thumbnailator.resizers.configurations.ScalingMode
import java.io.ByteArrayOutputStream
import java.io.File
@edingroot
edingroot / bash_strict_mode.md
Created February 10, 2024 12:26 — forked from mohanpedala/bash_strict_mode.md
set -e, -u, -o, -x pipefail explanation
#!/bin/sh
tstart=$(date +%s%N)
cstart=$(cat /sys/fs/cgroup/cpu/cpuacct.usage)
sleep 5
tstop=$(date +%s%N)
cstop=$(cat /sys/fs/cgroup/cpu/cpuacct.usage)
bc -l <<EOF
@edingroot
edingroot / clean_code.md
Created March 17, 2022 05:50 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

CC := g++
INCLUDE_PATH = -Isrc/http_server/core
CPPFLAGS := -std=c++14 $(INCLUDE_PATH)
HTTP_SERVER_SOURCES = $(shell find ./src/http_server -type f -name '*.cpp')
HTTP_SERVER_OBJS = $(HTTP_SERVER_SOURCES:%.cpp=%.o)
CONSOLE_CGI_SOURCES = $(shell find ./src/console_cgi -type f -name '*.cpp')
CONSOLE_CGI_OBJS = $(CONSOLE_CGI_SOURCES:%.cpp=%.o)
@edingroot
edingroot / ParallelFlowableLimit.java
Last active July 29, 2018 08:09
RxJava ParallelFlowable with throughput limitation
// PS. Another approach with Observable: https://stackoverflow.com/a/36383691
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
public class ParallelFlowableLimit {
public static void main(String[] args) {
// Flowable.range(0, 10)
Flowable.create(emitter -> {
/**
* Numerical Computation HW1.2 / by Chi Chang
* Ref:
* http://en.cppreference.com/w/cpp/numeric/math/fpclassify
* http://en.cppreference.com/w/cpp/types/numeric_limits/signaling_NaN
* https://en.wikipedia.org/wiki/NaN
*/
#include <iostream>
#include <cmath>
function fract(x) {
return x - Math.floor(x);
}
function packFloat(x) {
let s = x > 0 ? 1 : -1;
let e = Math.floor(Math.log2(s * x));
let m = s * x / Math.pow(2, e);
return [
Math.floor(fract((m - 1) * 256 * 256) * 256),
@edingroot
edingroot / DijkstraRouting.cpp
Last active January 4, 2018 17:04
Dijkstra’s Routing Algorithm (Homework of "Introduction to Computer Network")
/**
* NOTE: node id should start from 1
* topo.txt format (see the end of this file for example):
* #row id1 id2 cost
* 0 1 2 2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
public class Main {
public static void main(String[] args) {
int num = -32700;
// Signed int to 2 bytes (32767 ~ -32768)
byte b0, b1;
b1 = (byte) ((num & 0xff00) >> 8);
b0 = (byte) (num & 0xff);