-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (123 loc) · 3.43 KB
/
Copy pathmain.cpp
File metadata and controls
129 lines (123 loc) · 3.43 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <bits/stdc++.h>
#include "queue.hpp"
#include "skiplist.cpp"
#include "segmenttree.cpp"
#undef int
namespace py = pybind11;
using namespace std;
threadqueue taskqueue;
sum* sumtree=nullptr;
mintree* min_tree = nullptr;
maxtree* max_tree = nullptr;
skiplist* skip=nullptr;
int globalmax=1000;
vector<int> balances;
vector<bool> active;
set<pair<int,int>,greater<pair<int,int>>> leaderboard;
void background(){
while(true){
int userid,x;
if(taskqueue.trypop(userid,x)){
if(active[userid]){
leaderboard.erase({balances[userid],userid});
balances[userid]+=x;
leaderboard.insert({balances[userid], userid});
sumtree->update(0, userid, balances[userid], 0, globalmax - 1);
min_tree->update(0, userid, balances[userid], 0, globalmax - 1);
max_tree->update(0, userid, balances[userid], 0, globalmax - 1);
cout << "[Bank Worker] User " << userid << " processed $" << x
<< " -> New Balance: $" << balances[userid] << "\n";
}
}
else{
this_thread::sleep_for(chrono::milliseconds(1));
}
}
}
void start(int maxusers){
globalmax=maxusers;
balances.assign(maxusers,0);
active.assign(maxusers,false);
vector<long long> arr(maxusers,0);
sumtree=new sum(arr);
min_tree=new mintree(arr);
max_tree=new maxtree(arr);
skip=createskiplist();
thread worker(background);
worker.detach();
cout << "fastDB started!\n";
}
bool register1(int userid,int startbal){
if(userid<=0||userid>=globalmax){
return false;
}
if(active[userid]){
return false;
}
active[userid]=true;
balances[userid]=startbal;
leaderboard.insert({startbal,userid});
int p=globalmax-1;
sumtree->update(0,userid,startbal,0,p);
min_tree->update(0,userid,startbal,0,p);
max_tree->update(0,userid,startbal,0,p);
return true;
}
bool isregistered(int userid){
if(userid<0||userid>=globalmax){
return false;
}
return active[userid];
}
void transaction(int sender,int receiver,int amount){
taskqueue.push(sender,-amount);
taskqueue.push(receiver,amount);
}
int getbalance(int userid){
if(!isregistered(userid)){
return 0;
}
return balances[userid];
}
vector<int> gettopstats(int n){
int total=0,min_w=INT_MAX,max_w=INT_MIN,count=0;
for(auto it=leaderboard.begin();it!=leaderboard.end()&&count<n;++it){
int wealth=it->first;
total+=wealth;
min_w=std::min(min_w,wealth);
max_w=std::max(max_w,wealth);
count++;
}
if(count==0){
return {0,0,0};
}
return {total,min_w,max_w};
}
map<string,long long> rangequery(int left,int right){
if(left<0){
left=0;
}
if(right>=globalmax){
right=globalmax-1;
}
if(left>right){
return {{"sum",0},{"min",0},{"max",0}};
}
int p=globalmax-1;
return{
{"sum",sumtree->query(0,0,p,left,right)},
{"min",min_tree->query(0,0,p,left,right)},
{"max",max_tree->query(0,0,p,left,right)}
};
}
PYBIND11_MODULE(fastdb,m){
m.def("start",&start);
m.def("register1",®ister1);
m.def("isregistered",&isregistered);
m.def("transfer",&transaction);
m.def("topstats",&gettopstats);
m.def("getbalance",&getbalance);
m.def("rangequery",&rangequery);
}