-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexList.js
352 lines (266 loc) · 9.98 KB
/
LexList.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// useful Latex help
// https://en.wikibooks.org/wiki/LaTeX/Mathematics
//use for type of Latex parameter
const SYMBOL = "SYMBOL";
const CURSOR = "CURSOR";
// use for types of print
// when printing on the screen, the current location is shoen with a square so the users know where the current cursor is
// when printing for copy, we don't print the cursor
const ON_SCREEN = "ON_SCREEN";
const FOR_COPY = "FOR_COPY";
const CUSOR_SYMBOL = "\\squar";
// lexNode is a item in the linked list - each item holds one latex item
class lexNode {
constructor(value, valueType = SYMBOL) {
this.value = value; // the value of the item
this.valueType = valueType; //type of value - this is still under constructions - will see if needed
this.next = null; // pointer to the next item in the list
this.previous = null; //pinter to the previus item in the list
}
}
// This class holds a bi direction link list that holds the Latex Expressions
// It allow to edit the forumula and move back and forth as the user create the formula
// The print list reutnr a latex string that then can be used to print on the screen
export class lexList {
constructor() {
// head is pointing to the last item in the list
this.head = {
value: CUSOR_SYMBOL,
next: null,
previous: null
};
this.length = 1; // length of the lex list
this.cursor = this.head; //point to the current lexNode
}
printList(printType = ON_SCREEN) {
let lexString = "";
let currentList = this.head;
while (currentList !== null) {
lexString = lexString + currentList.value;
currentList = currentList.next;
}
console.log(lexString);
return lexString;
}
// Insert lexNode at end of the list
append(value) {
this.insert(value);
}
/*
// Insert lexNode at the start of the list
prepend(value) {
let newNode = new lexNode(value);
newNode.next = this.head;
this.head.previous = newNode;
this.head = newNode;
this.length++;
}
*/
cursorBack(){
let previousNode = this.cursor.previous;
let nextNode = this.cursor.next;
// The cursor is already in first poistion
if (previousNode == null) {
console.log("In cursorBack: cursor already in the begining");
return;
}
let previousPreviusNode = previousNode.previous;
// set the previus node to point to the one after the cursor
previousNode.next = nextNode;
previousNode.previous = this.cursor;
// Set the cursor before the previus node
this.cursor.next = previousNode;
this.cursor.previous = previousPreviusNode;
// the cursor is in 2nd position
if(previousPreviusNode == null){
this.head = this.cursor;
} else{
// set the previus node before the previus to cursor to point to cursor
previousPreviusNode.next = this.cursor;
}
}
cursorForward(){
let previousNode = this.cursor.previous;
let nextNode = this.cursor.next;
// Cursor at the end of the list
if (nextNode == null) {
console.log("In cursorForward: cursor already in the end of the list");
return;
}
let nextNextNode = nextNode.next;
//set the cursor to point to the node ofter next
this.cursor.next = nextNextNode;
this.cursor.previous = nextNode;
//set the next node before thec cursor
nextNode.next = this.cursor;
nextNode.previous = previousNode;
//Set the prviuse node to point to next note
previousNode.next = nextNode;
//set the next next node to point to cursor
// if the cursor was only one place from the end, there is no next next node
if(nextNextNode != null){
nextNextNode.previous = this.cursor;
}
}
// Insert lexNode where the current cursor is
// we used the saved pointer to define where it is
insert (value) {
let previousNode = this.cursor.previous;
// Debug sectino
/*
if(previousNode == null ){
console.log("in Insert: previous node is null");
}else{
console.log("in Insert: previous node: " + previousNode.value );
}
*/
// insert the lexNode at the curret location of the cursor -
// we need to add the node after the current location of the cursor
let newNode = new lexNode(value);
// Set the links for the new node
newNode.next = this.cursor;
newNode.previous = previousNode;
// set the links for the cursor to be after new node
this.cursor.previous = newNode;
//console.log("in Insert: cursor previous node: " + this.cursor.previous.value );
// Set the links to the node before the cursor
// inser the newNode between previus node and cursor
if(previousNode != null){
previousNode.next = newNode;
//console.log("in Insert: cursor previous node previus node is not null" );
} else { // the previus node was the begining of the list
this.head=newNode;
//console.log("in Insert: cursor previous node is null" );
}
this.length++;
}
// Insert lexNode at a given index
/*
insert (index, value) {
if (!Number.isInteger(index) || index < 0 || index > this.length + 1) {
console.log(`Invalid index. Current length is ${this.length}.`);
return this;
}
// If index is 0, prepend
if (index === 0) {
this.prepend(value);
return this;
}
// If index is equal to this.length, append
if (index === this.length) {
this.append(value);
return this;
}
// Reach the lexNode at that index
let newNode = new lexNode(value);
let previousNode = this.head;
for (let k = 0; k < index - 1; k++) {
previousNode = previousNode.next;
}
let nextNode = previousNode.next;
newNode.next = nextNode;
previousNode.next = newNode;
newNode.previous = previousNode;
nextNode.previous = newNode;
this.length++;
this.printList();
}
*/
// Remove a lexNode
remove () {
// All other cases
let previousNode = this.cursor.previous;
// The list contain only the cursor
if (previousNode == null) {
console.log("In remove: the cursor at the beging of the list");
return;
}
let previousPreviusNode = previousNode.previous;
// The cursor is the 2nd element - just remove the first elemnet and make cursor first
if (previousPreviusNode == null) {
console.log("In remove: only one item before the cursor");
this.cursor.previous = null;
this.head = this.cursor;
}
// the cursor is not in 2nd place
else {
// Point the cursor to the note before the previus
// point the node before the privius node to cursor
this.cursor.previous = previousPreviusNode;
previousPreviusNode.next = this.cursor;
}
this.length--;
}
}
empty(){
let newNode = new lexNode(CUSOR_SYMBOL);
newNode.next = null;
newNode.previous = null;
this.head = newNode;
this.length = 1; // length of the lex list
this.cursor = this.head; //point to the current lexNode
}
test the list
let myDoublyList = new lexList();
myDoublyList.append(" append-one "); // 10 <--> 5
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.append(" append-two "); // 10 <--> 5 <--> 16
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.append(" append-three "); // 1 <--> 10 <--> 5 <--> 16
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.cursorBack(); // 1 <--> 10 <--> 99 <--> 5 <--> 16
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.cursorBack(); // 1 <--> 10 <--> 99 <--> 5 <--> 16
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.insert(" inserted-four "); // Invalid index. Current length is 5.
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.cursorForward(); // 1 <--> 10 <--> 99 <--> 5 <--> 16 <--> 80
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.insert(" inserted-five ");
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.remove(); // 1 <--> 10 <--> 99 <--> 5 <--> 16 <--> 80
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.insert(" inserted-six ");
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.cursorBack();
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.remove(); // 1 <--> 10 <--> 99 <--> 5 <--> 16 <--> 80
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.insert(" inserted-item at the begining ");
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.cursorForward();
myDoublyList.printList();
//window.alert("waiting");
myDoublyList.empty();
myDoublyList.printList();
myDoublyList.append(" append-one "); // 10 <--> 5
myDoublyList.printList();