--- /dev/null
+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;
+ }
+}
public class Gene {
public int length;
- public String contents;
+ public ByteString contents;
public Bitmap startBitmapPtr; /* used for creating segments */
Gene(int myLength) {
*/
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';
arrayContents[i] = nucleotides[legitimateNumber % 4];
}
- contents = new String(arrayContents);
+ contents = new ByteString(arrayContents);
}
}
/* 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++) {
/* 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) {
int resizeRatio;
int growthFactor;
-
public Hashtable (int initNumBucket, int resizeRatio, int growthFactor) {
-
allocBuckets(initNumBucket);
numBucket = initNumBucket;
size = 0;
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();
}
}
- 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;
}
}
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);
}
}
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;
}
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);
}
* =============================================================================
*/
void create (Gene genePtr, Random randomPtr) {
- String geneString;
+ ByteString geneString;
int geneLength;
Bitmap startBitmapPtr;
int numStart;
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);
}
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);
}
public class Sequencer {
-
- public String sequence;
+ public ByteString sequence;
public Segments segmentsPtr;
segmentsPtr = mySegmentsPtr;
}
-
/* =============================================================================
* sequencer_run
* =============================================================================
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)) {
;
}
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];
/* 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 */
/* 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;
}
}
- String sequence = sequencerPtr.sequence;
+ ByteString sequence = sequencerPtr.sequence;
- String copyPtr = sequence;
+ ByteString copyPtr = sequence;
int sequenceLength = 0;
for (i = 0; i < numUniqueSegment; i++) {
}
}
+
+ 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;
}
public class constructEntry {
boolean isStart;
- String segment;
+ ByteString segment;
int endHash;
constructEntry startPtr;
constructEntry nextPtr;
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;