Skip to content

Commit

Permalink
Merge pull request #30 from saalfeldlab/feat/more_implementations
Browse files Browse the repository at this point in the history
Feat/more implementations
  • Loading branch information
cmhulbert committed Apr 1, 2024
2 parents 4b98f88 + 445ce21 commit 59cc854
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 131 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.janelia.saalfeldlab</groupId>
<artifactId>n5-imglib2</artifactId>
Expand Down
62 changes: 59 additions & 3 deletions src/main/java/net/imglib2/type/label/LabelMultisetEntryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;

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

public class LabelMultisetEntryList
extends MappedObjectArrayList<LabelMultisetEntry, LongMappedAccess> {
/**
* List of LabelMultisetEntries backed by a LongArray. Should ALWAYS remain sorted by ID. 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.
*/
public class LabelMultisetEntryList extends MappedObjectArrayList<LabelMultisetEntry, LongMappedAccess> {

public LabelMultisetEntryList() {

Expand Down Expand Up @@ -51,6 +56,7 @@ public LabelMultisetEntryList(final int capacity) {
public LabelMultisetEntryList(final LongMappedAccessData data, final long baseOffset) {

super(LabelMultisetEntry.type, data, baseOffset);
sortById();
}

protected int multisetSize() {
Expand All @@ -62,6 +68,45 @@ protected int multisetSize() {
return size;
}

@Override public boolean add(LabelMultisetEntry obj) {

final LabelMultisetEntry ref = createRef();
add(obj ,ref);
releaseRef(ref);
return true;
}

@Override public boolean add(LabelMultisetEntry obj, LabelMultisetEntry ref) {

final int idx = binarySearch(obj.getId(), ref);
if (idx >= 0) {
final LabelMultisetEntry entry = get(idx, ref);
entry.setCount(entry.getCount() + obj.getCount());
} else {
add(-(idx + 1), obj, ref);
}
return true;
}

@Override public boolean addAll(Collection<? extends LabelMultisetEntry> c) {

final LabelMultisetEntry ref = createRef();
for (LabelMultisetEntry entry : c) {
add(entry, ref);
}
return true;
}



@Override public boolean addAll(Collection<? extends LabelMultisetEntry> c, LabelMultisetEntry ref) {

for (LabelMultisetEntry entry : c) {
add(entry, ref);
}
return true;
}

/**
* Performs a binary search for entry with
* {@link LabelMultisetEntry#getId()} <tt>id</tt> in the entire list. Note
Expand Down Expand Up @@ -162,7 +207,6 @@ public int binarySearch(final long id, final int fromIndex, final int toIndex, L
/**
* Sort the list by {@link LabelMultisetEntry#getId()}.
*/
// TODO: should this be protected / package private?
public void sortById() {

sort((o1, o2) -> {
Expand All @@ -172,6 +216,18 @@ public void sortById() {
});
}

/**
* Sort the list by {@link LabelMultisetEntry#getId()}.
*/
public void sortByCount() {

sort((o1, o2) -> {
final int i1 = o1.getCount();
final int i2 = o2.getCount();
return Integer.compare(i1, i2);
});
}

/**
* Merge consecutive {@link LabelMultisetEntry entries} with the same
* {@link LabelMultisetEntry#getId() id}.
Expand Down
Loading

0 comments on commit 59cc854

Please sign in to comment.