Commits

Anonymous committed 1424dc1

Added utils.js

Comments (0)

Files changed (1)

+
+Array.prototype.maxBy = function(f) {
+    var max = this[0];
+    for (var i = 1; i < this.length; i++) {
+        if (f(this[i]) > f(max)) {
+            max = this[i];
+        }
+    }
+
+    return max;
+};
+
+Array.prototype.minBy = function(f) {
+    var min = this[0];
+    for (var i = 1; i < this.length; i++) {
+        if (f(this[i]) < f(min)) {
+            min = this[i];
+        }
+    }
+
+    return min;
+};
+
+var log = function (message) {
+    if (window.console !== undefined && window.console.log !== undefined) {
+        console.log(message);
+    }
+};
+
+function gcd(a,b)
+{
+    var temp;
+    if(a < 0) {a = -a;};
+    if(b < 0) {b = -b;};
+    if(b > a) {temp = a; a = b; b = temp;};
+    while (true) {
+       a %= b;
+       if(a == 0) {return b;};
+       b %= a;
+       if(b == 0) {return a;};
+    };
+    return b;
+}
+
+/**
+ * Searchs for the given value in an ordered array and returns its index.
+ *
+ * @param x The value to lookup.
+ * @param f A function to apply to the array elements before comparison.
+ * @param fail_ Function called when the search fails. Receives the last search
+ *              index. This argument is optional: by default returns -1.
+ * @return If the value is found, returns its index.
+ *         If the value is not found, returns the result of calling fail().
+ * @example [1, 2, 3, 4].binSearchBy(3, function (x) { return x; }) === 2
+ *          [[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]
+ *            .binSearch(3, function(x) { return x[0]; }) === 2
+ *          [1, 2, 4].binSearchBy(3, function (x) { return x; }) === -1
+ */
+Array.prototype.binSearchBy = function (x, f, fail_) {
+    var begin = 0,
+        end = this.length - 1,
+        middle,
+        point,
+        fail = fail_ || function(middle) { return -1; };
+
+        log(this[0].id);
+    while (begin <= end) {
+        middle = Math.floor((end - begin) / 2 + begin);
+        point = this[middle];
+
+        if (x < f(point)) {
+            end = middle - 1;
+        } else if (x > f(point)) {
+            begin = middle + 1;
+        } else {
+            return middle;
+        }
+    }
+
+    return fail(middle);
+};
+
+/**
+ * Searchs for the given value in an ordered array and returns its index.
+ *
+ * @param x The value to lookup.
+ * @param f A function to apply to the array elements before comparison.
+ * @return If the value is found, returns its index.
+ *         If the value is not found, returns the result of calling fail().
+ * @example [1, 2, 3, 4].searchBy(3, function (x) { return x; }) === 2
+ *          [[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]
+ *            .search(3, function(x) { return x[0]; }) === 2
+ *          [1, 2, 4].searchBy(3, function (x) { return x; }) === -1
+ */
+Array.prototype.searchBy = function (x, f) {
+    var begin = 0,
+        end = this.length - 1;
+
+    while (begin <= end) {
+    	if (x == f(this[begin])) {
+    		return begin;
+    	} 
+    	
+    	begin ++;
+    }
+
+    return -1;
+};