Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dca2ac1
Plan phase 5: the remaining jniast Mappings
timj Nov 17, 2026
2773407
Port LutMap
timj Nov 17, 2026
07ba4ad
Correct the CmpMap Equal quirk entry and note C's wcsaxes in TabMapping
timj Nov 17, 2026
9c6eb4e
Port MathMap
timj Nov 17, 2026
c27eb13
Simplify MathMaps and build them for non-linear units
timj Nov 17, 2026
9975d86
Read and write logarithmic FITS axes
timj Nov 17, 2026
774b3db
Correct the -LOG review findings
timj Nov 17, 2026
f11c127
Port TranMap
timj Nov 17, 2026
e9ad7f9
fixup! Plan phase 5: the remaining jniast Mappings
timj Nov 17, 2026
9dcce42
Port astEqual
timj Nov 17, 2026
5acb017
Compare brad's input with C's after simplifying, and record astEqual …
timj Nov 17, 2026
4a1dc51
Port RateMap
timj Nov 17, 2026
8e1fa2f
Convert between SpecFluxFrames with differing components
timj Nov 17, 2026
24b175d
Record how Rust differs from C for SpecFluxFrames whose SpecFrames do…
timj Nov 17, 2026
f810ae4
Port GrismMap and read and write grism axes
timj Nov 17, 2026
c05736c
fixup! Port GrismMap and read and write grism axes
timj Nov 17, 2026
7745648
Correct the GrismMap review findings
timj Nov 17, 2026
b61f6bf
Port PcdMap
timj Nov 17, 2026
ebc3f23
Treat a GrismMap input that is not finite as bad
timj Nov 17, 2026
52448a0
Port SwitchMap
timj Nov 17, 2026
41b9583
Record the SwitchMap round trip that inverts the inverse selector
timj Nov 17, 2026
8d3510c
Test that an infinite GrismMap input is bad
timj Nov 17, 2026
a1e54a1
Port IntraMap and the Transformer classes
timj Nov 17, 2026
2c112cf
fixup! Port IntraMap and the Transformer classes
timj Nov 18, 2026
8c2d00f
fixup! Port IntraMap and the Transformer classes
timj Nov 18, 2026
b1cda91
fixup! Port IntraMap and the Transformer classes
timj Nov 18, 2026
23e4bb1
Port DssMap
timj Nov 18, 2026
e762a25
Leave the WinMap a DssMap merges with at the list's Invert value, as C
timj Nov 18, 2026
a337b01
Leave a cancelled PolyMap at the list's Invert value, as C
timj Nov 18, 2026
395ced5
Check simplified Mappings against C's with astEqual, as C's harness does
timj Nov 18, 2026
26fa03a
Take the expected results of the test_equal.rs translations from C's …
timj Nov 18, 2026
5bf6462
Sweep the deferred lists after phase 5
timj Nov 18, 2026
7e25cf8
fixup! Sweep the deferred lists after phase 5
timj Nov 18, 2026
dfb1852
Test that nested series CmpMaps hold no full-size intermediates
timj Nov 18, 2026
cbca04c
fixup! Check simplified Mappings against C's with astEqual, as C's ha…
timj Nov 18, 2026
b004e00
Report phase 5
timj Nov 18, 2026
7f1919d
fixup! Report phase 5
timj Nov 18, 2026
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
213 changes: 213 additions & 0 deletions docs/reports/2026-11-17-phase5-report.md

Large diffs are not rendered by default.

269 changes: 269 additions & 0 deletions docs/reports/2026-11-17-phase5-rulings.md

Large diffs are not rendered by default.

326 changes: 320 additions & 6 deletions docs/rust-c-differences.md

Large diffs are not rendered by default.

283 changes: 283 additions & 0 deletions docs/superpowers/plans/2026-11-17-pure-java-ast-phase5-mappings.md

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/main/java/uk/ac/starlink/ast/AstObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,33 @@ public boolean sameObject(Object obj) {
return obj == this;
}

/**
* Whether {@code that} is equivalent to this Object, as C's {@code astEqual} (object.c {@code
* astEqual_}) decides it: the same object, or, if {@code that} is not null, an Object the
* {@code Equal} of this Object's class ({@link #equalTo}) finds equivalent. Unlike {@link
* #equals}, which compares descriptions, each class compares what it considers significant: a
* ZoomMap and an inverted ZoomMap of the reciprocal factor are equivalent, and Frames are
* equivalent when the conversion between them is a UnitMap.
*/
// C compares the pointers first: the same object, not an equal one.
@SuppressWarnings({"ReferenceEquality", "PMD.CompareObjectsWithEquals"})
final boolean astEqual(AstObject that) {
return this == that || (that != null && equalTo(that));
}

/**
* C's {@code Equal} for this Object's class, which {@link #astEqual} calls with an Object other
* than this one. The base Object class's (object.c {@code Equal}) finds Objects equivalent when
* they are the same object, or of the same AST class: C compares the class names and the sizes
* of the structures, and objects of one AST class have structures of one size, so the class
* names ({@link #astClass}) decide.
*/
// C compares the pointers first, and then the class names, not any equals method.
@SuppressWarnings({"ReferenceEquality", "PMD.CompareObjectsWithEquals"})
boolean equalTo(AstObject that) {
return this == that || astClass().equals(that.astClass());
}

/**
* Tests whether {@code obj} resembles this Object in all respects: whether it is of the same
* class and is written to a Channel (with Full -1 and no comments) as the same text. This may
Expand Down
97 changes: 96 additions & 1 deletion src/main/java/uk/ac/starlink/ast/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ private static final class Input {
/** The warnings from the most recent read or write. */
private List<String> warningList = new ArrayList<>();

/**
* The transformation functions registered with this Channel for the IntraMaps it reads (C's
* {@code astIntraReg}), or null if none is.
*/
private IntraRegistry intraRegistry;

/**
* Creates a Channel that reads from {@code in} and writes to {@code out}.
*
Expand Down Expand Up @@ -227,6 +233,26 @@ void deepCopy() {
fdOut = null;
fileReturned = new ArrayDeque<>();
warningList = new ArrayList<>(warningList);
if (intraRegistry != null) {
intraRegistry = intraRegistry.copy();
}
}

/**
* Registers {@code trans} as the transformation function called {@code name}, so that an
* IntraMap this Channel reads whose function has that name uses it, in preference to the
* process-wide table: C's {@code astIntraReg}, whose registrations are process-wide where
* these belong to the Channel. See {@link IntraRegistry#register}.
*
* @param nin the number of input coordinates of the function, or {@link IntraRegistry#AST__ANY}
* @param nout the number of output coordinates of the function, or {@link
* IntraRegistry#AST__ANY}
*/
void registerIntra(String name, int nin, int nout, Transformer trans) {
if (intraRegistry == null) {
intraRegistry = new IntraRegistry();
}
intraRegistry.register(name, nin, nout, trans);
}

// ------------------------------------------------------------------
Expand Down Expand Up @@ -367,6 +393,9 @@ private static void closeQuietly(Closeable c) {
* are discarded, and {@link #warnings} gives those of this one: each item in the description
* that no class recognises is reported, as a warning or, with Strict set, as an error.
*
* <p>An Object read that implements {@link NeedsChannelizing} has its {@code unChannelize}
* method called, as jniast's Channel calls it; an exception it throws is passed on unchanged.
*
* @return the Object read, or null if there are no more Objects to read
* @throws IOException if {@link #source} throws it
* @throws AstException with status {@code AST__BADIN} if a line cannot be interpreted while
Expand All @@ -381,12 +410,15 @@ public AstObject read() throws IOException {
warningList.clear();
NativeStream reader = new NativeStream(this::readLine, astClass(), getStrict(), getReportLevel());
List<String> returned = new ArrayList<>();
AstObject obj;
try {
return readObject(reader, mark -> returned.addAll(reader.lines.subList(mark, reader.lines.size())));
obj = readObject(reader, mark -> returned.addAll(reader.lines.subList(mark, reader.lines.size())));
} finally {
warningList = new ArrayList<>(reader.warnings);
returnLines(returned);
}
unChannelize(obj);
return obj;
}

/**
Expand All @@ -400,6 +432,17 @@ AstObject readObject(NativeStream reader, IntConsumer rewind) throws IOException
if (object == null) {
return null;
}
// The IntraMap loader finds the functions registered with this Channel while it reads.
IntraRegistry previous = IntraRegistry.enter(intraRegistry);
try {
return load(object, reader, rewind, cls);
} finally {
IntraRegistry.restore(previous);
}
}

/** The Object {@code object} describes, loaded as {@link #readObject} loads it. */
private static AstObject load(NativeStream.ReadObject object, NativeStream reader, IntConsumer rewind, String cls) {
AstObject loaded;
try {
loaded = ObjectRegistry.load(object.node);
Expand Down Expand Up @@ -435,12 +478,18 @@ AstObject readObject(NativeStream reader, IntConsumer rewind) throws IOException
* Writes an Object to this Channel, after any written before, as AST's native text. The
* warnings of the previous read or write are discarded.
*
* <p>An Object that implements {@link NeedsChannelizing} has its {@code channelize} method
* called before it is written, and its {@code unChannelize} method after, as jniast's Channel
* calls them; an exception either throws is passed on unchanged, and one from {@code
* channelize} stops the write before anything is written.
*
* @param obj the Object to write
* @return the number of Objects written: 1
* @throws IOException if {@link #sink} throws it
* @throws AstException with status {@code AST__WRERR} if the SinkFile cannot be opened
*/
public int write(AstObject obj) throws IOException {
channelize(obj);
warningList.clear();
String text = NativeWriter.render(NativeNode.of(obj), options());
for (String line : NativeReader.lines(text)) {
Expand All @@ -449,9 +498,55 @@ public int write(AstObject obj) throws IOException {
if (fdOut != null) {
fdOut.flush();
}
unChannelize(obj);
return 1;
}

/**
* Calls the {@code channelize} method of {@code obj} if it implements {@link
* NeedsChannelizing}, as jniast's Channel and FitsChan writes do (jni/Channel.c and
* jni/FitsChan.c {@code write}), passing on any exception unchanged, as the JNI code does
* whether or not the calling method declares it.
*/
// NeedsChannelizing.channelize is declared to throw any Exception.
@SuppressWarnings("PMD.AvoidCatchingGenericException")
static void channelize(AstObject obj) {
if (obj instanceof NeedsChannelizing) {
try {
((NeedsChannelizing) obj).channelize();
} catch (Exception e) {
throw Channel.<RuntimeException>passOn(e);
}
}
}

/**
* Calls the {@code unChannelize} method of {@code obj} if it implements {@link
* NeedsChannelizing}, as jniast's Channel and FitsChan reads and writes do, passing on any
* exception unchanged.
*/
// NeedsChannelizing.unChannelize is declared to throw any Exception.
@SuppressWarnings("PMD.AvoidCatchingGenericException")
static void unChannelize(AstObject obj) {
if (obj instanceof NeedsChannelizing) {
try {
((NeedsChannelizing) obj).unChannelize();
} catch (Exception e) {
throw Channel.<RuntimeException>passOn(e);
}
}
}

/**
* Throws {@code e} as it is, checked or not, as an exception pending in jniast's JNI code
* reaches the Java caller whatever the native method declares.
*/
// The cast is unchecked by design: the compiler takes E to be unchecked, and e is thrown as it is.
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
private static <E extends Exception> RuntimeException passOn(Exception e) throws E {
throw (E) e;
}

/** The options this Channel writes with. */
ChannelOptions options() {
return new ChannelOptions(getComment(), getFull(), getIndent());
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/uk/ac/starlink/ast/CmpMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,101 @@ boolean mapList(boolean series, boolean invert, List<Mapping> maps, List<Boolean
return invert || r1 || r2;
}

// ------------------------------------------------------------------
// Equality
// ------------------------------------------------------------------

/**
* cmpmap.c {@code Equal}: {@code that} is a CmpMap combining Mappings in the same way as this
* one, and the lists of Mappings the two decompose into ({@link #mapList}) are of the same
* length and hold equal Mappings ({@link #astEqual}), each compared with its Invert attribute
* set for the duration to the value its list gives it.
*
* <p>As in C, the attributes are set back in the order they were set, so that when the two
* lists hold the same object in the same place it is left with the value the first list gives
* it. If neither CmpMap may be decomposed ({@link #doNotSimplify}), each is decomposed one level
* further ({@link #expandInto}) and copies of the Mappings are compared instead, leaving the
* Mappings unchanged.
*/
// C compares the pointers: each list holds the CmpMap itself when it was not decomposed.
@SuppressWarnings({"ReferenceEquality", "PMD.CompareObjectsWithEquals"})
@Override
boolean equalTo(AstObject that) {
if (!(that instanceof CmpMap)) {
return false;
}
CmpMap other = (CmpMap) that;
if (series != other.series) {
return false;
}
List<Mapping> thisMaps = new ArrayList<>();
List<Boolean> thisInverts = new ArrayList<>();
mapList(series, getInvert(), thisMaps, thisInverts);
List<Mapping> thatMaps = new ArrayList<>();
List<Boolean> thatInverts = new ArrayList<>();
other.mapList(other.series, other.getInvert(), thatMaps, thatInverts);
boolean expanded = false;
if (thisMaps.size() == 1 && thatMaps.size() == 1 && thisMaps.get(0) == this && thatMaps.get(0) == other) {
thisMaps.clear();
thisInverts.clear();
thatMaps.clear();
thatInverts.clear();
expandInto(thisMaps, thisInverts);
other.expandInto(thatMaps, thatInverts);
expanded = true;
}
if (thisMaps.size() != thatMaps.size()) {
return false;
}
boolean result = false;
for (int i = 0; i < thisMaps.size(); i++) {
Mapping m1 = thisMaps.get(i);
Mapping m2 = thatMaps.get(i);
boolean inv1 = thisInverts.get(i);
boolean inv2 = thatInverts.get(i);
if (expanded) {
result = equalMapping(m1, inv1, m2, inv2);
} else {
int[] saved = swapInverts(m1, inv1, m2, inv2);
try {
result = m1.astEqual(m2);
} finally {
restoreInverts(m1, m2, saved);
}
}
if (!result) {
break;
}
}
return result;
}

/**
* Whether copies of {@code a} and {@code b}, with their Invert attributes set to {@code
* invertA} and {@code invertB}, are equal (C's {@code EqualMapping}, cmpmap.c); {@code a} and
* {@code b} are not changed.
*/
private static boolean equalMapping(Mapping a, boolean invertA, Mapping b, boolean invertB) {
Mapping aCopy = (Mapping) a.copy();
Mapping bCopy = (Mapping) b.copy();
aCopy.setInvert(invertA);
bCopy.setInvert(invertB);
return aCopy.astEqual(bCopy);
}

/**
* Appends the Mappings this CmpMap decomposes into one level down, even when it may not be
* decomposed (C's {@code ExpandCmpMap}, cmpmap.c): its components, in the order and with the
* Invert values {@link #decomposeInto} gives for its Invert attribute, each decomposed in the
* usual way ({@link #mapList}).
*/
private void expandInto(List<Mapping> maps, List<Boolean> inverts) {
boolean[] flags = new boolean[3];
Mapping[] parts = decomposeInto(flags);
parts[0].mapList(series, flags[1], maps, inverts);
parts[1].mapList(series, flags[2], maps, inverts);
}

/** cmpmap.c {@code Simplify}: {@link Simplifier#simplifyCmpMap}. */
@Override
Mapping simplifyStep() {
Expand Down
Loading
Loading