made Preprocessor.java independent of gnu.regexp and Vec.java
authoradam <adam@megacz.com>
Tue, 13 Jul 2004 01:49:26 +0000 (01:49 +0000)
committeradam <adam@megacz.com>
Tue, 13 Jul 2004 01:49:26 +0000 (01:49 +0000)
darcs-hash:20040713014926-5007d-90baa14164109bb9005592b35c243ba0393350fe.gz

src/org/ibex/util/Preprocessor.java

index a226731..89f4e4e 100644 (file)
@@ -7,7 +7,7 @@
 
 package org.ibex.util;
 
-import gnu.regexp.*;
+//import gnu.regexp.*;
 import java.util.*;
 import java.io.*;
 
@@ -31,6 +31,8 @@ import java.io.*;
 public class Preprocessor {
 
     public static String replaceAll(String source, String regexp, String replaceWith) {
+        return source.replaceAll(regexp, replaceWith);
+        /*
         try {
             RE re = new RE(regexp, 0, RESyntax.RE_SYNTAX_PERL5);
             return (String)re.substituteAll(source, replaceWith);
@@ -38,6 +40,7 @@ public class Preprocessor {
             e.printStackTrace();
             return null;
         }
+        */
     }
 
     public static void main(String[] args) throws Exception {
@@ -227,11 +230,175 @@ PROCESS:
         return err;
     }
 
+    private static class MyVec {
+        private Object[] store;
+        private int size = 0;
+    
+        public MyVec() { this(10); }
+        public MyVec(int i) { store = new Object[i]; }
+        public MyVec(int i, Object[] store) { size = i; this.store = store; }
+    
+        private void grow() { grow(store.length * 2); }
+        private void grow(int newsize) {
+            Object[] newstore = new Object[newsize];
+            System.arraycopy(store, 0, newstore, 0, size);
+            store = newstore;
+        }
+
+        public void removeAllElements() {
+            for(int i=0; i<size; i++) store[i] = null;
+            size = 0;
+        }
+
+        public void toArray(Object[] o) {
+            for(int i=0; i<size; i++)
+                o[i] = store[i];
+        }
+    
+        public int indexOf(Object o) {
+            for(int i=0; i<size; i++)
+                if (o == null ? store[i] == null : store[i].equals(o)) return i;
+        
+            return -1;
+        }
+    
+        public void addElement(Object o) {
+            if (size >= store.length - 1) grow();
+            store[size++] = o;
+        }
+
+        public Object peek() {
+            return lastElement();
+        }
+
+        public Object elementAt(int i) {
+            return store[i];
+        }
+    
+        public Object lastElement() {
+            if (size == 0) return null;
+            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) {
+            if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
+            if (newSize > store.length) grow(newSize * 2);
+            if (newSize < size)
+                for(int i=newSize; i<size; i++)
+                    store[i] = null;
+            size = newSize;
+        }
+
+        public void copyInto(Object[] out) {
+            for(int i=0; i<size; i++)
+                out[i] = store[i];
+        }
+
+        public void fromArray(Object[] in) {
+            setSize(in.length);
+            for(int i=0; i<size; i++)
+                store[i] = in[i];
+        }
+
+        public void removeElementAt(int i) {
+            if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
+            for(int j=i; j<size - 1; j++)
+                store[j] = store[j + 1];
+            setSize(size - 1);
+        }
+
+        public void setElementAt(Object o, int i) {
+            if (i >= size) setSize(i);
+            store[i] = o;
+        }
+
+        public void removeElement(Object o) {
+            int idx = indexOf(o);
+            if (idx != -1) removeElementAt(idx);
+        }
+
+        public void insertElementAt(Object o, int at) {
+            if (size == store.length) grow();
+            for(int i=size; i>at; i--)
+                store[i] = store[i-1];
+            store[at] = o;
+            size++;
+        }
+
+        public interface CompareFunc {
+            public int compare(Object a, Object b);
+        }
+
+        public void sort(CompareFunc c) {
+            sort(this, null, c, 0, size-1);
+        }
+
+        public static void sort(MyVec a, MyVec b, CompareFunc c) {
+            if (b != null && a.size != b.size) throw new IllegalArgumentException("MyVec a and b must be of equal size");
+            sort(a, b, c, 0, a.size-1);
+        }
+
+        private static final void sort(MyVec a, MyVec b, CompareFunc c, int start, int end) {
+            Object tmpa, tmpb = null;
+            if(start >= 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(MyVec vec, int a, int b) {
+            if(a != b) {
+                Object tmp = vec.store[a];
+                vec.store[a] = vec.store[b];
+                vec.store[b] = tmp;
+            }
+        }
+
+    }
+
     private void buildTrie(String prefix, Hashtable cases) {
         Enumeration caseKeys = cases.keys();
-        Vec keys = new Vec();
+        MyVec keys = new MyVec();
         while(caseKeys.hasMoreElements()) keys.addElement(caseKeys.nextElement());
-        keys.sort(new Vec.CompareFunc() { public int compare(Object a, Object b) {
+        keys.sort(new MyVec.CompareFunc() { public int compare(Object a, Object b) {
             return ((String)a).compareTo((String)b);
         } } );