strategy designpattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <string>
#include <iostream>
#include <assert.h>
using namespace std;
class SaleStrategy
{
public:
virtual float calculate(float price, int amount) = 0;
};
class RegularSaleStrategy : public SaleStrategy
{
public:
virtual float calculate(float price, int amount)
{
return price * amount;
}
};
class StudentSaleStrategy : public SaleStrategy
{
public:
virtual float calculate(float price, int amount)
{
const float DISCOUNT = 0.5f; //学生半价
return (1-DISCOUNT) * price * amount;
}
};
class BulkSaleStrategy : public SaleStrategy
{
public:
virtual float calculate(float price, int amount)
{
const float DISCOUNT = 0.3f; //批发折扣
const int BULK_LIMIT = 100; //>=100件按批发价
if (amount >= BULK_LIMIT)
return (1-DISCOUNT) * price * amount;
return price * amount;
}
};
class SaleManager
{
public:
SaleManager(SaleStrategy* pSaleStrategy = NULL) : m_pSaleStrategy(pSaleStrategy) {}
public:
void setStrategy(SaleStrategy* pSaleStrategy)
{
m_pSaleStrategy = pSaleStrategy;
}
float calculateTotal(float price, int amount)
{
assert(m_pSaleStrategy != NULL);
float calResult = m_pSaleStrategy->calculate(price, amount);
cout << "total price is: " << calResult << endl;
return calResult;
}
private:
SaleStrategy* m_pSaleStrategy;
};
int main(int argc, char** argv)
{
SaleStrategy* pSaleStrategy = new RegularSaleStrategy();
SaleStrategy* pStudentSaleStrategy = new StudentSaleStrategy();
SaleStrategy* pBulkSaleStrategy = new BulkSaleStrategy();
SaleManager saleManager(pSaleStrategy);
cout << "Regular Sale Strategy:\n";
saleManager.calculateTotal(2.5f, 100);
saleManager.setStrategy(pStudentSaleStrategy);
cout << "Student Sale Strategy:\n";
saleManager.calculateTotal(2.5f, 100);
saleManager.setStrategy(pBulkSaleStrategy);
cout << "Bulk Sale Strategy:\n";
saleManager.calculateTotal(2.5f, 100);
delete pBulkSaleStrategy;
delete pStudentSaleStrategy;
delete pSaleStrategy;
return 0;
}

singelton designpattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Singelton
{
private:
Singelton(){}
static Singelton* singel;
public:
static Singelton* GetInstance()
{
if(singel == NULL)
{
singel = new Singelton();
}
return singel;
}
};
Singelton* Singelton::singel = NULL;//注意静态变量类外初始化
int main()
{
Singelton* s1=Singelton::GetInstance();
Singelton* s2=Singelton::GetInstance();
if(s1 == s2)
cout<<"ok"<<endl;
else
cout<<"no"<<endl;
return 0;
}

c++

观察者模式

观察者模式定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include<vector>
#include<iostream>
#include<string>
using namespace std;
class Subject;
//观察者抽象类
class Observer{
public:
virtual void update(Subject *base)=0;
protected:
Subject * _subject;
};
//主题抽象类
class Subject{
private:
//观察者容器
vector<Observer *> _observer;
public:
//数据值,可以作为私有数据,然后定义一个借口去返回值,这里为了省事
string msg;
void regiObserver(Observer *obs){
_observer.push_back(obs);
}
void deleObserver(Observer *obs){
vector<Observer *>::iterator it;
for(it = _observer.begin(); it != _observer.end(); it++){
if( *it == obs){
_observer.erase(it);
break;
}
}
}
//通知所有的观察者
void notify(){
vector<Observer *>::iterator it;
for(it = _observer.begin(); it != _observer.end(); it++)
(*it)->update(this);
}
};
//秘书
class SecretarySubject:public Subject{
public:
void set(string s){
msg = s;
notify();
}
};
//打游戏的员工
class Gamesobserver :public Observer{
public:
Gamesobserver(Subject *base):Observer(){
_subject = base;
_subject->regiObserver(this);
}
void update(Subject *base){
msg = base->msg;
display();
}
void display(){
cout<<msg<<",关闭游戏界面"<<endl;
}
private:
string msg;
};
//炒股票的员工
class StockObserver:public Observer{
public:
StockObserver(Subject * base){
_subject = base;
_subject->regiObserver(this);
}
void update(Subject *base){
msg = base->msg;
display();
}
void display(){
cout<<msg<<",关闭炒股界面"<<endl;
}
private:
string msg;
};
int main()
{
SecretarySubject * sec = new SecretarySubject;
Gamesobserver * one = new Gamesobserver(sec);
StockObserver * two = new StockObserver(sec);
sec->set("老板来了");
//因为发现打游戏,开除了one
sec->deleObserver(one);
sec->set("老板来了");
return 0;
}

linux shell chmod

chmod [options] mode files

只能文件属主或特权用户才能使用该功能来改变文件存取模式。mode可以是数字形式或以who opcode permission形式表示。who是可选的,默认是a(所有用户)。只能选择一个opcode(操作码)。可指定多个mode,以逗号分开。

options:

-c,—changes
只输出被改变文件的信息

-f,—silent,—quiet
当chmod不能改变文件模式时,不通知文件的用户

—help
输出帮助信息。

-R,—recursive
可递归遍历子目录,把修改应到目录下所有文件和子目录

—reference=filename
参照filename的权限来设置权限

-v,—verbose
无论修改是否成功,输出每个文件的信息

—version
输出版本信息。

who

u
用户

g

o
其它

a
所有用户(默认)

opcode

+
增加权限

-
删除权限

=
重新分配权限

permission

r

w

x
执行

s
设置用户(或组)的ID号

t
设置粘着位(sticky bit),防止文件或目录被非属主删除

u
用户的当前权限

g
组的当前权限

o
其他用户的当前权限

作为选择,我们多数用三位八进制数字的形式来表示权限,第一位指定属主的权限,第二位指定组权限,第三位指定其他用户的权限,每位通过4(读)、2(写)、1(执行)三种数值的和来确定权限。如6(4+2)代表有读写权,7(4+2+1)有读、写和执行的权限。

还可设置第四位,它位于三位权限序列的前面,第四位数字取值是4,2,1,代表意思如下:

4,执行时设置用户ID,用于授权给基于文件属主的进程,而不是给创建此进程的用户。

2,执行时设置用户组ID,用于授权给基于文件所在组的进程,而不是基于创建此进程的用户。

1,设置粘着位。

实例:

$ chmod u+x file     给file的属主增加执行权限
$ chmod 751 file     给file的属主分配读、写、执行(7)的权限,给file的所在组分配读、执行(5)的权限,给其他用户分配执行(1)的权限
$ chmod u=rwx,g=rx,o=x file 上例的另一种形式
$ chmod =r file     为所有用户分配读权限
$ chmod 444 file      同上例
$ chmod a-wx,a+r file      同上例
$ chmod -R u+r directory   递归地给directory目录下所有文件和子目录的属主分配读的权限
$ chmod 4755   设置用ID,给属主分配读、写和执行权限,给组和其他用户分配读、执行的权限。

局部敏感哈希(LSH)

Goal

Finding Similar Items

  • 在高维空间上找到那些相近的元素。比如,在做微博文本挖掘的时候,会发现很多微博是高度相似的,因为大量的微博都是转发其他人的微博,并且没有添加评论,导致很多数据是重复或者高度相似的。这给我们进行数据处理带来很大的困扰,我们得想办法把找出这些相似的微博,再对其进行去重处理。
    Finding Similar Documents

Steps

Essebtial Steps For Similar Docs

Shingling

Min-Hashing

Convert large sets to short signatures, while preserving similarity.

LSH

General idea: Use a function f(x,y) that tells whether x and y is a candidate pair: a pair of elements whose similarity must be evaluated

我们对签名矩阵按行进行分组,将矩阵分成b组,每组由r行组成.

分组之后,我们对最小签名向量的每一组进行hash,各个组设置不同的桶空间。只要两列有一组的最小签名部分相同,那么这两列就会hash到同一个桶而成为候选相似项。签名的分析我们知道,对于某个具体的行,两个签名相同的概率p =两列的相似度= sim(S1,S2),然后:

  1. 在某个组中所有行的两个签名值都相等概率是p^r;

  2. 在某个组中至少有一对签名不相等的概率是1−p^r;

  3. 在每一组中至少有一对签名不相等的概率是(1−p^r)^b;

  4. 至少有一个组的所有对的签名相等的概率是1−(1−p^r)^b;

于是两列成为候选相似对的概率是1−(1−p^r)^b,它采样值以及曲线如下:
r=5,b=20