1 | class LFUCache { |
LRU Cache
Using linked list: 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
88class LinkedNode {
public:
LinkedNode *prev, *next;
int val;
LinkedNode(int val) {
this->val = val;
this->prev = nullptr;
this->next = nullptr;
}
};
class LinkedList {
private:
LinkedNode *head, *tail;
public:
LinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void erase(LinkedNode *p) {
if (p == tail) {
tail = tail->prev;
} else if (p == head) {
head = head->next;
}
if (p->prev) {
p->prev->next = p->next;
}
if (p->next) {
p->next->prev = p->prev;
}
}
LinkedNode *begin() { return head; }
LinkedNode *rbegin() { return tail; }
void push_front(int val) {
LinkedNode *node = new LinkedNode(val);
node->prev = nullptr;
node->next = head;
if (head) {
head->prev = node;
}
head = node;
if (tail == nullptr) {
tail = head;
}
}
void pop_back() { erase(tail); }
};
class LRUCache {
LinkedList lst;
unordered_map<int, LinkedNode *> key_to_node;
unordered_map<int, int> key_to_value;
int capacity;
public:
LRUCache(int capacity) { this->capacity = capacity; }
int get(int key) {
auto it = key_to_node.find(key);
if (it == key_to_node.end()) {
return -1;
}
int value = key_to_value[key];
put(key, value);
return value;
}
void put(int key, int value) {
auto it = key_to_node.find(key);
if (it != key_to_node.end()) {
lst.erase(it->second);
key_to_node.erase(key);
key_to_value.erase(key);
}
lst.push_front(key);
key_to_node[key] = lst.begin();
key_to_value[key] = value;
if (key_to_node.size() > capacity) {
LinkedNode *tail = lst.rbegin();
key_to_node.erase(tail->val);
key_to_value.erase(tail->val);
lst.pop_back();
}
}
};
Using STL: 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
36class LRUCache {
int capacity;
list<pair<int, int>> lst;
unordered_map<int, list<pair<int, int>>::iterator> mp;
public:
LRUCache(int capacity) {
this->capacity = capacity;
lst.clear();
mp.clear();
}
int get(int key) {
auto it = mp.find(key);
if (it == mp.end()) {
return -1;
}
int value = it->second->second;
put(key, value);
return value;
}
void put(int key, int value) {
auto keyToIter = mp.find(key);
if (keyToIter != mp.end()) {
lst.erase(keyToIter->second);
mp.erase(keyToIter);
}
lst.push_front({key, value});
mp[key] = lst.begin();
if (lst.size() > capacity) {
mp.erase(lst.rbegin()->first);
lst.pop_back();
}
}
};
Shuffle An Array
1 | import copy |
Heap Sort
1 | #include <bits/stdc++.h> |
Merge Sort
1 | #include <bits/stdc++.h> |
Bubble Sort
1 | #include <bits/stdc++.h> |
Ubuntu更改鼠标滚轮速度
安装 imwheel
$ sudo apt-get install imwheel
编辑imwheelrc配置
$ vim ~/.imwheelrc
1 | ".*" |
配置开机启动
$ gnome-session-properties
Install and Config MySQL 8 on Ubuntu
Remove Completely and Install
1 | sudo apt-get remove --purge "mysql*" |
Enable Remote Access
1 | su root |
In /etc/mysql/mysql.conf.d/mysqld.cnf
:
- replace
bind-address = 127.0.0.1
withbind-address = 0.0.0.0
Then restart mysql by sudo service mysql restart
.
Protobuf Examples
Installation
https://github.com/protocolbuffers/protobuf/blob/master/src/README.md
Proto
pb/entities.proto
: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25syntax = "proto3";
package entities;
option go_package = "pb/entities";
enum Gender {
UNKNOWN = 0;
MALE = 1;
FEMALE = 2;
}
message Person {
Gender gender = 1;
string name = 2;
repeated string tags = 3;
double height = 4;
int32 age = 5;
oneof important {
string task = 6;
int32 problem = 7;
double point = 8;
}
map<string, string> extra = 10;
}
Golang
https://developers.google.com/protocol-buffers/docs/gotutorial
$ protoc --proto_path=./ --go_out=./ pb/entities.proto
1 | package main |
Python
https://developers.google.com/protocol-buffers/docs/pythontutorial
$ protoc --proto_path=./ --python_out=../python pb/entities.proto
1 | # coding: utf-8 |
Learning Thrift
IDL and Code Generation
easy.thrift
: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17namespace go easy
namespace py easy
namespace cpp easy
service Easy {
void ping();
string say_hello(1: list<string> words);
i32 add(1: i32 x, 2: i32 y);
}
service Hard {
void ping();
string say_hello(1: list<string> words);
i32 add(1: i32 x, 2: i32 y);
}$ thrift -r --gen go ./easy.thrift
Golang
goland
: 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116package main
import (
"context"
"errors"
"math/rand"
"os"
"strings"
"time"
"go-learn/gen-go/easy"
"github.com/apache/thrift/lib/go/thrift"
logger "github.com/sirupsen/logrus"
"github.com/x-cray/logrus-prefixed-formatter"
)
func init() {
logger.SetFormatter(&prefixed.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
ForceFormatting: true,
DisableColors: true,
})
logger.SetOutput(os.Stdout)
logger.SetLevel(logger.DebugLevel)
}
type EasyHandler struct{}
func (h *EasyHandler) Ping(ctx context.Context) error {
logger.Infof("ping")
if rand.Int()%2 == 0 {
logger.Infof("return error")
return errors.New("a random error")
}
return nil
}
func (h *EasyHandler) SayHello(ctx context.Context, words []string) (string, error) {
return strings.Join(words, ", "), nil
}
func (h *EasyHandler) Add(ctx context.Context, x int32, y int32) (int32, error) {
return x + y, nil
}
func runEasy() error {
processor := easy.NewEasyProcessor(&EasyHandler{})
transport, err := thrift.NewTServerSocket(":27024")
if err != nil {
return err
}
transportFactory := thrift.NewTTransportFactory()
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)
return server.Serve()
}
func runClient() error {
var transport thrift.TTransport
transport, err := thrift.NewTSocket(":27024")
if err != nil {
return err
}
transportFactory := thrift.NewTTransportFactory()
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
transport, err = transportFactory.GetTransport(transport)
if err != nil {
return err
}
defer transport.Close()
if err := transport.Open(); err != nil {
return err
}
iprot := protocolFactory.GetProtocol(transport)
oprot := protocolFactory.GetProtocol(transport)
client := easy.NewEasyClient(thrift.NewTStandardClient(iprot, oprot))
if err := client.Ping(context.Background()); err != nil {
logger.Errorf("ping error: %v", err)
}
if sum, err := client.Add(context.Background(), 1, 2); err != nil {
logger.Errorf("add error: %v", err)
} else {
logger.Infof("result of add: %v", sum)
}
if ret, err := client.SayHello(context.Background(), []string{"hello", "world"}); err != nil {
logger.Errorf("say hello error: %v", err)
} else {
logger.Infof("result of say hello: %v", ret)
}
return nil
}
func main() {
go func() {
err := runEasy()
if err != nil {
logger.Errorf("run server error: %v", err)
}
}()
time.Sleep(time.Second * 2)
go func() {
err := runClient()
if err != nil {
logger.Errorf("run client error: %v", err)
}
}()
time.Sleep(time.Second * 2)
}
Python
python-server
: 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# coding: utf-8
from easy.easy import Easy
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class EasyHandler:
def __init__(self):
pass
@staticmethod
def ping():
print("ping")
@staticmethod
def say_hello(words):
return ", ".join(words)
@staticmethod
def add(x, y):
return x + y
if __name__ == '__main__':
handler = EasyHandler()
processor = Easy.Processor(handler)
transport = TSocket.TServerSocket(host='127.0.0.1', port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
server.serve()
python-client
: 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# coding: utf-8
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket
from thrift.transport import TTransport
from easy.easy import Easy
def main():
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Easy.Client(protocol)
transport.open()
print(client.ping())
print(client.add(1, 2))
print(client.say_hello(["hello", "world"]))
transport.close()
if __name__ == '__main__':
main()