-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.js
210 lines (196 loc) · 4.59 KB
/
Utility.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
import ObjectHelper from "./Helpers/ObjectHelper.js"
export class NumberRange {
constructor(start = 0, end = 0) {
this.start = start
this.end = end
}
isNumberIn_inclusive(number) {
return number >= this.start && number <= this.end
}
isNumberIn_exclusive(number) {
return number >= this.start && number < this.end
}
toString() {
return `${this.start} - ${this.end}`
}
middle() {
return (this.start + this.end) / 2
}
allValues(inclusive = false) {
let result = []
for (let i = this.start; i < this.end + inclusive; i++) {
result.push(i)
}
return result
}
length(inclusive = false) {
return this.end - this.start + inclusive
}
}
let next_id = 0
export function getUniqueid() {
next_id++
return next_id
}
export function getImg(img, extension = "png") {
return "./src/assets/" + img + "." + extension
}
export function functionMerger(...funcs) {
let list = funcs
for (let i = 0; i < list.length; i++) {
if (!(list[i] instanceof Function)) {
let ret_value = list[i]
list[i] = () => ret_value
}
}
let merger = (...args) => {
let obj = {}
for (const func of list) {
obj = ObjectHelper.merge(obj, func(...args))
}
return obj
}
return merger
}
export class Node {
constructor(value) {
this.value = value
}
getValue() {
return this.value
}
toString() {
return this.value.toString()
}
}
export class DoubleLinkedList {
constructor() {
this.length = 0
}
addValue(value) {
if (this.head == undefined) {
this.head = new Node(value)
this.tail = this.head
} else {
let newNode = new Node(value)
this.tail.next = newNode
newNode.prev = this.tail
this.tail = newNode
}
this.length += 1
}
removeNodeAt(index) {
if (index >= this.length) {
console.warn("cant remove node at index", index)
return false
}
if (index == 0) {
if (this.length == 1) {
this.head = undefined
this.tail = undefined
} else {
this.head = this.head.next
this.head.prev = undefined
}
} else if (index == this.length - 1) {
this.tail = this.tail.prev
this.tail.next = undefined
} else {
let i = 0
let node = this.head
while (true) {
if (i == index) {
node.prev.next = node.next
node.next.prev = node.prev
break
}
i++
node = node.next
}
}
this.length -= 1
return true
}
removeNode(node) {
if (node == this.tail) {
this.tail.prev.next = undefined
this.tail = this.tail.prev
} else if (node == this.head) {
this.head.next.prev = undefined
this.head = this.head.next
} else {
node.next.prev = node.prev
node.prev.next = node.next
}
this.length--
}
addNodeAfter(node, value) {
if (!(value instanceof Node)) {
value = new Node(value)
}
value.prev = node
value.next = node.next
if (node == this.tail) {
this.tail = value
} else if (node.next) node.next.prev = value
node.next = value
this.length++
}
connect2Nodes(leftNode, rightNode) {
let length = 0
let node = leftNode.next
while (node != rightNode) {
if (node == undefined) {
console.error(leftNode, "and", rightNode, "are not in order!")
return false
}
length++
node = node.next
}
this.length -= length
leftNode.next = rightNode
if (rightNode) rightNode.prev = leftNode
return true
}
cutAt(endNode) {
let node = endNode.next
let length = 0
while (node != undefined) {
length++
node = node.next
}
this.length -= length
this.tail = endNode
endNode.next = undefined
}
toString() {
let result = ""
let node = this.head
while (node != undefined) {
result += node.toString() + " -> "
node = node.next
}
return result
}
*[Symbol.iterator]() {
let node = this.head
while (node != null) {
yield node.value
node = node.next
}
}
toArray() {
let result = []
for (let value of this) result.push(value)
return result
}
sort(comparator = (a, b) => a - b) {
let array = this.toArray()
array.sort(comparator)
this.head = undefined
this.tail = undefined
this.length = 0
for (const value of array) this.addValue(value)
return this
}
}