package enju.ccg.ml

import scala.collection.mutable.HashMap

class FeatureIndexer[Feature] extends HashMap[Feature, Int] {
  def getIndex(key: Feature) = getOrElseUpdate(key, size)

  def removeIndexes(idxs: Seq[Int]): Unit = {
    val features = this.toSeq.sortWith(_._2 < _._2).map(_._1)
    val originalSize = size
    (0 to idxs.size) foreach { i =>
      val idx = if (i == idxs.size) originalSize else idxs(i)
      val lastIdx = if (i == 0) -1 else idxs(i - 1)
      (lastIdx + 1 until idx).foreach { f => this(features(f)) -= i }
      if (i != idxs.size) this -= features(idx)
    }
  }
  def removeElemsOver(lastIdx: Int): Unit = this.toSeq.foreach { case (feature, idx) =>
    if (idx >= lastIdx) this -= feature
  }
}

package enju.ccg.ml

import scala.collection.mutable.HashMap

class FeatureIndexer[Feature] extends HashMap[Feature, Int] {
  def getIndex(key: Feature) = getOrElseUpdate(key, size)

  def removeIndexes(idxs: Seq[Int]): Unit = {
    val features = this.toSeq.sortWith(_._2 < _._2).map(_._1)
    for (i <- 0 until features.size) {
      assert(this(features(i)) == i)
    }
    (0 to idxs.size) foreach { i =>
      val idx = if (i == idxs.size) size else idxs(i)
      val lastIdx = if (i == 0) -1 else idxs(i - 1)
      (lastIdx + 1 until idx).foreach { f => this(features(f)) -= i }
      if (i != idxs.size) this -= features(idx)
    }
  }
  def removeElemsOver(lastIdx: Int): Unit = this.toSeq.foreach { case (feature, idx) =>
    if (idx >= lastIdx) this -= feature
  }
}

--- 
+++ 
@@ -7,11 +7,9 @@
 
   def removeIndexes(idxs: Seq[Int]): Unit = {
     val features = this.toSeq.sortWith(_._2 < _._2).map(_._1)
-    for (i <- 0 until features.size) {
-      assert(this(features(i)) == i)
-    }
+    val originalSize = size
     (0 to idxs.size) foreach { i =>
-      val idx = if (i == idxs.size) size else idxs(i)
+      val idx = if (i == idxs.size) originalSize else idxs(i)
       val lastIdx = if (i == 0) -1 else idxs(i - 1)
       (lastIdx + 1 until idx).foreach { f => this(features(f)) -= i }
       if (i != idxs.size) this -= features(idx)
Fix a bug in removing zero elements
