Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitSetReadOnlyUnbackedObservableList should implement indexOf #1541

Merged
merged 4 commits into from Mar 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -344,6 +344,43 @@ public BitSetReadOnlyUnbackedObservableList(BitSet bitset) {
return -1;
}

@Override public int indexOf(Object obj) {
if (!(obj instanceof Number)) {
return -1;
}
Number n = (Number) obj;
int index = n.intValue();
if (!bitset.get(index)) {
return -1;
}

// is left most bit
if (index == 0) {
return 0;
}

// is right most bit
if (index == bitset.length() - 1) {
return size() - 1;
}

// count right bit
if (index > bitset.length() / 2) {
int count = 1;
for (int i = bitset.nextSetBit(index+1); i >= 0; i = bitset.nextSetBit(i+1)) {
count++;
}
return size() - count;
}

// count left bit
int count = 0;
for (int i = bitset.previousSetBit(index-1); i >= 0; i = bitset.previousSetBit(i-1)) {
count++;
}
return count;
}

@Override public int size() {
return bitset.cardinality();
}
Expand Down