X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Futil%2FVec.java;h=b20e352756c7d1f9c9977670c94acaa3b7d317fc;hb=de378041d5ca2aca1a2b5a31ef15ae90a86c977f;hp=7b55023a9a72616746728f9723250c98432c1b11;hpb=5d4a61fba1188ee6373e5ebf1e52d9f55eafdf50;p=org.ibex.core.git diff --git a/src/org/xwt/util/Vec.java b/src/org/xwt/util/Vec.java index 7b55023..b20e352 100644 --- a/src/org/xwt/util/Vec.java +++ b/src/org/xwt/util/Vec.java @@ -1,4 +1,10 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright (C) 2003 Adam Megacz all rights reserved. +// +// You may modify, copy, and redistribute this code under the terms of +// the GNU Library Public License version 2.1, with the exception of +// the portion of clause 6a after the semicolon (aka the "obnoxious +// relink clause") + package org.xwt.util; import java.util.*; @@ -8,15 +14,19 @@ import java.io.*; * An unsynchronized Vector implementation; same semantics as * java.util.Vector. Useful for JDK1.1 platforms that don't have * java.util.ArrayList. + * + * May contain nulls. + * * @see java.util.Vector */ -public class Vec implements Serializable { +public final class Vec implements Serializable { private Object[] store; private int size = 0; 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) { @@ -37,15 +47,20 @@ public class Vec implements Serializable { public int indexOf(Object o) { for(int i=0; i= store.length) grow(); + if (size >= store.length - 1) grow(); store[size++] = o; } + public Object peek() { + return lastElement(); + } + public Object elementAt(int i) { return store[i]; } @@ -55,6 +70,13 @@ public class Vec implements Serializable { return store[size - 1]; } + public void push(Object o) { addElement(o); } + public Object pop() { + Object ret = lastElement(); + if (size > 0) store[size--] = null; + return ret; + } + public int size() { return size; } public void setSize(int newSize) { @@ -71,6 +93,12 @@ public class Vec implements Serializable { out[i] = store[i]; } + public void fromArray(Object[] in) { + setSize(in.length); + for(int i=0; i= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits"); for(int j=i; j= end) return; + if(end-start <= 6) { + for(int i=start+1;i<=end;i++) { + tmpa = a.store[i]; + if (b != null) tmpb = b.store[i]; + int j; + for(j=i-1;j>=start;j--) { + if(c.compare(a.store[j],tmpa) <= 0) break; + a.store[j+1] = a.store[j]; + if (b != null) b.store[j+1] = b.store[j]; + } + a.store[j+1] = tmpa; + if (b != null) b.store[j+1] = tmpb; + } + return; + } + + Object pivot = a.store[end]; + int lo = start - 1; + int hi = end; + + do { + while(c.compare(a.store[++lo],pivot) < 0) { } + while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { } + swap(a, lo,hi); + if (b != null) swap(b, lo,hi); + } while(lo < hi); + + swap(a, lo,end); + if (b != null) swap(b, lo,end); + + sort(a, b, c, start, lo-1); + sort(a, b, c, lo+1, end); + } + + private static final void swap(Vec vec, int a, int b) { + if(a != b) { + Object tmp = vec.store[a]; + vec.store[a] = vec.store[b]; + vec.store[b] = tmp; + } + } }