Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#define ___CONCAT(a,b) a ## b
#define ___APPLY(fn,n) ___CONCAT(fn, n)
#define _TYPE_VAL_MAP(t, v) t v
#define _VAL_MAP(t, v) v
#define _MAP0(args...)
#define _MAP1(m, t, v, args...) m(t, v)
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Please provide a hostname or IP...\n");
exit(-1);
@JackyYin
JackyYin / 378.cpp
Created August 3, 2022 06:15
Kth Smallest Element in a Sorted Matrix
class MinHeap {
private:
// value -> (row, col)
vector<tuple<int, int, int>> nodes;
size_t used = 0;
public:
MinHeap(size_t n) {
nodes.resize(n);
}
@JackyYin
JackyYin / strlen.c
Last active March 9, 2022 08:05
Simple test for faster strlen implementation
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
size_t strlen_simple(const char *str)
{
size_t i = 0;
@JackyYin
JackyYin / lrucache.c
Last active February 27, 2022 17:47
Leetcode 146. LRU Cache
struct list_head {
struct list_head *next, *prev;
};
#define list_for_each(pos, head) \
for (pos = (head)->next; !list_is_head(pos, (head)); pos = pos->next)
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#include <stdio.h>
void g()
{
printf("456\n");
}
void f(int a, int b)
{
g();
}
int main() {
g:
.LFB0:
endbr64
subq $8, %rsp
leaq .LC0(%rip), %rdi
call puts@PLT
nop
addq $8, %rsp
ret
global main
section .text
f:
ret
main:
call f
xor rax, rax
ret
@JackyYin
JackyYin / trim_long_repeatitive_string.cpp
Last active November 15, 2021 14:04
A simple algorithm to find and trim long repetitive substring of string
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <iostream>
#define CHECK_REPEAT_THRESHOLD 1000
#define REPEAT_CHAR_THRESHOLD 200
@JackyYin
JackyYin / rlimit_memlock.c
Created October 30, 2021 07:18
Test RLIMIT_MEMLOCK and mlock behavior.
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main() {