Skip to content

Instantly share code, notes, and snippets.

@BaiGang
BaiGang / levenshtein.pl
Created October 28, 2011 07:19
Levenshtein distance of two strings in Perl.
sub levenshtein_dist {
my ($str1, $str2) = @_;
my ($len1, $len2) = (length $str1, length $str2);
if ($len1 == 0) {
return $len2;
}
if ($len2 == 0) {
@BaiGang
BaiGang / gist:2952423
Created June 19, 2012 05:18
cpp string builder
There is no an efficient StringBuilder class in C++ std library. std::ostringstream could be used as StringBuilder, but it is inefficient, and it use C++ iostream interface, which was proved a bad practice than printf family in C, although it is more "flexible" than printf family. Here are 2 thin wrapper for gnu asprintf family, which maybe more efficient and more readable than ostringstream.
strbuilder.h
// strbuilder.h
#include <stdio.h>
#include <string.h>
// This class is more simple to use, but it should be used for one-time printf
@BaiGang
BaiGang / hotdog.sh
Created July 13, 2012 07:37
A one-liner for extracting Baidu and Sogou's hot search keywords
# baidu realtime hotspot
curl http://top.baidu.com/buzz.php?p=top10 \
| perl -MEncode -pi -e '$_=encode_utf8(decode(gb2312=>$_))' \
| grep "td class=\"key\"" | sed -e 's/^.*_blank\">//g' | sed -e 's/<.*$//g'
# sogou top queries
curl http://top.sogou.com/hotword[0-3].html \
| perl -MEncode -pi -e '$_=encode_utf8(decode(gb2312=>$_))' \
| perl -ne 'chomp; my @titles = ($_ =~ /title=\".*?\"/g); for (my $i = 0; $i < scalar @titles; ++$i) {$titles[$i] =~ s/title=//; $titles[$i] =~ s/\"//g; print "$titles[$i]\n"}'
@BaiGang
BaiGang / perm.cc
Created November 5, 2012 02:36
Generate all permutations
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
void Perm(int m, int n, int* A) {
@BaiGang
BaiGang / armv6_atomic
Created October 15, 2013 14:06
Atomicity encapsulation for android arm devices.
#ifndef _VISION_PLAY_ARMV6_ATOMIC_H_
#define _VISION_PLAY_ARMV6_ATOMIC_H_
#if defined(__ARMEL__) && defined(__linux__)
typedef void (*LinuxKernelMemoryBarrierFunc)(void);
// Using the highly optimized device-specific memory barrier function.
inline void MemoryBarrier() {
(*(LinuxKernelMemoryBarrierFunc)0xffff0fa0)();