UNPKG

oset

Version:

Ordered Set Data Structure

33 lines (31 loc) 8.98 kB
/* ** OSet -- Ordered Set Data Structure ** Copyright (c) 2015-2023 Dr. Ralf S. Engelschall <rse@engelschall.com> ** ** Permission is hereby granted, free of charge, to any person obtaining ** a copy of this software and associated documentation files (the ** "Software"), to deal in the Software without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Software, and to ** permit persons to whom the Software is furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be included ** in all copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.OSet = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(_dereq_,module,exports){ "use strict";function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,_toPropertyKey(i.key),i)}}function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(e){var t=_toPrimitive(e,"string");return"symbol"===_typeof(t)?t:String(t)}function _toPrimitive(e,t){if("object"!==_typeof(e)||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var i=n.call(e,t||"default");if("object"!==_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var OMap=function(){function e(){return _classCallCheck(this,e),this._items=0,this._index={},this._ring={},this._ring.prev=this._ring,this._ring.next=this._ring,this}return _createClass(e,[{key:"size",value:function(){return this._items}},{key:"keys",value:function(){return this.each(function(e,t){this.push(t)},[])}},{key:"values",value:function(){return this.each(function(e){this.push(e)},[])}},{key:"find",value:function(e,t){return arguments<2&&(t=this),this.each(function(n,i,r){e.call(t,n,i,r)&&this.push(n)},[])}},{key:"each",value:function(e,t){arguments<2&&(t=this);for(var n=0,i=this._ring.next;i!==this._ring;)e.call(t,i.val,i.key,n++),i=i.next;return t}},{key:"has",value:function(e){return void 0!==this._index[e]}},{key:"get",value:function(e){var t=this._index[e];if(void 0!==t)return t.val}},{key:"set",value:function(e,t,n){var i=this._index[e];return void 0===i?(i={key:e,val:t},this._index[e]=i,n?(i.next=this._ring.next,i.prev=this._ring,i.next.prev=i,this._ring.next=i):(i.prev=this._ring.prev,i.next=this._ring,i.prev.next=i,this._ring.prev=i),this._items++):i.val=t,this}},{key:"del",value:function(e){var t=this._index[e];if(void 0===t)throw new Error("del: no such item");return delete this._index[e],t.prev.next=t.next,t.next.prev=t.prev,delete t.prev,delete t.next,this._items--,this}},{key:"clear",value:function(){for(;this._items>0;)this.del(this._ring.next.key);return this}},{key:"merge",value:function(e){var t=this;return e.each(function(e,n){t.set(n,e)}),e.clear(),this}},{key:"union",value:function(t){var n=new e;return this.each(function(e,t){n.set(t,e)}),t.each(function(e,t){n.set(t,e)}),n}},{key:"intersection",value:function(t){var n=new e;return this.each(function(e,i){if(t.has(i)){if(t.get(i)!==e)throw new Error('intersect: different values under key "'+i+'"');n.set(i,e)}}),n}},{key:"difference",value:function(t){var n=new e;return this.each(function(e,i){t.has(i)||n.set(i,e)}),n}},{key:"sort",value:function(e){var t=this;"function"!=typeof e&&(e=function(e,t){return e<t?-1:e>t?1:0});var n=this.each(function(e,t){this.push({key:t,val:e})},[]);return n.sort(function(t,n){return e(t.key,n.key)}),this.clear(),n.forEach(function(e){t.set(e.key,e.val)}),this}}]),e}();exports.default=OMap; },{}],2:[function(_dereq_,module,exports){ "use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,_toPropertyKey(r.key),r)}}function _createClass(t,e,n){return e&&_defineProperties(t.prototype,e),n&&_defineProperties(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function _toPropertyKey(t){var e=_toPrimitive(t,"string");return"symbol"===_typeof(e)?e:String(e)}function _toPrimitive(t,e){if("object"!==_typeof(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!==_typeof(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var OSet=function(){function t(){return _classCallCheck(this,t),this._items=0,this._ring={},this._ring.prev=this._ring,this._ring.next=this._ring,this}return _createClass(t,[{key:"size",value:function(){return this._items}},{key:"values",value:function(){return this.each(function(t){this.push(t)},[])}},{key:"find",value:function(t,e){return arguments<2&&(e=this),this.each(function(n,r){t.call(e,n,r)&&this.push(n)},[])}},{key:"each",value:function(t,e){arguments<2&&(e=this);for(var n=0,r=this._ring.next;r!==this._ring;)t.call(e,r.val,n++),r=r.next;return e}},{key:"has",value:function(t){for(var e=this._ring.next;e!==this._ring;){if(e.val===t)return!0;e=e.next}return!1}},{key:"add",value:function(t,e){if(!this.has(t)){var n={val:t};e?(n.next=this._ring.next,n.prev=this._ring,n.next.prev=n,this._ring.next=n):(n.prev=this._ring.prev,n.next=this._ring,n.prev.next=n,this._ring.prev=n),this._items++}return this}},{key:"del",value:function(t){for(var e=this._ring.next;e!==this._ring&&e.val!==t;)e=e.next;if(e===this._ring)throw new Error("del: no such item");return e.prev.next=e.next,e.next.prev=e.prev,delete e.prev,delete e.next,this._items--,this}},{key:"clear",value:function(){for(;this._items>0;)this.del(this._ring.next.val);return this}},{key:"merge",value:function(t){var e=this;return t.each(function(t){e.add(t)}),t.clear(),this}},{key:"union",value:function(e){var n=new t;return this.each(function(t){n.add(t)}),e.each(function(t){n.add(t)}),n}},{key:"intersection",value:function(e){var n=new t;return this.each(function(t){e.has(t)&&n.add(t)}),n}},{key:"difference",value:function(e){var n=new t;return this.each(function(t){e.has(t)||n.add(t)}),n}},{key:"sort",value:function(t){var e=this;"function"!=typeof t&&(t=function(t,e){return t<e?-1:t>e?1:0});var n=this.each(function(t){this.push(t)},[]);return n.sort(t),this.clear(),n.forEach(function(t){e.add(t)}),this}}]),t}();exports.default=OSet; },{}],3:[function(_dereq_,module,exports){ "use strict";var _osetSet=_interopRequireDefault(_dereq_("./oset-set")),_osetMap=_interopRequireDefault(_dereq_("./oset-map"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}module.exports={OSet:_osetSet.default,OMap:_osetMap.default}; },{"./oset-map":1,"./oset-set":2}]},{},[1,2,3])(3) });