-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
79 lines (68 loc) · 1.92 KB
/
util.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
/* ----------------------------------------------My Util -----------------------------------------------------*/
//在HashTable中
export function loseloseHashCode(key){
if(typeof key==="number"){
return key;
}
const tableKey=this.toStrFn(key);
let hash=0;
for(let i=0;i<tableKey.length;i++){
hash+=tableKey.charCodeAt(i);//String类的charCodeAt方法会返回指定位置字符的ASCII码
}
return hash%37;
}
//在HashTable中
export function djb2HashCode(key){
const tableKey=this.toStrFn(key);
let hash=5381;
for(let i=0;i<tableKey.length;i++){
hash=(hash*33)+tableKey.charCodeAt(i);
}
return hash%1013;
}
/* ----------------------------------------------My Util -----------------------------------------------------*/
export const Compare = {
LESS_THAN: -1,
BIGGER_THAN: 1,
EQUALS: 0
};
export const DOES_NOT_EXIST = -1;
export function lesserEquals(a, b, compareFn) {
const comp = compareFn(a, b);
return comp === Compare.LESS_THAN || comp === Compare.EQUALS;
}
export function biggerEquals(a, b, compareFn) {
const comp = compareFn(a, b);
return comp === Compare.BIGGER_THAN || comp === Compare.EQUALS;
}
export function defaultCompare(a, b) {
if (a === b) {
return Compare.EQUALS;
}
return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN;
}
export function defaultEquals(a, b) {
return a === b;
}
export function defaultToString(item) {
if (item === null) {
return 'NULL';
} else if (item === undefined) {
return 'UNDEFINED';
} else if (typeof item === 'string' || item instanceof String) {
return `${item}`;
}
return item.toString();
}
export function swap(array, a, b) {
/* const temp = array[a];
array[a] = array[b];
array[b] = temp; */
[array[a], array[b]] = [array[b], array[a]];
}
export function reverseCompare(compareFn) {
return (a, b) => compareFn(b, a);
}
export function defaultDiff(a, b) {
return Number(a) - Number(b);
}