Created
February 13, 2018 08:28
-
-
Save SiestaMadokaist/e93742e31f7d9f0eb2d2cc2a19dfd9f8 to your computer and use it in GitHub Desktop.
FundBalancing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FundData = require('./FundData'); | |
class FundBalancer { | |
constructor(){ | |
this.state = { data: [] } | |
}; | |
insert({ owner, current, expected }){ | |
const fd = new FundData({ owner, current, expected }); | |
this.data().push(fd); | |
} | |
data(){ | |
return this.state.data; | |
} | |
transferables(){ | |
return this.data().sort(FundData.compare); | |
} | |
static isEquilibrium(transferables){ | |
for(let t of transferables){ | |
if(t.hasTransferable()){ return false; } | |
} | |
return true; | |
} | |
transferActions(){ | |
const transferables = this.transferables(); | |
const actions = []; | |
while(!FundBalancer.isEquilibrium(transferables)){ | |
let source = transferables.shift(); | |
let destination = transferables.pop(); | |
let transferValue = Math.min(source.transferValue(), destination.transferValue()) | |
let action = { source: source.owner(), destination: destination.owner(), transferValue } | |
let sourceAfter = source.afterAction(action); | |
let destinationAfter = destination.afterAction(action); | |
actions.push(action); | |
transferables.push(sourceAfter); | |
transferables.push(destinationAfter); | |
transferables.sort(FundData.compare); | |
}; | |
return { actions, transferables}; | |
} | |
} | |
module.exports = FundBalancer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FundData { | |
static compare(fd1, fd2){ | |
return fd2.transferable() - fd1.transferable(); | |
} | |
constructor({ owner, current, expected, buffer = 0 }){ | |
this.props = { owner, current, expected, buffer }; | |
} | |
current(){ | |
return this.props.current; | |
} | |
expected(){ | |
return this.props.expected; | |
} | |
owner(){ | |
return this.props.owner; | |
} | |
buffer(){ | |
return this.props.buffer; | |
} | |
hasTransferable(){ | |
return this.transferable() > this.buffer(); | |
} | |
transferable(){ | |
return this.props.current - this.props.expected; | |
} | |
transferValue(){ | |
return Math.abs(this.transferable()); | |
} | |
afterAction(action){ | |
if(this.owner() === action.source){ | |
const amount = this.current() - action.transferValue; | |
return new FundData({ owner: this.owner(), current: amount, expected: this.expected() }); | |
} else if(this.owner() === action.destination){ | |
const amount = this.current() + action.transferValue; | |
return new FundData({ owner: this.owner(), current: amount, expected: this.expected() }); | |
}; | |
} | |
} | |
module.exports = FundData; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment