js-vdc/vdc.js

31 lines
595 B
JavaScript
Raw Normal View History

2016-01-16 18:47:42 +00:00
/* vdc.js (C) 2013-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
2013-12-14 07:43:55 +00:00
function VDC (opts) {
if(!(this instanceof VDC)) return new VDC(opts);
2016-01-16 18:47:42 +00:00
var o = opts || {b:2, n:0};
2013-12-14 07:43:55 +00:00
this._b = o.b || 2;
2016-01-16 18:47:42 +00:00
this._n = 0;
2013-12-14 07:43:55 +00:00
this.last = 0;
this.reset(o);
2016-01-16 18:47:42 +00:00
}
2013-12-14 07:43:55 +00:00
VDC.prototype.reset = function(opts) {
2016-01-16 18:47:42 +00:00
this._n = (opts||{n:0}).n||0;
};
2013-12-14 07:43:55 +00:00
VDC.prototype.next = function() {
var n = this._n++;
var p = 0, q = 1;
while(n >= 1) {
p = p * this._b + (n % this._b);
q *= this._b;
2016-01-16 18:47:42 +00:00
n = (n/this._b)>>>0;
2013-12-14 07:43:55 +00:00
}
2016-01-16 18:47:42 +00:00
this.last = p/q;
return this.last;
};
2013-12-14 07:43:55 +00:00
if(typeof module !== 'undefined') {
module.exports = VDC;
}