Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use alphanumeric sorting for VisitOrder#createByName #36

Merged
merged 8 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies {
spotless {
java {
licenseHeaderFile(rootProject.file("HEADER")).yearSeparator(", ")
targetExclude 'src/main/java/net/fabricmc/mappingio/libs/**/*.java'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* The Alphanum Algorithm is an improved sorting algorithm for strings
* containing numbers. Instead of sorting numbers in ASCII order like
* a standard sort, this algorithm sorts numbers in numeric order.
*
* The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
* Released under the MIT License - https://opensource.org/licenses/MIT
*
* Copyright 2007-2017 David Koelle
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.

* This is an updated version with enhancements made by Daniel Migowski,
* Andre Bogus, and David Koelle. Updated by David Koelle in 2017.
*
* Copied from https://web.archive.org/web/20210506213829/http://www.davekoelle.com/files/AlphanumComparator.java,
* with some code style improvements applied.
*/

package net.fabricmc.mappingio.libs.alphanum;

import java.util.Comparator;

import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class AlphanumComparator implements Comparator<String> {
private boolean isDigit(char ch) {
return ((ch >= 48) && (ch <= 57));
}

/** Length of string is passed in for improved efficiency (only need to calculate it once). **/
private String getChunk(String s, int sLength, int marker) {
StringBuilder chunk = new StringBuilder();
char c = s.charAt(marker);
chunk.append(c);
marker++;

if (isDigit(c)) {
while (marker < sLength) {
c = s.charAt(marker);
if (!isDigit(c)) break;
chunk.append(c);
marker++;
}
} else {
while (marker < sLength) {
c = s.charAt(marker);
if (isDigit(c)) break;

chunk.append(c);
marker++;
}
}

return chunk.toString();
}

public int compare(String s1, String s2) {
if ((s1 == null) || (s2 == null)) {
return 0;
}

int thisMarker = 0;
int thatMarker = 0;
int s1Length = s1.length();
int s2Length = s2.length();

while (thisMarker < s1Length && thatMarker < s2Length) {
String thisChunk = getChunk(s1, s1Length, thisMarker);
thisMarker += thisChunk.length();

String thatChunk = getChunk(s2, s2Length, thatMarker);
thatMarker += thatChunk.length();

// If both chunks contain numeric characters, sort them numerically
int result = 0;

if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0))) {
// Simple chunk comparison by length.
int thisChunkLength = thisChunk.length();
result = thisChunkLength - thatChunk.length();

// If equal, the first different number counts
if (result == 0) {
for (int i = 0; i < thisChunkLength; i++) {
result = thisChunk.charAt(i) - thatChunk.charAt(i);

if (result != 0) {
return result;
}
}
}
} else {
result = thisChunk.compareTo(thatChunk);
}

if (result != 0) return result;
}

return s1Length - s2Length;
}
}
21 changes: 21 additions & 0 deletions src/main/java/net/fabricmc/mappingio/libs/alphanum/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright 2007-2017 David Koelle

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 23 additions & 17 deletions src/main/java/net/fabricmc/mappingio/tree/VisitOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Comparator;
import java.util.List;

import net.fabricmc.mappingio.libs.alphanum.AlphanumComparator;
import net.fabricmc.mappingio.tree.MappingTreeView.ClassMappingView;
import net.fabricmc.mappingio.tree.MappingTreeView.ElementMappingView;
import net.fabricmc.mappingio.tree.MappingTreeView.FieldMappingView;
Expand All @@ -39,11 +40,15 @@ public static VisitOrder createByInputOrder() {
return new VisitOrder();
}

public static VisitOrder createByName() {
/**
* Sorts classes by their source name, members by source name and descriptor, and locals by lv- and lvt-index.
* @param alphanumeric makes sure e.g. {@code mapping10} is ordered after {@code mapping2}. More performance intensive.
*/
public static VisitOrder createByName(boolean alphanumeric) {
return new VisitOrder()
.classesBySrcName()
.fieldsBySrcNameDesc()
.methodsBySrcNameDesc()
.classesBySrcName(alphanumeric)
.fieldsBySrcNameDesc(alphanumeric)
.methodsBySrcNameDesc(alphanumeric)
.methodArgsByLvIndex()
.methodVarsByLvIndex();
}
Expand All @@ -56,8 +61,8 @@ public VisitOrder classComparator(Comparator<ClassMappingView> comparator) {
return this;
}

public VisitOrder classesBySrcName() {
return classComparator(compareBySrcName());
public VisitOrder classesBySrcName(boolean alphanumeric) {
return classComparator(compareBySrcName(alphanumeric));
}

public VisitOrder fieldComparator(Comparator<FieldMappingView> comparator) {
Expand All @@ -66,8 +71,8 @@ public VisitOrder fieldComparator(Comparator<FieldMappingView> comparator) {
return this;
}

public VisitOrder fieldsBySrcNameDesc() {
return fieldComparator(compareBySrcNameDesc());
public VisitOrder fieldsBySrcNameDesc(boolean alphanumeric) {
return fieldComparator(compareBySrcNameDesc(alphanumeric));
}

public VisitOrder methodComparator(Comparator<MethodMappingView> comparator) {
Expand All @@ -76,8 +81,8 @@ public VisitOrder methodComparator(Comparator<MethodMappingView> comparator) {
return this;
}

public VisitOrder methodsBySrcNameDesc() {
return methodComparator(compareBySrcNameDesc());
public VisitOrder methodsBySrcNameDesc(boolean alphanumeric) {
return methodComparator(compareBySrcNameDesc(alphanumeric));
}

public VisitOrder methodArgComparator(Comparator<MethodArgMappingView> comparator) {
Expand Down Expand Up @@ -138,26 +143,26 @@ public VisitOrder methodVarsFirst() {

// customization helpers

public static <T extends ElementMappingView> Comparator<T> compareBySrcName() {
return (a, b) -> compare(a.getSrcName(), b.getSrcName());
public static <T extends ElementMappingView> Comparator<T> compareBySrcName(boolean alphanumeric) {
return (a, b) -> compare(a.getSrcName(), b.getSrcName(), alphanumeric);
}

public static <T extends MemberMappingView> Comparator<T> compareBySrcNameDesc() {
public static <T extends MemberMappingView> Comparator<T> compareBySrcNameDesc(boolean alphanumeric) {
return (a, b) -> {
int cmp = compare(a.getSrcName(), b.getSrcName());
int cmp = compare(a.getSrcName(), b.getSrcName(), alphanumeric);

return cmp != 0 ? cmp : compare(a.getSrcDesc(), b.getSrcDesc());
return cmp != 0 ? cmp : compare(a.getSrcDesc(), b.getSrcDesc(), alphanumeric);
};
}

public static Comparator<ElementMappingView> compareBySrcNameShortFirst() {
return (a, b) -> compareShortFirst(a.getSrcName(), b.getSrcName());
}

public static int compare(String a, String b) {
public static int compare(String a, String b, boolean alphanumeric) {
if (a == null || b == null) return compareNullLast(a, b);

return a.compareTo(b);
return alphanumeric ? alphanumComparator.compare(a, b) : a.compareTo(b);
}

public static int compareShortFirst(String a, String b) {
Expand Down Expand Up @@ -264,6 +269,7 @@ public boolean isMethodVarsFirst() {
return methodVarsFirst;
}

private static final AlphanumComparator alphanumComparator = new AlphanumComparator();
private Comparator<ClassMappingView> classComparator;
private Comparator<FieldMappingView> fieldComparator;
private Comparator<MethodMappingView> methodComparator;
Expand Down