2015年1月5日星期一

Java Interface Comparable VS Comparator

Java 对于array和collection都提供了sort的功能,但是对于sort有不同的interface实现方式,一个是comparable一个是comparator,看着太迷糊了,google下区别吧。
原来看他们名字就有hint,一个是something comparable,一个是something to compare a pair。下面是对2者的对比,很到位。

2015年1月4日星期日

Array算法之Longest increasing subsequence (LIS)

    今日在复习Longest increasing subsequence (LIS)的时候在wiki看到一个算法复杂度居然只有nlogn,但是wiki没有讲的太明白,然后又到万能的google一搜索,在princeton讲义pdf学到一个绝逼的算法,我和我的小伙伴都惊呆了。
    玩过接龙poker的人都知道接龙的greedy 算法吧,这算法就是用接龙greedy 算法进化而来。
greedy 算法就是把牌放在最左边并且符合条件的pile
如果牌数字依次是 6, 3, 5, 10, 11, 2, 9, 14, 13, 7, 4, 8, 12
6    5    10    11    14
3    4     9      8     13
2           7             12
这么一看是不是觉得距离获得LIS 不远了,还有一个tricky的地方是在加入每一个牌的时候要保留一个指向前面一个pile最顶端牌的指针,如图:

这样可以顺指针找到lis了。
在找leftmost pile的时候用binary search可以把复杂度降低到nlogn

java 面试速成学习贴

1. 在java里面只有object reference和primitive like int, char是存在stack里面的,new 出来的objects都是在heap
2. local varibles必须赋初值,不然会有compiling error
3. "+"有连接string的功能 print("s"+3+4)    output: s34
                                            print(3+4+"s")       output:7s
4. while(x=y){} is illegal in java, unless x and y are bool type.
5. access array is easier
float f[] = new float[10];
for(float x:f){
    x = 3;
}
6. Random generator.
Random generator = new Random();
int i = generator.nextInt(4);
7.一个class有static先run static的,并且按照顺序,只初始化一次
8.java不能用super.super, 要access grandparent必须通过parent
9.protected类型变量和函数只能被继承类使用
10.多态性. fields and static methods没有多态性,也就是说一旦被super cast就会用super class的value和method
11.interface也可以被继承 fields in interface all static and final
12.初始化一个int list
List s = Arrays.asList(2,2,3,2,2);

2013年7月26日星期五

C++中Static作用和使用方法(转)

在看设计模式的时候,无意中看到singleton使用了static,很好奇static的完整用法,于是就在网上找到下面的一篇好文章。
1、什么是static?
     static 是C++中很常用的修饰符,它被用来控制变量的存储方式和可见性。
 2、为什么要引入static?
     函数内部定义的变量,在程序执行到它的定义处时,编译器为它在栈上分配空间,大家知道,函数在栈上分配的空间在此函数执行结束时会释放掉,这样就产生了一个问题: 如果想将函数中此变量的值保存至下一次调用时,如何实现? 最容易想到的方法是定义一个全局的变量,但定义为一个全局变量有许多缺点,最明显的缺点是破坏了此变量的访问范围(使得在此函数中定义的变量,不仅仅受此函数控制)。
 3、什么时候用static?
     需要一个数据对象为整个类而非某个对象服务,同时又力求不破坏类的封  装性,即要求此成员隐藏在类的内部,对外不可见。
 4、static的内部机制:
    静态数据成员要在程序一开始运行时就必须存在。因为函数在程序运行中被调用,所以静态数据成员不能在任何函数内分配空间和初始化。
     这样,它的空间分配有三个可能的地方,一是作为类的外部接口的头文件,那里有类声明;二是类定义的内部实现,那里有类的成员函数定义;三是应用程序的main()函数前的全局数据声明和定义处。
     静态数据成员要实际地分配空间,故不能在类的声明中定义(只能声明数据成员)。类声明只声明一个类的“尺寸和规格”,并不进行实际的内存分配,所以在类声明中写成定义是错误的。它也不能在头文件中类声明的外部定义,因为那会造成在多个使用该类的源文件中,对其重复定义。
     static被引入以告知编译器,将变量存储在程序的静态存储区而非栈上空间,静态 数据成员按定义出现的先后顺序依次初始化,注意静态成员嵌套时,要保证所嵌套的成员已经初始化了。消除时的顺序是初始化的反顺序。
 5、static的优势:
     可以节省内存,因为它是所有对象所公有的,因此,对多个对象来说,静态数据成员只存储一处,供所有对象共用。静态数据成员的值对每个对象都是一样,但它的值是可以更新的。只要对静态数据成员的值更新一次,保证所有对象存取更新后的相同的值,这样可以提高时间效率。
 6、引用静态数据成员时,采用如下格式: <类名>::<静态成员名> 如果静态数据成员的访问权限允许的话(即public的成员),可在程序中,按上述格式 来引用静态数据成员。
 7、注意事项:
     (1)类的静态成员函数是属于整个类而非类的对象,所以它没有this指针,这就导致 了它仅能访问类的静态数据和静态成员函数。
     (2)不能将静态成员函数定义为虚函数。
     (3)由于静态成员声明于类中,操作于其外,所以对其取地址操作,就多少有些特殊 ,变量地址是指向其数据类型的指针 ,函数地址类型是一个“nonmember函数指针”。
     (4)由于静态成员函数没有this指针,所以就差不多等同于nonmember函数,结果就 产生了一个意想不到的好处:成为一个callback函数,使得我们得以将C++和C-based X W indow系统结合,同时也成功的应用于线程函数身上。
     (5)static并没有增加程序的时空开销,相反她还缩短了子类对父类静态成员的访问 时间,节省了子类的内存空间。
     (6)静态数据成员在定义或说明时前面加关键字static。
     (7)静态数据成员是静态存储的,所以必须对它进行初始化。
     (8)静态成员初始化与一般数据成员初始化不同:
     初始化在类体外进行,而前面不加static,以免与一般静态变量或对象相混淆;
     初始化时不加该成员的访问权限控制符private,public等;
     初始化时使用作用域运算符来标明它所属类;
     所以我们得出静态数据成员初始化的格式:
     <数据类型><类名>::<静态数据成员名>=<值>;
     (9)为了防止父类的影响,可以在子类定义一个与父类相同的静态变量,以屏蔽父类的影响。这里有一点需要注意:我们说静态成员为父类和子类共享,但我们有重复定义了静态成员,这会不会引起错误呢?不会,我们的编译器采用了一种绝妙的手法:name-mangling 用以生成唯一的标志。

    静态数据成员   
        在类中,静态成员可以实现多个对象之间的数据共享,并且使用静态数据成员还不会破坏隐藏的原则,即保证了安全性。因此,静态成员是类的所有对象中共享的成员,而不是某个对象的成员。      
        静态数据成员的使用方法和注意事项如下:   
1、静态数据成员函数的实现中不能直接饮用类说明的非静态成员,可以引用类中说明的静态成员。如果静态成员函数中要引用非静态成员,可以通过对象来引用。
class A
{
public:
  static void func(A & obj);
  intgetval() const; //non-static member function
private:
 intval;
};
voidA::func(A & obj)
{
  int n = obj.getval();
}

2、静态数据成员是静态存储的,它是静态生存期,必须对它进行初始化。   

2011年6月9日星期四

The code to change UIView to images

NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];

// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

- (UIImage *)imageFromView:(UIView *)myView{
if (UIGraphicsBeginImageContextWithOptions!=NULL)
{
UIGraphicsBeginImageContextWithOptions(myView.bounds.size,NO,0.0);
}
else
{
UIGraphicsBeginImageContext(myView.bounds.size);
}
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [[viewImage retain] autorelease];
}

2010年4月18日星期日

Pacemaker simulator

System Design

The system includes three main parts; the signal generator, the DE2 board with PCB board, and Oscilloscope. The signal generator is responsible to send pulse signal to the A/D. And the PCB board has A/D and D/A convertor and is connected with DE2 board by GPIO. The D/A is responsible to send the results to the Oscilloscope.
Algorithm Design

The experiment procedure
The input is set to be a serial of pulse signal, with amplitude 1v, frequency 0.8Hz, and pulse width 60ms. Users can use the switches on the right side to change the value of Ta and Td. When users change the value, they have to press the reset button.

The result is shown as below. We can observe that when the input rising edge happens in the watching period, it can trigger the rising edge interrupt. When it is closed to the previous output pulse, it can’t trigger any.

IM magincian-Vimisoft




This is the main project I did in Vimicro Inc, as a software engineer. IM Magician is a software to be used as a supplementary software for other popular chatting programs, such as QQ, Skype, and MSN, etc. IM Magician can provide various special image effects that can give user a much interesting and interactive video chatting experience. To enhance the user experience of the web camera, IM Magician has provide very powerful image processing features, such as Special Image Effect, Video Frame, Animation Flash, Face Tracking, Masking, and Magic Mirror, etc..
IM Magician not only can be used as a supplementary software, it can also to be used as a standalone PC Camera software, with functions of Capture Still Image, Record Audio, Record Video, and Play Back, it also provides motion-detection based surveillance features.
Basically, it has two main parts. One part is a filter based on Directshow, and all image effects are realized in this filter. Another part is an application based on .Net, which can use the filter and also realize some other functions such as image taking and audio/vedio recording.
I was responsible to write a component to realize different image effect. I got the image algorithms from algorithm team including some demo codes and documents, and realized those algorithms into my filter. I was also responsible to write some image/vedio tools for our products. Users can use those tools to design their own frames, record any part of their screens, and design different masks for face tracking. Besides, I had to maintain our products. If users had problems with our software, I had to solve those bugs.