John Mercier

java programming and scjp

  • Blog
  • Projects

Circular String 2

Posted on January 28, 2009 by John J Mercier

Filed under Programming | 0 Comments

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;
}
}
}

Share |

« StringReaderInputStr... | Main | Album Creator 1.0.a »


Comments:

Post a Comment:
  • HTML Syntax: Allowed

Search

Tag Cloud

activism addthis.com data_structure downloading facebook google google-buzz introduction java javablackbelt jdbc johnmercier.com jsp linux model-1 netbeans nvidia objectivism official-english programming projects pti roller scjp server sql theme uncertainty velocity welfare-state

Friends

  • Ed
  • Shane

Links

  • Glazed Lists
  • JGoodies
  • Java Specialists
  • Swing 2.0
  • Swing Generics
  • ideone
  • pircbot

Feeds

  • All
  • /General
  • /Projects
  • /Programming
  • Comments

Referrers

  • direct (27)
  • www.semrush.com/info (1)

Navigation

  • John Mercier
  • Weblog
  • Login

©2010 John J Mercier.

Designed by Free CSS Templates. Template by E. Strokin. Powered by Roller Weblogger 4.0.1.