Skip to content

Instantly share code, notes, and snippets.

@shininglion
shininglion / brachless_list.cpp
Created October 4, 2023 08:26
branchless v.s. traditional linked list implementation
#include <iostream>
struct Node {
void* data;
Node* next;
};
class List
{
public:
@shininglion
shininglion / record_and_replay.py
Last active November 23, 2022 01:30
record function calls in python for replay in the future
class CommandWatcher(object):
def __init__(self):
self.cmd_list = []
def __del__(self):
print("=== command list ===")
print(self.__class__.__name__)
print(self._dumpCmd())
# decorator usage
@shininglion
shininglion / exponent_2.cpp
Last active September 13, 2022 12:59
metaprogram for getting exponent part of power of 2
namespace util
{
template <unsigned N>
struct Exp2 {
// N & (~N + 1): get the right most set bit
// ex: 0x1010 -> N & (~N + 1) will obtain 0x0010
// Hence N ^ (N & (~N + 1)) will unset the rightmost set bit
// In the end, this value will be the exponent part of associated with the leftmost set bit
// ex: 0x1010 -> leftmost set bit is 0x1000 -> exponent part is 3 (2^3 = 0x1000)
static constexpr int val = Exp2<N ^ (N & (~N +1))>::val;
@shininglion
shininglion / str_to_var.py
Created January 5, 2022 03:36
Use string to access python variable
def print_local(var_str):
def local_def():
print(local_def)
local_local = 2
symtbl = locals()
print(symtbl)
if var_str in symtbl:
print(symtbl[var_str])
else:
@shininglion
shininglion / Makefile
Last active November 20, 2020 02:55
when header and related shared lib mismatch
CC = g++ -O3
EXE = run
SO = libtestlib.so
all: $(SO) $(EXE)
$(EXE): main.cpp testlib.h
$(CC) -o $@ $< -ldl
$(SO): testlib.cpp testlib.h
@shininglion
shininglion / codeforces-1321C.c
Created March 12, 2020 09:18
Accepted implementation for the problem 1321C in codeforces
/*
* =====================================================================================
*
* Filename: 1321C.cpp
*
* Description: codeforces 1321C. Remove Adjacent
*
* Version: 1.0
* Created: 2020年03月12日
* Revision: none
@shininglion
shininglion / codeforces-1208D.cpp
Created September 5, 2019 15:06
Accepted implementation for the problem 1208D in codeforces
/*
* =====================================================================================
*
* Filename: 1208D.cpp
*
* Description: codeforces 1208D. Restore Permutation
*
* Version: 1.0
* Created: 2019年09月05日
* Revision: none
@shininglion
shininglion / codeforces-1208B.cpp
Created August 31, 2019 07:07
Accepted implementation for the problem 1208B in codeforces
/*
* =====================================================================================
*
* Filename: 1208B.cpp
*
* Description: codeforces 1208B: Uniqueness
*
* Version: 1.0
* Created: 2019年08月31日
* Revision: none
@shininglion
shininglion / type_family.py
Created December 30, 2018 05:14
Create a user-defined type family that allow composition
from enum import Enum
class DatabaseType(Enum):
INTEGER = 'INTEGER'
REAL = 'REAL'
TEXT = 'TEXT'
LIST = 'LIST'
MAP = 'MAP'
class TypeInterface:
@shininglion
shininglion / request_member.cpp
Created December 30, 2018 01:03
Note the comment at line 191 ~ 194 (error: request for member '...' in '...')
#include <string>
#include <unordered_map>
#include <iostream>
/*******************************************************************************
* Interfaces for Database types
*******************************************************************************/
enum DatabaseType {INTEGER, REAL, TEXT, MAP, LIST};