e-structures
Version:
Extended data structures library for JavaScript
1 lines • 11.2 kB
JavaScript
var eGlobal={};"undefined"!=typeof module&&null!==module&&module.exports&&(module.exports=eGlobal);var eLinkedList=function(){var e=null,t=null,n=0,r={},i=function(){return{data:null,next:null}};return r.getType=function(){return"eLinkedList"},r.size=function(){return n},r.getFirst=function(){return e},r.getLast=function(){return t},r.isEmpty=function(){return n<=0},r.nodeAtPosition=function(e){var t,i;if(!("number"!=typeof e||e<1||e>n)){if(e===n)return r.getLast();for(t=r.getFirst(),i=1;i<e;i++)t=t.next;return t}},r.remove=function(i){var o,u;if(!(!i||i<1||i>n))return 1===i?(u=e,e=e.next,n--,u):i===n?(o=r.nodeAtPosition(i-1),u=t,o.next=null,n--,u):(o=r.nodeAtPosition(i-1),u=r.nodeAtPosition(i),o.next=r.nodeAtPosition(i+1),n--,u)},r.clear=function(){e=null,t=null,n=0},r.removeFirst=function(){return n>0&&(e=e.next,n--,!0)},r.add=function(o){return o?(null===e?(e=i(),t=e):(t.next=i(),t=t.next),t.data=o,n++,r.size()):-1},r.set=function(e,t){if(arguments.length<2||"number"!=typeof e||e<1||e>n)return!1;var i=r.nodeAtPosition(e);return i.data=t,!0},r.insertAsFirst=function(t){var o=i();return o.next=e,e=o,o.data=t,n++,r.size()},r.iterate=function(t){for(var n=e,r=1;null!==n;)t(n.data,r),n=n.next,r++},r.toArray=function(){var e=[];return r.iterate(function(t){e.push(t)}),e},r.equal=function(e){if(!e||!e.hasOwnProperty("getType")||e.getType()!==this.getType())return!1;if(this.size()!==e.size())return!1;var t=!0,n=!0;return this.iterate(function(r,i){var o=eCompare(e.nodeAtPosition(i).data,r);o?t=o:n=o}),n===!1?n:t},r};eGlobal.eLinkedList=eLinkedList;var eList=function(){var e=function(e){return"object"==typeof e&&(!!e.hasOwnProperty("getType")&&("eList"===e.getType()&&void 0))},t=function(e){return!("number"!=typeof e||e%1!==0||e<0)},n={};n.getType=function(){return"eList"};var r=[];n.size=function(){return r.length},n.add=function(e,i){if(0===arguments.length||arguments.length>2)return-1;if(1===arguments.length)return r.push(arguments[0]),n.size();if(2===arguments.length){if(t(e)===!1)return-1;if(r.length-1<e)return r.push(i),n.size();if(e<r.length-1)return r.splice(e,0,i),n.size()}return-1},n.addAll=function(i,o){if(0===arguments.length||arguments.length>2)return-1;if(1===arguments.length)return e(arguments[0])===!1?null:(r=r.concat(arguments[0].toArray()),n.size());if(2===arguments.length){if(t(i)===!1)return-1;if(e(o)===!1)return null;if(r.length-1<i)return r=r.concat(o.toArray()),n.size();if(i<r.length-1)return o.getIterator().iterate(function(e,t){r.splice(i,0,e),i++}),n.size()}return-1},n.set=function(e,i){return arguments.length<2||arguments.length>2||t(e)===!1||e>n.size()-1?-1:(r[e]=i,r[e])},n.get=function(e){return void 0===e||e<0||0===n.size()||arguments.length>1||e>=n.size()?-1:t(e)===!1?-1:r[e]},n.remove=function(e){return void 0===e||e<0||0===n.size()||arguments.length>1||e>=n.size()?-1:t(e)===!1?-1:r.splice(e,1)},n.toArray=function(){return r},n.toString=function(){return r.toString()},n.forEach=function(e){n.getIterator().iterate(e)};var i={currentPosition:0,next:function(){var e=n.get(n.getIterator().currentPosition);return this.currentPosition+=1,e},hasNext:function(){return this.currentPosition<n.size()},iterate:function(e){for(;n.getIterator().hasNext();)e(n.getIterator().next(),n.getIterator().currentPosition);n.getIterator().reset()},reset:function(){n.getIterator().currentPosition=0}};return n.getIterator=function(){return i},n.equal=function(e,t){if(!e||!e.hasOwnProperty("getType")||"eList"!==e.getType())return!1;if(this.size()!==e.size())return!1;var n=t||eCompare,r=!0,i=!0;return this.forEach(function(t,o){var u=n(e.get(o-1),t);u?r=u:i=u}),i?r:i},n};eGlobal.eList=eList;var eMap=function(){var e=function(e){return!(e.length<2||e.length>2)&&"string"==typeof e[0]},t=function(){return n.length===r.length},n=[],r=[],i={};i.getType=function(){return"eMap"},i.size=function(){return t()?r.length:-1},i.isEmpty=function(){return t()?0===r.length:-1},i.clear=function(){return n=[],r=[],i.size()},i.containsKey=function(e){if(!e)throw new eException((!1),"No argument provided. Key must be provided",0);return n.indexOf(e.toLowerCase())!==-1},i.put=function(o,u){return e(arguments)&&!i.containsKey(o)&&t()?(n.push(o.toLowerCase()),r.push(u),i.size()):-1},i.set=function(e,t){return e&&i.containsKey(e)&&t?(r[n.indexOf(e.toLowerCase())]=t,i.get(e)):-1},i.get=function(e){if(e&&i.containsKey(e))return r[n.indexOf(e.toLowerCase())]},i.getKeySet=function(){return n},i.remove=function(e){if(e&&i.containsKey(e)){var t=n.indexOf(e);return n.splice(t,1),r.splice(t,1),i.size()}},i.values=function(){var e=new eList;return i.getIterator().iterate(function(t,n){e.add(n)}),e};var o={currentPosition:0,next:function(){var e=n[i.getIterator().currentPosition],t=r[i.getIterator().currentPosition];return i.getIterator().currentPosition+=1,[e,t]},hasNext:function(){return i.getIterator().currentPosition<i.size()},iterate:function(e){for(;i.getIterator().hasNext();){var t=i.getIterator().next();e(t[0],t[1])}i.getIterator().reset()},reset:function(){i.getIterator().currentPosition=0}};return i.getIterator=function(){return o},i};eGlobal.eMap=eMap;var ePriorityQueue=function(e){var t={};t.getType=function(){return"ePriorityQueue"};var n=[];return t.size=function(){return n.length},t.put=function(r){return 0===arguments.length?-1:(n.push(r),e&&"function"==typeof e?n.sort(e):n.sort(),t.size())},t.peek=function(){return 0===n.length?-1:n[0]},t.remove=function(){return 0===n.length?-1:n.shift()},t.clear=function(){n=[]},t.toArray=function(){return n},t};eGlobal.ePriorityQueue=ePriorityQueue;var eQueue=function(){var e={};e.getType=function(){return"eQueue"};var t=[];return e.size=function(){return t.length},e.put=function(n){return 0===arguments.length?-1:(t.push(n),e.size())},e.peek=function(){return 0===t.length?-1:t[0]},e.remove=function(){return 0===t.length?-1:t.shift()},e.clear=function(){t=[]},e.toArray=function(){return t},e};eGlobal.eQueue=eQueue;var eSet=function(e){var t={},n=[];return t.getType=function(){return"eSet"},t.size=function(){return n.length},t.add=function(e){return!(!e||t.exists(e))&&(n.push(e),!0)},t.get=function(e){if(e&&!(e>n.length-1))return n[e]},t.addAll=function(e){if(e&&e instanceof Array)for(var t=0;t<e.length;t++)this.add(e[t]);return!1},t.exists=function(e){if(!e)return!1;for(var t=0;t<n.length;t++)if(eCompare(n[t],e))return!0;return!1},t.pop=function(){if(0!==this.length){var e=n.splice(n.length-1,1);return e[0]}},t.shift=function(){if(0!==this.length){var e=n.splice(0,1);return e[0]}},t.union=function(e){if(e&&(e instanceof Array||e.getType()===this.getType())){var t;return e instanceof Array?(t=new eSet(e),t.addAll(e),t):(t=new eSet(this.toArray()),t.addAll(e.toArray()),t)}return-1},t.intersection=function(e){var t=this;if(e&&(e instanceof Array||e.getType()===this.getType())){var n;return e instanceof Array?(n=new eSet,eForEach(e,function(e){t.exists(e)&&n.add(e)}),n):(n=new eSet,eForEach(e.toArray(),function(e){t.exists(e)&&n.add(e)}),n)}return-1},t.difference=function(e){var t=this;if(e&&(e instanceof Array||e.getType()===this.getType())){var n,r;return e instanceof Array?(n=new eSet,r=new eSet(e),eForEach(t.toArray(),function(e){r.exists(e)||n.add(e)}),n):(n=new eSet,eForEach(t.toArray(),function(t){e.exists(t)||n.add(t)}),n)}return-1},t.removeFromIndex=function(e){if(e&&!(e>n.length-1)){var t=n.splice(e,1);return t[0]}},t.remove=function(e){return!!e&&(t.forEach(function(t,r){if(eCompare(e,t))return n.splice(r,1),!0}),!1)},t.toggle=function(e){return!!e&&void(this.exists(e)?this.remove(e):this.add(e))},t.forEach=function(e){if("function"!=typeof e)throw new TypeError(e+" is not a function");for(var t=0;t<n.length;t++)e(n[t],t,n)},t.isMaths=function(){var e=!0;return this.forEach(function(t){"number"!=typeof t&&(e=!1)}),e},t.max=function(){if(this.isMaths())return Math.max.apply(null,this.toArray())},t.min=function(){if(this.isMaths())return Math.min.apply(null,this.toArray())},t.mean=function(){if(this.isMaths()){var e=0,t=[];return this.forEach(function(n,r,i){e+=n,t=i}),e/t.length}},t.variance=function(e){if(this.isMaths()&&this.size()>0){var t="number"==typeof e&&e%1===0?e:2,n=this.mean(),r=[],i=0;this.forEach(function(e){r.push(Math.pow(e-n,2))});for(var o=0;o<r.length;o++)i+=r[o];return parseFloat((i/(this.size()-1)).toFixed(t))}},t.standardDeviation=function(e){var t=this.variance();if(t){var n="number"==typeof e&&e%1===0?e:2;return parseFloat(Math.sqrt(t).toFixed(n))}},t.clear=function(){n=[]},t.toArray=function(){return n},e&&e instanceof Array&&t.addAll(e),t};eGlobal.eSet=eSet;var eStack=function(){var e={};e.getType=function(){return"eStack"};var t=[];return e.size=function(){return t.length},e.isEmpty=function(){return 0===t.length},e.put=function(n){return 0===arguments.length?-1:(t.unshift(n),e.size())},e.peek=function(){return e.isEmpty()?-1:t[0]},e.remove=function(){return e.isEmpty()?-1:t.shift()},e.clear=function(){t=[]},e.toArray=function(){return t},e};eGlobal.eStack=eStack;var diff=function(e,t,n,r){var i=[],o={};if(typeof e!=typeof t)return i.push({event:"different types"}),i;if("object"==typeof t&&0===Object.keys(t).length)return i.push({event:"deleted",referenceKey:"",referenceId:""}),i;if("object"!=typeof e&&"object"!=typeof t){if(e!==t)return i.push({event:"changed"}),i}else{for(var u={},f=Object.keys(e),a=0;a<f.length;a++){var c=f[a];if(t.hasOwnProperty(c))if(e[c]===t[c]);else if(e[c]instanceof Array||t[c]instanceof Array)if(e[c].length>0&&"object"!=typeof e[c][0]){var s=e[c].join(", "),l=t[c].join(", ");console.log(l),s!==l&&i.push({event:"changed",referenceKey:c,referenceId:r?r:"",oldValue:s,newValue:l})}else{for(var h=0;h<e[c].length;h++)u=find(e[c][h],t[c]),u?i=i.concat(diff(e[c][h],u,c,e[c][h]._id)):(o={event:"removed",referenceKey:c+"."+e[c][h]._id,referenceId:e[c][h]._id},i.push(o));for(var g=0;g<t[c].length;g++)u=find(t[c][g],e[c]),u||(i=i.concat(diff({},t[c][g],c,t[c][g]._id)))}else if("object"==typeof e[c]||"object"==typeof t[c]){var p="";p=n?n+"."+c:c,i=i.concat(diff(e[c],t[c],p,r))}else o=n?{event:"changed",referenceKey:n+"."+c,referenceId:r?r:"",oldValue:e[c].toString(),newValue:t[c].toString()}:{event:"changed",referenceKey:c,referenceId:r?r:"",oldValue:e[c].toString(),newValue:t[c].toString()},i.push(o);else o={event:"removed",referenceKey:c},i.push(o)}if(t)for(f=Object.keys(t),a=0;a<f.length;a++){var d=f[a];if(!e.hasOwnProperty(d)){var y="";y=n?n+"."+d:d,"object"==typeof t[d]?t[d]instanceof Array?t[d].length>0&&"object"!==t[d][0]?i.push({event:"set",referenceKey:y,referenceId:r?r:"",oldValue:"",newValue:t[d].join(", ")}):_.each(t[d],function(e){i=i.concat(diff({},e,y,r))}):i=i.concat(diff({},t[d],y,r)):(o={event:"set",referenceKey:y,referenceId:r?r:"",oldValue:"",newValue:t[d]?t[d].toString():""},i.push(o))}}}return i},find=function(e,t){for(var n=0;n<t.length;n++)return 0===diff(e,t[n]).length?t[n]:null},eException=function(e,t,n){return{getType:function(){return"eException"},status:e||null,reason:t||null,errorCode:n||null}},eCompare=function(e,t){return 0===diff(e,t).length},eForEach=function(e,t){if("function"!=typeof t)throw new TypeError(t+" is not a function");if("object"==typeof e)for(var n=Object.keys(e),r=0;r<n.length;r++){var i=n[r];t(e[i],r,e)}};