Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
panxiaojun233 committed Dec 29, 2020
1 parent d4b0e51 commit 8bb3516
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 270 deletions.
140 changes: 101 additions & 39 deletions dubbo-common/src/main/java/org/apache/dubbo/common/utils/BitList.java
Original file line number Diff line number Diff line change
@@ -1,154 +1,216 @@
package org.apache.dubbo.common.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
*
*/
public class BitList<E> implements BitListInterf<E> {
BitListInterf<E> delegate;
import org.roaringbitmap.IntIterator;
import org.roaringbitmap.RoaringBitmap;

public BitList(List<E> unmodifiableList, boolean empty) {
delegate = new RoaringBitList<>(unmodifiableList, empty);
public class BitList<E> implements List<E>{
private final RoaringBitmap rootMap;
private final List<E> unmodifiableList;

// FIXME 看一下高版本的RoaringBitMap,是否有内置函数支持更高效的遍历
BitList(List<E> unmodifiableList, boolean empty) {
this.unmodifiableList = unmodifiableList;
this.rootMap = new RoaringBitmap();
if (!empty) {
this.rootMap.add(0L, unmodifiableList.size());
}
}

private BitList(BitListInterf<E> delegate) {
this.delegate = delegate;
private BitList(List<E> unmodifiableList, RoaringBitmap rootMap) {
this.unmodifiableList = unmodifiableList;
this.rootMap = rootMap;
}

public BitList(List<E> unmodifiableList) {
this(unmodifiableList, false);
}

public BitList<E> intersect(BitList<E> b, List<E> totalList) {
return new BitList<E>(delegate.intersect(b, totalList));
}

@Override
public List<E> getUnmodifiableList() {
return delegate.getUnmodifiableList();
return unmodifiableList;
}

public void addIndex(int index) {
delegate.addIndex(index);
this.rootMap.add(index);
}

public BitList<E> intersect(List<E> b, List<E> totalList) {
RoaringBitmap resultMap = rootMap.clone();
resultMap.and(((BitList) b).rootMap);
return new BitList<>(totalList, resultMap);
}

@Override
public int size() {
return delegate.size();
return rootMap.getCardinality();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
return rootMap.isEmpty();
}

@Override
public boolean contains(Object o) {
return delegate.contains(o);
int idx = unmodifiableList.indexOf(o);
return idx >= 0 && rootMap.contains(idx);
}

@Override
public Iterator<E> iterator() {
return delegate.iterator();
return new Iterator<E>() {
private int prev = -1;

@Override
public boolean hasNext() {
return -1 != rootMap.nextValue(prev + 1);
}

@Override
public E next() {
prev = (int) rootMap.nextValue(prev + 1);
return unmodifiableList.get(prev);
}

@Override
public void remove() {
rootMap.remove(prev);
}
};
}

@Override
public Object[] toArray() {
return delegate.toArray();
int size = size();
Object[] obj = new Object[size];
for (int i = 0; i < size; i++) {
obj[i] = unmodifiableList.get(rootMap.select(i));
}
return obj;
}

@Override
public <T> T[] toArray(T[] a) {
return delegate.toArray(a);
int size = size();
Object[] arr = toArray();
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(arr, size, a.getClass());
System.arraycopy(arr, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return null;
}

@Override
public boolean add(E e) {
return delegate.add(e);
throw new UnsupportedOperationException();
}

@Override
public boolean remove(Object o) {
return delegate.remove(o);
int idx = unmodifiableList.indexOf(o);
if (idx > -1) {
rootMap.remove(idx);
}
return true;
}

@Override
public boolean containsAll(Collection<?> c) {
return delegate.containsAll(c);
throw new UnsupportedOperationException();
}

@Override
public boolean addAll(Collection<? extends E> c) {
return delegate.addAll(c);
throw new UnsupportedOperationException();
}

@Override
public boolean addAll(int index, Collection<? extends E> c) {
return delegate.addAll(index, c);
throw new UnsupportedOperationException();
}

@Override
public boolean removeAll(Collection<?> c) {
return delegate.removeAll(c);
for (Object o : c) {
remove(o);
}
return true;
}

@Override
public boolean retainAll(Collection<?> c) {
return delegate.retainAll(c);
return false;
}

@Override
public void clear() {
delegate.clear();
rootMap.clear();
}

@Override
public E get(int index) {
return delegate.get(index);
int real = rootMap.select(index);
return unmodifiableList.get(real);
}

@Override
public E set(int index, E element) {
return delegate.set(index, element);
throw new UnsupportedOperationException();
}

@Override
public void add(int index, E element) {
delegate.add(index, element);
throw new UnsupportedOperationException();
}

@Override
public E remove(int index) {
return delegate.remove(index);
rootMap.remove(index);
return null;
}

@Override
public int indexOf(Object o) {
return delegate.indexOf(o);
IntIterator intIterator = rootMap.getIntIterator();
int st = 0;
while (intIterator.hasNext()) {
int idxInMap = intIterator.next();
if (unmodifiableList.get(idxInMap).equals(o)) {
return st;
}
st++;
}
return -1;
}

@Override
public int lastIndexOf(Object o) {
return delegate.lastIndexOf(o);
throw new UnsupportedOperationException();
}


@Override
public ListIterator<E> listIterator() {
return delegate.listIterator();
throw new UnsupportedOperationException();
}


@Override
public ListIterator<E> listIterator(int index) {
return delegate.listIterator(index);
throw new UnsupportedOperationException();
}


@Override
public List<E> subList(int fromIndex, int toIndex) {
return delegate.subList(fromIndex, toIndex);
throw new UnsupportedOperationException();
}


Expand Down

This file was deleted.

Loading

0 comments on commit 8bb3516

Please sign in to comment.