Skip to content

Commit

Permalink
[TimSort] performance
Browse files Browse the repository at this point in the history
  • Loading branch information
lucher authored Dec 31, 2023
1 parent 9e042dd commit 21d2c25
Showing 1 changed file with 24 additions and 38 deletions.
62 changes: 24 additions & 38 deletions src/main/java/cn/nextop/TimArrSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,17 @@ private void mergeAt(int i) {
private void mergeLo(int base1, int len1, int base2, int len2) {
T[] tmp = ensureCapacity(len1);
this.copy(base1, tmp, 0, len1);
locate2(base1); int cursor1 = 0;
var c = this.c; /* performance */
locate(base2); int len = base2 + len2;

//
locate2(base1);
int cursor1 = 0;
int cursor2 = base2;
int len = base2 + len2;
while (len1 > 0 && len2 > 0) {
T t1 = tmp[cursor1], t2 = get(cursor2);
if(c.compare(t1, t2) < 0) {/* t1 win */
set(t1); cursor1++; len1--;
while (true) {
T t1 = tmp[cursor1], t2 = get();
if (c.compare(t1, t2) < 0) {/* t1 win */
set(t1); if (--len1 == 0) break; cursor1++;
} else {
set(t2); cursor2++; len2--;
set(t2); if(--len2 == 0) break; next(1);
}
next2(1);
}
Expand All @@ -192,19 +190,16 @@ private void mergeLo(int base1, int len1, int base2, int len2) {
private void mergeHi(int base1, int len1, int base2, int len2) {
T[] tmp = ensureCapacity(len2);
this.copy(base2, tmp, 0, len2);
var c = this.c; /* performance */
int len = base2 + len2, cursor = len - 1;
var c = this.c; int cursor2 = len2 - 1;
locate(base2 - 1); locate2(base2 + len2 - 1);

//
locate2(cursor);
int cursor2 = len2 - 1;
int cursor1 = base2 - 1;
while (len1 > 0 && len2 > 0) {
T t1 = get(cursor1), t2 = tmp[cursor2];
if(c.compare(t1, t2) > 0) {/* t1 win */
set(t1); cursor1--; len1--;
while (true) {
T t1 = get(), t2 = tmp[cursor2];
if (c.compare(t1, t2) > 0) {/* t1 win */
set(t1); if (--len1 == 0) break; prev(1);
} else {
set(t2); cursor2--; len2--;
set(t2); if (--len2 == 0) break; cursor2--;
}
prev2(1);
} if(len2 > 0) this.copy(tmp, 0, base1, len2);
Expand Down Expand Up @@ -260,17 +255,13 @@ private T get() { /* for read */
return a.get(row)[col];
}

private T get(int i) { /* for read */
if (i != index) locate(i);
return get(); /* current */
}

private void set(T s) { /* for write */
this.a.get(row2)[col2] = s;
}

private void prev(int n) { /* for read */
int[] l = this.lens; int i = index - n;
if (hitCache(i)) return;/* cache hit */
if (this.row > 0) if (i < l[row - 1]) {
do { this.row--; } /* change row */
while ( row > 0 && i < l[row - 1] );
Expand All @@ -288,9 +279,10 @@ private void prev2(int n) { /* for write */
}

private void next(int n) { /* for read */
int i = index + n;
if (i >= lens[row]) {
do { this.row++; }
int i = this.index + n;
if (hitCache(i)) return;
if (i >= this.lens[row]) {
do { this.row++; }/*!*/
while (i >= lens[row]);
col = i - lens[row - 1];
} else col += n; index = i;
Expand Down Expand Up @@ -319,8 +311,6 @@ private void skipEmpty() { /* skip */

private void locate(int i) { /* for read */
if (i == index) return;
if (hasCache(i)) return;
updateCache();/* cache */
int n = abs(i - this.index);
final boolean b = i > index;
if (b) next(n); else prev(n);
Expand All @@ -336,14 +326,10 @@ private void locate2(int i) { /* for write */
/**
*
*/
private boolean hasCache(int i) {
if(index3 != i) return false;
this.index = i; this.row = row3;
this.col = this.col3; return true;
}

private void updateCache() {
row3 = row; col3 = col;
this.index3 = this.index;
private boolean hitCache(int i) {
if(this.index3 != i) return false;
index = i; col = col3; row = row3;
this.row3 = row; this.col3 = col;
this.index3 = index; return true;
}
}

0 comments on commit 21d2c25

Please sign in to comment.