-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsefulFunctions.gs
104 lines (89 loc) · 2.2 KB
/
UsefulFunctions.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/** Collection of helpful functions that don't fit anywhere else */
/**
* search through a sorted an array sorted by col, for a specified key.
* returns value
* returns -1 if not found
*/
function binarySearch(arr, key, index, low, high) {
var mid = parseInt((high + low) / 2);
if (arr[mid][0] == key) {
return arr[mid][index]
} else if (low >= high) {
return -1
} else if (arr[mid][0] > key) {
return binarySearch(arr, key, index, low, mid - 1)
} else {
return binarySearch(arr, key, index, mid + 1, high)
}
}
/*
* indexOf() - To find the index of a specific key in the column of a 2D array
* Input
* arr: array to search
* key: the value to find
* col: which column to search
* low/high: range in which to search for the key
*
* output: index position of the key
*
* example: indexOf([a,b,c,f,h,j],a,0,0,5) returns 0.
* returns -1 if not found
*/
function indexOf(arr, key, col, low, high) {
var mid = parseInt((high + low) / 2);
if (arr[mid][col] == key) {
return mid
} else if (low >= high) {
return -1
} else if (arr[mid][col] > key) {
return indexOf(arr, key, col, low, mid - 1)
} else {
return indexOf(arr, key, col, mid + 1, high)
}
}
/*
* Function mergeArrays
* Smears an array a b into an array a.
* inputs: arrays a, b
* returns: array c
*
* example:
* mergeArrays( [1,2,3,4],[a, ,b, ,c] )
* gives the result
* [a, 1, b, 2, c, 3, 4]
*
*/
function mergeArrays(aArr, bArr) {
var row = 0,
bLen = bArr.length,
aLen = aArr.length;
var result = new Array();
if (aLen == 0) {
return bArr
}
for (var i = 0; i < (aLen); i++) {
// find first vacant row to throw the ID in
while (bArr[row] != "" && row < bArr.length) {
result[row] = bArr[row]
row++
}
// Vacant row found. put an ID from IDs in the empty space
result[row] = aArr[i]
row++
}
return result
}
/**
*
* sort a 2D array by column k
*
*/
function sortBy(k) {
return function (a, b) {
if (a[k] === b[k]) {
return 0;
} else {
return (a[k] < b[k]) ? -1 : 1;
}
}
}