<commit_before>package scalan.collections

import scalan._
import scala.reflect.runtime.universe._

trait ArrayBuffers extends Base { self: Scalan =>
  trait ArrayBuffer[T] {
    implicit val eItem: Elem[T]
    def apply(i: Rep[Int]): Rep[T] 
    def length : Rep[Int]
    def mapBy[R: Elem](f: Rep[T => R]): Rep[ArrayBuffer[R]]
    def map[R: Elem](f: Rep[T] => Rep[R]): Rep[ArrayBuffer[R]] = mapBy(fun(f))
    def update(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]]
    def insert(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]]
    def +=(v: Rep[T]): Rep[ArrayBuffer[T]]
    def ++=(a: Arr[T]): Rep[ArrayBuffer[T]]
    def remove(i: Rep[Int], n: Rep[Int]): Rep[ArrayBuffer[T]]
    def reset():  Rep[ArrayBuffer[T]]     
    def toArray: Arr[T]
  }

  type ArrBuf[T] = Rep[ArrayBuffer[T]]

  object ArrayBuffer { 
    def apply[T: Elem](v: Rep[T]) = initArrayBuffer[T](v)
    def create[T: Elem](count: Rep[Int], f:Rep[Int]=>Rep[T]) = createArrayBuffer(count, f)
    def make[T](name: Rep[String])(implicit e:Elem[T]) = makeArrayBuffer[T](name)(e)
    def empty[T: Elem] = emptyArrayBuffer[T]
    def fromArray[T: Elem](arr: Arr[T]) = createArrayBufferFromArray(arr)
//    {
//      val buf = empty[T]
//      buf ++= arr
//    }
  }

  implicit val arrayBufferFunctor = new Functor[ArrayBuffer] {
    def tag[T](implicit tT: WeakTypeTag[T]) = weakTypeTag[ArrayBuffer[T]]
    def lift[T](implicit eT: Elem[T]) = element[ArrayBuffer[T]]
    def unlift[T](implicit eFT: Elem[ArrayBuffer[T]]) = eFT.eItem
    def getElem[T](fa: Rep[ArrayBuffer[T]]) = !!!("Operation is not supported by ArrayBuffer container " + fa)
    def unapply[T](e: Elem[_]) = e match {
      case e: ArrayBufferElem[_] => Some(e.asElem[ArrayBuffer[T]])
      case _ => None
    }
    def map[A:Elem,B:Elem](xs: Rep[ArrayBuffer[A]])(f: Rep[A] => Rep[B]) = xs.mapBy(fun(f))
  }

  case class ArrayBufferElem[A](override val eItem: Elem[A])
    extends EntityElem1[A, ArrayBuffer[A], ArrayBuffer](eItem, container[ArrayBuffer]) {
    def parent: Option[Elem[_]] = None
    override def isEntityType = eItem.isEntityType
    override lazy val tyArgSubst: Map[String, TypeDesc] = {
      Map("A" -> Left(eItem))
    }
    lazy val tag = {
      implicit val tag1 = eItem.tag
      weakTypeTag[ArrayBuffer[A]]
    }
    protected def getDefaultRep = ArrayBuffer.empty[A](eItem)
  }

  implicit def arrayBufferElement[A](implicit eItem: Elem[A]): Elem[ArrayBuffer[A]] = new ArrayBufferElem[A](eItem)
  implicit def ArrayBufferElemExtensions[A](eArr: Elem[ArrayBuffer[A]]): ArrayBufferElem[A] = eArr.asInstanceOf[ArrayBufferElem[A]]

  implicit def resolveArrayBuffer[T: Elem](buf: Rep[ArrayBuffer[T]]): ArrayBuffer[T]

  def emptyArrayBuffer[T: Elem]: Rep[ArrayBuffer[T]]
  def initArrayBuffer[T: Elem](v: Rep[T]): Rep[ArrayBuffer[T]]
  def makeArrayBuffer[T](name: Rep[String])(implicit e:Elem[T]): Rep[ArrayBuffer[T]]
  def createArrayBuffer[T: Elem](count: Rep[Int], f:Rep[Int=>T]): Rep[ArrayBuffer[T]]
  def createArrayBufferFromArray[T: Elem](arr: Arr[T]): Rep[ArrayBuffer[T]]
}

trait ArrayBuffersStd extends ArrayBuffers { self: ScalanStd =>
  implicit class SeqArrayBuffer[T](val impl: scala.collection.mutable.ArrayBuffer[T])(implicit val eItem: Elem[T]) extends ArrayBuffer[T] {
    def apply(i: Rep[Int]): Rep[T] = impl.apply(i)
    def length : Rep[Int] = impl.length
    def mapBy[R: Elem](f: Rep[T => R]): Rep[ArrayBuffer[R]] = new SeqArrayBuffer[R](impl.map(f))
    def update(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]] = { impl.update(i, v); this }
    def insert(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]] = { impl.insert(i, v); this }
    def +=(v: Rep[T]): Rep[ArrayBuffer[T]] = { impl += v; this }
    def ++=(a: Arr[T]): Rep[ArrayBuffer[T]] = { impl ++= a; this }
    def remove(i: Rep[Int], n: Rep[Int]): Rep[ArrayBuffer[T]] = { impl.remove(i, n); this }
    def reset():  Rep[ArrayBuffer[T]] = { impl.clear(); this }
    def toArray: Arr[T] = impl.toArray(eItem.classTag)
  }  

  implicit def resolveArrayBuffer[T: Elem](buf: Rep[ArrayBuffer[T]]): ArrayBuffer[T] = buf

  def emptyArrayBuffer[T: Elem]: Rep[ArrayBuffer[T]] = scala.collection.mutable.ArrayBuffer.empty[T]
  def initArrayBuffer[T: Elem](v: Rep[T]): Rep[ArrayBuffer[T]] = scala.collection.mutable.ArrayBuffer(v)
  def makeArrayBuffer[T](name: Rep[String])(implicit e:Elem[T]): Rep[ArrayBuffer[T]] = scala.collection.mutable.ArrayBuffer.empty[T]
  def createArrayBuffer[T: Elem](count: Rep[Int], f:Rep[Int=>T]): Rep[ArrayBuffer[T]] = {
    val buf = scala.collection.mutable.ArrayBuffer.empty[T]
    for (i <- 0 until count) {
      buf += f(i)
    }
    buf
  }
  def createArrayBufferFromArray[T: Elem](arr: Arr[T]): Rep[ArrayBuffer[T]] = {
    val buf = scala.collection.mutable.ArrayBuffer.empty[T]
    buf ++= arr
    buf
  }
}

trait ArrayBuffersExp extends ArrayBuffers with ViewsDslExp { self: ScalanExp =>
  case class ViewArrayBuffer[A, B](source: Rep[ArrayBuffer[A]], override val innerIso: Iso[A, B])
    extends View1[A, B, ArrayBuffer](arrayBufferIso(innerIso)) {
    override def toString = s"ViewArrayBuffer[${innerIso.eTo.name}]($source)"
    override def equals(other: Any) = other match {
      case v: ViewArrayBuffer[_, _] => source == v.source && innerIso.eTo == v.innerIso.eTo
      case _ => false
    }
  }

  object UserTypeArrayBuffer {
    def unapply(s: Exp[_]): Option[Iso[_, _]] = {
      s.elem match {
        case pae: ArrayBufferElem[_] =>
          pae.eItem match {
            case e: ViewElem[_, _] => Some(e.iso)
            case _ => None
          }
        case _ => None
      }
    }
  }

  override def unapplyViews[T](s: Exp[T]): Option[Unpacked[T]] = (s match {
    case Def(view: ViewArrayBuffer[_, _]) =>
      Some((view.source, view.iso))
    case UserTypeArrayBuffer(iso: Iso[a, b]) =>
      val newIso = arrayBufferIso(iso)
      val repr = reifyObject(UnpackView(s.asRep[ArrayBuffer[b]], newIso))
      Some((repr, newIso))
    case _ =>
      super.unapplyViews(s)
  }).asInstanceOf[Option[Unpacked[T]]]


  protected def hasArrayBufferViewArg(s: Exp[_]): Boolean = s match {
    case Def(_: ViewArrayBuffer[_, _]) => true
    case _ => false
  }

  val HasArrayBufferViewArg = HasArg(hasArrayBufferViewArg)

  def mapUnderlyingArrayBuffer[A,B,C](view: ViewArrayBuffer[A,B], f: Rep[B=>C]): Rep[ArrayBuffer[C]] = {
    val iso = view.innerIso
    implicit val eA = iso.eFrom
    implicit val eC: Elem[C] = f.elem.eRange
    ArrayBuffer.fromArray(view.source.toArray.mapBy(iso.toFun >> f))
  }

  override def rewriteDef[T](d: Def[T]) = d match {
    //------------------------------------------------------------
    // Iso lifting rules
    case ArrayBufferUpdate(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), i, v@HasViews(_, _)) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val v1 = iso.innerIso.from(v.asRep[b]).asRep[a]
      ViewArrayBuffer(buf1.update(i, v1), iso.innerIso)

    case ArrayBufferInsert(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), i, v@HasViews(_, _)) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val v1 = iso.innerIso.from(v.asRep[b]).asRep[a]
      ViewArrayBuffer(buf1.insert(i, v1), iso.innerIso)

    case ArrayBufferAppend(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), v@HasViews(_, _)) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val v1 = iso.innerIso.from(v.asRep[b]).asRep[a]
      ViewArrayBuffer(buf1 += v1, iso.innerIso)

    case ArrayBufferAppendArray(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), a@HasViews(_, Def(_: ArrayIso[_, _]))) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val arrIso = arrayIso(iso.innerIso)
      val a1 = arrIso.from(a.asRep[Array[b]]).asRep[Array[a]]
      ViewArrayBuffer(buf1 ++= a1, iso.innerIso)

    case mk@MakeArrayBuffer(ctx) if UnpackableElem.unapply(mk.eItem).isDefined =>
       mk.eItem match {
         case UnpackableElem(iso: Iso[a, b]) =>
           implicit val eA = iso.eFrom
           ViewArrayBuffer(ArrayBuffer.make[a](ctx), iso)<commit_msg>[Rules] iso lifting over ArrayBufferFromElem<commit_after>package scalan.collections

import scalan._
import scala.reflect.runtime.universe._

trait ArrayBuffers extends Base { self: Scalan =>
  trait ArrayBuffer[T] {
    implicit val eItem: Elem[T]
    def apply(i: Rep[Int]): Rep[T] 
    def length : Rep[Int]
    def mapBy[R: Elem](f: Rep[T => R]): Rep[ArrayBuffer[R]]
    def map[R: Elem](f: Rep[T] => Rep[R]): Rep[ArrayBuffer[R]] = mapBy(fun(f))
    def update(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]]
    def insert(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]]
    def +=(v: Rep[T]): Rep[ArrayBuffer[T]]
    def ++=(a: Arr[T]): Rep[ArrayBuffer[T]]
    def remove(i: Rep[Int], n: Rep[Int]): Rep[ArrayBuffer[T]]
    def reset():  Rep[ArrayBuffer[T]]     
    def toArray: Arr[T]
  }

  type ArrBuf[T] = Rep[ArrayBuffer[T]]

  object ArrayBuffer { 
    def apply[T: Elem](v: Rep[T]) = initArrayBuffer[T](v)
    def create[T: Elem](count: Rep[Int], f:Rep[Int]=>Rep[T]) = createArrayBuffer(count, f)
    def make[T](name: Rep[String])(implicit e:Elem[T]) = makeArrayBuffer[T](name)(e)
    def empty[T: Elem] = emptyArrayBuffer[T]
    def fromArray[T: Elem](arr: Arr[T]) = createArrayBufferFromArray(arr)
//    {
//      val buf = empty[T]
//      buf ++= arr
//    }
  }

  implicit val arrayBufferFunctor = new Functor[ArrayBuffer] {
    def tag[T](implicit tT: WeakTypeTag[T]) = weakTypeTag[ArrayBuffer[T]]
    def lift[T](implicit eT: Elem[T]) = element[ArrayBuffer[T]]
    def unlift[T](implicit eFT: Elem[ArrayBuffer[T]]) = eFT.eItem
    def getElem[T](fa: Rep[ArrayBuffer[T]]) = !!!("Operation is not supported by ArrayBuffer container " + fa)
    def unapply[T](e: Elem[_]) = e match {
      case e: ArrayBufferElem[_] => Some(e.asElem[ArrayBuffer[T]])
      case _ => None
    }
    def map[A:Elem,B:Elem](xs: Rep[ArrayBuffer[A]])(f: Rep[A] => Rep[B]) = xs.mapBy(fun(f))
  }

  case class ArrayBufferElem[A](override val eItem: Elem[A])
    extends EntityElem1[A, ArrayBuffer[A], ArrayBuffer](eItem, container[ArrayBuffer]) {
    def parent: Option[Elem[_]] = None
    override def isEntityType = eItem.isEntityType
    override lazy val tyArgSubst: Map[String, TypeDesc] = {
      Map("A" -> Left(eItem))
    }
    lazy val tag = {
      implicit val tag1 = eItem.tag
      weakTypeTag[ArrayBuffer[A]]
    }
    protected def getDefaultRep = ArrayBuffer.empty[A](eItem)
  }

  implicit def arrayBufferElement[A](implicit eItem: Elem[A]): Elem[ArrayBuffer[A]] = new ArrayBufferElem[A](eItem)
  implicit def ArrayBufferElemExtensions[A](eArr: Elem[ArrayBuffer[A]]): ArrayBufferElem[A] = eArr.asInstanceOf[ArrayBufferElem[A]]

  implicit def resolveArrayBuffer[T: Elem](buf: Rep[ArrayBuffer[T]]): ArrayBuffer[T]

  def emptyArrayBuffer[T: Elem]: Rep[ArrayBuffer[T]]
  def initArrayBuffer[T: Elem](v: Rep[T]): Rep[ArrayBuffer[T]]
  def makeArrayBuffer[T](name: Rep[String])(implicit e:Elem[T]): Rep[ArrayBuffer[T]]
  def createArrayBuffer[T: Elem](count: Rep[Int], f:Rep[Int=>T]): Rep[ArrayBuffer[T]]
  def createArrayBufferFromArray[T: Elem](arr: Arr[T]): Rep[ArrayBuffer[T]]
}

trait ArrayBuffersStd extends ArrayBuffers { self: ScalanStd =>
  implicit class SeqArrayBuffer[T](val impl: scala.collection.mutable.ArrayBuffer[T])(implicit val eItem: Elem[T]) extends ArrayBuffer[T] {
    def apply(i: Rep[Int]): Rep[T] = impl.apply(i)
    def length : Rep[Int] = impl.length
    def mapBy[R: Elem](f: Rep[T => R]): Rep[ArrayBuffer[R]] = new SeqArrayBuffer[R](impl.map(f))
    def update(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]] = { impl.update(i, v); this }
    def insert(i: Rep[Int], v: Rep[T]): Rep[ArrayBuffer[T]] = { impl.insert(i, v); this }
    def +=(v: Rep[T]): Rep[ArrayBuffer[T]] = { impl += v; this }
    def ++=(a: Arr[T]): Rep[ArrayBuffer[T]] = { impl ++= a; this }
    def remove(i: Rep[Int], n: Rep[Int]): Rep[ArrayBuffer[T]] = { impl.remove(i, n); this }
    def reset():  Rep[ArrayBuffer[T]] = { impl.clear(); this }
    def toArray: Arr[T] = impl.toArray(eItem.classTag)
  }  

  implicit def resolveArrayBuffer[T: Elem](buf: Rep[ArrayBuffer[T]]): ArrayBuffer[T] = buf

  def emptyArrayBuffer[T: Elem]: Rep[ArrayBuffer[T]] = scala.collection.mutable.ArrayBuffer.empty[T]
  def initArrayBuffer[T: Elem](v: Rep[T]): Rep[ArrayBuffer[T]] = scala.collection.mutable.ArrayBuffer(v)
  def makeArrayBuffer[T](name: Rep[String])(implicit e:Elem[T]): Rep[ArrayBuffer[T]] = scala.collection.mutable.ArrayBuffer.empty[T]
  def createArrayBuffer[T: Elem](count: Rep[Int], f:Rep[Int=>T]): Rep[ArrayBuffer[T]] = {
    val buf = scala.collection.mutable.ArrayBuffer.empty[T]
    for (i <- 0 until count) {
      buf += f(i)
    }
    buf
  }
  def createArrayBufferFromArray[T: Elem](arr: Arr[T]): Rep[ArrayBuffer[T]] = {
    val buf = scala.collection.mutable.ArrayBuffer.empty[T]
    buf ++= arr
    buf
  }
}

trait ArrayBuffersExp extends ArrayBuffers with ViewsDslExp { self: ScalanExp =>
  case class ViewArrayBuffer[A, B](source: Rep[ArrayBuffer[A]], override val innerIso: Iso[A, B])
    extends View1[A, B, ArrayBuffer](arrayBufferIso(innerIso)) {
    override def toString = s"ViewArrayBuffer[${innerIso.eTo.name}]($source)"
    override def equals(other: Any) = other match {
      case v: ViewArrayBuffer[_, _] => source == v.source && innerIso.eTo == v.innerIso.eTo
      case _ => false
    }
  }

  object UserTypeArrayBuffer {
    def unapply(s: Exp[_]): Option[Iso[_, _]] = {
      s.elem match {
        case pae: ArrayBufferElem[_] =>
          pae.eItem match {
            case e: ViewElem[_, _] => Some(e.iso)
            case _ => None
          }
        case _ => None
      }
    }
  }

  override def unapplyViews[T](s: Exp[T]): Option[Unpacked[T]] = (s match {
    case Def(view: ViewArrayBuffer[_, _]) =>
      Some((view.source, view.iso))
    case UserTypeArrayBuffer(iso: Iso[a, b]) =>
      val newIso = arrayBufferIso(iso)
      val repr = reifyObject(UnpackView(s.asRep[ArrayBuffer[b]], newIso))
      Some((repr, newIso))
    case _ =>
      super.unapplyViews(s)
  }).asInstanceOf[Option[Unpacked[T]]]


  protected def hasArrayBufferViewArg(s: Exp[_]): Boolean = s match {
    case Def(_: ViewArrayBuffer[_, _]) => true
    case _ => false
  }

  val HasArrayBufferViewArg = HasArg(hasArrayBufferViewArg)

  def mapUnderlyingArrayBuffer[A,B,C](view: ViewArrayBuffer[A,B], f: Rep[B=>C]): Rep[ArrayBuffer[C]] = {
    val iso = view.innerIso
    implicit val eA = iso.eFrom
    implicit val eC: Elem[C] = f.elem.eRange
    ArrayBuffer.fromArray(view.source.toArray.mapBy(iso.toFun >> f))
  }

  override def rewriteDef[T](d: Def[T]) = d match {
    //------------------------------------------------------------
    // Iso lifting rules
    case ArrayBufferFromElem(HasViews(v, Def(iso: IsoUR[a, b]))) =>
      implicit val eA = iso.eFrom
      val v1 = v.asRep[a]
      ViewArrayBuffer(ArrayBufferFromElem(v1), iso)

    case ArrayBufferUpdate(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), i, v@HasViews(_, _)) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val v1 = iso.innerIso.from(v.asRep[b]).asRep[a]
      ViewArrayBuffer(buf1.update(i, v1), iso.innerIso)

    case ArrayBufferInsert(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), i, v@HasViews(_, _)) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val v1 = iso.innerIso.from(v.asRep[b]).asRep[a]
      ViewArrayBuffer(buf1.insert(i, v1), iso.innerIso)

    case ArrayBufferAppend(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), v@HasViews(_, _)) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val v1 = iso.innerIso.from(v.asRep[b]).asRep[a]
      ViewArrayBuffer(buf1 += v1, iso.innerIso)

    case ArrayBufferAppendArray(HasViews(buf, Def(iso: ArrayBufferIso[a, b])), a@HasViews(_, Def(_: ArrayIso[_, _]))) =>
      implicit val eA = iso.innerIso.eFrom
      val buf1 = buf.asRep[ArrayBuffer[a]]
      val arrIso = arrayIso(iso.innerIso)
      val a1 = arrIso.from(a.asRep[Array[b]]).asRep[Array[a]]
      ViewArrayBuffer(buf1 ++= a1, iso.innerIso)

    case mk@MakeArrayBuffer(ctx) if UnpackableElem.unapply(mk.eItem).isDefined =>
       mk.eItem match {
         case UnpackableElem(iso: Iso[a, b]) =>
           implicit val eA = iso.eFrom
           ViewArrayBuffer(ArrayBuffer.make[a](ctx), iso)
{'fraction_non_alphanumeric': 0.17068735604315674, 'fraction_numerical': 0.002545763122802764, 'mean_word_length': 3.83699794781589, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '252994', 'n_tokens_mistral': 6095, 'n_tokens_neox': 6025, 'n_words': 1231}