Skip to content

Instantly share code, notes, and snippets.

@nishidy
nishidy / ReflectionTest.java
Last active October 23, 2022 05:21
Java reflection/unsafe
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
class S {
private void privatePrintS () { System.out.println("This is in S."); };
public void publicPrintS () { System.out.println("This is in S."); };
}
@nishidy
nishidy / segmenttree.py
Last active October 10, 2022 04:39
Segment Tree in Python
class SegmentTree:
def __init__(s_):
s_.segtree=[]
s_.lazytree=[]
s_.treelayer=0
s_.capacity=0
s_.datalength=0
s_.compare=max
s_.firstleaf=0
s_.numleafs=0 # alias to firstleaf
@nishidy
nishidy / dijkstra_pq_umap.cpp
Created April 14, 2022 12:20
Dijkstra with unordered_map instead of array
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <queue>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <string>
using namespace std;
@nishidy
nishidy / dijskstra_pq.cpp
Last active April 14, 2022 12:22
Dijkstra with array instead of unordered_map
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int LIMIT = 1e5;
@nishidy
nishidy / qs.cpp
Last active April 8, 2022 01:37
Quick sort in C++ with concise debug message (going from both lowest index and highest index toward the middle)
#include <iostream>
#include <string>
#include <utility>
using namespace std;
void ___m1(string* s, int lo, int hi){
cout << "Let's sort! [" << lo << "," << hi << "] ";
for(int i=0;i<lo;i++) cout << (*s)[i];
cout << "|";
@nishidy
nishidy / quick_sort.py
Last active April 8, 2022 01:24
Quick Sort in Python
class Sorter:
def __init__(self,data):
self.orig = data.copy()
self.data = data
def do_qsort(self):
print("Original data",self.orig)
self.run_qsort(0,len(self.data)-1)
print("Sorted data ",self.data)
@nishidy
nishidy / convertFunctionFromSnakeTo.sh
Last active January 28, 2022 09:35
To convert function name from snake case to camel case or pascal case in go on OS X with bash 3
#!/bin/bash
if [[ $# -eq 1 ]] ;then
case $1 in
-c) option="camel" ;;
-p) option="pascal" ;;
*) echo "This option $1 is not supported."; exit 1 ;;
esac
else
echo "Give an option -c (Camel case) or -p (Pascal case)."
@nishidy
nishidy / client.py
Last active April 29, 2019 12:34
TCP KEEPALIVE and TIME_WAIT
import socket
import time
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('xxxxxxxx', 8080))
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
s.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 35)
s.send(b'GET / HTTP/1.1\r\nHost: localhost')
data = s.recv(1024)
print(data)
@nishidy
nishidy / server.py
Last active April 29, 2019 02:45
TCP_DEFER_ACCEPT test program for server (python)
import socket
QUEUELIMIT = 5
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('',8080))
s.listen(QUEUELIMIT)
s.setsockopt(socket.SOL_TCP, socket.TCP_DEFER_ACCEPT, 1)
while True:
conn, addr = s.accept()
@nishidy
nishidy / client.py
Last active April 2, 2019 17:14
TCP_DEFER_ACCEPT test program for client (python)
import socket
import time
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('127.0.0.1', 8080))
time.sleep(1)
s.send(b'GET / HTTP/1.1\r\nHost: localhost')
data = s.recv(1024)
print(data)