in src/main/java/org/apache/datasketches/memory/internal/ResourceImpl.java [516:533]
static final long nativeOverlap(final MemorySegment segA, final MemorySegment segB) { //used in test
if (!segA.isNative() || !segB.isNative()) { return 0; } //both segments must be native
//Assume that memory addresses increase from left to right.
//Identify the left and right edges of two regions, A and B in memory.
final long bytesA = segA.byteSize();
final long bytesB = segB.byteSize();
final long lA = segA.address(); //left A
final long lB = segB.address(); //left B
final long rA = lA + bytesA; //right A
final long rB = lB + bytesB; //right B
if ((rA <= lB) || (rB <= lA)) { return 0; } //Eliminate the totally disjoint case:
final long result = (bytesA == bytesB) //Two major cases: equal and not equal in size
? nativeOverlapEqualSizes(lA, rA, lB, rB)
: nativeOverlapNotEqualSizes(lA, rA, lB, rB);
return (lB < lA) ? -result : result; //if lB is lower in memory than lA, we return a negative result
}