package javacompat
Import Path
github.com/d2lang/elk-go/internal/javacompat (on go.dev)
Dependency Relation
imports 2 packages, and imported by 13 packages
Involved Source Files
gwt_priority_queue.go
gwt_stable_sort.go
identity.go
jsmath.go
numeric.go
ordered.go
priority_queue.go
Package javacompat contains the small parts of the Java runtime whose exact
behavior is observable in ELK layouts.
Package-Level Type Names (total 8)
Type Parameters:
T: any
Comparator has the contract of java.util.Comparator.compare: negative when
a is less than b, zero when they compare equal, and positive otherwise.
func NewGWTPriorityQueue[T](compare Comparator[T]) *GWTPriorityQueue[T]
func NewPriorityQueue[T](compare Comparator[T]) *PriorityQueue[T]
func NewPriorityQueueCapacity[T](capacity int, compare Comparator[T]) *PriorityQueue[T]
Type Parameters:
T: any
GWTPriorityQueue reproduces GWT 2.11.0's java.util.PriorityQueue
implementation linked into elkjs 0.12.0. The relevant GWT source is
unchanged from the 2.9.0 runtime used by the former elkjs 0.8.2 baseline.
Its bulk-add heapification and equal-item sift-down behavior differ
observably from OpenJDK's PriorityQueue.
(*GWTPriorityQueue[T]) Add(item T)
AddAll appends the complete collection and then invokes GWT's recursive
makeHeap routine. It does not repeatedly call Offer.
(*GWTPriorityQueue[T]) IsEmpty() bool
(*GWTPriorityQueue[T]) Len() int
(*GWTPriorityQueue[T]) Offer(item T) bool
(*GWTPriorityQueue[T]) Peek() (T, bool)
(*GWTPriorityQueue[T]) Poll() (T, bool)
func NewGWTPriorityQueue[T](compare Comparator[T]) *GWTPriorityQueue[T]
Type Parameters:
K: comparable
V: any
IdentityMap provides IdentityHashMap equality semantics for identity-bearing
comparable keys. K should be a pointer or an arena handle, never a value
object whose == operation represents structural equality. Iteration is made
deterministic in first-insertion order; ELK does not rely on the unspecified
table order of java.util.IdentityHashMap.
The zero value is ready to use.
(*IdentityMap[K, V]) Clear()
(*IdentityMap[K, V]) ContainsKey(key K) bool
(*IdentityMap[K, V]) Get(key K) (V, bool)
(*IdentityMap[K, V]) IsEmpty() bool
(*IdentityMap[K, V]) Keys() []K
(*IdentityMap[K, V]) Len() int
(*IdentityMap[K, V]) Put(key K, value V) (V, bool)
(*IdentityMap[K, V]) Range(visit func(K, V) bool)
(*IdentityMap[K, V]) Remove(key K) (V, bool)
(*IdentityMap[K, V]) Values() []V
func NewIdentityMap[K, V]() *IdentityMap[K, V]
Type Parameters:
K: comparable
IdentitySet is IdentityMap's set counterpart. K should be a pointer or an
arena handle so Go equality is object identity.
(*IdentitySet[K]) Add(value K) bool
(*IdentitySet[K]) Clear()
(*IdentitySet[K]) Contains(value K) bool
(*IdentitySet[K]) IsEmpty() bool
(*IdentitySet[K]) Len() int
(*IdentitySet[K]) Range(visit func(K) bool)
(*IdentitySet[K]) Remove(value K) bool
(*IdentitySet[K]) Values() []K
func NewIdentitySet[K]() *IdentitySet[K]
Type Parameters:
K: comparable
V: any
OrderedMap is an insertion-ordered map matching LinkedHashMap's default
(accessOrder=false) behavior. Replacing a value does not move its key;
removing and reinserting a key appends it at the end.
The zero value is ready to use.
Clear removes all entries.
ContainsKey reports whether key is present.
First returns the oldest entry.
Get looks up a value.
IsEmpty reports whether the map has no entries.
Keys returns keys in insertion order.
Last returns the newest entry.
Len returns the number of entries.
Put inserts or replaces a value. It returns the old value and whether the
key was already present.
PutIfAbsent inserts only when key is absent and returns the existing or new
value plus whether an existing entry was found.
Range visits entries in insertion order until visit returns false.
Structural mutation during Range is unsupported, matching Java iterator
fail-fast semantics without exposing a concurrent-modification exception.
Remove deletes key, preserving the relative order of all remaining keys.
Values returns values in key insertion order.
func NewOrderedMap[K, V]() *OrderedMap[K, V]
Type Parameters:
T: comparable
OrderedSet is an insertion-ordered set matching LinkedHashSet.
The zero value is ready to use.
Add inserts value and reports whether the set changed.
Clear removes every value.
Contains reports whether value is present.
IsEmpty reports whether the set is empty.
Len returns the number of values.
Range visits values in insertion order until visit returns false.
Remove deletes value and reports whether the set changed.
Values returns values in insertion order.
func NewOrderedSet[T]() *OrderedSet[T]
Type Parameters:
T: any
PriorityQueue is a clean-room binary min-heap whose tie behavior is pinned by
elkjs oracle vectors. Java does not define FIFO ordering for equal elements,
but ELK observes the heap-array order, so left-child and strict-comparison
choices are part of this compatibility type's tested behavior.
Add inserts item. Java's add and offer have identical heap behavior.
Clear removes every element while releasing references held by the backing
slice.
Elements returns a copy in PriorityQueue iterator order (the heap-array
order). This order is deliberately not sorted, just like Java's iterator.
IsEmpty reports whether the queue is empty.
Len returns the number of queued elements.
Offer inserts item and returns true, matching PriorityQueue.offer.
Peek returns the least element without removing it.
Poll removes and returns the least element.
RemoveFirst removes the first element in the queue's internal array for
which match returns true. This is PriorityQueue.remove(Object) with equality
supplied explicitly, which also supports non-comparable Go values.
func NewPriorityQueue[T](compare Comparator[T]) *PriorityQueue[T]
func NewPriorityQueueCapacity[T](capacity int, compare Comparator[T]) *PriorityQueue[T]
Random is a bit-for-bit implementation of java.util.Random from Java 8.
Random is not safe for concurrent use. ELK owns one instance per layout, as
the Java implementation does, so synchronization would only obscure the
order in which random values are consumed.
NextBoolean implements Random.nextBoolean().
NextBytes implements Random.nextBytes(byte[]). Java's byte is signed, but
assigning its low eight bits to a Go byte gives the same byte sequence.
NextDouble implements Random.nextDouble().
NextFloat implements Random.nextFloat().
NextGaussian implements Random.nextGaussian(), including its cached second
sample. ELK 0.8.1 does not use this in a layout path, but providing the Java
behavior prevents consumers from accidentally substituting math/rand.
Math-library results may differ by a final bit across platforms, just as
Java's Math implementation may, while the random-value consumption is exact.
NextInt implements Random.nextInt().
NextIntN implements Random.nextInt(bound). It panics for a non-positive
bound, corresponding to Java's IllegalArgumentException.
NextLong implements Random.nextLong(). The low 32 bits are sign-extended
before addition, as required by Java's two signed int32 draws.
SetSeed implements Random.setSeed(long). It also clears the cached value
used by NextGaussian.
func NewRandom(seed int64) *Random
func github.com/d2lang/elk-go/internal/layered/p3order.NewBarycenterHeuristic(arena *layered.LGraphArena, resolver *p3order.ForsterConstraintResolver, random *Random, distributor *p3order.BarycenterPortDistributor) *p3order.BarycenterHeuristic
func github.com/d2lang/elk-go/internal/layered/p3order.NewGraphInfoHolder(arena *layered.LGraphArena, graphID layered.LGraphID, crossMinType p3order.CrossMinType, graphs []*p3order.GraphInfoHolder, random *Random) (*p3order.GraphInfoHolder, error)
func github.com/d2lang/elk-go/internal/layered/p5edges/orthogonal.BreakNonCriticalCycles(segments []*orthogonal.HyperEdgeSegment, random *Random)
func github.com/d2lang/elk-go/internal/layered/p5edges/orthogonal.DetectCycles(segments []*orthogonal.HyperEdgeSegment, criticalOnly bool, random *Random) []*orthogonal.HyperEdgeSegmentDependency
Package-Level Functions (total 34)
BooleanHashCode implements Boolean.hashCode(boolean).
DoubleCompare implements Double.compare. In particular, -0.0 sorts before
+0.0 and every NaN sorts after positive infinity and compares equal to every
other NaN.
DoubleHashCode implements Double.hashCode(double).
DoubleToInt and DoubleToLong implement Java's narrowing primitive casts:
NaN becomes zero, finite out-of-range values and infinities saturate, and
finite in-range values truncate toward zero.
func DoubleToLong(value float64) int64
DoubleToLongBits implements Double.doubleToLongBits, including NaN
canonicalization. Use math.Float64bits when Java's doubleToRawLongBits is
specifically required.
FloatCompare implements Float.compare.
FloatHashCode implements Float.hashCode(float).
FloatToIntBits implements Float.floatToIntBits, including NaN
canonicalization.
Type Parameters:
T: any
GWTStableSort reproduces the object-array legacy merge sort in GWT 2.11.0's
emulated java.util.Arrays. The relevant implementation is unchanged from
GWT 2.9.0. Comparator invocation order is observable when a comparator
records comparisons or derives transitive state, as ELK's model-order
comparators do.
HashMapSpread reproduces the one-step high-bit spread observed by ELK's
Java-compatible hash tables. Its behavior is pinned by independent vectors.
IntCompare and LongCompare implement Integer.compare and Long.compare.
Narrowing integral conversions implement Java casts.
func IntToShort(value int32) int16
JSMathSin and JSMathCos reproduce the fdlibm implementation used by V8's
Math.sin and Math.cos for the medium-size arguments emitted by ELK Radial.
The kernels and constants are adapted from Node.js v24.19.0's pinned V8
deps/v8/src/base/ieee754.cc (itself adapted from fdlibm). Very large
arguments fall back to Go's math package; layout angles are many orders of
magnitude below that boundary.
func LongCompare(a, b int64) int
LongHashCode implements Long.hashCode(long).
func MathMaxDouble(a, b float64) float64
MathMinDouble and MathMaxDouble preserve Java's NaN and signed-zero rules.
MathRoundDouble and MathRoundFloat implement Math.round.
func MathRoundFloat(value float32) int32
Type Parameters:
T: any
Type Parameters:
K: comparable
V: any
Type Parameters:
K: comparable
Type Parameters:
K: comparable
V: any
NewOrderedMap constructs an empty insertion-ordered map.
Type Parameters:
T: comparable
NewOrderedSet constructs an empty insertion-ordered set.
Type Parameters:
T: any
NewPriorityQueue returns an empty Java-compatible min-priority queue.
Type Parameters:
T: any
NewPriorityQueueCapacity is NewPriorityQueue with a capacity hint. Java's
growth policy does not affect heap order, but avoiding reallocations is handy
for the larger layered-layout queues.
NewRandom constructs a Java-compatible pseudo-random number generator.
StringHashCode implements String.hashCode over Java UTF-16 code units, not
Unicode code points. This distinction matters for supplementary characters.
UnsignedRightShiftInt and UnsignedRightShiftLong implement Java's >>>. Java
masks the shift distance to five or six bits respectively.
func UnsignedRightShiftLong(value int64, distance uint) int64
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |