"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/scalalib/overrides-2.12/scala/collection/mutable/ArrayBuilder.scala" (14 Nov 2020, 19145 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 ReusableBuilder[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    *  This builder can be reused.
  117    *
  118    *  @tparam T     type of elements for the array builder, subtype of `AnyRef` with a `ClassTag` context bound.
  119    */
  120   final class ofRef[T <: AnyRef : ClassTag] extends ArrayBuilder[T] {
  121 
  122     private var elems: Array[T] = _
  123     private var capacity: Int = 0
  124     private var size: Int = 0
  125 
  126     private def mkArray(size: Int): Array[T] = {
  127       val newelems = new Array[T](size)
  128       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  129       newelems
  130     }
  131 
  132     private def resize(size: Int) {
  133       elems = mkArray(size)
  134       capacity = size
  135     }
  136 
  137     override def sizeHint(size: Int) {
  138       if (capacity < size) resize(size)
  139     }
  140 
  141     private def ensureSize(size: Int) {
  142       if (capacity < size || capacity == 0) {
  143         var newsize = if (capacity == 0) 16 else capacity * 2
  144         while (newsize < size) newsize *= 2
  145         resize(newsize)
  146       }
  147     }
  148 
  149     def +=(elem: T): this.type = {
  150       ensureSize(size + 1)
  151       elems(size) = elem
  152       size += 1
  153       this
  154     }
  155 
  156     override def ++=(xs: TraversableOnce[T]): this.type = (xs.asInstanceOf[AnyRef]) match {
  157       case xs: WrappedArray.ofRef[_] =>
  158         ensureSize(this.size + xs.length)
  159         Array.copy(xs.array, 0, elems, this.size, xs.length)
  160         size += xs.length
  161         this
  162       case _ =>
  163         super.++=(xs)
  164     }
  165 
  166     def clear() { size = 0 }
  167 
  168     def result() = {
  169       if (capacity != 0 && capacity == size) {
  170         capacity = 0
  171         elems
  172       }
  173       else mkArray(size)
  174     }
  175 
  176     override def equals(other: Any): Boolean = other match {
  177       case x: ofRef[_] => (size == x.size) && (elems == x.elems)
  178       case _ => false
  179     }
  180 
  181     override def toString = "ArrayBuilder.ofRef"
  182   }
  183 
  184   /** A class for array builders for arrays of `byte`s. It can be reused. */
  185   final class ofByte extends ArrayBuilder[Byte] {
  186 
  187     private var elems: Array[Byte] = _
  188     private var capacity: Int = 0
  189     private var size: Int = 0
  190 
  191     private def mkArray(size: Int): Array[Byte] = {
  192       val newelems = new Array[Byte](size)
  193       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  194       newelems
  195     }
  196 
  197     private def resize(size: Int) {
  198       elems = mkArray(size)
  199       capacity = size
  200     }
  201 
  202     override def sizeHint(size: Int) {
  203       if (capacity < size) resize(size)
  204     }
  205 
  206     private def ensureSize(size: Int) {
  207       if (capacity < size || capacity == 0) {
  208         var newsize = if (capacity == 0) 16 else capacity * 2
  209         while (newsize < size) newsize *= 2
  210         resize(newsize)
  211       }
  212     }
  213 
  214     def +=(elem: Byte): this.type = {
  215       ensureSize(size + 1)
  216       elems(size) = elem
  217       size += 1
  218       this
  219     }
  220 
  221     override def ++=(xs: TraversableOnce[Byte]): this.type = xs match {
  222       case xs: WrappedArray.ofByte =>
  223         ensureSize(this.size + xs.length)
  224         Array.copy(xs.array, 0, elems, this.size, xs.length)
  225         size += xs.length
  226         this
  227       case _ =>
  228         super.++=(xs)
  229     }
  230 
  231     def clear() { size = 0 }
  232 
  233     def result() = {
  234       if (capacity != 0 && capacity == size) {
  235         capacity = 0
  236         elems
  237       }
  238       else mkArray(size)
  239     }
  240 
  241     override def equals(other: Any): Boolean = other match {
  242       case x: ofByte => (size == x.size) && (elems == x.elems)
  243       case _ => false
  244     }
  245 
  246     override def toString = "ArrayBuilder.ofByte"
  247   }
  248 
  249   /** A class for array builders for arrays of `short`s. It can be reused. */
  250   final class ofShort extends ArrayBuilder[Short] {
  251 
  252     private var elems: Array[Short] = _
  253     private var capacity: Int = 0
  254     private var size: Int = 0
  255 
  256     private def mkArray(size: Int): Array[Short] = {
  257       val newelems = new Array[Short](size)
  258       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  259       newelems
  260     }
  261 
  262     private def resize(size: Int) {
  263       elems = mkArray(size)
  264       capacity = size
  265     }
  266 
  267     override def sizeHint(size: Int) {
  268       if (capacity < size) resize(size)
  269     }
  270 
  271     private def ensureSize(size: Int) {
  272       if (capacity < size || capacity == 0) {
  273         var newsize = if (capacity == 0) 16 else capacity * 2
  274         while (newsize < size) newsize *= 2
  275         resize(newsize)
  276       }
  277     }
  278 
  279     def +=(elem: Short): this.type = {
  280       ensureSize(size + 1)
  281       elems(size) = elem
  282       size += 1
  283       this
  284     }
  285 
  286     override def ++=(xs: TraversableOnce[Short]): this.type = xs match {
  287       case xs: WrappedArray.ofShort =>
  288         ensureSize(this.size + xs.length)
  289         Array.copy(xs.array, 0, elems, this.size, xs.length)
  290         size += xs.length
  291         this
  292       case _ =>
  293         super.++=(xs)
  294     }
  295 
  296     def clear() { size = 0 }
  297 
  298     def result() = {
  299       if (capacity != 0 && capacity == size) {
  300         capacity = 0
  301         elems
  302       }
  303       else mkArray(size)
  304     }
  305 
  306     override def equals(other: Any): Boolean = other match {
  307       case x: ofShort => (size == x.size) && (elems == x.elems)
  308       case _ => false
  309     }
  310 
  311     override def toString = "ArrayBuilder.ofShort"
  312   }
  313 
  314   /** A class for array builders for arrays of `char`s. It can be reused. */
  315   final class ofChar extends ArrayBuilder[Char] {
  316 
  317     private var elems: Array[Char] = _
  318     private var capacity: Int = 0
  319     private var size: Int = 0
  320 
  321     private def mkArray(size: Int): Array[Char] = {
  322       val newelems = new Array[Char](size)
  323       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  324       newelems
  325     }
  326 
  327     private def resize(size: Int) {
  328       elems = mkArray(size)
  329       capacity = size
  330     }
  331 
  332     override def sizeHint(size: Int) {
  333       if (capacity < size) resize(size)
  334     }
  335 
  336     private def ensureSize(size: Int) {
  337       if (capacity < size || capacity == 0) {
  338         var newsize = if (capacity == 0) 16 else capacity * 2
  339         while (newsize < size) newsize *= 2
  340         resize(newsize)
  341       }
  342     }
  343 
  344     def +=(elem: Char): this.type = {
  345       ensureSize(size + 1)
  346       elems(size) = elem
  347       size += 1
  348       this
  349     }
  350 
  351     override def ++=(xs: TraversableOnce[Char]): this.type = xs match {
  352       case xs: WrappedArray.ofChar =>
  353         ensureSize(this.size + xs.length)
  354         Array.copy(xs.array, 0, elems, this.size, xs.length)
  355         size += xs.length
  356         this
  357       case _ =>
  358         super.++=(xs)
  359     }
  360 
  361     def clear() { size = 0 }
  362 
  363     def result() = {
  364       if (capacity != 0 && capacity == size) {
  365         capacity = 0
  366         elems
  367       }
  368       else mkArray(size)
  369     }
  370 
  371     override def equals(other: Any): Boolean = other match {
  372       case x: ofChar => (size == x.size) && (elems == x.elems)
  373       case _ => false
  374     }
  375 
  376     override def toString = "ArrayBuilder.ofChar"
  377   }
  378 
  379   /** A class for array builders for arrays of `int`s. It can be reused. */
  380   final class ofInt extends ArrayBuilder[Int] {
  381 
  382     private var elems: Array[Int] = _
  383     private var capacity: Int = 0
  384     private var size: Int = 0
  385 
  386     private def mkArray(size: Int): Array[Int] = {
  387       val newelems = new Array[Int](size)
  388       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  389       newelems
  390     }
  391 
  392     private def resize(size: Int) {
  393       elems = mkArray(size)
  394       capacity = size
  395     }
  396 
  397     override def sizeHint(size: Int) {
  398       if (capacity < size) resize(size)
  399     }
  400 
  401     private def ensureSize(size: Int) {
  402       if (capacity < size || capacity == 0) {
  403         var newsize = if (capacity == 0) 16 else capacity * 2
  404         while (newsize < size) newsize *= 2
  405         resize(newsize)
  406       }
  407     }
  408 
  409     def +=(elem: Int): this.type = {
  410       ensureSize(size + 1)
  411       elems(size) = elem
  412       size += 1
  413       this
  414     }
  415 
  416     override def ++=(xs: TraversableOnce[Int]): this.type = xs match {
  417       case xs: WrappedArray.ofInt =>
  418         ensureSize(this.size + xs.length)
  419         Array.copy(xs.array, 0, elems, this.size, xs.length)
  420         size += xs.length
  421         this
  422       case _ =>
  423         super.++=(xs)
  424     }
  425 
  426     def clear() { size = 0 }
  427 
  428     def result() = {
  429       if (capacity != 0 && capacity == size) {
  430         capacity = 0
  431         elems
  432       }
  433       else mkArray(size)
  434     }
  435 
  436     override def equals(other: Any): Boolean = other match {
  437       case x: ofInt => (size == x.size) && (elems == x.elems)
  438       case _ => false
  439     }
  440 
  441     override def toString = "ArrayBuilder.ofInt"
  442   }
  443 
  444   /** A class for array builders for arrays of `long`s. It can be reused. */
  445   final class ofLong extends ArrayBuilder[Long] {
  446 
  447     private var elems: Array[Long] = _
  448     private var capacity: Int = 0
  449     private var size: Int = 0
  450 
  451     private def mkArray(size: Int): Array[Long] = {
  452       val newelems = new Array[Long](size)
  453       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  454       newelems
  455     }
  456 
  457     private def resize(size: Int) {
  458       elems = mkArray(size)
  459       capacity = size
  460     }
  461 
  462     override def sizeHint(size: Int) {
  463       if (capacity < size) resize(size)
  464     }
  465 
  466     private def ensureSize(size: Int) {
  467       if (capacity < size || capacity == 0) {
  468         var newsize = if (capacity == 0) 16 else capacity * 2
  469         while (newsize < size) newsize *= 2
  470         resize(newsize)
  471       }
  472     }
  473 
  474     def +=(elem: Long): this.type = {
  475       ensureSize(size + 1)
  476       elems(size) = elem
  477       size += 1
  478       this
  479     }
  480 
  481     override def ++=(xs: TraversableOnce[Long]): this.type = xs match {
  482       case xs: WrappedArray.ofLong =>
  483         ensureSize(this.size + xs.length)
  484         Array.copy(xs.array, 0, elems, this.size, xs.length)
  485         size += xs.length
  486         this
  487       case _ =>
  488         super.++=(xs)
  489     }
  490 
  491     def clear() { size = 0 }
  492 
  493     def result() = {
  494       if (capacity != 0 && capacity == size) {
  495         capacity = 0
  496         elems
  497       }
  498       else mkArray(size)
  499     }
  500 
  501     override def equals(other: Any): Boolean = other match {
  502       case x: ofLong => (size == x.size) && (elems == x.elems)
  503       case _ => false
  504     }
  505 
  506     override def toString = "ArrayBuilder.ofLong"
  507   }
  508 
  509   /** A class for array builders for arrays of `float`s. It can be reused. */
  510   final class ofFloat extends ArrayBuilder[Float] {
  511 
  512     private var elems: Array[Float] = _
  513     private var capacity: Int = 0
  514     private var size: Int = 0
  515 
  516     private def mkArray(size: Int): Array[Float] = {
  517       val newelems = new Array[Float](size)
  518       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  519       newelems
  520     }
  521 
  522     private def resize(size: Int) {
  523       elems = mkArray(size)
  524       capacity = size
  525     }
  526 
  527     override def sizeHint(size: Int) {
  528       if (capacity < size) resize(size)
  529     }
  530 
  531     private def ensureSize(size: Int) {
  532       if (capacity < size || capacity == 0) {
  533         var newsize = if (capacity == 0) 16 else capacity * 2
  534         while (newsize < size) newsize *= 2
  535         resize(newsize)
  536       }
  537     }
  538 
  539     def +=(elem: Float): this.type = {
  540       ensureSize(size + 1)
  541       elems(size) = elem
  542       size += 1
  543       this
  544     }
  545 
  546     override def ++=(xs: TraversableOnce[Float]): this.type = xs match {
  547       case xs: WrappedArray.ofFloat =>
  548         ensureSize(this.size + xs.length)
  549         Array.copy(xs.array, 0, elems, this.size, xs.length)
  550         size += xs.length
  551         this
  552       case _ =>
  553         super.++=(xs)
  554     }
  555 
  556     def clear() { size = 0 }
  557 
  558     def result() = {
  559       if (capacity != 0 && capacity == size) {
  560         capacity = 0
  561         elems
  562       }
  563       else mkArray(size)
  564     }
  565 
  566     override def equals(other: Any): Boolean = other match {
  567       case x: ofFloat => (size == x.size) && (elems == x.elems)
  568       case _ => false
  569     }
  570 
  571     override def toString = "ArrayBuilder.ofFloat"
  572   }
  573 
  574   /** A class for array builders for arrays of `double`s. It can be reused. */
  575   final class ofDouble extends ArrayBuilder[Double] {
  576 
  577     private var elems: Array[Double] = _
  578     private var capacity: Int = 0
  579     private var size: Int = 0
  580 
  581     private def mkArray(size: Int): Array[Double] = {
  582       val newelems = new Array[Double](size)
  583       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  584       newelems
  585     }
  586 
  587     private def resize(size: Int) {
  588       elems = mkArray(size)
  589       capacity = size
  590     }
  591 
  592     override def sizeHint(size: Int) {
  593       if (capacity < size) resize(size)
  594     }
  595 
  596     private def ensureSize(size: Int) {
  597       if (capacity < size || capacity == 0) {
  598         var newsize = if (capacity == 0) 16 else capacity * 2
  599         while (newsize < size) newsize *= 2
  600         resize(newsize)
  601       }
  602     }
  603 
  604     def +=(elem: Double): this.type = {
  605       ensureSize(size + 1)
  606       elems(size) = elem
  607       size += 1
  608       this
  609     }
  610 
  611     override def ++=(xs: TraversableOnce[Double]): this.type = xs match {
  612       case xs: WrappedArray.ofDouble =>
  613         ensureSize(this.size + xs.length)
  614         Array.copy(xs.array, 0, elems, this.size, xs.length)
  615         size += xs.length
  616         this
  617       case _ =>
  618         super.++=(xs)
  619     }
  620 
  621     def clear() { size = 0 }
  622 
  623     def result() = {
  624       if (capacity != 0 && capacity == size) {
  625         capacity = 0
  626         elems
  627       }
  628       else mkArray(size)
  629     }
  630 
  631     override def equals(other: Any): Boolean = other match {
  632       case x: ofDouble => (size == x.size) && (elems == x.elems)
  633       case _ => false
  634     }
  635 
  636     override def toString = "ArrayBuilder.ofDouble"
  637   }
  638 
  639   /** A class for array builders for arrays of `boolean`s. It can be reused. */
  640   class ofBoolean extends ArrayBuilder[Boolean] {
  641 
  642     private var elems: Array[Boolean] = _
  643     private var capacity: Int = 0
  644     private var size: Int = 0
  645 
  646     private def mkArray(size: Int): Array[Boolean] = {
  647       val newelems = new Array[Boolean](size)
  648       if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
  649       newelems
  650     }
  651 
  652     private def resize(size: Int) {
  653       elems = mkArray(size)
  654       capacity = size
  655     }
  656 
  657     override def sizeHint(size: Int) {
  658       if (capacity < size) resize(size)
  659     }
  660 
  661     private def ensureSize(size: Int) {
  662       if (capacity < size || capacity == 0) {
  663         var newsize = if (capacity == 0) 16 else capacity * 2
  664         while (newsize < size) newsize *= 2
  665         resize(newsize)
  666       }
  667     }
  668 
  669     def +=(elem: Boolean): this.type = {
  670       ensureSize(size + 1)
  671       elems(size) = elem
  672       size += 1
  673       this
  674     }
  675 
  676     override def ++=(xs: TraversableOnce[Boolean]): this.type = xs match {
  677       case xs: WrappedArray.ofBoolean =>
  678         ensureSize(this.size + xs.length)
  679         Array.copy(xs.array, 0, elems, this.size, xs.length)
  680         size += xs.length
  681         this
  682       case _ =>
  683         super.++=(xs)
  684     }
  685 
  686     def clear() { size = 0 }
  687 
  688     def result() = {
  689       if (capacity != 0 && capacity == size) {
  690         capacity = 0
  691         elems
  692       }
  693       else mkArray(size)
  694     }
  695 
  696     override def equals(other: Any): Boolean = other match {
  697       case x: ofBoolean => (size == x.size) && (elems == x.elems)
  698       case _ => false
  699     }
  700 
  701     override def toString = "ArrayBuilder.ofBoolean"
  702   }
  703 
  704   /** A class for array builders for arrays of `Unit` type. It can be reused. */
  705   final class ofUnit extends ArrayBuilder[Unit] {
  706 
  707     private var size: Int = 0
  708 
  709     def +=(elem: Unit): this.type = {
  710       size += 1
  711       this
  712     }
  713 
  714     override def ++=(xs: TraversableOnce[Unit]): this.type = {
  715       size += xs.size
  716       this
  717     }
  718 
  719     def clear() { size = 0 }
  720 
  721     def result() = {
  722       val ans = new Array[Unit](size)
  723       var i = 0
  724       while (i < size) { ans(i) = (); i += 1 }
  725       ans
  726     }
  727 
  728     override def equals(other: Any): Boolean = other match {
  729       case x: ofUnit => (size == x.size)
  730       case _ => false
  731     }
  732 
  733     override def toString = "ArrayBuilder.ofUnit"
  734   }
  735 }