<commit_before>package de.lmu.ifi.dbs.elki.utilities.datastructures.heap;

import java.util.HashMap;

/**
 * A heap as used in OPTICS that allows updating entries.
 * 
 * @author <NAME>
 * 
 * @param <O> object type
 */
public class UpdatableHeap<O> extends Heap<O> {
  /**
   * Serial version
   */
  private static final long serialVersionUID = 1L;

  /**
   * Holds the indices in the heap of each element.
   */
  private HashMap<O, Integer> index = new HashMap<O, Integer>();

  /**
   * Simple constructor with default size.
   */
  public UpdatableHeap() {
    super();
  }

  /**
   * Constructor with predefined size.
   * 
   * @param size
   */
  public UpdatableHeap(int size) {
    super(size);
  }

  @Override
  public void clear() {
    super.clear();
    index.clear();
  }

  @Override
  public synchronized boolean offer(O e) {
    Integer pos = index.get(e);
    if(pos == null) {
      // LoggingUtil.logExpensive(Level.INFO, "Inserting: "+e);
      // insert
      return super.offer(e);
    }
    else {
      // update
      if(compareExternal(e, pos) < 0) {
        // LoggingUtil.logExpensive(Level.INFO,
        // "Updating value: "+e+" vs. "+castQueueElement(pos));
        modCount++;
        putInQueue(pos, e);
        heapifyUpParent(pos);
        // We have changed - return true according to {@link Collection#put}
        return true;
      }
      else {
        // LoggingUtil.logExpensive(Level.INFO,
        // "Keeping value: "+e+" vs. "+castQueueElement(pos));
        // Ignore, no improvement. Return success anyway.
        return true;
      }
    }
  }

  @Override
  protected void putInQueue(int pos, Object e) {
    super.putInQueue(pos, e);
    // Keep index up to date
    if(e != null) {
      O n = castQueueElement(pos);
      index.put(n, pos);
    }
  }

  @Override
  protected synchronized O removeAt(int pos) {
    O node = super.removeAt(pos);
    // Keep index up to date
    index.remove(node);
    return node;
  }

  @Override
  public O poll() {
    O node = super.poll();
    index.remove(node);
    return node;
  }
}<commit_msg>Add "removeObject" operation, patch by Tobias.<commit_after>package de.lmu.ifi.dbs.elki.utilities.datastructures.heap;

import java.util.HashMap;

/**
 * A heap as used in OPTICS that allows updating entries.
 * 
 * @author <NAME>
 * 
 * @param <O> object type
 */
public class UpdatableHeap<O> extends Heap<O> {
  /**
   * Serial version
   */
  private static final long serialVersionUID = 1L;

  /**
   * Holds the indices in the heap of each element.
   */
  private HashMap<O, Integer> index = new HashMap<O, Integer>();

  /**
   * Simple constructor with default size.
   */
  public UpdatableHeap() {
    super();
  }

  /**
   * Constructor with predefined size.
   * 
   * @param size
   */
  public UpdatableHeap(int size) {
    super(size);
  }

  @Override
  public void clear() {
    super.clear();
    index.clear();
  }

  @Override
  public synchronized boolean offer(O e) {
    Integer pos = index.get(e);
    if(pos == null) {
      // LoggingUtil.logExpensive(Level.INFO, "Inserting: "+e);
      // insert
      return super.offer(e);
    }
    else {
      // update
      if(compareExternal(e, pos) < 0) {
        // LoggingUtil.logExpensive(Level.INFO,
        // "Updating value: "+e+" vs. "+castQueueElement(pos));
        modCount++;
        putInQueue(pos, e);
        heapifyUpParent(pos);
        // We have changed - return true according to {@link Collection#put}
        return true;
      }
      else {
        // LoggingUtil.logExpensive(Level.INFO,
        // "Keeping value: "+e+" vs. "+castQueueElement(pos));
        // Ignore, no improvement. Return success anyway.
        return true;
      }
    }
  }

  @Override
  protected void putInQueue(int pos, Object e) {
    super.putInQueue(pos, e);
    // Keep index up to date
    if(e != null) {
      O n = castQueueElement(pos);
      index.put(n, pos);
    }
  }

  @Override
  protected synchronized O removeAt(int pos) {
    O node = super.removeAt(pos);
    // Keep index up to date
    index.remove(node);
    return node;
  }

  /**
   * Remove the given object from the queue.
   * 
   * @param e Obejct to remove
   * @return Existing entry
   */
  public O removeObject(O e) {
    Integer pos = index.get(e);
    if(pos != null) {
      return removeAt(pos);
    }
    else {
      return null;
    }
  }

  @Override
  public O poll() {
    O node = super.poll();
    index.remove(node);
    return node;
  }
}
{'fraction_non_alphanumeric': 0.1324018629407851, 'fraction_numerical': 0.0008871146595697493, 'mean_word_length': 2.2516222062004325, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '6818035', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1456, 'n_words': 392}