working genome on byte strings
[IRC.git] / Robust / src / Benchmarks / SingleTM / Genome / ByteString.java
1 public class ByteString {
2   byte value[];
3   int count;
4   int offset;
5   private int cachedHashcode;
6
7   private ByteString() {
8   }
9
10   public ByteString(byte str[]) {
11     this.value=str;
12     this.count=str.length;
13     this.offset=0;
14   }
15
16   public int compareTo(ByteString s) {
17     int smallerlength=count<s.count?count:s.count;
18
19     int off=offset;
20     int soff=s.offset;
21     for( int i = 0; i < smallerlength; i++) {
22       int valDiff = this.value[i+offset] - s.value[i+soff];
23       if( valDiff != 0 ) {
24         return valDiff;
25       }
26     }
27     return count-s.count;
28   }
29
30   public boolean endsWith(ByteString suffix) {
31     return regionMatches(count - suffix.count, suffix, 0, suffix.count);
32   }
33
34   public ByteString substring(int beginIndex) {
35     return substring(beginIndex, this.count);
36   }
37
38   public ByteString subString(int beginIndex, int endIndex) {
39     return substring(beginIndex, endIndex);
40   }
41
42   public ByteString substring(int beginIndex, int endIndex) {
43     ByteString str=new ByteString();
44     if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
45       // FIXME
46       System.printString("Index error: "+beginIndex+" "+endIndex+" "+count+"\n"+this);
47     }
48     str.value=this.value;
49     str.count=endIndex-beginIndex;
50     str.offset=this.offset+beginIndex;
51     return str;
52   }
53
54   public ByteString subString(int beginIndex) {
55     return this.subString(beginIndex, this.count);
56   }
57
58   public int lastindexOf(int ch) {
59     return this.lastindexOf(ch, count - 1);
60   }
61
62   public ByteString concat(ByteString str) {
63     ByteString newstr=new ByteString();
64     newstr.count=this.count+str.count;
65     byte charstr[]=new byte[newstr.count];
66     newstr.value=charstr;
67     newstr.offset=0;
68     for(int i=0; i<count; i++) {
69       charstr[i]=value[i+offset];
70     }
71     int stroffset=str.offset;
72     for(int i=0; i<str.count; i++) {
73       charstr[i+count]=str.value[stroffset];
74     }
75     return newstr;
76   }
77
78   public int lastindexOf(int ch, int fromIndex) {
79     int off=offset;
80     for(int i=fromIndex; i>0; i--)
81       if (this.value[i+offset]==ch)
82         return i;
83     return -1;
84   }
85
86   public int indexOf(int ch) {
87     return this.indexOf(ch, 0);
88   }
89
90   public int indexOf(int ch, int fromIndex) {
91     int off=offset;
92     for(int i=fromIndex; i<count; i++)
93       if (this.value[i+off]==ch)
94         return i;
95     return -1;
96   }
97
98   public int indexOf(ByteString str) {
99     return this.indexOf(str, 0);
100   }
101
102   public int indexOf(ByteString str, int fromIndex) {
103     if (fromIndex<0)
104       fromIndex=0;
105     for(int i=fromIndex; i<=(count-str.count); i++)
106       if (regionMatches(i, str, 0, str.count))
107         return i;
108     return -1;
109   }
110
111   public int lastIndexOf(ByteString str, int fromIndex) {
112     int k=count-str.count;
113     if (k>fromIndex)
114       k=fromIndex;
115     for(; k>=0; k--) {
116       if (regionMatches(k, str, 0, str.count))
117         return k;
118     }
119     return -1;
120   }
121
122   public int lastIndexOf(ByteString str) {
123     return lastIndexOf(str, count-str.count);
124   }
125
126   public boolean startsWith(ByteString str) {
127     return regionMatches(0, str, 0, str.count);
128   }
129
130   public boolean startsWith(ByteString str, int toffset) {
131     return regionMatches(toffset, str, 0, str.count);
132   }
133
134   public boolean regionMatches(int toffset, ByteString other, int ooffset, int len) {
135     if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
136       return false;
137     for(int i=0; i<len; i++)
138       if (other.value[i+other.offset+ooffset]!=
139           this.value[i+this.offset+toffset])
140         return false;
141     return true;
142   }
143
144   public byte[] getBytes() {
145     byte str[]=new byte[count];
146     for(int i=0; i<count; i++)
147       str[i]=(byte)value[i+offset];
148     return str;
149   }
150
151   public int length() {
152     return count;
153   }
154
155   public byte byteAt(int i) {
156     return value[i+offset];
157   }
158
159   public int hashCode() {
160     if (cachedHashcode!=0)
161       return cachedHashcode;
162     int hash=0;
163     int off=offset;
164     for(int index = 0; index < count; index++) {
165       byte c = value[index+off];
166       hash = c + (hash << 6) + (hash << 16) - hash;
167     }
168     cachedHashcode=hash<0?-hash:hash;
169     return hash;
170   }
171
172   public boolean equals(Object o) {
173     if (o.getType()!=getType())
174       return false;
175     ByteString s=(ByteString)o;
176     if (s.count!=count)
177       return false;
178     for(int i=0; i<count; i++) {
179       if (s.value[i+s.offset]!=value[i+offset])
180         return false;
181     }
182     return true;
183   }
184 }