簡體   English   中英

Java通用循環緩沖區排序

[英]Java generic circular buffer sort

我分配了一個實現通用循環緩沖區的任務。 一切都很好,我只需要執行一種sort方法,但是我想不出一種通用的解決方案。 你能給我一個提示嗎? 謝謝!

public class CircularBuffer<T> {

public T[] elements = null;

private int capacity = 0;
private int dataStart = 0;
private int dataEnd = 0;   

@SuppressWarnings("unchecked")
public CircularBuffer(int capacity) {
    this.capacity = capacity;
    this.elements = (T[]) new Object[capacity];
}

public boolean isEmpty() {
    return dataStart == dataEnd;
}

public boolean isFull() {
    if (dataStart == 0) {
        return dataEnd == capacity - 1 ;
    }
    return dataStart - dataEnd == 1;
}

public int size() {
    return dataEnd - dataStart;
}

public void put(T t) {
    if (isFull()) {
        throw new RuntimeException("Buffer is full");
    }
    if (dataEnd < capacity) {
        elements[dataEnd] = t;
        dataEnd++;
    }
}

public T get() {
    if (isEmpty()) {
        throw new RuntimeException("Buffer is empty");
    }
    return elements[dataStart++];
}

public Object[] toObjectArray() {
    Object[] newArray = new Object[size()];
    for (int i = dataStart; i < dataEnd; i++) {
        newArray[i] = elements[i];
    }
    return newArray;
}

@SuppressWarnings("unchecked")
public <Q> Q[] toArray(Q[] a) {
    if (a.length < size())
        return (Q[]) Arrays.copyOf(elements, size(), a.getClass());
    System.arraycopy(elements, 0, a, 0, size());
    return a;
}

public List<T> asList(List<T> a) {
    List<T> list = new ArrayList<>(size());
    for (int i = dataStart; i < dataEnd; i++) {
        list.add(elements[i]);
    }
    return list;
}

public void addAll(List<? extends T> toAdd) {
    if (toAdd.size() > capacity - size()) {
        throw new RuntimeException("Not enough space to add all elements");
    }
    else {
        for (int i = 0; i < toAdd.size(); i++) {
            elements[dataEnd] = toAdd.get(i);
            dataEnd++;
        }
    }
}

public void sort(Comparator<? super T> comparator) {
    // TODO
}

}

最簡單的選擇是將內容顯示為List ,對其進行排序,然后從現在已排序的列表中替換舊內容。

    public void sort(Comparator<? super T> comparator) {
        // Get them all out - not sure why you have a parameter to `asList`
        List<T> all = asList(Collections.emptyList());
        // Sort them.
        Collections.<T>sort(all);
        // Clear completely.
        dataStart = dataEnd = 0;
        addAll(all);
    }

您將需要更改類的簽名,以確保T是可排序的。

public class CircularBuffer<T extends Comparable<T>> {

好,
A)這不是循環的-當索引dataStartdataEnd超過capacity時,應將其重置。 一種簡單的方法是替換以下內容:

dataEnd++;

有:

dataEnd = (dataEnd + 1) % capacity;

B)一旦這樣做,我猜測“排序”是指僅在dataStartdataEnd之間的部分。 由於具有循環性質,這並非微不足道。 我認為您需要在循環緩沖區上實現其中一種。

我會做這樣的事情:

public void sort(Comparator<? super T> comparator) {
    if (dataStart > dataEnd) {
        System.arraycopy(elements, dataStart, elements, dataEnd, capacity-dataStart);
        dataEnd = dataEnd + capacity-dataStart;
        dataStart = 0;
    }
    Arrays.sort(elements, dataStart, dataEnd, comparator);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM