[Assorted-commits] SF.net SVN: assorted: [430] scala-commons/trunk/src/commons/Collections. scala
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-15 02:31:29
|
Revision: 430 http://assorted.svn.sourceforge.net/assorted/?rev=430&view=rev Author: yangzhang Date: 2008-02-14 18:31:34 -0800 (Thu, 14 Feb 2008) Log Message: ----------- added: separateHeads, groupByHeaders, multimap, IdMapper, serialize, name-munging, sum/mean overloads Modified Paths: -------------- scala-commons/trunk/src/commons/Collections.scala Modified: scala-commons/trunk/src/commons/Collections.scala =================================================================== --- scala-commons/trunk/src/commons/Collections.scala 2008-02-15 02:30:16 UTC (rev 429) +++ scala-commons/trunk/src/commons/Collections.scala 2008-02-15 02:31:34 UTC (rev 430) @@ -123,7 +123,9 @@ def sum(xs: Iterator[Int]) = xs.foldLeft(0)(_+_) def sum(xs: Seq[Double]) = xs reduceLeft ((x:Double,y:Double)=>x+y) + def sum(xs: Iterator[Double]) = xs reduceLeft ((_:Double)+(_:Double)) def mean(xs: Seq[Int]) = sum(xs.elements) / xs.size + def mean(xs: Seq[Double]) = sum(xs.elements) / xs.size def median(xs: Seq[Long]) = xs(xs.length / 2) /** @@ -657,7 +659,10 @@ } def camelToUnder(s: String) = camelToLower(s, "_") def camelToHyphen(s: String) = camelToLower(s, "-") + def camelToSpaced(s: String) = camelToLower(s, " ") + def spacedToHyphen(s: String) = s replaceAll (" ", "-") + // TODO: this isn't really rot encoding def rot(n: Int, s: String) = s map (_ + n toChar) mkString @@ -693,4 +698,79 @@ case Seq(a, b, rest @ _*) => Stream cons ((a,b), pairs(rest)) } } + + /** + * Maps unique IDs to distinct objects. + */ + class IdMapper[a] extends mut.HashMap[a,Int] { + val i = Iterator from 0 + override def default(k: a) = { + val n = i.next + this(k) = n + n + } + } + + /** + * Serialize an Int to a String of four bytes (little-endian). + */ + def serializeInt(i: Int) = + Array( + (i >>> 0) & 0xff, + (i >>> 8) & 0xff, + (i >>> 16) & 0xff, + (i >>> 24) & 0xff + ).map(_.toChar).mkString + + /** + * For each x in xs, if p(x), then x is a header, and it owns all the xs + * after it until the next x for which p(x) holds. The return value is the + * header and its body. + * <p> + * <code> + * groupByHeaders([1,2,3,4,5,6,7,8,9])(_%3==0) == [[3,4,5],[6,7,8],[9]] + * </code> + */ + def groupByHeaders[a](xs: Seq[a])(p: a => Boolean) = { + val ys = new mut.ArrayBuffer[mut.ArrayBuffer[a]] + for (x <- xs) { + if (p(x)) ys += new mut.ArrayBuffer[a] + if (!ys.isEmpty) ys.last += x + } + ys + } +// def groupByHeaders[a](xs: Seq[a])(p: a => Boolean) = { +// def r(seq: Seq[a]): Stream[List[a]] = seq match { +// case Seq() => Stream.empty +// case Seq(y,ys@_*) => { +// val (as,bs) = spanBy(ys)(not(p)) +// Stream.cons(y :: as.toList, r(bs)) +// } +// } +// r(xs dropWhile (not(p))) +// } + + /** + * <code> + * separateHeads([[a,b,c],[d,e,f,g],[],[h]]) + * == [(a,[b,c]),(d,[e,f,g]),(h,[])] + * </code> + */ + def separateHeads[a](xss: Seq[Seq[a]]) = + for (xs <- xss; (Seq(a),b) = span(1)(xs)) yield (a,b) + + + /** + * Construct a multimap out of the given key-value pairs; the result maps + * keys to sets of values. + */ + def multimap[a,b](xs: Iterable[(a,b)]) = { + val h = new mut.HashMap[a,mut.Set[b]] with mut.MultiMap[a,b] { + override def makeSet = new mut.HashSet[b] + } + for ((k,v) <- xs) { + h add (k,v) + } + h + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |