diff --git a/src/main/java/net/imglib2/type/label/FromIntegerTypeConverter.java b/src/main/java/net/imglib2/type/label/FromIntegerTypeConverter.java index 3075142..37a6493 100644 --- a/src/main/java/net/imglib2/type/label/FromIntegerTypeConverter.java +++ b/src/main/java/net/imglib2/type/label/FromIntegerTypeConverter.java @@ -28,11 +28,8 @@ public static VolatileLabelMultisetType getAppropriateVolatileType() { @Override public void convert(final I input, final LabelMultisetType output) { + final long newVal = input.getIntegerLong(); - final long[] data = getListData(output).data; - if (ByteUtils.getLong(data, Integer.BYTES) != newVal) { - ByteUtils.putLong(newVal, data, Integer.BYTES); - output.getAccess().setArgMax(output.getIndex(), newVal); - } + output.set(newVal, 1); } } diff --git a/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java b/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java index 96345a8..3b17b6c 100644 --- a/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java +++ b/src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java @@ -9,9 +9,9 @@ import java.util.Set; /** - * List of LabelMultisetEntries backed by a LongArray. Should ALWAYS remain sorted by ID. It is sorted after construction, and + * List of LabelMultisetEntries backed by a LongArray. Should ALWAYS remain sorted by ID, ascending. It is sorted after construction, and * all add operations should insert appropriately to remain sorted. If `sortByCount` is ever called (or the order is manually changed) - * it is the developers responsiblity to `sortById()` prior to any additional calls. + * it is the developers responsibility to `sortById()` prior to any additional calls. */ public class LabelMultisetEntryList extends MappedObjectArrayList { @@ -224,7 +224,10 @@ public void sortByCount() { sort((o1, o2) -> { final int i1 = o1.getCount(); final int i2 = o2.getCount(); - return Integer.compare(i1, i2); + final int countCompare = Integer.compare(i1, i2); + if (countCompare == 0) + return Long.compare(o2.getId(), o1.getId()); + return countCompare; }); } diff --git a/src/main/java/net/imglib2/type/label/LabelMultisetType.java b/src/main/java/net/imglib2/type/label/LabelMultisetType.java index 5346fef..80b9f81 100644 --- a/src/main/java/net/imglib2/type/label/LabelMultisetType.java +++ b/src/main/java/net/imglib2/type/label/LabelMultisetType.java @@ -90,6 +90,51 @@ public void releaseRef(LabelMultisetEntry ref) { super.releaseRef(ref); } } + + @Override + public RefIterator iterator() { + + return new RefIterator() { + + private LabelMultisetEntry ref = reference != null ? reference : createRef(); + + private int i = 0; + + @Override + public boolean hasNext() { + + if (i < size()) + return true; + else { + release(); + return false; + } + } + + @Override + public LabelMultisetEntry next() { + + return get(i++, ref); + } + + @Override + public void release() { + + if (reference == null && ref != null) { + releaseRef(ref); + ref = null; + } + } + + @Override + public void reset() { + + if (reference == null && ref == null) + ref = createRef(); + i = 0; + } + }; + } }; this.img = img; @@ -120,7 +165,6 @@ public void release() { if (reference == null) { it.release(); } - } @Override @@ -161,11 +205,15 @@ public void add(final long id, final int count) { public void add(LabelMultisetEntry entry) { - final Label id = entry.id; - final LabelMultisetEntryList lmel = labelMultisetEntries(); - lmel.add(entry); - if (count(id) > count(argMax())) - updateArgMax(id.id()); + final Label label = entry.id; + final LabelMultisetEntryList entryList = labelMultisetEntries(); + entryList.add(entry); + + final long argMax = argMax(); + final long id = label.id(); + if (id == argMax) return; + if (entryList.size() == 1 || count(id) > count(argMax)) + updateArgMax(id); } public void addAll(Collection entries) { @@ -422,7 +470,8 @@ LabelMultisetEntryList labelMultisetEntries() { } private void updateEntriesLocation() { - + if (access.getCurrentStorageArray().length == 0) + return; access.getValue(i.get(), entries); } diff --git a/src/main/java/net/imglib2/type/label/LabelMultisetTypeDownscaler.java b/src/main/java/net/imglib2/type/label/LabelMultisetTypeDownscaler.java index 1375422..9ed4788 100644 --- a/src/main/java/net/imglib2/type/label/LabelMultisetTypeDownscaler.java +++ b/src/main/java/net/imglib2/type/label/LabelMultisetTypeDownscaler.java @@ -32,7 +32,7 @@ public static VolatileLabelMultisetArray createDownscaledCell( final int[] downscaleFactor, final int maxNumEntriesPerPixel) { - final RandomAccess randomAccess = source.randomAccess(); + final RandomAccess sourceAccess = source.randomAccess(); final int numDim = source.numDimensions(); final long[] sourceShape = new long[numDim]; @@ -59,7 +59,12 @@ public static VolatileLabelMultisetArray createDownscaledCell( final TLongArrayList argMax = new TLongArrayList(); - final LabelMultisetEntry addAllRef = list.createRef(); + final LabelMultisetEntry emptyZeroEntry = new LabelMultisetEntry(0, 0); + + final LabelMultisetEntry addRef = list.createRef(); + final LabelMultisetEntry entrySetRef = list.createRef(); + + int nonEmptyListCount = 0; for (int d = 0; d < numDim; ) { @@ -70,9 +75,16 @@ public static VolatileLabelMultisetArray createDownscaledCell( // populate list with all entries from all types for (int g = 0; g < numDim; ) { - final LabelMultisetType lmt = randomAccess.setPositionAndGet(totalOffset); + final LabelMultisetType lmt = sourceAccess.setPositionAndGet(totalOffset); + if ( lmt.getAccess().getListDataUsedSizeInBytes() == 0) { + /* this cell is non-existent, skip to the next cell */ + break; + } + + + for (final LabelMultisetType.Entry