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