Skip to content

Commit 6256423

Browse files
authored
Merge pull request #59 from csvistool/bugsFix
fixed skiplist bug
2 parents 5a7e2d8 + d3013d7 commit 6256423

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

src/algo/SkipList.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ export default class SkipList extends Algorithm {
329329
this.implementAction(this.clearAll.bind(this));
330330
}
331331

332+
compare(a, b) {
333+
const numA = parseInt(a);
334+
const numB = parseInt(b);
335+
336+
const isNumA = !isNaN(numA);
337+
const isNumB = !isNaN(numB);
338+
339+
if (isNumA && isNumB) {
340+
return numA - numB;
341+
} else if (!isNumA && !isNumB) {
342+
return a.localeCompare(b);
343+
} else {
344+
return isNumA ? -1 : 1;
345+
}
346+
}
347+
332348
add(value, heads) {
333349
this.commands = [];
334350

@@ -400,12 +416,14 @@ export default class SkipList extends Algorithm {
400416

401417
// Find column where new element will be inserted
402418
let newCol = 1;
403-
while (value > this.data[newCol][0]) {
419+
// while (value > this.data[newCol][0]) {
420+
while (this.compare(value, this.data[newCol][0]) > 0) {
404421
newCol++;
405422
}
406423

407424
//this step should be skipped in the case of a duplicate
408-
if (value !== this.data[newCol][0]) {
425+
// if (value !== this.data[newCol][0]) {
426+
if (this.compare(value, this.data[newCol][0]) !== 0) {
409427
// Move IDs and data in next columns to the right
410428
for (let col = this.nodeID.length - 1; col >= newCol; col--) {
411429
this.nodeID[col + 1] = this.nodeID[col];
@@ -434,12 +452,14 @@ export default class SkipList extends Algorithm {
434452
while (row >= 0) {
435453
// Move right until next element is greater or equal
436454
let nextCol = this.getNextCol(col, row);
437-
if (value === this.data[nextCol][row]) {
455+
// if (value === this.data[nextCol][row]) {
456+
if (this.compare(value, this.data[nextCol][row]) === 0) {
438457
col = nextCol;
439458
foundDuplicate = true;
440459
break;
441460
}
442-
while (value > this.data[nextCol][row]) {
461+
// while (value > this.data[nextCol][row]) {
462+
while (this.compare(value, this.data[nextCol][row]) > 0) {
443463
this.cmd(
444464
act.move,
445465
highlightID,
@@ -451,7 +471,8 @@ export default class SkipList extends Algorithm {
451471
nextCol = this.getNextCol(col, row);
452472
}
453473

454-
if (value === this.data[nextCol][row]) {
474+
// if (value === this.data[nextCol][row]) {
475+
if (this.compare(value, this.data[nextCol][row]) === 0) {
455476
col = nextCol;
456477
foundDuplicate = true;
457478
break;
@@ -570,7 +591,8 @@ export default class SkipList extends Algorithm {
570591
while (row >= 0) {
571592
// Move right until next element is greater or equal
572593
let nextCol = this.getNextCol(col, row);
573-
while (value > this.data[nextCol][row]) {
594+
// while (value > this.data[nextCol][row]) {
595+
while (this.compare(value, this.data[nextCol][row]) > 0) {
574596
this.cmd(
575597
act.move,
576598
highlightID,
@@ -582,7 +604,8 @@ export default class SkipList extends Algorithm {
582604
nextCol = this.getNextCol(col, row);
583605
}
584606

585-
if (value === this.data[nextCol][row]) {
607+
// if (value === this.data[nextCol][row]) {
608+
if (this.compare(value, this.data[nextCol][0]) === 0) {
586609
removedCol = nextCol;
587610
col = nextCol;
588611
this.cmd(
@@ -694,10 +717,12 @@ export default class SkipList extends Algorithm {
694717
this.cmd(act.setHighlight, highlightID, 1);
695718
this.cmd(act.step);
696719

697-
while (row >= 0 && value !== this.data[col][row]) {
720+
// while (row >= 0 && value !== this.data[col][row]) {
721+
while (row >= 0 && this.compare(value, this.data[col][row]) !== 0) {
698722
// Move right until next element is greater or equal
699723
let nextCol = this.getNextCol(col, row);
700-
while (value >= this.data[nextCol][row]) {
724+
// while (value >= this.data[nextCol][row]) {
725+
while (this.compare(value, this.data[nextCol][row]) >= 0) {
701726
this.cmd(
702727
act.move,
703728
highlightID,
@@ -710,7 +735,8 @@ export default class SkipList extends Algorithm {
710735
}
711736

712737
// Move highlight circle downward if data has not been found
713-
if (value !== this.data[col][row]) {
738+
// if (value !== this.data[col][row]) {
739+
if (this.compare(value, this.data[col][row]) !== 0) {
714740
row--;
715741
this.cmd(
716742
act.move,

0 commit comments

Comments
 (0)