/*!
 * @author electricessence / https://github.com/electricessence/
 * Based Upon: http://referencesource.microsoft.com/#System/CompMod/system/collections/generic/queue.cs
 * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
 */
!function(e){if("object"==typeof module&&"object"==typeof module.exports){var t=e(require,exports);void 0!==t&&(module.exports=t)}else"function"==typeof define&&define.amd&&define(["require","exports","../Compare","./Array/Utility","../Types","../Integer","./Enumeration/EnumeratorBase","./Enumeration/forEach","../Exceptions/NotImplementedException","../Exceptions/InvalidOperationException","../Exceptions/ArgumentOutOfRangeException"],e)}(function(e,t){"use strict";function r(e,t){if(0>e)throw new _["default"](t,e,"Must be greater than zero")}function i(e,t){u["default"].assert(e,t),r(e,t)}var a=e("../Compare"),n=e("./Array/Utility"),o=e("../Types"),u=e("../Integer"),c=e("./Enumeration/EnumeratorBase"),s=e("./Enumeration/forEach"),p=e("../Exceptions/NotImplementedException"),y=e("../Exceptions/InvalidOperationException"),_=e("../Exceptions/ArgumentOutOfRangeException"),f=4,l=32,d=100,h=f,v=[],m=function(){function e(e){var t=this;if(t._head=0,t._tail=0,t._size=0,t._version=0,e)if(o["default"].isNumber(e)){var r=e;i(r,"capacity"),t._array=r?n.initialize(r):v}else{var a=e;t._array=n.initialize(o["default"].isArrayLike(a)?a.length:h),s["default"](a,function(e){return t.enqueue(e)}),t._version=0}else t._array=v;t._capacity=t._array.length}return Object.defineProperty(e.prototype,"count",{get:function(){return this._size},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isReadOnly",{get:function(){return!1},enumerable:!0,configurable:!0}),e.prototype.add=function(e){this.enqueue(e)},e.prototype.clear=function(){var e=this,t=e._array,r=e._head,i=e._tail,a=e._size;return i>r?n.clear(t,r,a):(n.clear(t,r,t.length-r),n.clear(t,0,i)),e._head=0,e._tail=0,e._size=0,e._version++,e.trimExcess(),a},e.prototype.dump=function(e){if(void 0===e&&(e=1/0),o["default"].isNumber(e,!1)&&0>e)throw new _["default"]("max",e,"must be greater than or equal to 0.");var t=this,r=[];if(isFinite(e))for(u["default"].assert(e,"max");e--&&t._size;)r.push(t.dequeue());else for(;t._size;)r.push(t.dequeue());return t.trimExcess(),r},e.prototype.contains=function(e){var t=this,r=t._size;if(!r)return!1;for(var i=t._array,n=t._head,o=t._capacity;r-- >0;){if(a.areEqual(i[n],e))return!0;n=(n+1)%o}return!1},e.prototype.copyTo=function(e,t){if(void 0===t&&(t=0),null==e)throw new Error("ArgumentNullException: array cannot be null.");i(t,"arrayIndex");var r=this,a=r._size;if(a){var o=a,u=r._array,c=r._capacity,s=r._head,p=c-s,y=a>p?p:a;return n.copyTo(u,e,s,t,y),o-=y,o>0&&n.copyTo(u,e,0,t+c-s,o),e}},e.prototype.toArray=function(){var e=this,t=e._size,r=n.initialize(t);return t?e.copyTo(r):r},e.prototype.remove=function(e){throw new p["default"]("ICollection<T>.remove is not implemented in Queue<T> since it would require destroying the underlying array to remove the item.")},e.prototype.dispose=function(){var e=this;e.clear(),e._array!=v&&(e._array.length=e._capacity=0,e._array=v),e._version=0},e.prototype.forEach=function(e){for(var t=this,r=t.toArray(),i=t._size,a=0;i>a&&e(r[a],a)!==!1;a++);},e.prototype.setCapacity=function(e){i(e,"capacity");var t=this,r=t._array,a=t._capacity;if(e!=a){var o=t._head,u=t._tail,c=t._size;if(r!=v&&e>a&&u>o)return r.length=t._capacity=e,void t._version++;var s=n.initialize(e);c>0&&(u>o?n.copyTo(r,s,o,0,c):(n.copyTo(r,s,o,0,a-o),n.copyTo(r,s,0,a-o,u))),t._array=s,t._capacity=e,t._head=0,t._tail=c==e?0:c,t._version++}},e.prototype.enqueue=function(e){var t=this,r=t._array,i=t._size,a=t._capacity;if(i==a){var n=a*d;a+f>n&&(n=a+f),t.setCapacity(n),r=t._array,a=t._capacity}var o=t._tail;r[o]=e,t._tail=(o+1)%a,t._size=i+1,t._version++},e.prototype.dequeue=function(e){void 0===e&&(e=!1);var t=this;{if(0!=t._size){var r=t._array,i=t._head,a=t._array[i];return r[i]=null,t._head=(i+1)%t._capacity,t._size--,t._size<t._capacity/2&&t.trimExcess(l),t._version++,a}if(e)throw new y["default"]("Cannot dequeue an empty queue.")}},e.prototype.tryDequeue=function(e){if(!this._size)return!1;var t=this.dequeue();return e&&e(t),!0},e.prototype._getElement=function(e){i(e,"index");var t=this;return t._array[(t._head+e)%t._capacity]},e.prototype.peek=function(){if(0==this._size)throw new y["default"]("Cannot call peek on an empty queue.");return this._array[this._head]},e.prototype.trimExcess=function(e){var t=this,r=t._size;r<Math.floor(.9*t._capacity)&&(isNaN(e)||r>e)&&t.setCapacity(r)},e.prototype.getEnumerator=function(){var e,t,r=this;return new c["default"](function(){t=r._version,e=0},function(i){if(t!=r._version)throw new y["default"]("Collection was changed during enumeration.");return e==r._size?i.yieldBreak():i.yieldReturn(r._getElement(e++))})},e}();Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=m});
//# sourceMappingURL=Queue.js.map
