<commit_before>package it.polimi.genomics.GMQLServer

import it.polimi.genomics.core.DataStructures.Builtin.MapFunctionFactory
import it.polimi.genomics.core.DataStructures.RegionAggregate.RegionsToRegion
import it.polimi.genomics.core._
import it.polimi.genomics.core.ParsingType

/**
 * Created by pietro on 22/07/15.
 */
object DefaultRegionsToRegionFactory extends MapFunctionFactory{

  def get(name : String, output_name : Option[String]) = {
    name match {
      case "COUNT" => getCount(output_name)
      case _ => throw new Exception("No map function with the given name (" + name + ") found.")
    }
  }

  def get(name : String, position : Int, output_name : Option[String]) = {
    name match {
      case "SUM" => getSum(position,output_name)
      case "MIN" => getMin(position,output_name)
      case "MAX" => getMax(position,output_name)
      case "AVG" => getAvg(position,output_name)
      case "BAG" => getBAG(position,output_name)
      case _ => throw new Exception("No map function with the given name (" + name + ") found.")
    }
  }

  private def getCount(output_name:Option[String]) = new RegionsToRegion {
    override val resType = ParsingType.DOUBLE
    override val index: Int = 0<commit_msg>Median in COVER [issue #36]<commit_after>package it.polimi.genomics.GMQLServer

import it.polimi.genomics.core.DataStructures.Builtin.MapFunctionFactory
import it.polimi.genomics.core.DataStructures.RegionAggregate.{RegionsToMeta, RegionsToRegion}
import it.polimi.genomics.core._
import it.polimi.genomics.core.ParsingType
import it.polimi.genomics.core.ParsingType.PARSING_TYPE

/**
 * Created by pietro on 22/07/15.
 */
object DefaultRegionsToRegionFactory extends MapFunctionFactory{

  def get(name : String, output_name : Option[String]) = {
    name match {
      case "COUNT" => getCount(output_name)
      case _ => throw new Exception("No map function with the given name (" + name + ") found.")
    }
  }

  def get(name : String, position : Int, output_name : Option[String]) = {
    name match {
      case "SUM" => getSum(position,output_name)
      case "MIN" => getMin(position,output_name)
      case "MAX" => getMax(position,output_name)
      case "AVG" => getAvg(position,output_name)
      case "MEDIAN" => getMEDIAN(position,output_name)
      case "BAG" => getBAG(position,output_name)
      case _ => throw new Exception("No map function with the given name (" + name + ") found.")
    }
  }

  private def getMEDIAN(position:Int,new_name:Option[String]) = new RegionsToRegion {
    override val resType: PARSING_TYPE = ParsingType.DOUBLE
    override val index: Int = position
    override val associative: Boolean = false
    override val funOut: (GValue, Int) => GValue = {(v1,v2)=>v1}
    override val fun: (List[GValue]) => GValue = {
      (line) => {
        /*val values: List[Double] = line.map{ gvalue => val s = gvalue.asInstanceOf[GDouble].v;s}.sorted*/
        val values: List[Double] = line.flatMap((gvalue) => {
          if (gvalue.isInstanceOf[GDouble]) Some(gvalue.asInstanceOf[GDouble].v) else None
        }).sorted
        if (!values.isEmpty) {
          if (/*line*/values.length % 2 == 0) {
            val right = /*line*/values.length / 2
            val left = (/*line*/values.length / 2) - 1
            GDouble((values(left) + values(left)) / 2)
          }
          else
            GDouble(values(/*line*/values.length / 2))
        }
        else GNull()
      }
    }
  }

  private def getCount(output_name:Option[String]) = new RegionsToRegion {
    override val resType = ParsingType.DOUBLE
    override val index: Int = 0
{'fraction_non_alphanumeric': 0.12378842425920798, 'fraction_numerical': 0.007200221545278316, 'mean_word_length': 3.3051251489868894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '6350826', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1132, 'n_words': 265}