Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 46 additions & 52 deletions js/helper.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
/* use strict-mode provided by ecma-script5, see http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ for details */
"use strict";
'use strict';

/*
* checks, whether two arrays (this and parameter that) have elements in common
* true, when this is the case
*/
Array.prototype.haveIntersection = function (that) {
var intersect = false;
for (var i in this) {
for (var j in that) {
if (this[i] === that[j] && this.hasOwnProperty(i) && that.hasOwnProperty(j)) {
intersect = true;
break;
}
}
if (intersect) break;
}
return intersect;
};
/*
* create Object.create method, if not already existing
* mainly neccessary because of Opera, which does not implement it yet
*/
(function () {
if (Object.create)
return;
Object.create = function (o) {
if (arguments.length > 1) {
throw new Error('Object.create implementation only accepts the first parameter.');
}
function F() {};
F.prototype = o;
return new F();
};
})();
Array.prototype.haveIntersection = (function (that) {
let set = new Set(this);
return that.some((val) => set.has(val));
})(
/*
* create Object.create method, if not already existing
* mainly neccessary because of Opera, which does not implement it yet
*/
function () {
if (Object.create) return;
Object.create = function (o) {
if (arguments.length > 1) {
throw new Error('Object.create implementation only accepts the first parameter.');
}
function F() {}
F.prototype = o;
return new F();
};
}
)();
/*
* Given an arbitrary number of arrays, these function calculates the Cartesian Product
* Taken from: http://gotochriswest.com/blog/2011/05/02/cartesian-product-of-multiple-arrays/
Expand All @@ -45,33 +36,36 @@ Array.prototype.haveIntersection = function (that) {
* - https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/foreach
*/
Array.cartesianProduct = function () {
return Array.prototype.reduce.call(arguments, function(previousValue, currentValue) {
var ret = [];
previousValue.forEach(function(previousValue) {
currentValue.forEach(function(currentValue) {
ret.push(previousValue.concat([currentValue]));
});
});
return ret;
}, [[]]);
return Array.prototype.reduce.call(
arguments,
function (previousValue, currentValue) {
var ret = [];
previousValue.forEach(function (previousValue) {
currentValue.forEach(function (currentValue) {
ret.push(previousValue.concat([currentValue]));
});
});
return ret;
},
[[]]
);
};


// specifically for arrays sorted using the same order
Array.prototype.subsetOfSorted = function (other) {
var j = 0;
for (var i = 0; i < this.length; i += 1) {
while (this[i] !== other[j]) {
++j;
if (j >= other.length) {
return false
}
}
}
return true;
var j = 0;
for (var i = 0; i < this.length; i += 1) {
while (this[i] !== other[j]) {
++j;
if (j >= other.length) {
return false;
}
}
}
return true;
};

/* returns an array's last element */
Array.prototype.last = function () {
return this[this.length - 1];
}
return this[this.length - 1];
};