<commit_before>package com.avsystem.scex
package compiler

import java.{util => ju, lang => jl}
import scala.collection.immutable.SortedMap

/**
 * Created: 24-10-2013
 * Author: ghik
 */
class PositionMapping(
  private val shiftMapping: SortedMap[Int, ShiftInfo],
  private val reverseShiftMapping: SortedMap[Int, ShiftInfo]) {

  def apply(pos: Int) = shiftMapping.to(pos).lastOption match {
    case Some((offset, ShiftInfo(totalPrevShift, added, removed))) =>
      if (pos - offset < removed)
        offset + totalPrevShift
      else
        pos + totalPrevShift - removed + added

    case None =>
      pos
  }

  def reverse =
    new PositionMapping(reverseShiftMapping, shiftMapping)

  override def equals(other: Any) = other match {
    case op: PositionMapping => shiftMapping == op.shiftMapping
  }

  override lazy val hashCode =
    shiftMapping.hashCode()
}

object PositionMapping {
  val empty = new PositionMapping(SortedMap.empty, SortedMap.empty)
}

case class ShiftInfo(totalPrevShift: Int, added: Int, removed: Int) {
  def this(totalPrevShift: Int, amount: Int) =
    this(totalPrevShift, math.max(0, amount), math.max(0, -amount))

  def update(amount: Int) = if (amount > 0)
    copy(added = added + amount)
  else if (amount < 0)
    copy(removed = removed - amount)
  else this

  def totalShift = totalPrevShift + added - removed
}

object ShiftInfo {
  def apply(totalPrevShift: Int, amount: Int) =
    new ShiftInfo(totalPrevShift, amount)
}<commit_msg>bugfix<commit_after>package com.avsystem.scex
package compiler

import java.{util => ju, lang => jl}
import scala.collection.immutable.SortedMap

/**
 * Created: 24-10-2013
 * Author: ghik
 */
class PositionMapping(
  private val shiftMapping: SortedMap[Int, ShiftInfo],
  private val reverseShiftMapping: SortedMap[Int, ShiftInfo]) {

  def apply(pos: Int) = shiftMapping.to(pos).lastOption match {
    case Some((offset, ShiftInfo(totalPrevShift, added, removed))) =>
      if (pos - offset < removed)
        offset + totalPrevShift
      else
        pos + totalPrevShift - removed + added

    case None =>
      pos
  }

  def reverse =
    new PositionMapping(reverseShiftMapping, shiftMapping)

  override def equals(other: Any) = other match {
    case op: PositionMapping => shiftMapping == op.shiftMapping
    case _ => false
  }

  override lazy val hashCode =
    shiftMapping.hashCode()
}

object PositionMapping {
  val empty = new PositionMapping(SortedMap.empty, SortedMap.empty)
}

case class ShiftInfo(totalPrevShift: Int, added: Int, removed: Int) {
  def this(totalPrevShift: Int, amount: Int) =
    this(totalPrevShift, math.max(0, amount), math.max(0, -amount))

  def update(amount: Int) = if (amount > 0)
    copy(added = added + amount)
  else if (amount < 0)
    copy(removed = removed - amount)
  else this

  def totalShift = totalPrevShift + added - removed
}

object ShiftInfo {
  def apply(totalPrevShift: Int, amount: Int) =
    new ShiftInfo(totalPrevShift, amount)
}
{'fraction_non_alphanumeric': 0.10705921712947473, 'fraction_numerical': 0.008029441284710606, 'mean_word_length': 3.7235387045813586, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '6441984', 'n_tokens_mistral': 950, 'n_tokens_neox': 906, 'n_words': 262}