<commit_before>package scalan.collections

import scalan._

/**
  * Created by dkolmakov on 12/28/15.
  */
trait MapViews extends MapOps with Views { self: Scalan =>
}

trait MapViewsStd extends MapViews with MapOpsStd with ViewsDslStd { self: ScalanStd =>
}

trait MapViewsExp extends MapViews with MapOpsExp with ViewsDslExp { self: ScalanExp =>

  case class ViewMap[K1, V1, K2, V2](source: MM[K1, V1])(implicit iso1: Iso[K1, K2], iso2: Iso[V1, V2]) extends View2[K1, V1, K2, V2, MMap] {
    implicit val k1 = iso1.eFrom
    implicit val k2 = iso1.eTo
    implicit val v1 = iso2.eFrom
    implicit val v2 = iso2.eTo
    lazy val iso = MapIso(iso1, iso2)
//    def copy(source: MM[K1, V1]) = ViewMap(source)
    override def toString = s"ViewMap[${iso1.eTo.name}]($source)"
    override def equals(other: Any) = other match {
      case v: ViewMap[_, _, _, _] => source == v.source && iso1.eTo == v.iso1.eTo && iso2.eTo == v.iso2.eTo
      case _ => false
    }
  }

  override def unapplyViews[T](s: Exp[T]): Option[Unpacked[T]] = (s match {
    case Def(view: ViewMap[_,_,_,_]) => Some((view.source, view.iso))
    case _ =>  super.unapplyViews(s)
  }).asInstanceOf[Option[Unpacked[T]]]

  override def rewriteDef[T](d: Def[T]) = d match {
    case VarMM(map) => map

    case MapFromArray(HasViews(source: Arr[(a1, a2)] @unchecked, Def(arrIso: ArrayIso[_,_]))) =>
      arrIso.innerIso match {
        case Def(pairIso: PairIso[_, _, b1, b2]) =>
          val iso1 = pairIso.iso1.asInstanceOf[Iso[a1, b1]]
          val iso2 = pairIso.iso2.asInstanceOf[Iso[a2, b2]]
          implicit val eA1 = iso1.eFrom
          implicit val eB1 = iso1.eTo
          implicit val eA2 = iso2.eFrom
          implicit val eB2 = iso2.eTo
          val mNew = MMap.fromArray(source)
          ViewMap(mNew)(iso1, iso2)
      }

    // clean k2 (and clean v2 if needed)
    case MapApply(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key@HasViews(_, _)) =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = mapIso.iso1.from(key.asRep[k2]).asRep[k]
      mapIso.iso2.to(map1.apply(key1))
    // clean v2 if still needed after k2 is cleaned
    case MapApply(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key) if mapIso.iso1.isIdentity =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = key.asRep[k]
      mapIso.iso2.to(map1.apply(key1))

    // clean k2 (and clean v2 if needed)
    case MapContains(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key@HasViews(_, _)) =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = mapIso.iso1.from(key.asRep[k2]).asRep[k]
      map1.contains(key1)
    // clean v2 if still needed after k2 is cleaned
    case MapContains(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key) if mapIso.iso1.isIdentity =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = key.asRep[k]
      map1.contains(key1)

    case MapUnion(HasViews(sourceMap1: Rep[MMap[k,v]] @unchecked, Def(mapIso1: MapIso[_,_,k2,v2])), HasViews(sourceMap2, mapIso2)) if(mapIso1 == mapIso2)=> {
      implicit val eK = mapIso1.iso1.eFrom.asElem[k]
      implicit val eV = mapIso1.iso2.eFrom.asElem[v]
      val union = sourceMap1.union(sourceMap2.asRep[MMap[k,v]])
      mapIso1.asInstanceOf[MapIso[k,v,k2,v2]].to(union)
    }

    case MapUsingFunc(count, LambdaResultHasViews(f, Def(iso: PairIso[a1, a2, b1, b2]))) => {
      val f1 = f.asRep[Int=>(b1,b2)]
      implicit val eA1 = iso.iso1.eFrom<commit_msg>[Rules] edit mapUnion rule<commit_after>package scalan.collections

import scalan._

/**
  * Created by dkolmakov on 12/28/15.
  */
trait MapViews extends MapOps with Views { self: Scalan =>
}

trait MapViewsStd extends MapViews with MapOpsStd with ViewsDslStd { self: ScalanStd =>
}

trait MapViewsExp extends MapViews with MapOpsExp with ViewsDslExp { self: ScalanExp =>

  case class ViewMap[K1, V1, K2, V2](source: MM[K1, V1])(implicit iso1: Iso[K1, K2], iso2: Iso[V1, V2]) extends View2[K1, V1, K2, V2, MMap] {
    implicit val k1 = iso1.eFrom
    implicit val k2 = iso1.eTo
    implicit val v1 = iso2.eFrom
    implicit val v2 = iso2.eTo
    lazy val iso = MapIso(iso1, iso2)
//    def copy(source: MM[K1, V1]) = ViewMap(source)
    override def toString = s"ViewMap[${iso1.eTo.name}]($source)"
    override def equals(other: Any) = other match {
      case v: ViewMap[_, _, _, _] => source == v.source && iso1.eTo == v.iso1.eTo && iso2.eTo == v.iso2.eTo
      case _ => false
    }
  }

  override def unapplyViews[T](s: Exp[T]): Option[Unpacked[T]] = (s match {
    case Def(view: ViewMap[_,_,_,_]) => Some((view.source, view.iso))
    case _ =>  super.unapplyViews(s)
  }).asInstanceOf[Option[Unpacked[T]]]

  override def rewriteDef[T](d: Def[T]) = d match {
    case VarMM(map) => map

    case MapFromArray(HasViews(source: Arr[(a1, a2)] @unchecked, Def(arrIso: ArrayIso[_,_]))) =>
      arrIso.innerIso match {
        case Def(pairIso: PairIso[_, _, b1, b2]) =>
          val iso1 = pairIso.iso1.asInstanceOf[Iso[a1, b1]]
          val iso2 = pairIso.iso2.asInstanceOf[Iso[a2, b2]]
          implicit val eA1 = iso1.eFrom
          implicit val eB1 = iso1.eTo
          implicit val eA2 = iso2.eFrom
          implicit val eB2 = iso2.eTo
          val mNew = MMap.fromArray(source)
          ViewMap(mNew)(iso1, iso2)
      }

    // clean k2 (and clean v2 if needed)
    case MapApply(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key@HasViews(_, _)) =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = mapIso.iso1.from(key.asRep[k2]).asRep[k]
      mapIso.iso2.to(map1.apply(key1))
    // clean v2 if still needed after k2 is cleaned
    case MapApply(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key) if mapIso.iso1.isIdentity =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = key.asRep[k]
      mapIso.iso2.to(map1.apply(key1))

    // clean k2 (and clean v2 if needed)
    case MapContains(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key@HasViews(_, _)) =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = mapIso.iso1.from(key.asRep[k2]).asRep[k]
      map1.contains(key1)
    // clean v2 if still needed after k2 is cleaned
    case MapContains(HasViews(map, Def(mapIso: MapIso[k,v,k2,v2])), key) if mapIso.iso1.isIdentity =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val map1 = map.asRep[MMap[k,v]]
      val key1 = key.asRep[k]
      map1.contains(key1)

    case MapUnion(HasViews(left, Def(mapIso: MapIso[k,v,k2,v2])), right@HasViews(_, Def(_: MapIso[_,_,_,_]))) =>
      implicit val eK = mapIso.iso1.eFrom.asElem[k]
      implicit val eV = mapIso.iso2.eFrom.asElem[v]
      val left1 = left.asRep[MMap[k,v]]
      val right1 = mapIso.from(right.asRep[MMap[k2,v2]]).asRep[MMap[k,v]]
      ViewMap(left1 union right1)(mapIso.iso1, mapIso.iso2)

    case MapUsingFunc(count, LambdaResultHasViews(f, Def(iso: PairIso[a1, a2, b1, b2]))) => {
      val f1 = f.asRep[Int=>(b1,b2)]
      implicit val eA1 = iso.iso1.eFrom
{'fraction_non_alphanumeric': 0.14682126107347576, 'fraction_numerical': 0.03478374153204794, 'mean_word_length': 3.1905021834061134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '7482722', 'n_tokens_mistral': 3190, 'n_tokens_neox': 3160, 'n_words': 653}