diff --git a/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java b/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java index 5b1c966..9c35ece 100644 --- a/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java +++ b/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java @@ -3,7 +3,6 @@ import net.imglib2.type.label.LabelMultisetType.Entry; import org.apache.commons.lang3.builder.HashCodeBuilder; -import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.Set; @@ -13,7 +12,7 @@ * all add operations should insert appropriately to remain sorted. If `sortByCount` is ever called (or the order is manually changed) * it is the developers responsibility to `sortById()` prior to any additional calls. */ -public class LabelMultisetEntryList extends MappedObjectArrayList implements Comparable { +public class LabelMultisetEntryList extends MappedObjectArrayList { public LabelMultisetEntryList() { @@ -43,7 +42,9 @@ public boolean equals(Object o) { if (!(o instanceof LabelMultisetEntryList)) return false; final LabelMultisetEntryList other = (LabelMultisetEntryList)o; if (other.size() != size()) return false; - return compareTo(other) == 0; + final ComparableLabelMultisetEntryList thisComparable = new ComparableLabelMultisetEntryList(this); + final ComparableLabelMultisetEntryList otherComparable = new ComparableLabelMultisetEntryList(other); + return thisComparable.compareTo(otherComparable) == 0; } public LabelMultisetEntryList(final int capacity) { @@ -394,28 +395,4 @@ public void mergeWith(final Set> entrySet) { } releaseRef(e1); } - - @Override public int compareTo(LabelMultisetEntryList o) { - - final int size = size(); - - final int sizeCompare = Integer.compare(size, o.size()); - if (sizeCompare != 0) return sizeCompare; - - final RefIterator thisIter = iterator(); - final RefIterator otherIter = o.iterator(); - - for (int i = 0; i < size; i++) { - final LabelMultisetEntry thisNext = thisIter.next(); - final LabelMultisetEntry otherNext = otherIter.next(); - - final int idCompare = Long.compare(thisNext.getId(), otherNext.getId()); - if (idCompare != 0) return idCompare; - - final int countCompare = Long.compare(thisNext.getCount(), otherNext.getCount()); - if (countCompare != 0) return countCompare; - } - - return 0; - } } diff --git a/src/main/java/net/imglib2/type/label/LabelMultisetType.java b/src/main/java/net/imglib2/type/label/LabelMultisetType.java index db7616b..514b0b6 100644 --- a/src/main/java/net/imglib2/type/label/LabelMultisetType.java +++ b/src/main/java/net/imglib2/type/label/LabelMultisetType.java @@ -287,8 +287,7 @@ public LabelMultisetType copy() { access.isValid(), access.argMaxCopy()); /* get a new type instance */ - final LabelMultisetType that = new LabelMultisetType(null, accessCopy); - return that; + return new LabelMultisetType(null, accessCopy); } } @@ -709,7 +708,9 @@ public int compareTo(final LabelMultisetType arg0) { final int countCompare = Long.compare(count(ours), count(theirs)); if (countCompare != 0) return countCompare; - return labelMultisetEntries().compareTo(arg0.labelMultisetEntries()); + final ComparableLabelMultisetEntryList thisComparable = new ComparableLabelMultisetEntryList(labelMultisetEntries()); + final ComparableLabelMultisetEntryList otherComparable = new ComparableLabelMultisetEntryList(arg0.labelMultisetEntries()); + return thisComparable.compareTo(otherComparable); } @Override diff --git a/src/main/java/net/imglib2/type/label/LabelUtils.java b/src/main/java/net/imglib2/type/label/LabelUtils.java index fb98194..b54c22d 100644 --- a/src/main/java/net/imglib2/type/label/LabelUtils.java +++ b/src/main/java/net/imglib2/type/label/LabelUtils.java @@ -13,13 +13,13 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Collection; +import java.util.Set; import java.util.TreeMap; import static gnu.trove.impl.Constants.DEFAULT_CAPACITY; import static gnu.trove.impl.Constants.DEFAULT_LOAD_FACTOR; import static net.imglib2.type.label.AbstractLabelMultisetLoader.argMaxListSizeInBytes; import static net.imglib2.type.label.AbstractLabelMultisetLoader.listOffsetsSizeInBytes; -import static net.imglib2.type.label.ByteUtils.INT_SIZE; public class LabelUtils { @@ -51,44 +51,47 @@ public static byte[] serializeLabelMultisetTypes( final LabelMultisetEntry entryReference = new LabelMultisetEntry(0, 1); - final int argMaxSize = INT_SIZE; // in reality, the int value is always `0`, with size of 4 bytes - final int offsetListSize = INT_SIZE * numElements; final ByteArrayOutputStream dataBuffer = new ByteArrayOutputStream(); /* No longer serialized out ArgMax; we do this by specifying the ArgMax size as 0; * It's now calculated during deserializtaion instead.*/ writeInt(dataBuffer, 0, ByteOrder.BIG_ENDIAN); - final TreeMap listOffsets = new TreeMap<>(); + final TreeMap listOffsets = new TreeMap<>(); int nextListOffset = 0; - int nonEmptyListCount = 0; + boolean allListsEmpty = true; final ByteArrayOutputStream entryList = new ByteArrayOutputStream(); + ComparableLabelMultisetEntryList listRef = new ComparableLabelMultisetEntryList(); for (final LabelMultisetType lmt : lmts) { - // It's not isEmpty() and size() are not equivalent in this case. - // size() refers to the sum of the count() of each entry. - // entries can have a count of `0` - //noinspection SizeReplaceableByIsEmpty - if (!(lmt.isEmpty() || (lmt.size() == 0) )) - nonEmptyListCount++; - final Integer listOffset = listOffsets.get(lmt.labelMultisetEntries()); + lmt.getAccess().getValue(lmt.index().get(), listRef); + final Integer listOffset = listOffsets.putIfAbsent(listRef, nextListOffset); if (listOffset != null) { writeInt(dataBuffer, listOffset, ByteOrder.BIG_ENDIAN); } else { - writeInt(entryList, lmt.entrySet().size(), ByteOrder.LITTLE_ENDIAN); - for (final Entry