Skip to content
Snippets Groups Projects
Commit 4d74c658 authored by tjc's avatar tjc
Browse files

optimise and format

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@2144 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent e0c3c9f6
No related branches found
No related tags found
No related merge requests found
...@@ -20,13 +20,13 @@ ...@@ -20,13 +20,13 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* *
* $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/util/StringVector.java,v 1.1 2004-06-09 09:53:11 tjc Exp $ * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/util/StringVector.java,v 1.2 2004-12-20 15:18:57 tjc Exp $
*/ */
package uk.ac.sanger.artemis.util; package uk.ac.sanger.artemis.util;
import java.util.Vector; import java.util.Vector;
import java.util.StringTokenizer; //import java.util.StringTokenizer;
import java.util.Collections; import java.util.Collections;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
...@@ -35,105 +35,118 @@ import java.util.Comparator; ...@@ -35,105 +35,118 @@ import java.util.Comparator;
* This class implements a Vector of String objects. * This class implements a Vector of String objects.
* *
* @author Kim Rutherford * @author Kim Rutherford
* @version $Id: StringVector.java,v 1.1 2004-06-09 09:53:11 tjc Exp $ * @version $Id: StringVector.java,v 1.2 2004-12-20 15:18:57 tjc Exp $
**/ **/
public class StringVector { public class StringVector
{
/** Storage for String objects. */
private Vector vector;
/** /**
* Create a new vector of String objects. * Create a new vector of String objects.
**/ **/
public StringVector () { public StringVector()
vector = new Vector (); {
vector = new Vector(2);
} }
/** /**
* Create a new vector which contains the given Strings. * Create a new vector which contains the given Strings.
**/ **/
public StringVector (final String [] new_strings) { public StringVector(final String[] new_strings)
vector = new Vector (); {
for (int i = 0 ; i < new_strings.length ; ++i) { int len = new_strings.length;
add (new_strings[i]); vector = new Vector(len);
} for(int i = 0; i < len; ++i)
add(new_strings[i]);
} }
/** /**
* Create a new vector which contains the given Strings. * Create a new vector which contains the given Strings.
**/ **/
public StringVector (final StringVector new_strings) { public StringVector(final StringVector new_strings)
vector = new Vector (); {
for (int i = 0 ; i < new_strings.size () ; ++i) { int len = new_strings.size();
add (new_strings.elementAt (i)); vector = new Vector(len);
} for(int i = 0; i < len; ++i)
add(new_strings.elementAt (i));
} }
/** /**
* Create a new vector which contains only the given String. * Create a new vector which contains only the given String.
**/ **/
public StringVector (final String new_string) { public StringVector(final String new_string)
vector = new Vector (); {
add (new_string); vector = new Vector();
add(new_string);
} }
/** /**
* Performs the same function as Vector.addElement () * Performs the same function as Vector.addElement()
**/ **/
public void add (final String node) { public void add(final String node)
vector.addElement (node); {
vector.addElement(node);
} }
/** /**
* Call add () on each of the String objects in the given StringVector. * Call add() on each of the String objects in the given StringVector.
**/ **/
public void add (final StringVector new_strings) { public void add(final StringVector new_strings)
for (int i = 0 ; i < new_strings.size () ; ++i) { {
add (new_strings.elementAt (i)); for (int i = 0; i < new_strings.size(); ++i)
} add (new_strings.elementAt(i));
} }
/** /**
* Performs the same function as Vector.removeElement () * Performs the same function as Vector.removeElement()
**/ **/
public boolean remove (final String node) { public boolean remove(final String node)
return vector.removeElement (node); {
return vector.removeElement(node);
} }
/** /**
* Return the elements of the Vector as an String array. * Return the elements of the Vector as an String array.
**/ **/
public String [] getArray () { public String[] getArray()
final String [] return_array = new String [size ()]; {
final String[] return_array = new String[size()];
vector.copyInto (return_array); vector.copyInto(return_array);
return return_array; return return_array;
} }
/** /**
* Return the elements of the Vector as Collection. * Return the elements of the Vector as Collection.
**/ **/
public Collection asCollection () { public Collection asCollection()
return (Collection) vector.clone (); {
return (Collection)vector.clone();
} }
/** /**
* Performs the same function as Vector.elementAt () * Performs the same function as Vector.elementAt()
**/ **/
public String elementAt (final int index) { public String elementAt(final int index)
return (String) vector.elementAt (index); {
return (String)vector.elementAt(index);
} }
/** /**
* Performs the same function as Vector.setElementAt () * Performs the same function as Vector.setElementAt ()
**/ **/
public void setElementAt (final String string, final int index) { public void setElementAt(final String string, final int index)
vector.setElementAt (string, index); {
vector.setElementAt(string, index);
} }
/** /**
* Performs the same function as Vector.size () * Performs the same function as Vector.size ()
**/ **/
public int size () { public int size()
return vector.size (); {
return vector.size();
} }
/** /**
...@@ -142,72 +155,62 @@ public class StringVector { ...@@ -142,72 +155,62 @@ public class StringVector {
* @return the index of the first occurrence of the argument in this * @return the index of the first occurrence of the argument in this
* vector; returns -1 if the object is not found. * vector; returns -1 if the object is not found.
**/ **/
public int indexOf (final String string) { public int indexOf(final String string)
return vector.indexOf (string); {
return vector.indexOf(string);
} }
/** /**
* Return true if this object contains the given String, testing for * Return true if this object contains the given String, testing for
* equality using the equals method. * equality using the equals method.
**/ **/
public boolean contains (final String string) { public boolean contains(final String string)
if (indexOf (string) == -1) { {
if(indexOf(string) == -1)
return false; return false;
} else { else
return true; return true;
}
} }
/** /**
* Sorts the elements of the vector using quicksort from the collections * Sorts the elements of the vector using quicksort from the collections
* package. * package.
*/ */
public void sort () { public void sort()
final Comparator comparator = {
new Comparator () { final Comparator comparator = new Comparator()
public int compare (Object fst, Object snd) { {
if (fst == null) { public int compare(Object fst, Object snd)
if (snd == null) { {
return 0; if(fst == null)
} else { {
return -1; if(snd == null)
} return 0;
} else { else
if (snd == null) { return -1;
return 1; }
} else
} {
return ((String) fst).compareTo ((String) snd); if(snd == null)
return 1;
} }
}; return ((String)fst).compareTo((String) snd);
}
};
Collections.sort (vector, comparator); Collections.sort(vector, comparator);
} }
/** /**
* Return a new copy of this object. * Return a new copy of this object.
**/ **/
public StringVector copy () { public StringVector copy()
final StringVector new_string_vector = new StringVector (); {
final StringVector new_string_vector = new StringVector(this);
new_string_vector.vector = (Vector) vector.clone (); // new_string_vector.vector = (Vector)vector.clone();
return new_string_vector; return new_string_vector;
} }
/**
* Return a copy of this vector with all the Strings turned to lower case.
**/
public StringVector toLowerCase () {
final StringVector return_vector = new StringVector ();
for (int i = 0; i < size (); ++i) {
return_vector.add (elementAt (i).toLowerCase ());
}
return return_vector;
}
/** /**
* Return a StringVector containing the values of the given String after * Return a StringVector containing the values of the given String after
* splitting using the given characters. If the argument String is zero * splitting using the given characters. If the argument String is zero
...@@ -219,33 +222,49 @@ public class StringVector { ...@@ -219,33 +222,49 @@ public class StringVector {
* false then the split_characters will be treated as a block (and "foo" * false then the split_characters will be treated as a block (and "foo"
* would be returned in the example. * would be returned in the example.
**/ **/
public static StringVector getStrings (final String argument, public static StringVector getStrings(final String argument,
final String split_characters, String split_characters,
final boolean keep_zero_char_tokens) { final boolean keep_zero_char_tokens)
final StringVector return_vector = new StringVector (); {
final StringVector return_vector = new StringVector();
final StringTokenizer tokeniser =
new StringTokenizer (argument, split_characters, true);
String last_value = null; String last_value = null;
while (tokeniser.hasMoreTokens ()) { int ind1 = 0;
final String value = tokeniser.nextToken (); int ind2;
int argLen = argument.length();
String value;
while(ind1 < argLen)
{
ind2 = argument.indexOf(split_characters,ind1);
if(ind2 == ind1)
{
ind1++;
continue;
}
if(ind2 < 0)
ind2 = argLen;
value = argument.substring(ind1,ind2);
ind1 = ind2+1;
if (value.length () == 1 && if(value.length() == 1 &&
split_characters.indexOf (value.charAt (0)) != -1) { split_characters.indexOf(value.charAt(0)) != -1)
{
// ignore the split characters // ignore the split characters
if (keep_zero_char_tokens && if(keep_zero_char_tokens &&
(last_value == null || (last_value == null ||
last_value != null && last_value.length () == 1 && last_value != null && last_value.length () == 1 &&
split_characters.indexOf (last_value) != -1)) { split_characters.indexOf (last_value) != -1))
{
// we need to add a space because of two split_characters in a row // we need to add a space because of two split_characters in a row
return_vector.add (""); return_vector.add("");
} }
} else { }
return_vector.add (value); else
} return_vector.add(value);
last_value = value; last_value = value;
} }
...@@ -259,9 +278,10 @@ public class StringVector { ...@@ -259,9 +278,10 @@ public class StringVector {
* length or it consists only of the characters used to split, the return * length or it consists only of the characters used to split, the return
* vector will be zero length. * vector will be zero length.
**/ **/
public static StringVector getStrings (final String argument, public static StringVector getStrings(final String argument,
final String split_characters) { final String split_characters)
return getStrings (argument, split_characters, false); {
return getStrings(argument, split_characters, false);
} }
/** /**
...@@ -271,12 +291,24 @@ public class StringVector { ...@@ -271,12 +291,24 @@ public class StringVector {
* String is zero length or it consists only of whitespace, the return * String is zero length or it consists only of whitespace, the return
* vector will be zero length. * vector will be zero length.
**/ **/
public static StringVector getStrings (final String argument) { public static StringVector getStrings(final String argument)
return getStrings (argument, " \t", false); {
return getStrings(argument, " ", false);
}
public static void main(String args[])
{
String argument = "a c g t c g c a t c g a c t c";
long startTime = System.currentTimeMillis();
for(int i=0; i<10000000; i++)
{
getStrings(argument, " ", true);
}
long endTime = System.currentTimeMillis();
System.out.println("TIME TAKEN "+ Long.toString(endTime-startTime));
} }
/**
* Storage for String objects.
**/
private Vector vector;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment