summaryrefslogtreecommitdiff
path: root/src/library/scalax/collection/SizedIterable.scala
blob: 4463c268b9654d5ae6b120ddb90926a7a4dc612f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*                     __                                               *\
**     ________ ___   / /  ___     Scala API                            **
**    / __/ __// _ | / /  / _ |    (c) 2002-2009, LAMP/EPFL             **
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
** /____/\___/_/ |_/____/_/ | |                                         **
**                          |/                                          **
\*                                                                      */

// $Id: Collection.scala 12340 2007-07-17 15:29:47Z mcdirmid $


package scalax.collection

/** Variant of <code>Iterable</code> which also demands
 *  implementation of a `size` method.
 *  Basically, this trait just adds size to Iterable,
 *  and provides an optimized implementation of toArray based on it.
 *
 *  @author Martin Odersky
 *  @version 2.8
 */
trait SizedIterable[+A] extends Iterable[A] {

  /** Returns the number of elements in this collection.
    *
    *  @return number of collection elements.
    */
  def size : Int

  /** Converts this iterable to a fresh Array with <code>size</code> elements.
    */
  override def toArray[B >: A]: Array[B] = {
    val result = new Array[B](size)
    copyToArray(result, 0)
    result
  }
}