Skip to content

Commit

Permalink
Merge pull request #28 from saalfeldlab/0.12.0
Browse files Browse the repository at this point in the history
0.12.0
  • Loading branch information
cmhulbert committed Oct 13, 2023
2 parents e5deda0 + 007d6a3 commit d27d4a5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>net.imglib2</groupId>
<artifactId>imglib2-label-multisets</artifactId>
<version>0.11.7-SNAPSHOT</version>
<version>0.12.0-SNAPSHOT</version>

<name>ImgLib2 Label Multisets</name>
<description>Implementation of label multisets as an ImgLib2 native type.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.imglib2.type.label.LabelMultisetType.Entry;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;

Expand All @@ -13,6 +14,14 @@ public LabelMultisetEntryList() {
super(LabelMultisetEntry.type);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof LabelMultisetEntryList)) return false;
final long[] thisData = ((LongMappedAccessData) data).getData();
final long[] otherData = ((LongMappedAccessData) ((LabelMultisetEntryList) o).data).getData();
return Arrays.equals(thisData, otherData);
}

public LabelMultisetEntryList(final int capacity) {

super(LabelMultisetEntry.type, capacity);
Expand Down
60 changes: 26 additions & 34 deletions src/main/java/net/imglib2/type/label/LabelMultisetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,26 @@ private LabelMultisetType(final NativeImg<?, VolatileLabelMultisetArray> img, fi
this(img, access, 0);
}

private LabelMultisetEntry reference = null;

private LabelMultisetType(final NativeImg<?, VolatileLabelMultisetArray> img, final VolatileLabelMultisetArray access, final int idx) {

this.entries = new LabelMultisetEntryList();
this.entries = new LabelMultisetEntryList() {
@Override
public LabelMultisetEntry createRef() {
if (reference == null) {
return super.createRef();
} else return reference;
}

@Override
public void releaseRef(LabelMultisetEntry ref) {
if (reference != ref) {
super.releaseRef(ref);
}
}
};

this.img = img;
this.access = access;
this.i.set(idx);
Expand All @@ -100,10 +117,14 @@ public LabelMultisetEntry next() {
return it.next();
}



@Override
public void release() {
if (reference == null) {
it.release();
}

it.release();
}

@Override
Expand Down Expand Up @@ -333,38 +354,9 @@ public Set<Entry<Label>> entrySet() {
return entrySet;
}

public Set<LabelMultisetEntry> entrySetWithRef(LabelMultisetEntry ref) {

access.getValue(i.get(), entries);
return new AbstractSet<LabelMultisetEntry>() {

@Override
public Iterator<LabelMultisetEntry> iterator() {

return new Iterator<LabelMultisetEntry>() {

int idx = 0;

@Override
public boolean hasNext() {

return idx < size();
}

@Override
public LabelMultisetEntry next() {

return entries.get(idx++, ref);
}
};
}

@Override
public int size() {

return entries.size();
}
};
public Set<Entry<Label>> entrySetWithRef(LabelMultisetEntry ref) {
reference = ref;
return entrySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public static VolatileLabelMultisetArray createDownscaledCell(
// populate list with all entries
for (int g = 0; g < numDim; ) {

randomAccess.setPosition(totalOffset);
final LabelMultisetType labelMultisetType = randomAccess.setPositionAndGet(totalOffset);

for (LabelMultisetEntry sourceEntry : randomAccess.get().entrySetWithRef(iteratorEntry)) {
for (LabelMultisetType.Entry<Label> sourceEntry : labelMultisetType.entrySetWithRef(iteratorEntry)) {
final long id = sourceEntry.getElement().id();
final int count = sourceEntry.getCount();
if (cellEntryMap.containsKey(id)) {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/net/imglib2/type/label/LabelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static byte[] serializeLabelMultisetTypes(

final LabelMultisetEntry entryReference = new LabelMultisetEntry(0, 1);

final int entrySizeInBytes = entryReference.getSizeInBytes();
final int entrySizeInBytes = entryReference.getSizeInBytes(); // One Long for the element ID, and one int for the count ( 8 + 4 = 12)

final int argMaxSize = INT_SIZE; // in reality, the int value is always `0`, with size of 4 bytes
final int offsetListSize = INT_SIZE * numElements;
Expand All @@ -76,17 +76,16 @@ public static byte[] serializeLabelMultisetTypes(
if (offsetsForHash != listOffsets.getNoEntryValue()) {
writeInt(dataBuffer, offsetsForHash, ByteOrder.BIG_ENDIAN);
} else {
for (final LabelMultisetEntry entry : lmt.entrySetWithRef(entryReference)) {
for (final Entry<Label> entry : lmt.entrySetWithRef(entryReference)) {
/* NOTE: This is unnecessary data, but an artifact of prior serialization strategy.
* It used to be that the entry we iterator from here was added to a temporary LabelMultisetEntryList
* and then that list was serialized. However, the way this logic was written, the list would alawys
* only contain 1 element.
* We don't do this now, but unfortunately, this mean that we now how to serialize an unnecessary `1`
* integer to mock the size of the list that is expected during deserialization. */
writeInt(entryList, 1, ByteOrder.nativeOrder()); //the old list was always only 1 element in size, stored as the first 4 bytes of a long
for (int i = 0; i < entrySizeInBytes; i++) {
entryList.write(entry.access.getByte(i));
}
writeLong(entryList, entry.getElement().id(), ByteOrder.LITTLE_ENDIAN);
writeInt(entryList, entry.getCount(), ByteOrder.LITTLE_ENDIAN);
}
listOffsets.put(listHash, nextListOffset);
writeInt(dataBuffer, nextListOffset, ByteOrder.BIG_ENDIAN);
Expand All @@ -104,6 +103,10 @@ private static void writeInt(ByteArrayOutputStream dataBuffer, int value, ByteOr
dataBuffer.write(ByteBuffer.allocate(4).order(byteOrder).putInt(value).array(), 0, 4);
}

private static void writeLong(ByteArrayOutputStream dataBuffer, long value, ByteOrder byteOrder) {
dataBuffer.write(ByteBuffer.allocate(8).order(byteOrder).putLong(value).array(), 0, 8);
}

public static LabelMultisetType getOutOfBounds() {

return getOutOfBounds(1);
Expand Down Expand Up @@ -143,13 +146,14 @@ public static VolatileLabelMultisetArray fromBytes(final byte[] bytes, final int
if (argMaxSize == 0) {
argMax = new long[listEntryOffsets.length];
final TIntLongHashMap entryOffsetToArgMax = new TIntLongHashMap(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, -1, -1);
LabelMultisetEntryList lmel = null;
for (int i = 0; i < listEntryOffsets.length; i++) {
final int listDataIdx = listEntryOffsets[i];
final Long cachedArgMax = entryOffsetToArgMax.get(listDataIdx);
final long cachedArgMax = entryOffsetToArgMax.get(listDataIdx);
if (cachedArgMax != entryOffsetToArgMax.getNoEntryValue()) {
argMax[i] = cachedArgMax;
} else {
final LabelMultisetEntryList lmel = new LabelMultisetEntryList();
if (lmel == null) lmel = new LabelMultisetEntryList();
lmel.referToDataAt(listData, listDataIdx);
argMax[i] = LabelUtils.getArgMax(lmel);
entryOffsetToArgMax.put(listDataIdx, argMax[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MappedObjectArrayList<O extends MappedObject<O, T>, T extends Mappe

private final O type;

private MappedAccessData<T> data;
protected MappedAccessData<T> data;

private long baseOffset;

Expand Down

0 comments on commit d27d4a5

Please sign in to comment.