-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
94 lines (81 loc) · 1.52 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
function clamp(v, min, max) {
return Math.min(Math.max(v, min), max);
}
function hash(num) {
num ^= num << 13;
num ^= num >> 17;
num ^= num << 5;
return (num * 0x4f6cdd1d) | 0;
}
function time(description, fn) {
let startTime = Date.now();
let ret = new Promise((resolve) => {
requestAnimationFrame(() => {
let r = fn();
let endTime = Date.now();
console.log(description, (endTime - startTime) / 1000);
resolve(r);
});
});
return ret;
}
const M = 1<<30
function randf(pos, seed) {
let r = hash(pos.y*7 ^ hash(pos.x * 11 ^ hash(seed)));
return Math.abs((r % M)/M);
}
function scaleExp(n, growth) {
if (n === 0 || growth === 0) {
return 1;
}
let total = 0;
for (let i=0; i<n; ++i) {
total += Math.pow(growth, i);
}
return 1/total;
}
class PriorityFringe {
constructor(keyfn) {
this.items = new PriorityQueue(keyfn);
}
put(item) {
this.items.add(item);
}
take() {
return this.items.remove()
}
isEmpty() {
return this.items.heap.length === 0;
}
forEach(fn) {
this.items.heap.forEach(fn);
}
}
class RandomFringe {
constructor(seed) {
this.seed = seed;
this.items = [];
}
put(item) {
this.items.push(item);
}
take() {
this.seed = hash(this.seed);
let ind = Math.abs(this.seed) % this.items.length;
let last = this.items.pop();
if (ind === this.items.length) {
return last;
} else {
let item = this.items[ind];
this.items[ind] = last;
return item;
}
}
isEmpty() {
return this.items.length === 0;
}
forEach(fn) {
this.items.forEach(fn);
}
}