diff options
author | Paul Phillips <paulp@improving.org> | 2010-12-20 06:38:16 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-12-20 06:38:16 +0000 |
commit | f40a20b0f418209715529ca5b373f641a5011e0b (patch) | |
tree | 64e8462fc2d0ddb6fc4192f64e73a628d8a4ae3e /src/scalap | |
parent | 0e306e1f90f51efa2d423fd7df810f7cc8dfa915 (diff) | |
download | scala-f40a20b0f418209715529ca5b373f641a5011e0b.tar.gz scala-f40a20b0f418209715529ca5b373f641a5011e0b.tar.bz2 scala-f40a20b0f418209715529ca5b373f641a5011e0b.zip |
I'm wandering around trunk looking for slowness.
are constant distractions which I've meant forever to fix anyway, such
as the importing of collection.mutable._ (or any other package with lots
of reused names.) Why is this relevant to performance? Well,
var x = new HashSet[Int]
What's that? What does 'x += 1' mean? These are questions we can all
live without.
There's almost nothing left which references HashSet or HashMap or Stack
or other ambiguous classes without a qualifier: I can finish trunk but
I can't keep it clean on my own. (Where should I be writing this stuff
down, I wonder.) No review.
Diffstat (limited to 'src/scalap')
-rw-r--r-- | src/scalap/scala/tools/scalap/Arguments.scala | 96 |
1 files changed, 30 insertions, 66 deletions
diff --git a/src/scalap/scala/tools/scalap/Arguments.scala b/src/scalap/scala/tools/scalap/Arguments.scala index ebe3d972f0..a364b54120 100644 --- a/src/scalap/scala/tools/scalap/Arguments.scala +++ b/src/scalap/scala/tools/scalap/Arguments.scala @@ -8,18 +8,16 @@ package scala.tools.scalap -import scala.collection.mutable._ - +import scala.collection.mutable +import mutable.{ Buffer, ListBuffer } object Arguments { - case class Parser(optionPrefix: Char) { - - val options: Set[String] = new HashSet - val prefixes: Set[String] = new HashSet - val optionalArgs: Set[String] = new HashSet - val prefixedBindings: Map[String, Char] = new HashMap - val optionalBindings: Map[String, Char] = new HashMap + val options: mutable.Set[String] = new mutable.HashSet + val prefixes: mutable.Set[String] = new mutable.HashSet + val optionalArgs: mutable.Set[String] = new mutable.HashSet + val prefixedBindings: mutable.Map[String, Char] = new mutable.HashMap + val optionalBindings: mutable.Map[String, Char] = new mutable.HashMap def argumentError(message: String): Unit = Console.println(message) @@ -48,14 +46,9 @@ object Arguments { this } - def parseBinding(str: String, separator: Char): (String, String) = { - val eqls = str.indexOf(separator) - if (eqls < 0) { - argumentError("missing '" + separator + "' in binding '" + str + "'") - Pair("", "") - } else - Pair(str.substring(0, eqls).trim(), - str.substring(eqls + 1).trim()) + def parseBinding(str: String, separator: Char): (String, String) = (str indexOf separator) match { + case -1 => argumentError("missing '" + separator + "' in binding '" + str + "'") ; Pair("", "") + case idx => Pair(str take idx trim, str drop (idx + 1) trim) } def parse(args: Array[String]): Arguments = { @@ -73,7 +66,7 @@ object Arguments { else if (args(i).charAt(0) != optionPrefix) { res.addOther(args(i)) i += 1 - } else if (options contains args(i)) { + } else if (options(args(i))) { res.addOption(args(i)) i += 1 } else if (optionalArgs contains args(i)) { @@ -126,78 +119,49 @@ object Arguments { def parse(options: String*)(args: Array[String]): Arguments = { val parser = new Parser('-') - val iter = options.iterator - while (iter.hasNext) - parser withOption iter.next - parser.parse(args) + options foreach (parser withOption _) + parser parse args } } class Arguments { - - private val options: Set[String] = new HashSet - private val arguments: Map[String, String] = new HashMap - private val prefixes: Map[String, Set[String]] = new HashMap - private val bindings: Map[String, Map[String, String]] = new HashMap - private val others: Buffer[String] = new ListBuffer + private val options = new mutable.HashSet[String] + private val arguments = new mutable.HashMap[String, String] + private val prefixes = new mutable.HashMap[String, mutable.HashSet[String]] + private val bindings = new mutable.HashMap[String, mutable.HashMap[String, String]] + private val others = new ListBuffer[String] def addOption(option: String): Unit = options += option - def addArgument(option: String, arg: String) { - arguments(option) = arg - } + def addArgument(option: String, arg: String): Unit = arguments(option) = arg def addPrefixed(prefix: String, arg: String): Unit = - if (prefixes isDefinedAt prefix) - prefixes(prefix) += arg - else { - prefixes(prefix) = new HashSet - prefixes(prefix) += arg - } + prefixes.getOrElseUpdate(prefix, new mutable.HashSet) += arg def addBinding(tag: String, key: String, value: String): Unit = - if (key.length() > 0) { - if (bindings isDefinedAt tag) - bindings(tag)(key) = value - else { - bindings(tag) = new HashMap - bindings(tag)(key) = value - } - } + if (key.length > 0) + bindings.getOrElseUpdate(tag, new mutable.HashMap)(key) = value - def addBinding(tag: String, binding: Pair[String, String]) { + def addBinding(tag: String, binding: Pair[String, String]): Unit = addBinding(tag, binding._1, binding._2) - } def addOther(arg: String): Unit = others += arg - def contains(option: String): Boolean = options contains option + def contains(option: String): Boolean = options(option) def getArgument(option: String): Option[String] = arguments get option - def getSuffixes(prefix: String): Set[String] = - prefixes.get(prefix) match { - case None => new HashSet - case Some(set) => set - } + def getSuffixes(prefix: String): mutable.Set[String] = + prefixes.getOrElse(prefix, new mutable.HashSet) def containsSuffix(prefix: String, suffix: String): Boolean = - prefixes.get(prefix) match { - case None => false - case Some(set) => set contains suffix - } + prefixes get prefix exists (set => set(suffix)) - def getBindings(tag: String): Map[String, String] = - bindings.get(tag) match { - case None => new HashMap - case Some(map) => map - } + def getBindings(tag: String): mutable.Map[String, String] = + bindings.getOrElse(tag, new mutable.HashMap) def getBinding(option: String, key: String): Option[String] = - bindings.get(option) match { - case None => None - case Some(map) => map get key - } + bindings get option flatMap (_ get key) def getOthers: List[String] = others.toList |