From 2f3933148d86a27bc0679002b48ab439e27c73fe Mon Sep 17 00:00:00 2001 From: weisburd Date: Wed, 19 May 2010 03:37:26 +0000 Subject: [PATCH] Added fast split(str, delimiter) methodf git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@3384 348d0f76-0448-11de-a6fe-93d51630548a --- .../org/broadinstitute/sting/utils/Utils.java | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/java/src/org/broadinstitute/sting/utils/Utils.java b/java/src/org/broadinstitute/sting/utils/Utils.java index b4d4bce02..547858fd5 100755 --- a/java/src/org/broadinstitute/sting/utils/Utils.java +++ b/java/src/org/broadinstitute/sting/utils/Utils.java @@ -25,12 +25,19 @@ package org.broadinstitute.sting.utils; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + import net.sf.samtools.util.StringUtil; + import org.apache.log4j.Logger; import org.broadinstitute.sting.utils.collections.Pair; -import java.util.*; - /** * Created by IntelliJ IDEA. * User: depristo @@ -150,6 +157,40 @@ public class Utils { return join(recordSeperator,joinedKeyValues); } + /** + * Splits a String using indexOf instead of regex to speed things up. + * + * @param str the string to split. + * @param delimiter the delimiter used to split the string. + * @return an array of tokens. + */ + public static ArrayList split(String str, String delimiter) { + return split(str, delimiter, 10); + } + + /** + * Splits a String using indexOf instead of regex to speed things up. + * + * @param str the string to split. + * @param delimiter the delimiter used to split the string. + * @param expectedNumTokens The number of tokens expected. This is used to initialize the ArrayList. + * @return an array of tokens. + */ + public static ArrayList split(String str, String delimiter, int expectedNumTokens) { + final ArrayList result = new ArrayList(expectedNumTokens); + + int delimiterIdx = -1; + do { + final int tokenStartIdx = delimiterIdx + 1; + delimiterIdx = str.indexOf(delimiter, tokenStartIdx); + final String token = (delimiterIdx != -1 ? str.substring(tokenStartIdx, delimiterIdx) : str.substring(tokenStartIdx) ); + result.add(token); + } while( delimiterIdx != -1 ); + + return result; + } + + /** * join an array of strings given a seperator * @param separator the string to insert between each array element @@ -177,12 +218,14 @@ public class Utils { //} public static String join(String separator, Collection objects) { - final StringBuilder ret = new StringBuilder(); - for(final Object o : objects) { - if(ret.length() != 0) { - ret.append(separator); - } - ret.append(o.toString()); + if(objects.isEmpty()) { + return ""; + } + Iterator iter = objects.iterator(); + final StringBuilder ret = new StringBuilder(iter.next().toString()); + while(iter.hasNext()) { + ret.append(separator); + ret.append(iter.next().toString()); } return ret.toString();