changes
authorbdemsky <bdemsky>
Wed, 21 Oct 2009 00:02:57 +0000 (00:02 +0000)
committerbdemsky <bdemsky>
Wed, 21 Oct 2009 00:02:57 +0000 (00:02 +0000)
Robust/src/Benchmarks/SingleTM/Genome/ByteString.java [new file with mode: 0644]
Robust/src/Benchmarks/SingleTM/Genome/Gene.java
Robust/src/Benchmarks/SingleTM/Genome/Genome.java
Robust/src/Benchmarks/SingleTM/Genome/Hashtable.java
Robust/src/Benchmarks/SingleTM/Genome/List.java
Robust/src/Benchmarks/SingleTM/Genome/Pair.java
Robust/src/Benchmarks/SingleTM/Genome/Segments.java
Robust/src/Benchmarks/SingleTM/Genome/Sequencer.java
Robust/src/Benchmarks/SingleTM/Genome/constructEntry.java

diff --git a/Robust/src/Benchmarks/SingleTM/Genome/ByteString.java b/Robust/src/Benchmarks/SingleTM/Genome/ByteString.java
new file mode 100644 (file)
index 0000000..20f6c46
--- /dev/null
@@ -0,0 +1,171 @@
+public class ByteString {
+  byte value[];
+  int count;
+  int offset;
+  private int cachedHashcode;
+
+  private ByteString() {
+  }
+
+  public ByteString(byte str[]) {
+    this.value=str;
+    this.count=str.length;
+    this.offset=0;
+  }
+
+  public boolean endsWith(String suffix) {
+    return regionMatches(count - suffix.count, suffix, 0, suffix.count);
+  }
+
+
+  public ByteString substring(int beginIndex) {
+    return substring(beginIndex, this.count);
+  }
+
+  public ByteString subString(int beginIndex, int endIndex) {
+    return substring(beginIndex, endIndex);
+  }
+
+  public ByteString substring(int beginIndex, int endIndex) {
+    ByteString str=new ByteString();
+    if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
+      // FIXME
+      System.printString("Index error: "+beginIndex+" "+endIndex+" "+count+"\n"+this);
+    }
+    str.value=this.value;
+    str.count=endIndex-beginIndex;
+    str.offset=this.offset+beginIndex;
+    return str;
+  }
+
+  public ByteString subString(int beginIndex) {
+    return this.subString(beginIndex, this.count);
+  }
+
+  public int lastindexOf(int ch) {
+    return this.lastindexOf(ch, count - 1);
+  }
+
+  public static ByteString concat2(ByteString s1, ByteString s2) {
+    if (s1==null)
+      return "null".concat(s2);
+    else
+      return s1.concat(s2);
+  }
+
+  public ByteString concat(ByteString str) {
+    ByteString newstr=new ByteString();
+    newstr.count=this.count+str.count;
+    byte charstr[]=new byte[newstr.count];
+    newstr.value=charstr;
+    newstr.offset=0;
+    for(int i=0; i<count; i++) {
+      charstr[i]=value[i+offset];
+    }
+    for(int i=0; i<str.count; i++) {
+      charstr[i+count]=str.value[i+str.offset];
+    }
+    return newstr;
+  }
+
+  public int lastindexOf(int ch, int fromIndex) {
+    for(int i=fromIndex; i>0; i--)
+      if (this.byteAt(i)==ch)
+       return i;
+    return -1;
+  }
+
+  public int indexOf(int ch) {
+    return this.indexOf(ch, 0);
+  }
+
+  public int indexOf(int ch, int fromIndex) {
+    for(int i=fromIndex; i<count; i++)
+      if (this.byteAt(i)==ch)
+       return i;
+    return -1;
+  }
+
+  public int indexOf(ByteString str) {
+    return this.indexOf(str, 0);
+  }
+
+  public int indexOf(ByteString str, int fromIndex) {
+    if (fromIndex<0)
+      fromIndex=0;
+    for(int i=fromIndex; i<=(count-str.count); i++)
+      if (regionMatches(i, str, 0, str.count))
+       return i;
+    return -1;
+  }
+
+  public int lastIndexOf(ByteString str, int fromIndex) {
+    int k=count-str.count;
+    if (k>fromIndex)
+      k=fromIndex;
+    for(; k>=0; k--) {
+      if (regionMatches(k, str, 0, str.count))
+       return k;
+    }
+    return -1;
+  }
+
+  public int lastIndexOf(ByteString str) {
+    return lastIndexOf(str, count-str.count);
+  }
+
+  public boolean startsWith(ByteString str) {
+    return regionMatches(0, str, 0, str.count);
+  }
+
+  public boolean startsWith(ByteString str, int toffset) {
+    return regionMatches(toffset, str, 0, str.count);
+  }
+
+  public boolean regionMatches(int toffset, ByteString other, int ooffset, int len) {
+    if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
+      return false;
+    for(int i=0; i<len; i++)
+      if (other.value[i+other.offset+ooffset]!=
+          this.value[i+this.offset+toffset])
+       return false;
+    return true;
+  }
+
+  public byte[] getBytes() {
+    byte str[]=new byte[count];
+    for(int i=0; i<count; i++)
+      str[i]=(byte)value[i+offset];
+    return str;
+  }
+
+  public int length() {
+    return count;
+  }
+
+  public byte byteAt(int i) {
+    return value[i+offset];
+  }
+  public int hashCode() {
+    if (cachedHashcode!=0)
+      return cachedHashcode;
+    int hashcode=0;
+    for(int i=0; i<count; i++)
+      hashcode=hashcode*31+value[i+offset];
+    cachedHashcode=hashcode;
+    return hashcode;
+  }
+
+  public boolean equals(Object o) {
+    if (o.getType()!=getType())
+      return false;
+    ByteString s=(ByteString)o;
+    if (s.count!=count)
+      return false;
+    for(int i=0; i<count; i++) {
+      if (s.value[i+s.offset]!=value[i+offset])
+       return false;
+    }
+    return true;
+  }
+}
index c69b3882a603bdcc0866124174e3833fe5591bf3..b44de8ba550a16a03127be855d1c4b7ee3723cf5 100644 (file)
@@ -1,6 +1,6 @@
 public class Gene {
   public int length;
-  public String contents;
+  public ByteString contents;
   public Bitmap startBitmapPtr; /* used for creating segments */
   
   Gene(int myLength) {
@@ -17,8 +17,8 @@ public class Gene {
  */
   void create (Random randomObj) {
     int i;
-    char[] nucleotides = new char[4];
-    char[] arrayContents = new char[length];
+    byte[] nucleotides = new byte[4];
+    byte[] arrayContents = new byte[length];
     nucleotides[0] = 'a';
     nucleotides[1] = 'c';
     nucleotides[2] = 'g';
@@ -32,6 +32,6 @@ public class Gene {
       arrayContents[i] = nucleotides[legitimateNumber % 4];
     }
     
-    contents = new String(arrayContents);
+    contents = new ByteString(arrayContents);
   }  
 }
index b44620e044e2552b78e35cc1fb6bd965beb4af56..49c5cc0027296eba9673dba175b847bab90b42ed 100644 (file)
@@ -95,7 +95,7 @@ public class Genome extends Thread {
 
     /* Create and Start Threads */
 
-    String gene = g.genePtr.contents;
+    ByteString gene = g.genePtr.contents;
     Genome[] gn = new Genome[g.numThread];
 
     for(int i = 1; i<g.numThread; i++) {
@@ -117,8 +117,8 @@ public class Genome extends Thread {
 
     /* Check result */
     {
-      String sequence = g.sequencerPtr.sequence;
-      boolean result = (gene.compareTo(sequence) == 0) ? true:false;
+      ByteString sequence = g.sequencerPtr.sequence;
+      boolean result = gene.compareTo(sequence) == 0;
       System.out.println("Sequence matches gene: " + (result ? "yes" : "no"));
       //DEBUG
       //if (result) {
index 449e0e4a232eadb48fdaa3d319e7e4d306fbced5..468aab444ae146b2f5a6978ab2429150be5449b9 100644 (file)
@@ -5,9 +5,7 @@ public class Hashtable {
     int resizeRatio;
     int growthFactor;
     
-    
     public Hashtable (int initNumBucket, int resizeRatio, int growthFactor) {
-
       allocBuckets(initNumBucket);
       numBucket = initNumBucket;
       size = 0;
@@ -15,7 +13,7 @@ public class Hashtable {
       growthFactor = ((growthFactor < 0) ? 3 : growthFactor);
     }
     
-    public boolean TMhashtable_insert (String keyPtr, String dataPtr) {
+    public boolean TMhashtable_insert (ByteString keyPtr, ByteString dataPtr) {
       int i = hashSegment(keyPtr) % numBucket;
 
       Pair findPair = new Pair();
@@ -48,13 +46,13 @@ public class Hashtable {
       }
     }
     
-    int hashSegment (String str) {
+    int hashSegment (ByteString str) {
       int hash = 0;
 
       int index = 0;
       /* Note: Do not change this hashing scheme */
       for(index = 0; index < str.length(); index++) {
-        char c = str.charAt(index);
+        char c = str.byteAt(index);
         hash = c + (hash << 6) + (hash << 16) - hash;
       }
   
index 68386a89443e306648b98d4e4f0c06770c2667e1..4412815a613b50ca277ec76eb059072a6758f68f 100644 (file)
@@ -59,8 +59,8 @@ public class List {
   }
 
   int compareSegment (Pair a, Pair b) { 
-    String aString = a.firstPtr;
-    String bString = b.firstPtr;
+    ByteString aString = a.firstPtr;
+    ByteString bString = b.firstPtr;
     return aString.compareTo(bString);
   }
 }
index 89886bddd58d7fbdcdc8ae284a1c4fbf02eeb21e..97f3a12b5765ccd42265684fc530270bf5fe718d 100644 (file)
@@ -1,13 +1,13 @@
 public class Pair {
-    String firstPtr;
-    String secondPtr;
+    ByteString firstPtr;
+    ByteString secondPtr;
     
     public Pair() {
       firstPtr = null;
       secondPtr = null;
     }
     
-    public Pair(String myFirstPtr, String mySecondPtr) { 
+    public Pair(ByteString myFirstPtr, ByteString mySecondPtr) { 
       firstPtr = myFirstPtr;
       secondPtr = mySecondPtr;
     }
index de1495b6045e853607630cb0acd185f68c920c7a..c1c21e97065136b4ad96f793d18d381814057608 100644 (file)
@@ -3,14 +3,14 @@ public class Segments {
   public int minNum;
   Vector contentsPtr;
   /* private: */
-  String strings[];
+  ByteString strings[];
 
   Segments (int myLength, int myMinNum) {
     minNum = myMinNum;
     length = myLength;
 
-    strings = new String[(int)minNum];
-    contentsPtr = new Vector((int)minNum);
+    strings = new ByteString[minNum];
+    contentsPtr = new Vector(minNum);
   }
 
 
@@ -20,7 +20,7 @@ public class Segments {
    * =============================================================================
    */
   void create (Gene genePtr, Random randomPtr) {
-    String geneString;
+    ByteString geneString;
     int geneLength;
     Bitmap startBitmapPtr;
     int numStart;
@@ -36,15 +36,14 @@ public class Segments {
     for (i = 0; i < minNum; i++) {
       int j = (int)(randomPtr.random_generate() % numStart);
       boolean status = startBitmapPtr.set(j);
-      strings[i] = geneString.substring((int)j, (int)(j+length)); // WRITE SUBSTRING FUNCTION
+      strings[i] = geneString.substring(j, j+length);
       contentsPtr.addElement(strings[i]);
     }
 
     /* Make sure segment covers start */
     i = 0;
     if (!startBitmapPtr.isSet(i)) {
-      String string;
-      string = geneString.subString((int)i, (int)(i+length)); // USE BYTE SUBSTRING FUNCTION
+      ByteString string = geneString.subString(i, i+length);
       contentsPtr.addElement(string);
       startBitmapPtr.set(i);
     }
@@ -61,7 +60,7 @@ public class Segments {
       if (i == i_stop) {
         /* Found big enough hole */
         i = i - 1;
-        String string = geneString.subString((int)i, (int)(i+length)); // USE BYTE SUBSTRING FUNCTION
+        ByteString string = geneString.subString(i, i+length);
         contentsPtr.addElement(string);
         startBitmapPtr.set(i);
       }
index 2fb7f834a9e7e45ff2e97236ed6b5e372c952bf9..bad30de80bcc33ad0de9991f2d5fae91e5a5d3b0 100644 (file)
@@ -1,6 +1,5 @@
 public class Sequencer {
-
-  public String sequence;
+  public ByteString sequence;
 
   public Segments segmentsPtr;
 
@@ -54,7 +53,6 @@ public class Sequencer {
     segmentsPtr = mySegmentsPtr;  
   }
 
-
   /* =============================================================================
    * sequencer_run
    * =============================================================================
@@ -106,7 +104,7 @@ public class Sequencer {
         int ii;
         int ii_stop = Math.imin(i_stop, (i+CHUNK_STEP1));
         for (ii = i; ii < ii_stop; ii++) {
-          String segment = (String)segmentsContentsPtr.elementAt(ii);
+          ByteString segment = (ByteString)segmentsContentsPtr.elementAt(ii);
           if(!uniqueSegmentsPtr.TMhashtable_insert(segment, segment)) {
             ;
           }
@@ -164,17 +162,17 @@ public class Sequencer {
 
       while(it.nextPtr != null) {
         it = it.nextPtr;    
-        String segment = it.dataPtr.firstPtr;
+        ByteString segment = it.dataPtr.firstPtr;
         int newj;
         int startHash;
         boolean status;
 
         /* Find an empty constructEntries entry */
         atomic {
-          while(constructEntries[entryIndex].segment != null) { 
+         while(constructEntries[entryIndex].segment != null) { 
             entryIndex = (entryIndex + 1) % numUniqueSegment; /* look for empty */
           }
-          constructEntries[entryIndex].segment = segment;
+         constructEntries[entryIndex].segment = segment;
         }
 
         constructEntry constructEntryPtr = constructEntries[entryIndex];
@@ -252,39 +250,36 @@ public class Sequencer {
 
         /*  ConstructEntries[entryIndex] is local data */
         constructEntry endConstructEntryPtr = constructEntries[entryIndex];
-        String endSegment = endConstructEntryPtr.segment;
+        ByteString endSegment = endConstructEntryPtr.segment;
         int endHash = endConstructEntryPtr.endHash;
 
         LinkedList chainPtr = buckets[(endHash % numBucket)]; /* buckets: constant data */
         LinkedListIterator it = (LinkedListIterator)chainPtr.iterator();
         while (it.hasNext()) {
           constructEntry startConstructEntryPtr = (constructEntry)it.next();
-          String startSegment = startConstructEntryPtr.segment;
-          int newLength = 0;
+          ByteString startSegment = startConstructEntryPtr.segment;
 
           /* endConstructEntryPtr is local except for properties startPtr/endPtr/length */
           atomic {
-            if(startConstructEntryPtr.isStart &&
-                (endConstructEntryPtr.startPtr != startConstructEntryPtr) &&
-                (startSegment.substring(0, (int)substringLength).compareTo(endSegment.substring((int)(segmentLength-substringLength))) == 0))
-            {
-              startConstructEntryPtr.isStart = false;
-              constructEntry startConstructEntry_endPtr;
-              constructEntry endConstructEntry_startPtr;
+           trans2(startSegment, endSegment, startConstructEntryPtr, endConstructEntryPtr, segmentLength, substringLength, endInfoEntries, entryIndex);
+           //            if(startConstructEntryPtr.isStart &&
+           //   (endConstructEntryPtr.startPtr != startConstructEntryPtr) &&
+           //   (startSegment.substring(0, (int)substringLength).compareTo(endSegment.substring((int)(segmentLength-substringLength))) == 0))
+           // {
+           // startConstructEntryPtr.isStart = false;
 
               /* Update endInfo (appended something so no inter end) */
-              endInfoEntries[entryIndex].isEnd = false;
+              //endInfoEntries[entryIndex].isEnd = false;
               /* Update segment chain construct info */
-              startConstructEntry_endPtr = startConstructEntryPtr.endPtr;
-              endConstructEntry_startPtr = endConstructEntryPtr.startPtr;
-              startConstructEntry_endPtr.startPtr = endConstructEntry_startPtr;
-              endConstructEntryPtr.nextPtr = startConstructEntryPtr;
-              endConstructEntry_startPtr.endPtr = startConstructEntry_endPtr;
-              endConstructEntryPtr.overlap = substringLength;
-              newLength = endConstructEntry_startPtr.length + startConstructEntryPtr.length - substringLength;
-              endConstructEntry_startPtr.length = newLength;
-            } else {/* if (matched) */
-            }
+           //   constructEntry startConstructEntry_endPtr = startConstructEntryPtr.endPtr;
+            //  constructEntry endConstructEntry_startPtr = endConstructEntryPtr.startPtr;
+            //  startConstructEntry_endPtr.startPtr = endConstructEntry_startPtr;
+            //  endConstructEntryPtr.nextPtr = startConstructEntryPtr;
+           // endConstructEntry_startPtr.endPtr = startConstructEntry_endPtr;
+           // endConstructEntryPtr.overlap = substringLength;
+           // int newLength = endConstructEntry_startPtr.length + startConstructEntryPtr.length - substringLength;
+           // endConstructEntry_startPtr.length = newLength;
+           // }
           }
 
           if (!endInfoEntries[entryIndex].isEnd) { /* if there was a match */
@@ -316,14 +311,14 @@ public class Sequencer {
           /* entry 0 is handled seperately from the loop below */
           endInfoEntries[0].jumpToNext = i;
           if (endInfoEntries[0].isEnd) {
-            String segment = constructEntries[0].segment;
+            ByteString segment = constructEntries[0].segment;
             constructEntries[0].endHash = hashString(segment.subString((int)index)); // USE BYTE SUBSTRING FUNCTION
           }
           /* Continue scanning (do not reset i) */
           for (j = 0; i < numUniqueSegment; i+=endInfoEntries[i].jumpToNext) {
 
             if (endInfoEntries[i].isEnd) {
-              String segment = constructEntries[i].segment;
+              ByteString segment = constructEntries[i].segment;
               constructEntries[i].endHash = hashString(segment.substring((int)index)); // USE BYTE SUBSTRING FUNCTION
               endInfoEntries[j].jumpToNext = Math.imax((int)1, (int)(i - j));
               j = i;
@@ -351,9 +346,9 @@ public class Sequencer {
         }
       }
 
-      String sequence = sequencerPtr.sequence;
+      ByteString sequence = sequencerPtr.sequence;
 
-      String copyPtr = sequence;
+      ByteString copyPtr = sequence;
       int sequenceLength = 0;
 
       for (i = 0; i < numUniqueSegment; i++) {
@@ -378,19 +373,40 @@ public class Sequencer {
     }
   }
 
+
+  static void trans2(ByteString startSegment, ByteString endSegment, constructEntry startConstructEntryPtr, constructEntry endConstructEntryPtr, int segmentLength, int substringLength, endInfoEntry endInfoEntries[], int entryIndex) {
+    if(startConstructEntryPtr.isStart &&
+       (endConstructEntryPtr.startPtr != startConstructEntryPtr) &&
+       (startSegment.substring(0, (int)substringLength).compareTo(endSegment.substring((int)(segmentLength-substringLength))) == 0))
+      {
+       startConstructEntryPtr.isStart = false;
+       
+       /* Update endInfo (appended something so no inter end) */
+       endInfoEntries[entryIndex].isEnd = false;
+       /* Update segment chain construct info */
+       constructEntry startConstructEntry_endPtr = startConstructEntryPtr.endPtr;
+       constructEntry endConstructEntry_startPtr = endConstructEntryPtr.startPtr;
+       startConstructEntry_endPtr.startPtr = endConstructEntry_startPtr;
+       endConstructEntryPtr.nextPtr = startConstructEntryPtr;
+       endConstructEntry_startPtr.endPtr = startConstructEntry_endPtr;
+       endConstructEntryPtr.overlap = substringLength;
+       int newLength = endConstructEntry_startPtr.length + startConstructEntryPtr.length - substringLength;
+       endConstructEntry_startPtr.length = newLength;
+      }
+  }
+
   /* =============================================================================
    * hashString
    * -- uses sdbm hash function
    * =============================================================================
    */
-  static int hashString (String str)
-  {
+  static int hashString (ByteString str) {
     int hash = 0;
 
     int index = 0;
     // Note: Do not change this hashing scheme 
     for(index = 0; index < str.length(); index++) {
-      char c = str.charAt(index);
+      byte c = str.byteAt(index);
       hash = c + (hash << 6) + (hash << 16) - hash;
     }
 
index e0c17ecb7fff4414e35b34febc3290d4ed7186bd..2473594463d35ff26648417e4de36d4c8872c4a4 100644 (file)
@@ -1,6 +1,6 @@
 public class constructEntry {
     boolean isStart;
-    String segment;
+    ByteString segment;
     int endHash;
     constructEntry startPtr;
     constructEntry nextPtr;
@@ -8,7 +8,7 @@ public class constructEntry {
     int overlap;
     int length;
       
-    constructEntry(String mySegment, boolean myStart, int myEndHash, constructEntry myStartPtr, constructEntry myNextPtr, constructEntry myEndPtr, int myOverlap, int myLength) {
+    constructEntry(ByteString mySegment, boolean myStart, int myEndHash, constructEntry myStartPtr, constructEntry myNextPtr, constructEntry myEndPtr, int myOverlap, int myLength) {
       segment = mySegment;
       isStart = myStart;
       endHash = myEndHash;