package sssj.index.minibatch.components;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import sssj.base.Vector;

public class Residuals implements Iterable<Vector> {
  private Queue<Vector> queue = new LinkedList<>(); // TODO use ArrayDequeue?

  public void add(Vector residual) {
    queue.add(residual);
  }

  @Override
  public Iterator<Vector> iterator() {
    return queue.iterator();
  }

  @Override
  public String toString() {
    return "Residuals = [" + queue + "]";
  }

  public Vector get(long candidateID) {
    for (Vector v : queue)
      if (candidateID == v.timestamp())
        return v;
    return null;
  }

  /**
   * Get the residual of the vector with id {@code candidateID}, while at the same time pruning all the residuals with timestamp less than {@code lowWatermark}.
   *
   * @param candidateID the id of the candidate vector
   * @param lowWatermark the minimum id of the vector to be retained
   * @return the residual of the candidate
   */
  public Vector getAndPrune(long candidateID, long lowWatermark) {
    for (Iterator<Vector> it = queue.iterator(); it.hasNext();) {
      final Vector v = it.next();
      if (lowWatermark > v.timestamp())
        it.remove();
      if (candidateID == v.timestamp())
        return v;
    }
    return null;
  }
}
@@ -46,81 +46,62 @@
 ort 
-java.util.Iterator;%0Aimport java.util.LinkedList;%0Aimport java.util.Queue;%0A
+it.unimi.dsi.fastutil.longs.Long2ReferenceOpenHashMap;
 %0Aimp
@@ -150,53 +150,51 @@
 als 
-implements Iterable%3CVector%3E %7B%0A  private Queue
+%7B%0A  private final Long2ReferenceOpenHashMap
 %3CVec
@@ -202,21 +202,19 @@
 or%3E 
-queue
+map
  = new L
 inke
@@ -213,48 +213,37 @@
 ew L
-inkedList%3C%3E(); // TODO use ArrayDequeue?
+ong2ReferenceOpenHashMap%3C%3E();
 %0A%0A  
@@ -285,1006 +285,211 @@
     
-queue.add(residual);%0A  %7D%0A%0A  @Override%0A  public Iterator%3CVector%3E iterator() %7B%0A    return queue.iterator();%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return %22Residuals = %5B%22 + queue + %22%5D%22;%0A  %7D%0A%0A  public Vector get(long candidateID) %7B%0A    for (Vector v : queue)%0A      if (candidateID == v.timestamp())%0A        return v;%0A    return null;%0A  %7D%0A%0A  /**%0A   * Get the residual of the vector with id %7B@code candidateID%7D, while at the same time pruning all the residuals with timestamp less than %7B@code lowWatermark%7D.%0A   *%0A   * @param candidateID the id of the candidate vector%0A   * @param lowWatermark the minimum id of the vector to be retained%0A   * @return the residual of the candidate%0A   */%0A  public Vector getAndPrune(long candidateID, long lowWatermark) %7B%0A    for (Iterator%3CVector%3E it = queue.iterator(); it.hasNext();) %7B%0A      final Vector v = it.next();%0A      if (lowWatermark %3E v.timestamp())%0A        it.remove();%0A      if (candidateID == v.timestamp())%0A        return v;%0A    %7D%0A    return null
+map.put(residual.timestamp(), residual);%0A  %7D%0A%0A  public Vector get(long candidateID) %7B%0A    return map.get(candidateID);%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return %22Residuals = %5B%22 + map + %22%5D%22
 ;%0A  

Optimize MB residuals