Skip to content

Commit

Permalink
Check for name duplicates in containers
Browse files Browse the repository at this point in the history
  • Loading branch information
HGuillemet committed Jan 29, 2024
1 parent 2559658 commit ca50262
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ String translate(String text) {
return text;
}

/** De-deduplicate an array of strings, keeping order. */
private static String[] uniq(String[] a) {
if (a == null || a.length <= 1) return a;
return new LinkedHashSet<>(Arrays.asList(a)).toArray(new String[0]);
}

void containers(Context context, DeclarationList declList) throws ParserException {
List<String> basicContainers = new ArrayList<>();
for (Info info : infoMap.get("basic/containers")) {
Expand Down Expand Up @@ -339,16 +345,16 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
+ " public " + containerType.javaName + "(Pointer p) { super(p); }\n";
boolean purify = info != null && info.purify;
if (!constant && !purify && (dim == 0 || (containerType.arguments.length == 1 && indexType != null)) && firstType != null && secondType != null) {
String[] firstNames = firstType.javaNames != null ? firstType.javaNames : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? secondType.javaNames : new String[] {secondType.javaName};
String[] firstNames = firstType.javaNames != null ? uniq(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? uniq(secondType.javaNames) : new String[] {secondType.javaName};
String brackets = arrayBrackets + (dim > 0 ? "[]" : "");
for (int n = 0; n < firstNames.length || n < secondNames.length; n++) {
decl.text += " public " + containerType.javaName + "(" + firstNames[Math.min(n, firstNames.length - 1)] + brackets + " firstValue, "
+ secondNames[Math.min(n, secondNames.length - 1)] + brackets + " secondValue) "
+ "{ this(" + (dim > 0 ? "Math.min(firstValue.length, secondValue.length)" : "") + "); put(firstValue, secondValue); }\n";
}
} else if (resizable && !purify && firstType == null && secondType == null) {
for (String javaName : valueType.javaNames != null ? valueType.javaNames : new String[] {valueType.javaName}) {
for (String javaName : valueType.javaNames != null ? uniq(valueType.javaNames) : new String[] {valueType.javaName}) {
if (dim < 2 && !javaName.equals("int") && !javaName.equals("long")) {
decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(1); put(0, value); }\n";
}
Expand All @@ -363,7 +369,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
valueNames2 += separator + "value" + n;
separator = ", ";
n++;
} else for (String javaName : type.javaNames != null ? type.javaNames : new String[] {type.javaName}) {
} else for (String javaName : type.javaNames != null ? uniq(type.javaNames) : new String[] {type.javaName}) {
// variant, optional, etc
if (!javaName.substring(javaName.indexOf(' ') + 1).equals("Pointer")) {
decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(); put(value); }\n";
Expand Down Expand Up @@ -416,6 +422,8 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
+ " public native " + containerType.javaName + " first(" + params + separator + removeAnnotations(firstType.javaName) + " first);\n"
+ " " + indexAnnotation + "public native " + secondType.annotations + secondType.javaName + " second(" + params + "); "
+ " public native " + containerType.javaName + " second(" + params + separator + removeAnnotations(secondType.javaName) + " second);\n";
firstType.javaNames = uniq(firstType.javaNames);
secondType.javaNames = uniq(secondType.javaNames);
for (int i = 1; !constant && firstType.javaNames != null && i < firstType.javaNames.length; i++) {
decl.text += " @MemberSetter @Index" + indexFunction + " public native " + containerType.javaName + " first(" + params + separator + firstType.annotations + firstType.javaNames[i] + " first);\n";
}
Expand All @@ -433,6 +441,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
if (!constant) {
decl.text += " public native " + containerType.javaName + " put(" + params + separator + removeAnnotations(valueType.javaName) + " value);\n";
}
valueType.javaNames = uniq(valueType.javaNames);
for (int i = 1; !constant && valueType.javaNames != null && i < valueType.javaNames.length; i++) {
decl.text += " @ValueSetter @Index" + indexFunction + " public native " + containerType.javaName + " put(" + params + separator + valueType.annotations + valueType.javaNames[i] + " value);\n";
}
Expand All @@ -452,6 +461,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
if (!constant && !tuple) {
decl.text += " @ValueSetter public native " + containerType.javaName + " put(" + type.annotations + type.javaName + " value);\n";
}
type.javaNames = uniq(type.javaNames);
for (int i = 1; !constant && !tuple && type.javaNames != null && i < type.javaNames.length; i++) {
decl.text += " @ValueSetter public native " + containerType.javaName + " put(" + type.annotations + type.javaNames[i] + " value);\n";
}
Expand Down Expand Up @@ -532,8 +542,8 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
}

if (!constant && (dim == 0 || (containerType.arguments.length == 1 && indexType != null)) && firstType != null && secondType != null) {
String[] firstNames = firstType.javaNames != null ? firstType.javaNames : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? secondType.javaNames : new String[] {secondType.javaName};
String[] firstNames = firstType.javaNames != null ? uniq(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? uniq(secondType.javaNames) : new String[] {secondType.javaName};
String brackets = arrayBrackets + (dim > 0 ? "[]" : "");
for (int n = 0; n < firstNames.length || n < secondNames.length; n++) {
String firstName = firstNames[Math.min(n, firstNames.length - 1)];
Expand Down Expand Up @@ -566,7 +576,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
}
} else if (resizable && firstType == null && secondType == null) {
boolean first = true;
for (String javaName : valueType.javaNames != null ? valueType.javaNames : new String[] {valueType.javaName}) {
for (String javaName : valueType.javaNames != null ? uniq(valueType.javaNames) : new String[] {valueType.javaName}) {
javaName = removeAnnotations(javaName);
decl.text += "\n";
if (dim < 2) {
Expand Down

0 comments on commit ca50262

Please sign in to comment.