Circular String 2
Ok everyone, I finished making my CircularString class. For now, it does not use generics because I only need it for integers. That will be changed if I find a need for it. I have also made a CircularStringShift class that, instead of using a fillIndex, shifts the contents of the buffer and adds to the end. I tested both with 100,000,000 characters, and they are about the same speed. Here is the code.
public class CircularString {
int string[];
int fillIndex;
public CircularString(int bufSize) {
if (bufSize <= 0) {
throw new IllegalArgumentException("bufSize <= 0");
}
string = new int[bufSize];
fillIndex = 0;
}
public int[] getString() {
return string;
}
@Override
public String toString() {
String cs = "";
for(int i = 0; i < string.length; i++) {
cs += (char) string[i];
}
return cs;
}
public void add(int i) {
string[fillIndex] = i;
if (fillIndex + 1 >= string.length) {
fillIndex = 0;
} else {
fillIndex++;
}
}
private boolean matchVirtual(int virtI, int c) {
if (c == '*') {
return true;
} else {
virtI += fillIndex;
if (virtI < string.length) {
return string[virtI] == c;
} else {
return string[virtI - string.length] == c;
}
}
}
public boolean match(int string[]) {
boolean match = true;
for (int i = 0; i < string.length && i < this.string.length; i++) {
match = match && matchVirtual(i, string[i]);
}
return match;
}
public void clear() {
for(int i = 0; i < string.length; i++) {
string[i] = 0;
fillIndex = 0;
}
}
}
public class CircularStringShift {
int string[];
public CircularStringShift(int bufSize) {
if (bufSize <= 0) {
throw new IllegalArgumentException("bufSize <= 0");
}
string = new int[bufSize];
}
public int[] getString() {
return string;
}
@Override
public String toString() {
String cs = "";
for (int i = 0; i < string.length; i++) {
cs += (char) string[i];
}
return cs;
}
public void add(int i) {
for (int j = 0; j < string.length - 1; j++) {
string[j] = string[j + 1];
}
string[string.length - 1] = i;
}
public boolean match(int string[]) {
boolean match = true;
for (int i = 0; i < string.length && i < this.string.length; i++) {
if (string[i] != '*') {
match = match && this.string[i] == string[i];
}
}
return match;
}
public void clear() {
for (int i = 0; i < string.length; i++) {
string[i] = 0;
}
}
}