"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/scalalib/overrides-2.11/scala/collection/mutable/ArrayBuilder.scala" (14 Nov 2020, 20988 Bytes) of package /linux/www/scala-js-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Scala source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*                     __                                               *\
    2 **     ________ ___   / /  ___     Scala API                            **
    3 **    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
    4 **  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
    5 ** /____/\___/_/ |_/____/_/ | |                                         **
    6 **                          |/                                          **
    7 \*                                                                      */
    8 
    9 package scala
   10 package collection
   11 package mutable
   12 
   13 import scala.reflect.ClassTag
   14 import scala.runtime.BoxedUnit
   15 
   16 import scala.scalajs.js
   17 
   18 /** A builder class for arrays.
   19  *
   20  *  @since 2.8
   21  *
   22  *  @tparam T    the type of the elements for the builder.
   23  */
   24 abstract class ArrayBuilder[T] extends Builder[T, Array[T]] with Serializable
   25 
   26 /** A companion object for array builders.
   27  *
   28  *  @since 2.8
   29  */
   30 object ArrayBuilder {
   31 
   32   /** Creates a new arraybuilder of type `T`.
   33    *
   34    *  @tparam T     type of the elements for the array builder, with a `ClassTag` context bound.
   35    *  @return       a new empty array builder.
   36    */
   37   @inline
   38   def make[T: ClassTag](): ArrayBuilder[T] =
   39     new ArrayBuilder.generic[T](implicitly[ClassTag[T]].runtimeClass)
   40 
   41   /** A generic ArrayBuilder optimized for Scala.js.
   42    *
   43    *  @tparam T              type of elements for the array builder.
   44    *  @param  elementClass   runtime class of the elements in the array.
   45    */
   46   @inline
   47   private final class generic[T](elementClass: Class[_]) extends ArrayBuilder[T] {
   48 
   49     private val isCharArrayBuilder = classOf[Char] == elementClass
   50     private var elems: js.Array[Any] = js.Array()
   51 
   52     def +=(elem: T): this.type = {
   53       val unboxedElem =
   54         if (isCharArrayBuilder) elem.asInstanceOf[Char].toInt
   55         else if (elem == null) zeroOf(elementClass)
   56         else elem
   57       elems.push(unboxedElem)
   58       this
   59     }
   60 
   61     def clear(): Unit =
   62       elems = js.Array()
   63 
   64     def result(): Array[T] = {
   65       val elemRuntimeClass =
   66         if (classOf[Unit] == elementClass) classOf[BoxedUnit]
   67         else if (classOf[Null] == elementClass || classOf[Nothing] == elementClass) classOf[Object]
   68         else elementClass
   69       genericArrayBuilderResult(elemRuntimeClass, elems)
   70     }
   71 
   72     override def toString(): String = "ArrayBuilder.generic"
   73   }
   74 
   75   // Intrinsic
   76   private def zeroOf(runtimeClass: Class[_]): Any = runtimeClass match {
   77     case java.lang.Byte.TYPE      => 0.toByte
   78     case java.lang.Short.TYPE     => 0.toShort
   79     case java.lang.Character.TYPE => 0 // yes, as an Int
   80     case java.lang.Integer.TYPE   => 0
   81     case java.lang.Long.TYPE      => 0L
   82     case java.lang.Float.TYPE     => 0.0f
   83     case java.lang.Double.TYPE    => 0.0
   84     case java.lang.Boolean.TYPE   => false
   85     case java.lang.Void.TYPE      => ()
   86     case _                        => null
   87   }
   88 
   89   // Intrinsic
   90   private def genericArrayBuilderResult[T](runtimeClass: Class[_],
   91       a: js.Array[Any]): Array[T] = {
   92     val len = a.length
   93 
   94     if (classOf[Char] == runtimeClass) {
   95       val result = new Array[Char](len)
   96       var i = 0
   97       while (i != len) {
   98         result(i) = a(i).asInstanceOf[Int].toChar
   99         i += 1
  100       }
  101       result.asInstanceOf[Array[T]]
  102     } else {
  103       val result: Array[T] = java.lang.reflect.Array.newInstance(
  104           runtimeClass, len).asInstanceOf[Array[T]]
  105       var i = 0
  106       while (i != len) {
  107         result(i) = a(i).asInstanceOf[T]
  108         i += 1
  109       }
  110       result
  111     }
  112   }
  113 
  114   /** A class for array builders for arrays of reference types.
  115    *
  116    *  @tparam T     type of elements for the array builder, subtype of `AnyRef` with a `ClassTag` context bound.
  117    */
  118   @deprecatedInheritance("ArrayBuilder.ofRef is an internal implementation not intended for subclassing.", "2.11.0")
  119   class ofRef[T <: AnyRef : ClassTag] extends ArrayBuilder[T] {
  120 
  121     private var elems: Array[T] = _
  122     private var capacity: Int = 0
  123     private var size: Int = 0
  124 
  125     private def mkArray(size: Int): Array[T] = {
  126       val newelems = new Array[T](size)
  127       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  128       newelems
  129     }
  130 
  131     private def resize(size: Int) {
  132       elems = mkArray(size)
  133       capacity = size
  134     }
  135 
  136     override def sizeHint(size: Int) {
  137       if (capacity < size) resize(size)
  138     }
  139 
  140     private def ensureSize(size: Int) {
  141       if (capacity < size || capacity == 0) {
  142         var newsize = if (capacity == 0) 16 else capacity * 2
  143         while (newsize < size) newsize *= 2
  144         resize(newsize)
  145       }
  146     }
  147 
  148     def +=(elem: T): this.type = {
  149       ensureSize(size + 1)
  150       elems(size) = elem
  151       size += 1
  152       this
  153     }
  154 
  155     override def ++=(xs: TraversableOnce[T]): this.type = (xs.asInstanceOf[AnyRef]) match {
  156       case xs: WrappedArray.ofRef[_] =>
  157         ensureSize(this.size + xs.length)
  158         Array.copy(xs.array, 0, elems, this.size, xs.length)
  159         size += xs.length
  160         this
  161       case _ =>
  162         super.++=(xs)
  163     }
  164 
  165     def clear() {
  166       size = 0
  167     }
  168 
  169     def result() = {
  170       if (capacity != 0 && capacity == size) {
  171         capacity = 0
  172         elems
  173       }
  174       else mkArray(size)
  175     }
  176 
  177     override def equals(other: Any): Boolean = other match {
  178       case x: ofRef[_] => (size == x.size) && (elems == x.elems)
  179       case _ => false
  180     }
  181 
  182     override def toString = "ArrayBuilder.ofRef"
  183   }
  184 
  185   /** A class for array builders for arrays of `byte`s. */
  186   @deprecatedInheritance("ArrayBuilder.ofByte is an internal implementation not intended for subclassing.", "2.11.0")
  187   class ofByte extends ArrayBuilder[Byte] {
  188 
  189     private var elems: Array[Byte] = _
  190     private var capacity: Int = 0
  191     private var size: Int = 0
  192 
  193     private def mkArray(size: Int): Array[Byte] = {
  194       val newelems = new Array[Byte](size)
  195       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  196       newelems
  197     }
  198 
  199     private def resize(size: Int) {
  200       elems = mkArray(size)
  201       capacity = size
  202     }
  203 
  204     override def sizeHint(size: Int) {
  205       if (capacity < size) resize(size)
  206     }
  207 
  208     private def ensureSize(size: Int) {
  209       if (capacity < size || capacity == 0) {
  210         var newsize = if (capacity == 0) 16 else capacity * 2
  211         while (newsize < size) newsize *= 2
  212         resize(newsize)
  213       }
  214     }
  215 
  216     def +=(elem: Byte): this.type = {
  217       ensureSize(size + 1)
  218       elems(size) = elem
  219       size += 1
  220       this
  221     }
  222 
  223     override def ++=(xs: TraversableOnce[Byte]): this.type = xs match {
  224       case xs: WrappedArray.ofByte =>
  225         ensureSize(this.size + xs.length)
  226         Array.copy(xs.array, 0, elems, this.size, xs.length)
  227         size += xs.length
  228         this
  229       case _ =>
  230         super.++=(xs)
  231     }
  232 
  233     def clear() {
  234       size = 0
  235     }
  236 
  237     def result() = {
  238       if (capacity != 0 && capacity == size) {
  239         capacity = 0
  240         elems
  241       }
  242       else mkArray(size)
  243     }
  244 
  245     override def equals(other: Any): Boolean = other match {
  246       case x: ofByte => (size == x.size) && (elems == x.elems)
  247       case _ => false
  248     }
  249 
  250     override def toString = "ArrayBuilder.ofByte"
  251   }
  252 
  253   /** A class for array builders for arrays of `short`s. */
  254   @deprecatedInheritance("ArrayBuilder.ofShort is an internal implementation not intended for subclassing.", "2.11.0")
  255   class ofShort extends ArrayBuilder[Short] {
  256 
  257     private var elems: Array[Short] = _
  258     private var capacity: Int = 0
  259     private var size: Int = 0
  260 
  261     private def mkArray(size: Int): Array[Short] = {
  262       val newelems = new Array[Short](size)
  263       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  264       newelems
  265     }
  266 
  267     private def resize(size: Int) {
  268       elems = mkArray(size)
  269       capacity = size
  270     }
  271 
  272     override def sizeHint(size: Int) {
  273       if (capacity < size) resize(size)
  274     }
  275 
  276     private def ensureSize(size: Int) {
  277       if (capacity < size || capacity == 0) {
  278         var newsize = if (capacity == 0) 16 else capacity * 2
  279         while (newsize < size) newsize *= 2
  280         resize(newsize)
  281       }
  282     }
  283 
  284     def +=(elem: Short): this.type = {
  285       ensureSize(size + 1)
  286       elems(size) = elem
  287       size += 1
  288       this
  289     }
  290 
  291     override def ++=(xs: TraversableOnce[Short]): this.type = xs match {
  292       case xs: WrappedArray.ofShort =>
  293         ensureSize(this.size + xs.length)
  294         Array.copy(xs.array, 0, elems, this.size, xs.length)
  295         size += xs.length
  296         this
  297       case _ =>
  298         super.++=(xs)
  299     }
  300 
  301     def clear() {
  302       size = 0
  303     }
  304 
  305     def result() = {
  306       if (capacity != 0 && capacity == size) {
  307         capacity = 0
  308         elems
  309       }
  310       else mkArray(size)
  311     }
  312 
  313     override def equals(other: Any): Boolean = other match {
  314       case x: ofShort => (size == x.size) && (elems == x.elems)
  315       case _ => false
  316     }
  317 
  318     override def toString = "ArrayBuilder.ofShort"
  319   }
  320 
  321   /** A class for array builders for arrays of `char`s. */
  322   @deprecatedInheritance("ArrayBuilder.ofChar is an internal implementation not intended for subclassing.", "2.11.0")
  323   class ofChar extends ArrayBuilder[Char] {
  324 
  325     private var elems: Array[Char] = _
  326     private var capacity: Int = 0
  327     private var size: Int = 0
  328 
  329     private def mkArray(size: Int): Array[Char] = {
  330       val newelems = new Array[Char](size)
  331       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  332       newelems
  333     }
  334 
  335     private def resize(size: Int) {
  336       elems = mkArray(size)
  337       capacity = size
  338     }
  339 
  340     override def sizeHint(size: Int) {
  341       if (capacity < size) resize(size)
  342     }
  343 
  344     private def ensureSize(size: Int) {
  345       if (capacity < size || capacity == 0) {
  346         var newsize = if (capacity == 0) 16 else capacity * 2
  347         while (newsize < size) newsize *= 2
  348         resize(newsize)
  349       }
  350     }
  351 
  352     def +=(elem: Char): this.type = {
  353       ensureSize(size + 1)
  354       elems(size) = elem
  355       size += 1
  356       this
  357     }
  358 
  359     override def ++=(xs: TraversableOnce[Char]): this.type = xs match {
  360       case xs: WrappedArray.ofChar =>
  361         ensureSize(this.size + xs.length)
  362         Array.copy(xs.array, 0, elems, this.size, xs.length)
  363         size += xs.length
  364         this
  365       case _ =>
  366         super.++=(xs)
  367     }
  368 
  369     def clear() {
  370       size = 0
  371     }
  372 
  373     def result() = {
  374       if (capacity != 0 && capacity == size) {
  375         capacity = 0
  376         elems
  377       }
  378       else mkArray(size)
  379     }
  380 
  381     override def equals(other: Any): Boolean = other match {
  382       case x: ofChar => (size == x.size) && (elems == x.elems)
  383       case _ => false
  384     }
  385 
  386     override def toString = "ArrayBuilder.ofChar"
  387   }
  388 
  389   /** A class for array builders for arrays of `int`s. */
  390   @deprecatedInheritance("ArrayBuilder.ofInt is an internal implementation not intended for subclassing.", "2.11.0")
  391   class ofInt extends ArrayBuilder[Int] {
  392 
  393     private var elems: Array[Int] = _
  394     private var capacity: Int = 0
  395     private var size: Int = 0
  396 
  397     private def mkArray(size: Int): Array[Int] = {
  398       val newelems = new Array[Int](size)
  399       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  400       newelems
  401     }
  402 
  403     private def resize(size: Int) {
  404       elems = mkArray(size)
  405       capacity = size
  406     }
  407 
  408     override def sizeHint(size: Int) {
  409       if (capacity < size) resize(size)
  410     }
  411 
  412     private def ensureSize(size: Int) {
  413       if (capacity < size || capacity == 0) {
  414         var newsize = if (capacity == 0) 16 else capacity * 2
  415         while (newsize < size) newsize *= 2
  416         resize(newsize)
  417       }
  418     }
  419 
  420     def +=(elem: Int): this.type = {
  421       ensureSize(size + 1)
  422       elems(size) = elem
  423       size += 1
  424       this
  425     }
  426 
  427     override def ++=(xs: TraversableOnce[Int]): this.type = xs match {
  428       case xs: WrappedArray.ofInt =>
  429         ensureSize(this.size + xs.length)
  430         Array.copy(xs.array, 0, elems, this.size, xs.length)
  431         size += xs.length
  432         this
  433       case _ =>
  434         super.++=(xs)
  435     }
  436 
  437     def clear() {
  438       size = 0
  439     }
  440 
  441     def result() = {
  442       if (capacity != 0 && capacity == size) {
  443         capacity = 0
  444         elems
  445       }
  446       else mkArray(size)
  447     }
  448 
  449     override def equals(other: Any): Boolean = other match {
  450       case x: ofInt => (size == x.size) && (elems == x.elems)
  451       case _ => false
  452     }
  453 
  454     override def toString = "ArrayBuilder.ofInt"
  455   }
  456 
  457   /** A class for array builders for arrays of `long`s. */
  458   @deprecatedInheritance("ArrayBuilder.ofLong is an internal implementation not intended for subclassing.", "2.11.0")
  459   class ofLong extends ArrayBuilder[Long] {
  460 
  461     private var elems: Array[Long] = _
  462     private var capacity: Int = 0
  463     private var size: Int = 0
  464 
  465     private def mkArray(size: Int): Array[Long] = {
  466       val newelems = new Array[Long](size)
  467       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  468       newelems
  469     }
  470 
  471     private def resize(size: Int) {
  472       elems = mkArray(size)
  473       capacity = size
  474     }
  475 
  476     override def sizeHint(size: Int) {
  477       if (capacity < size) resize(size)
  478     }
  479 
  480     private def ensureSize(size: Int) {
  481       if (capacity < size || capacity == 0) {
  482         var newsize = if (capacity == 0) 16 else capacity * 2
  483         while (newsize < size) newsize *= 2
  484         resize(newsize)
  485       }
  486     }
  487 
  488     def +=(elem: Long): this.type = {
  489       ensureSize(size + 1)
  490       elems(size) = elem
  491       size += 1
  492       this
  493     }
  494 
  495     override def ++=(xs: TraversableOnce[Long]): this.type = xs match {
  496       case xs: WrappedArray.ofLong =>
  497         ensureSize(this.size + xs.length)
  498         Array.copy(xs.array, 0, elems, this.size, xs.length)
  499         size += xs.length
  500         this
  501       case _ =>
  502         super.++=(xs)
  503     }
  504 
  505     def clear() {
  506       size = 0
  507     }
  508 
  509     def result() = {
  510       if (capacity != 0 && capacity == size) {
  511         capacity = 0
  512         elems
  513       }
  514       else mkArray(size)
  515     }
  516 
  517     override def equals(other: Any): Boolean = other match {
  518       case x: ofLong => (size == x.size) && (elems == x.elems)
  519       case _ => false
  520     }
  521 
  522     override def toString = "ArrayBuilder.ofLong"
  523   }
  524 
  525   /** A class for array builders for arrays of `float`s. */
  526   @deprecatedInheritance("ArrayBuilder.ofFloat is an internal implementation not intended for subclassing.", "2.11.0")
  527   class ofFloat extends ArrayBuilder[Float] {
  528 
  529     private var elems: Array[Float] = _
  530     private var capacity: Int = 0
  531     private var size: Int = 0
  532 
  533     private def mkArray(size: Int): Array[Float] = {
  534       val newelems = new Array[Float](size)
  535       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  536       newelems
  537     }
  538 
  539     private def resize(size: Int) {
  540       elems = mkArray(size)
  541       capacity = size
  542     }
  543 
  544     override def sizeHint(size: Int) {
  545       if (capacity < size) resize(size)
  546     }
  547 
  548     private def ensureSize(size: Int) {
  549       if (capacity < size || capacity == 0) {
  550         var newsize = if (capacity == 0) 16 else capacity * 2
  551         while (newsize < size) newsize *= 2
  552         resize(newsize)
  553       }
  554     }
  555 
  556     def +=(elem: Float): this.type = {
  557       ensureSize(size + 1)
  558       elems(size) = elem
  559       size += 1
  560       this
  561     }
  562 
  563     override def ++=(xs: TraversableOnce[Float]): this.type = xs match {
  564       case xs: WrappedArray.ofFloat =>
  565         ensureSize(this.size + xs.length)
  566         Array.copy(xs.array, 0, elems, this.size, xs.length)
  567         size += xs.length
  568         this
  569       case _ =>
  570         super.++=(xs)
  571     }
  572 
  573     def clear() {
  574       size = 0
  575     }
  576 
  577     def result() = {
  578       if (capacity != 0 && capacity == size) {
  579         capacity = 0
  580         elems
  581       }
  582       else mkArray(size)
  583     }
  584 
  585     override def equals(other: Any): Boolean = other match {
  586       case x: ofFloat => (size == x.size) && (elems == x.elems)
  587       case _ => false
  588     }
  589 
  590     override def toString = "ArrayBuilder.ofFloat"
  591   }
  592 
  593   /** A class for array builders for arrays of `double`s. */
  594   @deprecatedInheritance("ArrayBuilder.ofDouble is an internal implementation not intended for subclassing.", "2.11.0")
  595   class ofDouble extends ArrayBuilder[Double] {
  596 
  597     private var elems: Array[Double] = _
  598     private var capacity: Int = 0
  599     private var size: Int = 0
  600 
  601     private def mkArray(size: Int): Array[Double] = {
  602       val newelems = new Array[Double](size)
  603       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  604       newelems
  605     }
  606 
  607     private def resize(size: Int) {
  608       elems = mkArray(size)
  609       capacity = size
  610     }
  611 
  612     override def sizeHint(size: Int) {
  613       if (capacity < size) resize(size)
  614     }
  615 
  616     private def ensureSize(size: Int) {
  617       if (capacity < size || capacity == 0) {
  618         var newsize = if (capacity == 0) 16 else capacity * 2
  619         while (newsize < size) newsize *= 2
  620         resize(newsize)
  621       }
  622     }
  623 
  624     def +=(elem: Double): this.type = {
  625       ensureSize(size + 1)
  626       elems(size) = elem
  627       size += 1
  628       this
  629     }
  630 
  631     override def ++=(xs: TraversableOnce[Double]): this.type = xs match {
  632       case xs: WrappedArray.ofDouble =>
  633         ensureSize(this.size + xs.length)
  634         Array.copy(xs.array, 0, elems, this.size, xs.length)
  635         size += xs.length
  636         this
  637       case _ =>
  638         super.++=(xs)
  639     }
  640 
  641     def clear() {
  642       size = 0
  643     }
  644 
  645     def result() = {
  646       if (capacity != 0 && capacity == size) {
  647         capacity = 0
  648         elems
  649       }
  650       else mkArray(size)
  651     }
  652 
  653     override def equals(other: Any): Boolean = other match {
  654       case x: ofDouble => (size == x.size) && (elems == x.elems)
  655       case _ => false
  656     }
  657 
  658     override def toString = "ArrayBuilder.ofDouble"
  659   }
  660 
  661   /** A class for array builders for arrays of `boolean`s. */
  662   class ofBoolean extends ArrayBuilder[Boolean] {
  663 
  664     private var elems: Array[Boolean] = _
  665     private var capacity: Int = 0
  666     private var size: Int = 0
  667 
  668     private def mkArray(size: Int): Array[Boolean] = {
  669       val newelems = new Array[Boolean](size)
  670       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  671       newelems
  672     }
  673 
  674     private def resize(size: Int) {
  675       elems = mkArray(size)
  676       capacity = size
  677     }
  678 
  679     override def sizeHint(size: Int) {
  680       if (capacity < size) resize(size)
  681     }
  682 
  683     private def ensureSize(size: Int) {
  684       if (capacity < size || capacity == 0) {
  685         var newsize = if (capacity == 0) 16 else capacity * 2
  686         while (newsize < size) newsize *= 2
  687         resize(newsize)
  688       }
  689     }
  690 
  691     def +=(elem: Boolean): this.type = {
  692       ensureSize(size + 1)
  693       elems(size) = elem
  694       size += 1
  695       this
  696     }
  697 
  698     override def ++=(xs: TraversableOnce[Boolean]): this.type = xs match {
  699       case xs: WrappedArray.ofBoolean =>
  700         ensureSize(this.size + xs.length)
  701         Array.copy(xs.array, 0, elems, this.size, xs.length)
  702         size += xs.length
  703         this
  704       case _ =>
  705         super.++=(xs)
  706     }
  707 
  708     def clear() {
  709       size = 0
  710     }
  711 
  712     def result() = {
  713       if (capacity != 0 && capacity == size) {
  714         capacity = 0
  715         elems
  716       }
  717       else mkArray(size)
  718     }
  719 
  720     override def equals(other: Any): Boolean = other match {
  721       case x: ofBoolean => (size == x.size) && (elems == x.elems)
  722       case _ => false
  723     }
  724 
  725     override def toString = "ArrayBuilder.ofBoolean"
  726   }
  727 
  728   /** A class for array builders for arrays of `Unit` type. */
  729   @deprecatedInheritance("ArrayBuilder.ofUnit is an internal implementation not intended for subclassing.", "2.11.0")
  730   class ofUnit extends ArrayBuilder[Unit] {
  731 
  732     private var elems: Array[Unit] = _
  733     private var capacity: Int = 0
  734     private var size: Int = 0
  735 
  736     private def mkArray(size: Int): Array[Unit] = {
  737       val newelems = new Array[Unit](size)
  738       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  739       newelems
  740     }
  741 
  742     private def resize(size: Int) {
  743       elems = mkArray(size)
  744       capacity = size
  745     }
  746 
  747     override def sizeHint(size: Int) {
  748       if (capacity < size) resize(size)
  749     }
  750 
  751     private def ensureSize(size: Int) {
  752       if (capacity < size || capacity == 0) {
  753         var newsize = if (capacity == 0) 16 else capacity * 2
  754         while (newsize < size) newsize *= 2
  755         resize(newsize)
  756       }
  757     }
  758 
  759     def +=(elem: Unit): this.type = {
  760       ensureSize(size + 1)
  761       elems(size) = elem
  762       size += 1
  763       this
  764     }
  765 
  766     override def ++=(xs: TraversableOnce[Unit]): this.type = xs match {
  767       case xs: WrappedArray.ofUnit =>
  768         ensureSize(this.size + xs.length)
  769         Array.copy(xs.array, 0, elems, this.size, xs.length)
  770         size += xs.length
  771         this
  772       case _ =>
  773         super.++=(xs)
  774     }
  775 
  776     def clear() {
  777       size = 0
  778     }
  779 
  780     def result() = {
  781       if (capacity != 0 && capacity == size) {
  782         capacity = 0
  783         elems
  784       }
  785       else mkArray(size)
  786     }
  787 
  788     override def equals(other: Any): Boolean = other match {
  789       case x: ofUnit => (size == x.size) && (elems == x.elems)
  790       case _ => false
  791     }
  792 
  793     override def toString = "ArrayBuilder.ofUnit"
  794   }
  795 }