UNPKG

typescript-dotnet-core

Version:

A the core classes and utilities of a JavaScript-Friendly .NET Based TypeScript Library.

3 lines (2 loc) 4.61 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tslib_1=require("tslib"),AU=require("./Array/Utility"),Compare_1=require("../Compare"),Types_1=require("../Types"),Integer_1=require("../Integer"),EnumeratorBase_1=require("./Enumeration/EnumeratorBase"),NotImplementedException_1=require("../Exceptions/NotImplementedException"),InvalidOperationException_1=require("../Exceptions/InvalidOperationException"),ArgumentOutOfRangeException_1=require("../Exceptions/ArgumentOutOfRangeException"),CollectionBase_1=require("./CollectionBase"),VOID0=void 0,MINIMUM_GROW=4,SHRINK_THRESHOLD=32,GROW_FACTOR_HALF=100,DEFAULT_CAPACITY=MINIMUM_GROW,emptyArray=Object.freeze([]),Queue=function(n){function e(e,t){void 0===t&&(t=Compare_1.areEqual);var r=n.call(this,VOID0,t)||this;if(r._head=0,r._tail=0,r._size=0,e)if(Types_1.default.isNumber(e)){var i=e;assertIntegerZeroOrGreater(i,"capacity"),r._array=i?AU.initialize(i):emptyArray}else{var a=e;r._array=AU.initialize(Types_1.default.isArrayLike(a)?a.length:DEFAULT_CAPACITY),r._importEntries(a)}else r._array=emptyArray;return r._capacity=r._array.length,r}return tslib_1.__extends(e,n),e.prototype.getCount=function(){return this._size},e.prototype._addInternal=function(e){var t=this,r=t._size,i=t._capacity;if(r==i){var a=i*GROW_FACTOR_HALF;a<i+MINIMUM_GROW&&(a=i+MINIMUM_GROW),t.setCapacity(a),i=t._capacity}var n=t._tail;return t._array[n]=e,t._tail=(n+1)%i,t._size=r+1,!0},e.prototype._removeInternal=function(e,t){throw new NotImplementedException_1.default("ICollection<T>.remove is not implemented in Queue<T> since it would require destroying the underlying array to remove the item.")},e.prototype._clearInternal=function(){var e=this,t=e._array,r=e._head,i=e._tail,a=e._size;return r<i?AU.clear(t,r,i):(AU.clear(t,r),AU.clear(t,0,i)),e._head=0,e._tail=0,e._size=0,e.trimExcess(),a},e.prototype._onDispose=function(){n.prototype._onDispose.call(this),this._array!=emptyArray&&(this._array.length=this._capacity=0,this._array=emptyArray)},e.prototype.dump=function(e){void 0===e&&(e=1/0);var t=this,r=[];if(isFinite(e)){if(Integer_1.default.assertZeroOrGreater(e),0!==e)for(;e--&&t._tryDequeueInternal(function(e){r.push(e)}););}else for(;t._tryDequeueInternal(function(e){r.push(e)}););return t.trimExcess(),t._signalModification(),r},e.prototype.forEach=function(e){return n.prototype.forEach.call(this,e,!0)},e.prototype.setCapacity=function(e){var t=this;assertIntegerZeroOrGreater(e,"capacity");var r=t._array,i=t._capacity;if(i<e&&t.throwIfDisposed(),e==i)return this;var a=t._head,n=t._tail,o=t._size;if(r!=emptyArray&&i<e&&a<n)return r.length=t._capacity=e,t._version++,this;var s=AU.initialize(e);return 0<o&&(a<n?AU.copyTo(r,s,a,0,o):(AU.copyTo(r,s,a,0,i-a),AU.copyTo(r,s,0,i-a,n))),t._array=s,t._capacity=e,t._head=0,t._tail=o==e?0:o,t._signalModification(!0),this},e.prototype.enqueue=function(e){return this.add(e)},e.prototype._tryDequeueInternal=function(e){if(!this._size)return!1;var t=this._array,r=this._head,i=this._array[r];return t[r]=null,this._head=(r+1)%this._capacity,this._size--,this._incrementModified(),e(i),!0},e.prototype.dequeue=function(e){void 0===e&&(e=!1);this.assertModifiable();var t=VOID0;if(!this.tryDequeue(function(e){t=e})&&e)throw new InvalidOperationException_1.default("Cannot dequeue an empty queue.");return t},e.prototype.tryDequeue=function(e){var t=this;return!!t._size&&(t.assertModifiable(),!!this._tryDequeueInternal(e)&&(t._size<t._capacity/2&&t.trimExcess(SHRINK_THRESHOLD),t._signalModification(),!0))},e.prototype._getElement=function(e){assertIntegerZeroOrGreater(e,"index");var t=this;return t._array[(t._head+e)%t._capacity]},e.prototype.peek=function(e){if(void 0===e&&(e=!1),0!=this._size)return this._array[this._head];if(e)throw new InvalidOperationException_1.default("Cannot call peek on an empty queue.");return VOID0},e.prototype.trimExcess=function(e){var t=this,r=t._size;r<Math.floor(.9*t._capacity)&&(!e&&0!==e||isNaN(e)||e<r)&&t.setCapacity(r)},e.prototype.getEnumerator=function(){var t,r,i,a=this;return a.throwIfDisposed(),new EnumeratorBase_1.FiniteEnumeratorBase(function(){r=a._version,i=a._size,t=0},function(e){return a.throwIfDisposed(),a.assertVersion(r),t==i?e.yieldBreak():e.yieldReturn(a._getElement(t++))})},e}(CollectionBase_1.default);function assertZeroOrGreater(e,t){if(e<0)throw new ArgumentOutOfRangeException_1.default(t,e,"Must be greater than zero");return!0}function assertIntegerZeroOrGreater(e,t){return Integer_1.default.assert(e,t),assertZeroOrGreater(e,t)}exports.Queue=Queue,exports.default=Queue; //# sourceMappingURL=Queue.js.map