-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoinSQL.js
305 lines (260 loc) · 8.12 KB
/
joinSQL.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Sort the first colomn of the array using a string compare function. If (a.length < b.length) => -1, (a.lenght = b.length) => compare the character. if (a.length > b.lenght) = 1
*
* @param {*[][]} arr array to be sorted
* @param {function(*a, *b)} newCallBack a new function to compare the value of the first column of the array. 0 otherwise
* @return {*[][]} the sorted array
*/
function mySort(arr,newCallBack){
var s = new QuickSort(newCallBack);
return s.sort(arr);
}
/**
* Merge two array with the same primary key in the first column of the two array using a string compare function. If (a.length < b.length) => -1, (a.lenght = b.length) => compare the character. if (a.length > b.lenght) = 1
*
* @param {*[][]} arr1 first array to be merge, arr1[0] is the key to compare with arr2[0]
* @param {*[][]} arr2 second array to be merge
* @param {function(*a, *b)} newCallBack a new function to compare the value of the array. 0 otherwise
* @return {*[][]} the merge array
*/
function myMerge(arr1,arr2,newCallBack){
var arr3;
if(arr1.length <= arr2.length)
arr3 = mergePrivate(arr1,arr2,newCallBack);
else
arr3 = mergePrivate(arr2,arr1,newCallBack);
return arr3;
}
/**
* The function implement the classical join of sql. The first array must have the first column as foreign key, and the second array must have the first column as primary key
*
* @param {*[][]} arr1 the first table, foreign key must the first coloumn
* @param {*[][]} arr2 the secon table, primary key must be the first coulmn
* @param {function(*a, *b)} newCallBack a new function to compare the value of the array. 0 otherwise
* @return {*[][]} the joined array
*/
function myJoin(arr1,arr2,newCallBack){
return mergePrivate(arr2,arr1,newCallBack);
}
/**
* Private method for myMerge function
*
* @param {*[][]} arr1 first array to be merge, arr1[0] is the key to compare with arr2[0]
* @param {*[][]} arr2 second array to be merge
* @param {function(*a, *b)} newCallBack a new function to compare the value of the array. 0 otherwise
* @return {*[][]} the merge array
*/
function mergePrivate(arr1,arr2, newCallBack){
var arr3,arr4,arr5;
arr3 = new Array();
arr5 = new Array();
var s = new QuickSort(newCallBack);
var ss = new Search(newCallBack)
var i =0;
var index = 0;
arr4 = s.sort(arr1);
for(i=0;i< arr2.length;i++){
if((index = ss.binarySearch(arr4, arr2[i][0]))>=0){
for(j=1;j<arr2[i].length;j++)
arr5.push(arr2[i][j]);
arr3.push(arr4[index].concat(arr5));
arr5 = new Array();
}
}
return (arr3.length !=0)?arr3:"there is no values";
}
/**
* Comparator : Class to implement algorithms to do the search of each value
*
* @param {function(a: *,b: *)} newCallBack If provided then all elements comparisons will be done through this callback.
*/
class Comparator {
constructor(newCallBack){
this.compare = ((newCallBack == 0)? this.compareDefault : newCallBack);
}
/**
* Default comparison function. It just assumes that "a" and "b" are strings .
*
* @param {(string|number)} a
* @param {(string|number)} b
* @returns {number}
*/
compareDefault(a, b) {
var i =0;
var ret = 0;
var temp =0;
a = (a.toString());
b = (b.toString());
if(a.length == b.length){
for(i=0;i<a.length;i++){
if((temp=((a.charAt(i)).charCodeAt() - (b.charAt(i)).charCodeAt()))<0){
ret = -1;
break;
}else{
if(temp >0){
ret = 1;
break;
}
}
}
}else{
if(a.length<b.length){
ret = -1;
}else{
ret = 1;
}
}
return ret;
}
/**
* Checks if two variables are equal.
*
* @param {*} a
* @param {*} b
* @return {boolean}
*/
equal(a, b) {
return this.compare(a, b) === 0;
}
/**
* Checks if variable "a" is less than "b".
*
* @param {*} a
* @param {*} b
* @return {boolean}
*/
lessThan(a, b) {
return this.compare(a, b) < 0;
}
/**
* Checks if variable "a" is greater than "b".
*
* @param {*} a
* @param {*} b
* @return {boolean}
*/
greaterThan(a, b) {
return this.compare(a, b) > 0;
}
/**
* Checks if variable "a" is less than or equal to "b".
*
* @param {*} a
* @param {*} b
* @return {boolean}
*/
lessThanOrEqual(a, b) {
return this.lessThan(a, b) || this.equal(a, b);
}
/**
* Checks if variable "a" is greater than or equal to "b".
*
* @param {*} a
* @param {*} b
* @return {boolean}
*/
greaterThanOrEqual(a, b) {
return this.greaterThan(a, b) || this.equal(a, b);
}
}
/**
* Sort class
*
* @param {function(a: *, b: *)} cnewCallback - If provided then all elements comparisons will be done through this callback.
*/
class Sort {
constructor(newCallBack) {
this.comparator = new Comparator(newCallBack);
}
sort() {
throw new Error('sort method must be implemented');
}
}
/**
* Quick Sort class
*
* @param {function(a: *, b: *)} newCallback - If provided then all elements comparisons will be done through this callback.
*/
class QuickSort extends Sort {
constructor(newCallBack){
super(newCallBack);
}
/**
* sort function of quicksort class
*
* @param {*[]} originalArray
* @return {*[]}
*/
sort(originalArray) {
// Clone original array to prevent it from modification.
var array = originalArray;
// If array has less than or equal to one elements then it is already sorted.
if (array.length <= 1) {
return array;
}
// Init left and right arrays.
const leftArray = [];
const rightArray = [];
// Take the first element of array as a pivot.
const pivotElement = array.shift();
const centerArray = [pivotElement];
// Split all array elements between left, center and right arrays.
while (array.length) {
const currentElement = array.shift();
if (this.comparator.equal(currentElement, pivotElement)) {
centerArray.push(currentElement);
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
leftArray.push(currentElement);
} else {
rightArray.push(currentElement);
}
}
// Sort left and right arrays.
const leftArraySorted = this.sort(leftArray);
const rightArraySorted = this.sort(rightArray);
// Let's now join sorted left array with center array and with sorted right array.
return leftArraySorted.concat(centerArray, rightArraySorted);
}
}
/**
* Binary search implementation.
*
* @param {function(a: *,b: *)} newCallBack If provided then all elements comparisons will be done through this callback.
*/
class Search {
constructor(newCallBack) {
this.comparator = new Comparator(newCallBack);
}
/*
* BinarySearch
*
* @param {*[]} sortedArray
* @param {*} seekElement
* @return {number}
*/
binarySearch(sortedArray, seekElement) {
// These two indices will contain current array (sub-array) boundaries.
let startIndex = 0;
let endIndex = sortedArray.length - 1;
// Let's continue to split array until boundaries are collapsed
// and there is nothing to split anymore.
while (startIndex <= endIndex) {
// Let's calculate the index of the middle element.
const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2);
// If we've found the element just return its position.
if (this.comparator.equal(sortedArray[middleIndex][0], seekElement)) {
return middleIndex;
}
// Decide which half to choose for seeking next: left or right one.
if (this.comparator.lessThan(sortedArray[middleIndex][0], seekElement)) {
// Go to the right half of the array.
startIndex = middleIndex + 1;
} else {
// Go to the left half of the array.
endIndex = middleIndex - 1;
}
}
// Return -1 if we have not found anything.
return -1;
}
}