-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
269 lines (234 loc) · 7.79 KB
/
code.cpp
File metadata and controls
269 lines (234 loc) · 7.79 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <climits>
#include <cmath>
#include <mutex>
#include <thread>
using namespace std;
/* =========================
User Entity
========================= */
class User {
public:
int id;
string name;
User(int id, const string& name) : id(id), name(name) {}
};
/* =========================
Balance Entity
- amount: current balance
- version: for optimistic locking
- mtx: mutex to simulate atomic read/write for thread safety
========================= */
struct Balance {
double amount;
int version;
mutex mtx;
Balance(double a = 0) : amount(a), version(0) {}
};
/* =========================
Split Entity
- Represents a share of an expense for a user
========================= */
class Split {
public:
User* user;
double amount;
Split(User* user, double amount) : user(user), amount(amount) {}
};
/* =========================
Expense Entity
- Each expense has a payer, splits, and description
========================= */
class Expense {
public:
int id;
User* paidBy;
vector<Split> splits;
string description;
Expense(int id, User* paidBy, const vector<Split>& splits, const string& description)
: id(id), paidBy(paidBy), splits(splits), description(description) {}
};
/* =========================
Group Entity
- Contains users, expenses, and balances
- balances store per-user balance with version for optimistic locking
========================= */
class Group {
public:
int id;
string name;
vector<User*> users;
vector<Expense*> expenses;
unordered_map<int, Balance> balances;
Group(int id, const string& name) : id(id), name(name) {}
void addUser(User* user) {
users.push_back(user);
balances[user->id] = Balance(0); // initialize balance
}
~Group() {
for (Expense* e : expenses) delete e;
}
};
/* =========================
Expense & Settlement Service
- Updates balances using optimistic locking
- Retry logic ensures safe updates in concurrent environment
========================= */
class ExpenseService {
public:
static void addExpense(Group* group, User* paidBy, const vector<Split>& splits, const string& description) {
int expenseId = group->expenses.size() + 1;
Expense* expense = new Expense(expenseId, paidBy, splits, description);
group->expenses.push_back(expense);
double total = 0;
for (const Split& s : splits) total += s.amount;
// Update payer balance
updateBalanceOptimistic(group, paidBy->id, total);
// Update participant balances
for (const Split& s : splits) updateBalanceOptimistic(group, s.user->id, -s.amount);
}
static void settle(Group* group, User* from, User* to, double amount) {
updateBalanceOptimistic(group, from->id, amount);
updateBalanceOptimistic(group, to->id, -amount);
}
private:
// Optimistic locking: read version, update if version unchanged, retry if conflict
static void updateBalanceOptimistic(Group* group, int userId, double delta) {
while (true) {
Balance& bal = group->balances[userId];
int currentVersion;
double currentAmount;
// Atomic read
{
lock_guard<mutex> lock(bal.mtx);
currentVersion = bal.version;
currentAmount = bal.amount;
}
double newAmount = currentAmount + delta;
// Attempt write if version unchanged
{
lock_guard<mutex> lock(bal.mtx);
if (bal.version == currentVersion) {
bal.amount = newAmount;
bal.version++;
break; // success
}
// else retry
}
}
}
};
/* =========================
Balance Service
- Display group balances
========================= */
class BalanceService {
public:
static void showGroupBalances(Group* group) {
cout << "\nBalances for group: " << group->name << "\n";
for (auto& entry : group->balances) {
cout << "UserId " << entry.first << " -> Balance: " << entry.second.amount << endl;
}
}
};
/* =========================
Debt Simplification
- Uses backtracking to minimize number of transactions
- Transactions represented as from → to → amount
========================= */
struct Transaction {
User* from;
User* to;
double amount;
};
class SimplifyDebtService {
public:
static vector<Transaction> simplify(Group* group) {
vector<double> debts;
vector<User*> userList;
// Collect non-zero balances
for (User* u : group->users) {
double bal = group->balances[u->id].amount;
if (fabs(bal) > 1e-6) {
debts.push_back(bal);
userList.push_back(u);
}
}
vector<Transaction> currentPath, bestPath;
int minTx = INT_MAX;
dfs(0, debts, userList, currentPath, bestPath, minTx);
return bestPath;
}
private:
static void dfs(int index, vector<double>& debts, vector<User*>& users,
vector<Transaction>& currentPath, vector<Transaction>& bestPath, int& minTx) {
// Skip settled balances
while (index < debts.size() && fabs(debts[index]) < 1e-6) index++;
if (index == debts.size()) {
if ((int)currentPath.size() < minTx) {
minTx = currentPath.size();
bestPath = currentPath;
}
return;
}
for (int i = index + 1; i < debts.size(); i++) {
if (debts[index] * debts[i] < 0) {
double settled = min(fabs(debts[index]), fabs(debts[i]));
Transaction t;
if (debts[index] < 0) {
t.from = users[index];
t.to = users[i];
t.amount = settled;
debts[i] += debts[index];
} else {
t.from = users[i];
t.to = users[index];
t.amount = settled;
debts[i] += debts[index];
}
currentPath.push_back(t);
dfs(index + 1, debts, users, currentPath, bestPath, minTx);
currentPath.pop_back();
if (debts[index] < 0) debts[i] -= debts[index];
else debts[i] -= debts[index];
}
}
}
};
/* =========================
Demo Main
========================= */
int main() {
// Create users
User* alice = new User(1, "Alice");
User* bob = new User(2, "Bob");
User* charlie = new User(3, "Charlie");
// Create group and add users
Group* tripGroup = new Group(1, "Trip");
tripGroup->addUser(alice);
tripGroup->addUser(bob);
tripGroup->addUser(charlie);
// Add expense: Alice pays 300 split equally
vector<Split> splits = { Split(alice, 100), Split(bob, 100), Split(charlie, 100) };
ExpenseService::addExpense(tripGroup, alice, splits, "Hotel");
BalanceService::showGroupBalances(tripGroup);
// Bob settles 50 with Alice
ExpenseService::settle(tripGroup, bob, alice, 50);
BalanceService::showGroupBalances(tripGroup);
// Compute simplified debts
auto settlements = SimplifyDebtService::simplify(tripGroup);
cout << "\nSimplified Transactions (minimal number of transactions):\n";
for (auto& t : settlements) {
cout << t.from->name << " pays " << t.to->name << " " << t.amount << endl;
}
// Cleanup
delete tripGroup;
delete alice;
delete bob;
delete charlie;
return 0;
}
//Optimistic locking will be better for splitwise as conflicts are rare and app is read heavy