Skip to content

Instantly share code, notes, and snippets.

View liuml07's full-sized avatar

Mingliang Liu liuml07

View GitHub Profile
@liuml07
liuml07 / myhadoop
Created March 17, 2017 22:54
Build and Start A Single Node Hadoop Cluster From Source
#!/bin/bash
export HADOOP_HOME=$HOME/Applications/hadoop
main() {
if [ "$1" == "rebuild" ]; then
myhadoop_build
myhadoop_config
rm -f $HADOOP_HOME/logs/*.* # Delete old logs
fi
@liuml07
liuml07 / lca.cpp
Created December 4, 2013 06:55
Find the lowest common ancestor of two tree nodes
TreeNode *LCA(TreeNode *root, TreeNode *p, TreeNode *q) {
if (root == nulltpr || root == p || root == q)
return root;
else {
TreeNode *L = LCA(root->left, p, q);
TreeNode *R = LCA(root->right, p, q);
if (L && R)
return root;
else
return L ? L : R;
@liuml07
liuml07 / reverse_int.cpp
Created December 2, 2013 09:25
Reverse bits in word by lookup table
static const unsigned char BitReverseTable256[256] =
{
# define R2(n) n, n + 2*64, n + 1*64, n + 3*64
# define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
# define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )
R6(0), R6(2), R6(1), R6(3)
};
// reverse 32-bit value, 8 bits at time
unsigned int reverse_int(unsigned int v) {
@liuml07
liuml07 / gcd.cpp
Created October 9, 2013 15:02
The greatest common divisor
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
@liuml07
liuml07 / std::string::ends_with.cpp
Last active February 8, 2022 15:20
The missing std::string::ends_with(string)
bool ends_with(const std::string &str, const std::string &ending) {
if (str.length() < ending.length())
return false;
return str.compare(str.length() - ending.length(), ending.length(), ending) == 0;
}
@liuml07
liuml07 / build_argv.c
Created July 4, 2013 09:56
Build the argv list from command line
int parseline(char *buf, char **argv) {
char *delim;
int argc = 0;
buf[strlen(buf) - 1] = ' ';
while (*buf && (*buf == ' '))
buf++;
while ((delim = strchr(buf, ' '))) {
argv[argc++] = buf;
*delim = '\0';
@liuml07
liuml07 / std::vector::remove_duplicate.cpp
Last active January 20, 2021 18:40
Remove duplicate elements in a std::vector
#include <algorithm>
#include <vector>
#include <set>
template <typename Type>
void remove_duplicate(std::vector<Type>& vec) {
std::sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
@liuml07
liuml07 / std::string::split.cpp
Last active December 17, 2015 22:09
The missing std::string::split() function
vector<string> split(const string &str, const char delim = ' ') {
stringstream ss(str);
vector<string> strvec;
string tmp;
while (std::getline(str, tmp, delim))
strvec.push_back(tmp);
return strvec; // std::move()
}
@liuml07
liuml07 / std::string::starts_with.cpp
Last active February 1, 2019 08:28
The missing std::string::starts_with(string)
inline bool starts_with(const string &big_str, const string &small_str) {
return big_str.compare(0, small_str.length(), small_str) == 0;
}
@liuml07
liuml07 / std::string::replace_all.cpp
Last active April 23, 2016 12:28
The missing std::string::replace_all(str, from, to)
size_t replace_all(string &str, const string &from, const string &to) {
size_t count = 0;
size_t pos = 0;
while ((pos = str.find(from, pos)) != string:npos) {
str.replace(pos, from.length(), to);
pos += to.length();
++count;
}