Skip to content

Instantly share code, notes, and snippets.

View hpcx82's full-sized avatar

hpcx hpcx82

  • Shanghai
View GitHub Profile
@hpcx82
hpcx82 / LazyReference.java
Created May 28, 2012 03:48
A generic lazy reference class
import java.util.concurrent.atomic.AtomicReference;
public class LazyReference{
abstract static class LazyRef<T> {
// BUG1: should be final
// member start with _
private final AtomicReference<T> _ref = new AtomicReference<T>();
public LazyRef()
@hpcx82
hpcx82 / permutate-recur.cpp
Created May 27, 2012 12:17
Use a bitmap(or hashmap), and recursively to get the permutation
#include <iostream>
using namespace std;
// permutate recusively - use a bitmap, index as the permutate value.
// If the target to permutation value doesn't fit in a bitmap, just use a hashmap.
void permutate()
{
static int a[] = {0, 0, 0, 0};
static int o[4];
@hpcx82
hpcx82 / quicksort.cpp
Created May 25, 2012 06:21
quick sort in functional style with: scala, c++, perl, java
#include <iostream>
#include <vector>
using namespace std;
vector<int> concat(const vector<int>& v1, const vector<int>& v2, const vector<int>& v3)
{
vector<int> res = v1;
res.insert(res.end(), v2.begin(), v2.end());
@hpcx82
hpcx82 / SmartPtr.cpp
Created April 21, 2012 00:17
A simple practising smartptr
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
class SmartPtr
{
T* p_;
int* count_;
@hpcx82
hpcx82 / SealedBase.cpp
Created April 21, 2012 00:13
sealed(final) class in C++
#include <iostream>
using namespace std;
// Use 2 language features:
// 1. Derived classes through private inheritance can access base class's protected member, but make it private to its sub classes
// 2. Virtual inheritance will cause the final derived class to call the virtual bases's constructor directly
// so the sealed class has no problem to create instance while its sub class can't create instance.
// This is also another use of CRTP (curious recurring template pattern)
template <class T>
class SealedBase
@hpcx82
hpcx82 / newcode.pl
Created April 16, 2012 13:55
create testing program in a second
#
# This script is to create startup code for different languages
# so you can quickly start your programming.
#
use strict;
use warnings;
use Getopt::Long;
sub help()
@hpcx82
hpcx82 / print1-1000.cpp
Created January 8, 2012 02:47
no loop, no conditional statement, no brute force to print out 1 to 1000
#include <iostream>
#include <functional>
int main()
{
std::function<void(int)> a[2];
// lambda: [&capture](parameters)->return_type {body}
a[0] = [&](int i) {std::cout << i << std::endl; a[i/1000](i+1);};
a[1] = [](int i) {return;};
a[0](1);
sub make_adder
{
  my $addpiece = shift;
  return sub { shift + $addpiece };
}
$f1 = make_adder(20);
&f1(1); # = 21;
&f1(2); # = 22;
using System;
using System.ComponentModel;
using System.Diagnostics;
namespace timeit
{
class Program
{
static int Main(string[] args)
{
template <typename T>
T max(const T& a, const T& b)
{
return a > b ? a : b;
}