Skip to content

Commit

Permalink
perf: use reference in iterataor, if provided
Browse files Browse the repository at this point in the history
don't try and update argmax if adding ID that's already argmax
don't count for argmax update if list only has 1 id
  • Loading branch information
cmhulbert committed Aug 8, 2024
1 parent a1ba8f1 commit 0ac589f
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions src/main/java/net/imglib2/type/label/LabelMultisetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,51 @@ public void releaseRef(LabelMultisetEntry ref) {
super.releaseRef(ref);
}
}

@Override
public RefIterator<LabelMultisetEntry> iterator() {

return new RefIterator<LabelMultisetEntry>() {

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;
Expand Down Expand Up @@ -120,7 +165,6 @@ public void release() {
if (reference == null) {
it.release();
}

}

@Override
Expand Down Expand Up @@ -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<? extends LabelMultisetEntry> entries) {
Expand Down

0 comments on commit 0ac589f

Please sign in to comment.