Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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)
@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.c
Last active April 2, 2019 17:13
TCP_DEFER_ACCEPT test program for client
#include <stdio.h> //printf(), fprintf(), perror()
#include <sys/socket.h> //socket(), connect(), recv()
#include <arpa/inet.h> // struct sockaddr_in, struct sockaddr, inet_ntoa(), inet_aton()
#include <stdlib.h> //atoi(), exit(), EXIT_FAILURE, EXIT_SUCCESS
#include <string.h> //memset()
#include <unistd.h> //close(), sleep()
#include <linux/tcp.h> //TCP_DEFER_ACCEPT
#define MSGSIZE 32
#define MAX_MSGSIZE 1024
@nishidy
nishidy / server.c
Created April 2, 2019 15:18
TCP_DEFER_ACCEPT test program for server
#include <stdio.h> //printf(), fprintf(), perror()
#include <sys/socket.h> //socket(), connect(), recv()
#include <arpa/inet.h> // struct sockaddr_in, struct sockaddr, inet_ntoa(), inet_aton()
#include <stdlib.h> //atoi(), exit(), EXIT_FAILURE, EXIT_SUCCESS
#include <string.h> //memset()
#include <unistd.h> //close()
#include <linux/tcp.h> //TCP_DEFER_ACCEPT
#include <time.h> //
#define MSGSIZE 32
@nishidy
nishidy / cs.sh
Last active August 18, 2017 01:19
Convenient Search command
#!/bin/bash
if [ -z "$*" ]; then
echo "cs [OPTIONS] DIR"
echo " ORDER BY OPTIONS:"
echo " -f : file count"
echo " -s : file size"
echo " -m : file modification time"
echo " -j : total size of files just under a directory"
echo " -a : total size of files all under a directory"