-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebtMatrix.js
91 lines (81 loc) · 2.32 KB
/
DebtMatrix.js
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
class DebtMatrix{
constructor(users){
var users = [...new Set(users)];
users.sort();
this.dim = users.length;
this.users = users;
this.M = new Array(this.dim).fill(new Array(this.dim).fill(0));
}
get allDebts(){
var debts = [];
for(var i = 0;i<this.dim;i++){
for(var j = 0;j<this.dim;j++){
if(this.M[i][j] > 0){
debts.push({
debtor = this.users[i],
creditor = this.users[j],
amount = this.M[i][j],
})
}
}
}
return debts;
}
static add(A,B){
var C = new DebtMatrix();
var debts = A.allDebts.concat(B.allDebts);
for(var d in debts){
C = C.addDebt(d);
}
return C;
}
static sub(A,B){
if(A.dim != B.dim)
throw "dimensions mismatch";
var C = new DebtMatrix(A.dim);
for(var i = 0;i<A.dim;i++){
for(var j = 0;j<A.dim;j++){
C.M[i][j] = A.M[i][j]-B.M[i][j];
}
}
return C;
}
get totalDebt(){
return this.M.reduce((a,b) => a+b.reduce((c,d) => c+d,0),0);
}
get totalTransactions(){
return this.M.reduce((a,b) => a+b.reduce((c,d) => c+(d>0?1:0),0),0);
}
userDebt(i){
return this.M[i].reduce((a,b) => a+b,0);
}
userTransactions(i){
return this.M[i].reduce((a,b) => a+(b>0?1:0),0);
}
uI(user){
return this.users.indexOf(user);
}
hasUsers(newUsers){
return newUsers.every(x => this.users.indexOf(x) >= 0);
}
expand(newUsers){
var allUsers = [...this.users].concat([debt.debtor,debt.creditor]);
var C = new DebtMatrix(allUsers);
var debts = this.allDebts;
for(var d in debts){
C = C.addDebt(d);
}
}
addDebt(debt){
var C = new DebtMatrix(this.users);
if(!this.hasUsers([debt.creditor,debt.debtor])){
C = this.expand([debt.creditor,debt.debtor]);
}
}
addEqualEvent(payer,users,cost){
var allUsers = [...this.users].concat(users);
var m = new DebtMatrix(allUsers);
for(var i = 0;i<users.length;i++){
}
}
}