X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Futil%2FVec.java;h=723314e9e299ecaff794859a509a03568be599af;hb=0b0673bbc7f06c5d5418d5ab7ad5961a464e2de0;hp=f40d34563dc6cfe2794cb3d7177c32dd32c8df14;hpb=1bd9bc0c2100ba4a1dc2a5f4ca25ec9e0d1b2265;p=org.ibex.core.git diff --git a/src/org/xwt/util/Vec.java b/src/org/xwt/util/Vec.java index f40d345..723314e 100644 --- a/src/org/xwt/util/Vec.java +++ b/src/org/xwt/util/Vec.java @@ -1,4 +1,4 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] package org.xwt.util; import java.util.*; @@ -17,6 +17,7 @@ public final class Vec implements Serializable { public Vec() { this(10); } public Vec(int i) { store = new Object[i]; } + public Vec(int i, Object[] store) { size = i; this.store = store; } private void grow() { grow(store.length * 2); } private void grow(int newsize) { @@ -42,12 +43,12 @@ public final class Vec implements Serializable { } public void addElement(Object o) { - if (size >= store.length) grow(); + if (size >= store.length - 1) grow(); store[size++] = o; } public Object peek() { - return lastElement(); + return lastElement(); } public Object elementAt(int i) { @@ -61,9 +62,9 @@ public final class Vec implements Serializable { public void push(Object o) { addElement(o); } public Object pop() { - Object ret = lastElement(); - store[size--] = null; - return ret; + Object ret = lastElement(); + if (size > 0) store[size--] = null; + return ret; } public int size() { return size; } @@ -116,4 +117,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); + } }