-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd462c6
commit fc6241d
Showing
16 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
!function(t){"use strict" | ||
;var e=setTimeout,n=clearTimeout,i={ | ||
now:function(){return Date.now()}, | ||
setTimeout:"undefined"==typeof window?setTimeout:function(){ | ||
return e.apply(window,arguments)}, | ||
clearTimeout:"undefined"==typeof window?clearTimeout:function(){ | ||
return n.apply(window,arguments)}} | ||
;function o(t,e){return t<e}class l{ | ||
constructor({objectPool:t,lessThanFunc:e}={}){ | ||
this._size=0,this._root=null,this.merge=r, | ||
this.collapse=s,this._objectPool=t,this._lessThanFunc=e||o | ||
}clear(){this._root=null,this._size=0}get size(){ | ||
return this._size}add(t){ | ||
let e=null!=this._objectPool?this._objectPool.get():null | ||
;return null==e?e={child:null,next:null,prev:null, | ||
item:t | ||
}:e.item=t,this._size++,this._root=r(this._root,e,this._lessThanFunc),e | ||
}getMin(){const{_root:t}=this | ||
;return null==t?void 0:t.item}getMinNode(){ | ||
return this._root}deleteMin(){const{_root:t}=this | ||
;if(null==t)return;const e=t.item | ||
;return this.delete(t),e}delete(t){var e | ||
;if(t===this._root)this._root=s(t.child,this._lessThanFunc);else{ | ||
if(null==t.prev){ | ||
if(this._objectPool)throw new Error("The node is already deleted. Don't use the objectPool to prevent this error.") | ||
;return} | ||
t.prev.child===t?t.prev.child=t.next:t.prev.next=t.next,null!=t.next&&(t.next.prev=t.prev), | ||
this._root=r(this._root,s(t.child,this._lessThanFunc),this._lessThanFunc) | ||
} | ||
t.child=null,t.prev=null,t.next=null,t.item=void 0,null===(e=this._objectPool)||void 0===e||e.release(t), | ||
this._size--}decreaseKey(t){ | ||
t!==this._root&&(t.prev.child===t?t.prev.child=t.next:t.prev.next=t.next, | ||
null!=t.next&&(t.next.prev=t.prev), | ||
this._root=r(this._root,t,this._lessThanFunc))} | ||
get isEmpty(){return null==this._root} | ||
[Symbol.iterator](){return this._iterate(!1)} | ||
nodes(){return{ | ||
[Symbol.iterator]:()=>this._iterate(!0)}} | ||
_iterate(t){const e=this._lessThanFunc | ||
;return function*n(i){ | ||
i&&(t?yield i:yield i.item,i.child&&(null!=i.child.next&&(i.child=s(i.child,e), | ||
i.child.prev=i),yield*n(i.child)))}(this._root)}} | ||
function r(t,e,n){let i,o | ||
;return null==t?e:null==e||t===e?t:(n(e.item,t.item)?(i=e, | ||
o=t):(i=t,o=e),o.next=i.child, | ||
null!=i.child&&(i.child.prev=o),o.prev=i,i.child=o, | ||
i.next=null,i.prev=null,i)}function s(t,e){ | ||
let n,i,o,l,s;if(null==t)return null | ||
;for(l=t,n=null;null!=l;){ | ||
if(i=l,o=i.next,null==o){i.prev=n,n=i;break} | ||
l=o.next,s=r(i,o,e),s.prev=n,n=s} | ||
for(s=null;null!=n;)l=n.prev,s=r(s,n,e),n=l | ||
;return s}function u(t,e){ | ||
return!(t.time>e.time)&&(t.time<e.time||!(t.id>e.id)&&t.id<e.id) | ||
}var h=function(){function t(){ | ||
this._now=1,this._nextId=0,this._handles=new l({ | ||
lessThanFunc:u})} | ||
return t.prototype.addTime=function(t){ | ||
this.setTime(this._now+t) | ||
},t.prototype.setTime=function(t){ | ||
var e=this._handles,n=this._now | ||
;if(t<this._now)throw new Error("time (".concat(t,") should be >= now (").concat(n,")")) | ||
;for(;;){var i=e.getMin();if(!i||i.time>t)break | ||
;this._handles.deleteMin(),this._now=i.time, | ||
i.callback()}this._now=t | ||
},t.prototype.now=function(){return this._now | ||
},t.prototype.setTimeout=function(t,e){ | ||
return this._handles.add(Object.freeze({ | ||
id:this._nextId++,time:this._now+e,callback:t})) | ||
},t.prototype.clearTimeout=function(t){ | ||
this._handles.delete(t)},t}() | ||
;t.TimeControllerMock=h,t.timeControllerDefault=i,Object.defineProperty(t,"__esModule",{ | ||
value:!0})}({}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ITimeController<THandle = any> { | ||
now(): number; | ||
setTimeout(handler: () => void, timeout: number): THandle; | ||
clearTimeout(handle: THandle): void; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
var timeControllerDefault = require('./timeControllerDefault.cjs'); | ||
var timeControllerMock = require('./timeControllerMock.cjs'); | ||
require('@flemist/pairing-heap'); | ||
|
||
|
||
|
||
exports.timeControllerDefault = timeControllerDefault.timeControllerDefault; | ||
exports.TimeControllerMock = timeControllerMock.TimeControllerMock; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type { ITimeController } from './contracts'; | ||
export { timeControllerDefault } from './timeControllerDefault'; | ||
export { TimeControllerMock } from './timeControllerMock'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { timeControllerDefault } from './timeControllerDefault.mjs'; | ||
export { TimeControllerMock } from './timeControllerMock.mjs'; | ||
import '@flemist/pairing-heap'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
const _setTimeout = setTimeout; | ||
const _clearTimeout = clearTimeout; | ||
const timeControllerDefault = { | ||
now: function now() { | ||
return Date.now(); | ||
}, | ||
setTimeout: typeof window === 'undefined' | ||
? setTimeout | ||
: function setTimeout() { | ||
return _setTimeout.apply(window, arguments); | ||
}, | ||
clearTimeout: typeof window === 'undefined' | ||
? clearTimeout | ||
: function clearTimeout() { | ||
return _clearTimeout.apply(window, arguments); | ||
}, | ||
}; | ||
|
||
exports.timeControllerDefault = timeControllerDefault; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { ITimeController } from './contracts'; | ||
export declare const timeControllerDefault: ITimeController; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const _setTimeout = setTimeout; | ||
const _clearTimeout = clearTimeout; | ||
const timeControllerDefault = { | ||
now: function now() { | ||
return Date.now(); | ||
}, | ||
setTimeout: typeof window === 'undefined' | ||
? setTimeout | ||
: function setTimeout() { | ||
return _setTimeout.apply(window, arguments); | ||
}, | ||
clearTimeout: typeof window === 'undefined' | ||
? clearTimeout | ||
: function clearTimeout() { | ||
return _clearTimeout.apply(window, arguments); | ||
}, | ||
}; | ||
|
||
export { timeControllerDefault }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
var pairingHeap = require('@flemist/pairing-heap'); | ||
|
||
function lessThanHandles(o1, o2) { | ||
if (o1.time > o2.time) { | ||
return false; | ||
} | ||
if (o1.time < o2.time) { | ||
return true; | ||
} | ||
if (o1.id > o2.id) { | ||
return false; | ||
} | ||
if (o1.id < o2.id) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
class TimeControllerMock { | ||
constructor() { | ||
this._now = 1; | ||
this._nextId = 0; | ||
this._handles = new pairingHeap.PairingHeap({ | ||
lessThanFunc: lessThanHandles, | ||
}); | ||
} | ||
addTime(time) { | ||
this.setTime(this._now + time); | ||
} | ||
setTime(time) { | ||
const { _handles, _now: now } = this; | ||
if (time < this._now) { | ||
throw new Error(`time (${time}) should be >= now (${now})`); | ||
} | ||
while (true) { | ||
const minHandle = _handles.getMin(); | ||
if (!minHandle || minHandle.time > time) { | ||
break; | ||
} | ||
this._handles.deleteMin(); | ||
this._now = minHandle.time; | ||
minHandle.callback(); | ||
} | ||
this._now = time; | ||
} | ||
now() { | ||
return this._now; | ||
} | ||
setTimeout(callback, timeout) { | ||
const node = this._handles.add(Object.freeze({ | ||
id: this._nextId++, | ||
time: this._now + timeout, | ||
callback, | ||
})); | ||
return node; | ||
} | ||
clearTimeout(handle) { | ||
this._handles.delete(handle); | ||
} | ||
} | ||
|
||
exports.TimeControllerMock = TimeControllerMock; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ITimeController } from './contracts'; | ||
import { PairingNode } from '@flemist/pairing-heap'; | ||
interface IHandle { | ||
id: number; | ||
time: number; | ||
callback: () => void; | ||
} | ||
export declare class TimeControllerMock implements ITimeController<PairingNode<IHandle>> { | ||
private readonly _handles; | ||
private _now; | ||
private _nextId; | ||
constructor(); | ||
addTime(time: number): void; | ||
setTime(time: number): void; | ||
now(): number; | ||
setTimeout(callback: () => void, timeout: number): PairingNode<IHandle>; | ||
clearTimeout(handle: PairingNode<IHandle>): void; | ||
} | ||
export {}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { PairingHeap } from '@flemist/pairing-heap'; | ||
|
||
function lessThanHandles(o1, o2) { | ||
if (o1.time > o2.time) { | ||
return false; | ||
} | ||
if (o1.time < o2.time) { | ||
return true; | ||
} | ||
if (o1.id > o2.id) { | ||
return false; | ||
} | ||
if (o1.id < o2.id) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
class TimeControllerMock { | ||
constructor() { | ||
this._now = 1; | ||
this._nextId = 0; | ||
this._handles = new PairingHeap({ | ||
lessThanFunc: lessThanHandles, | ||
}); | ||
} | ||
addTime(time) { | ||
this.setTime(this._now + time); | ||
} | ||
setTime(time) { | ||
const { _handles, _now: now } = this; | ||
if (time < this._now) { | ||
throw new Error(`time (${time}) should be >= now (${now})`); | ||
} | ||
while (true) { | ||
const minHandle = _handles.getMin(); | ||
if (!minHandle || minHandle.time > time) { | ||
break; | ||
} | ||
this._handles.deleteMin(); | ||
this._now = minHandle.time; | ||
minHandle.callback(); | ||
} | ||
this._now = time; | ||
} | ||
now() { | ||
return this._now; | ||
} | ||
setTimeout(callback, timeout) { | ||
const node = this._handles.add(Object.freeze({ | ||
id: this._nextId++, | ||
time: this._now + timeout, | ||
callback, | ||
})); | ||
return node; | ||
} | ||
clearTimeout(handle) { | ||
this._handles.delete(handle); | ||
} | ||
} | ||
|
||
export { TimeControllerMock }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
var tslib = require('tslib'); | ||
var rdtsc = require('rdtsc'); | ||
var timeControllerMock = require('./timeControllerMock.cjs'); | ||
require('@flemist/pairing-heap'); | ||
|
||
describe('time-controller > timeControllerMock perf', function () { | ||
this.timeout(600000); | ||
it('base', function () { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
let timeController; | ||
let handle; | ||
function func() { } | ||
const timeControllerFilled = new timeControllerMock.TimeControllerMock(); | ||
for (let i = 0; i < 100; i++) { | ||
timeControllerFilled.setTimeout(func, 4); | ||
} | ||
let result = rdtsc.calcPerformance(10000, () => { | ||
}, () => { | ||
timeController = new timeControllerMock.TimeControllerMock(); | ||
}, () => { | ||
handle = timeController.setTimeout(func, 4); | ||
}, () => { | ||
timeController.clearTimeout(handle); | ||
}, () => { | ||
handle = timeControllerFilled.setTimeout(func, 4); | ||
}, () => { | ||
timeControllerFilled.clearTimeout(handle); | ||
}); | ||
console.log(result); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { __awaiter } from 'tslib'; | ||
import { calcPerformance } from 'rdtsc'; | ||
import { TimeControllerMock } from './timeControllerMock.mjs'; | ||
import '@flemist/pairing-heap'; | ||
|
||
describe('time-controller > timeControllerMock perf', function () { | ||
this.timeout(600000); | ||
it('base', function () { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let timeController; | ||
let handle; | ||
function func() { } | ||
const timeControllerFilled = new TimeControllerMock(); | ||
for (let i = 0; i < 100; i++) { | ||
timeControllerFilled.setTimeout(func, 4); | ||
} | ||
let result = calcPerformance(10000, () => { | ||
}, () => { | ||
timeController = new TimeControllerMock(); | ||
}, () => { | ||
handle = timeController.setTimeout(func, 4); | ||
}, () => { | ||
timeController.clearTimeout(handle); | ||
}, () => { | ||
handle = timeControllerFilled.setTimeout(func, 4); | ||
}, () => { | ||
timeControllerFilled.clearTimeout(handle); | ||
}); | ||
console.log(result); | ||
}); | ||
}); | ||
}); |