forked from KNEMC/KLALB
KLALB V3.6.0 写了一半
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package org.kne.cloud.network.congestion;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
public class SendByteSlidingWindow<E> implements Iterable<E>{
|
||||
private Object[] array;
|
||||
private volatile int windowSize ;
|
||||
private volatile long windowPosition;
|
||||
|
||||
public int getWindowSize() {
|
||||
return windowSize;
|
||||
}
|
||||
|
||||
public void setWindowSize(int windowSize) {
|
||||
this.windowSize=windowSize;
|
||||
}
|
||||
|
||||
public long getWindowPosition() {
|
||||
return windowPosition;
|
||||
}
|
||||
|
||||
public void setWindowPosition(long windowPosition) {
|
||||
this.windowPosition=windowPosition;
|
||||
}
|
||||
|
||||
public SendByteSlidingWindow(int capacity,int windowSize){
|
||||
if(windowSize>capacity) {
|
||||
throw new IllegalArgumentException("windowSize>capacity!");
|
||||
}
|
||||
array= new Object[capacity];
|
||||
this.windowSize=windowSize;
|
||||
}
|
||||
|
||||
public boolean checkPush() {
|
||||
if(array[calcPosition(windowPosition-windowSize)]==null ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void push(E object) {
|
||||
|
||||
array[calcPosition(windowPosition)]= object;
|
||||
}
|
||||
|
||||
public E remove(long number) {
|
||||
if(number<windowPosition&&number>=windowPosition-windowSize) {
|
||||
int indp=calcPosition(number);
|
||||
E old=(E) array[indp];
|
||||
array[indp]=null;
|
||||
return old;
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
for (long i = windowPosition-windowSize; i < windowPosition; i++) {
|
||||
if(array[calcPosition(i)]!=null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public E get(long number) {
|
||||
if(number<windowPosition&&number>=windowPosition-windowSize) {
|
||||
return (E) array[calcPosition(number)];
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
private long pointer=windowPosition-windowSize;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return pointer<windowPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
return (E) array[calcPosition(pointer++)];
|
||||
}
|
||||
};
|
||||
}
|
||||
private int calcPosition(long number) {
|
||||
return (int) ((number+array.length)%array.length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user