X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Futil%2FVec.java;fp=src%2Forg%2Fxwt%2Futil%2FVec.java;h=a602089fa4248ff5f6da23dc666b495c5983d22b;hb=165fa8803768452068567e604e72840ab67e5548;hp=803ce3f8f9893e8dafa17c1f67b3205707100fb7;hpb=7801f1389822442ea539417754d0fc64a7d979ae;p=org.ibex.core.git diff --git a/src/org/xwt/util/Vec.java b/src/org/xwt/util/Vec.java index 803ce3f..a602089 100644 --- a/src/org/xwt/util/Vec.java +++ b/src/org/xwt/util/Vec.java @@ -116,4 +116,47 @@ public final class Vec implements Serializable { size++; } + public interface CompareFunc { + public int compare(Object a, Object b); + } + + public void sort(CompareFunc c) { + sort(c,0,size-1); + } + + private final void swap(int a, int b) { + if(a != b) { + Object tmp = store[a]; + store[a] = store[b]; + store[b] = tmp; + } + } + + private void sort(CompareFunc c, int start, int end) { + Object tmp; + if(start >= end) return; + if(end-start <= 6) { + for(int i=start+1;i<=end;i++) { + tmp = store[i]; + int j; + for(j=i-1;j>=start;j--) { + if(c.compare(store[j],tmp) <= 0) break; + store[j+1] = store[j]; + } + store[j+1] = tmp; + } + return; + } + Object pivot = store[end]; + int lo = start - 1; + int hi = end; + do { + while(c.compare(store[++lo],pivot) < 0) { } + while((hi > lo) && c.compare(store[--hi],pivot) > 0) { } + swap(lo,hi); + } while(lo < hi); + swap(lo,end); + sort(c,start,lo-1); + sort(c,lo+1,end); + } }