Skip to content

Commit 697e134

Browse files
committed
Merge pull request wyh267#1 from xtaci/master
fork
2 parents 1d71a63 + 4946788 commit 697e134

23 files changed

+1520
-350
lines changed

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CC=gcc
33
CPP=g++
44
AR=ar
55
RANLIB=ranlib
6-
CFLAGS= -O2 -Wall -Wno-unused-function
6+
CFLAGS= -g -O2 -Wall -Wno-unused-function
77
SRCDIR = ./src
88
INCLUDEDIR = -I./include -I.
99
DEPS =
@@ -30,7 +30,7 @@ PROGRAMS = m_based \
3030
heap_demo \
3131
interval_tree_demo \
3232
dos_tree_demo \
33-
skip_list_demo \
33+
skiplist_demo \
3434
lcs_demo\
3535
prim_mst_demo \
3636
directed_graph_demo \
@@ -56,7 +56,11 @@ PROGRAMS = m_based \
5656
heap_sort_demo \
5757
kruskal_mst_demo \
5858
LRU_cache_demo \
59-
base64_demo
59+
base64_demo\
60+
max_subarray_demo \
61+
disjoint-set_demo \
62+
relabel_to_front_demo \
63+
btree_demo
6064

6165
all: $(PROGRAMS)
6266

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
服务器端
88
(based on linux/gcc)
99
正确,易于使用和改造, 一个头文件一个算法,并附带一个demo.
10-
(corret! and ease of use, one .header file per algorithm)
10+
(correct! and ease of use, one .header file per algorithm)
1111

1212
####约定 ( Convention ):
1313

1414
1. 一个算法用一个.h文件表示放到include下. ( one .header file per algorithm. )
1515
2. 算法演示的demo程序放到src下. ( one demo per algorithm. )
1616
3. 程序正确通过后,请发起Pull Requests,代码被验证后入库,并在README中发布新算法实现。
1717
(Please Use Fork+Pull Requests !!! Correctness is the most important!)
18+
4. TAB = 4 space. set ts=4 in vim
1819

1920
####已实现 ( Implemented ):
2021

@@ -24,6 +25,7 @@
2425
2D Array
2526
Arbitary Integer
2627
Linear congruential generator
28+
Maximum subarray problem
2729

2830
Bit-Set data structure
2931
Queue data structure
@@ -47,6 +49,7 @@
4749
Interval tree
4850
Prefix Tree(Trie)
4951
*Suffix Tree(未实现)*
52+
B-Tree
5053

5154
Hash by multiplication
5255
Hash table
@@ -58,6 +61,7 @@
5861
Bloom Filter
5962
SHA-1 Message Digest Algorithm
6063
MD5
64+
Base64
6165

6266
Graph data structure
6367
Prim's minimum spanning tree
@@ -68,14 +72,18 @@
6872
Dijkstra's algorithm
6973
Bellman-Ford algorithm
7074
Edmonds-Karp Maximal Flow
75+
Push–Relabel algorithm
7176

7277
Huffman Coding
7378
Word segementation(CHN/GB18030) using HMM and viterbi algorithm.
7479
A* algorithm
7580
K-Means
7681
Knuth–Morris–Pratt algorithm
82+
Disjoint-Set
7783

7884
####贡献者 ( Contributors ) :
7985
Samana : for heavy work of MSVC compatability
8086
wycg1984: for K-Means
8187
xmuliang: for HeapSort, Kruskal MST
88+
wyh267: for base64, LRU
89+
ZhangYou0122 : Push-Relabel algorithm

include/LRU_cache.h

Lines changed: 88 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,80 @@
1-
1+
/*******************************************************************************
2+
*
3+
* /\ | _ _ ._ o _|_ |_ ._ _ _
4+
* /--\ | (_| (_) | | |_ | | | | | _>
5+
* _|
6+
*
7+
* Least Recently Used (LRU):
8+
* discards the least recently used items first. This algorithm requires
9+
* keeping track of what was used when, which is expensive if one wants to make
10+
* sure the algorithm always discards the least recently used item. General
11+
* implementations of this technique require keeping "age bits" for cache-lines
12+
* and track the "Least Recently Used" cache-line based on age-bits. In such an
13+
* implementation, every time a cache-line is used,
14+
* the age of all other cache-lines changes
15+
*
16+
* We use this double list to store the cache like this:
17+
*
18+
* NodeHead <==> Node1 <==> Node2 <==> ... ... <==> NodeN <==>NodeNear
19+
*
20+
* the "NodeHead" and "NodeNear" have no data , them just for head and near.
21+
*
22+
* Two Interface:
23+
* getValue : get the value by key ,and put the Node to first
24+
* putValue : add or update the Node's Key and Value, and then,
25+
* put the Node to first
26+
*
27+
* http://en.wikipedia.org/wiki/LRU_cache#Least_Recently_Used
28+
*
29+
******************************************************************************/
230

331
#ifndef _LRUCACHE_
432
#define _LRUCACHE_
533

6-
734
#include <iostream>
835
#include <string>
936
#include <stdio.h>
1037
#include <stdlib.h>
1138
#include <string.h>
1239

13-
using namespace std;
14-
15-
typedef struct _Node_{
16-
17-
int key;
18-
int value;
19-
20-
struct _Node_ *next;
21-
struct _Node_ *pre;
22-
23-
}CacheNode;
24-
25-
26-
27-
class LRUCache{
28-
29-
public:
30-
31-
LRUCache(int cache_size=10)
32-
{
33-
cache_size_=cache_size;
34-
cache_real_size_=0;
35-
p_cache_list_head=new CacheNode();
36-
p_cache_list_near=new CacheNode();
37-
p_cache_list_head->next=p_cache_list_near;
38-
p_cache_list_head->pre=NULL;
39-
p_cache_list_near->pre=p_cache_list_head;
40-
p_cache_list_near->next=NULL;
41-
40+
namespace alg {
41+
class LRUCache{
42+
typedef struct _Node_{
43+
int key;
44+
int value;
45+
46+
struct _Node_ *next;
47+
struct _Node_ *pre;
48+
49+
} CacheNode;
50+
51+
public:
52+
LRUCache(int cache_size=10) {
53+
cache_size_ = cache_size;
54+
cache_real_size_ = 0;
55+
p_cache_list_head = new CacheNode();
56+
p_cache_list_near = new CacheNode();
57+
p_cache_list_head->next = p_cache_list_near;
58+
p_cache_list_head->pre = NULL;
59+
p_cache_list_near->pre = p_cache_list_head;
60+
p_cache_list_near->next = NULL;
4261
}
43-
~LRUCache()
44-
{
62+
63+
~LRUCache() {
4564
CacheNode *p;
46-
p=p_cache_list_head->next;
47-
while(p!=NULL)
48-
{
65+
p = p_cache_list_head->next;
66+
while(p!=NULL) {
4967
delete p->pre;
5068
p=p->next;
5169
}
5270

5371
delete p_cache_list_near;
5472
}
5573

56-
57-
int getValue(int key)
58-
{
74+
int getValue(int key) {
5975
CacheNode *p=p_cache_list_head->next;
60-
while(p->next!=NULL)
61-
{
62-
63-
if(p->key == key) //catch node
64-
{
65-
76+
while (p->next!=NULL) {
77+
if (p->key == key) { //catch node
6678
detachNode(p);
6779
addToFront(p);
6880
return p->value;
@@ -72,87 +84,68 @@ class LRUCache{
7284
return -1;
7385
}
7486

75-
bool putValue(int key,int value)
76-
{
87+
bool putValue(int key,int value) {
7788
CacheNode *p=p_cache_list_head->next;
78-
while(p->next!=NULL)
79-
{
80-
81-
82-
if(p->key == key) //catch node
83-
{
89+
while (p->next!=NULL) {
90+
if(p->key == key) { //catch node
8491
p->value=value;
8592
getValue(key);
8693
return true;
8794
}
8895
p=p->next;
8996
}
90-
91-
92-
if(cache_real_size_ >= cache_size_)
93-
{
94-
cout << "free" <<endl;
97+
98+
if (cache_real_size_ >= cache_size_) {
99+
std::cout << "free" <<std::endl;
95100
p=p_cache_list_near->pre->pre;
96101
delete p->next;
97102
p->next=p_cache_list_near;
98103
p_cache_list_near->pre=p;
99104
}
100-
101-
p=new CacheNode();//(CacheNode *)malloc(sizeof(CacheNode));
102-
103-
if(p==NULL)
105+
106+
p = new CacheNode();//(CacheNode *)malloc(sizeof(CacheNode));
107+
108+
if (p==NULL)
104109
return false;
105110

106111
addToFront(p);
107112
p->key=key;
108113
p->value=value;
109-
114+
110115
cache_real_size_++;
111-
116+
112117
return true;
113118
}
114-
void displayNodes()
115-
{
119+
120+
void displayNodes() {
116121
CacheNode *p=p_cache_list_head->next;
117-
118-
while(p->next!=NULL)
119-
{
120-
cout << " Key : " << p->key << " Value : " << p->value << endl;
122+
123+
while(p->next!=NULL) {
124+
std::cout << " Key : " << p->key << " Value : " << p->value << std::endl;
121125
p=p->next;
122-
126+
123127
}
124-
cout << endl;
125-
128+
std::cout << std::endl;
126129
}
127-
128-
129-
private:
130-
131-
int cache_size_;
132-
int cache_real_size_;
133-
CacheNode *p_cache_list_head;
134-
CacheNode *p_cache_list_near;
135-
136-
void detachNode(CacheNode *node)
137-
{
130+
131+
private:
132+
int cache_size_;
133+
int cache_real_size_;
134+
CacheNode *p_cache_list_head;
135+
CacheNode *p_cache_list_near;
136+
137+
void detachNode(CacheNode *node) {
138138
node->pre->next=node->next;
139139
node->next->pre=node->pre;
140140
}
141-
void addToFront(CacheNode *node)
142-
{
143-
141+
142+
void addToFront(CacheNode *node) {
144143
node->next=p_cache_list_head->next;
145144
p_cache_list_head->next->pre=node;
146145
p_cache_list_head->next=node;
147146
node->pre=p_cache_list_head;
148-
149147
}
150-
151-
};
152-
153-
148+
};
149+
}
154150

155151
#endif
156-
157-
158-

0 commit comments

Comments
 (0)