Skip to content

Instantly share code, notes, and snippets.

@BamboWu
BamboWu / empty_constructor.cpp
Created January 24, 2021 01:18
/posts/languages/2021-1-23-C++EmptyConstructor.md
#include <iostream>
class Kernel {
int cnt;
public:
Kernel() {
std::cout << "Empty constructor\n";
}
Kernel(const int i) : cnt(i) {
std::cout << "Initialize constructor\n";
@BamboWu
BamboWu / unordered_map_vs_map.cpp
Last active December 11, 2019 03:40
/posts/languages/2019-11-30-C++containers
#include <iostream>
#include <iomanip>
#include <cstdint>
#include <map>
#include <unordered_map>
#include <assert.h>
const int lens[] = {1, 256, 65536, 16777216};
__inline__ uint64_t rdtsc() {
@BamboWu
BamboWu / vercmp.py
Created September 26, 2019 22:13
/posts/linux/2019-9-26-FixBrokenPackages
#!/usr/bin/env python3
import re
import sys
import json
import argparse
def ver_cmp(ver_list):
latest = 0
for idx in range(1, len(ver_list)):
@BamboWu
BamboWu / jordan.cxx
Created May 7, 2019 05:51
/posts/HPC/2019-5-5-MPIFoundation
#include <iostream>
#include <mpi.h>
#define MPI_CHECK(expr, err_str) \
{ int err_no = expr; \
if (err_no) { \
std::cout << "MPI error " << err_no << ": " << err_str << std::endl; \
return err_no; \
} \
}
@BamboWu
BamboWu / lambda_syntax_example.cpp
Last active April 25, 2019 15:48
/posts/languages/2019-3-11-C++Lambda
auto eq = [vid](int dst) -> bool { return dst == vid; };
@BamboWu
BamboWu / Kosaraju.cpp
Created April 25, 2019 15:45
/posts/graph/2019-3-10-StronglyConnectedComponent
#include <iostream>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
#define MAX 101
struct Graph {
vector<int> * v;
vector<int> * vi;
@BamboWu
BamboWu / DFS.cpp
Created April 25, 2019 15:40
/posts/graph/2018-10-19-TopologicalSort
int * topoSort(vector<int> graph[], int N)
{
int * p = new int[N]; // caller should delete after use
int idx = N - 1; // fill the array from back
bool * visited = new bool[N];
// initialize all vertices as unvisited
for (int i = 0; i < N; ++i) {
visited[i] = false;
}
// DFS starts from all vertices
@BamboWu
BamboWu / basic_example.cpp
Created April 25, 2019 15:30
/posts/languages/2018-10-19-C++DynamicMemory
#include <iostream>
int main() {
int * num = new int; // pointer = new type
std::cout << "Input number of numbers: ";
std::cin >> *num; // get a number at runtime
if (0 >= *num) {
return - 1;
}
//float * accs = new float[*num]; // pointer = new type [number_of_elements]
@BamboWu
BamboWu / BFS.cpp
Created April 25, 2019 15:26
/posts/graph/2018-10-18-WeaklyConnectedComponent
/* BFS (label propogation) */
int findCircleNum(vector<vector<int>>& M) {
int num = M.size();
vector<int> root;
int ans = 0;
// initial labels to vertices' ids
for (int i = 0; i < num; ++i) {
root.push_back(i);
}
// let each vetex propogate label in a BFS way
@BamboWu
BamboWu / Cantor.hpp
Created April 25, 2019 15:21
/posts/graph/2018-7-21-PairKeyMap
uint64_t Cantor(uint64_t x, uint64_t y) {
uint64_t res;
res = (x + y) * (x + y + 1) / 2 + x;
return res;
}