<commit_before>/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.util.indexing;

import gnu.trove.TIntHashSet;
import gnu.trove.TIntIterator;

import java.util.*;

/**
 * @author <NAME>
 *         Date: Dec 20, 2007
 */
class ValueContainerImpl<Value> extends UpdatableValueContainer<Value> implements Cloneable{
  private HashMap<Value, Object> myInputIdMapping;

  public ValueContainerImpl() {
    myInputIdMapping = new HashMap<Value, Object>(16, 0.98f);
  }

  @Override
  public void addValue(int inputId, Value value) {
    final Object input = myInputIdMapping.get(value);
    if (input == null) {
      //idSet = new TIntHashSet(3, 0.98f);
      myInputIdMapping.put(value, inputId);
    }
    else {
      final TIntHashSet idSet;
      if (input instanceof Integer) {
        idSet = new IdSet(3, 0.98f);
        idSet.add(((Integer)input).intValue());
        myInputIdMapping.put(value, idSet);
      }
      else {
        idSet = (TIntHashSet)input;
      }
      idSet.add(inputId);
    }
  }

  @Override
  public int size() {
    return myInputIdMapping.size();
  }

  @Override
  public void removeAssociatedValue(int inputId) {
    Value toRemove = null;
    for (final Iterator<Value> valueIterator = getValueIterator(); valueIterator.hasNext();) {
      final Value value = valueIterator.next();
      if (isAssociated(value, inputId)) {
        assert toRemove == null;
        toRemove = value;
      }
    }

    if (toRemove != null) {
      removeValue(inputId, toRemove);
    }
  }

  @Override
  public boolean removeValue(int inputId, Value value) {
    final Object input = myInputIdMapping.get(value);
    if (input == null) {
      return false;
    }
    if (input instanceof TIntHashSet) {
      final TIntHashSet idSet = (TIntHashSet)input;
      final boolean reallyRemoved = idSet.remove(inputId);
      if (reallyRemoved) {
        idSet.compact();
      }
      if (!idSet.isEmpty()) {
        return reallyRemoved;
      }
    }
    else if (input instanceof Integer) {
      if (((Integer)input).intValue() != inputId) {
        return false;
      }
    }
    myInputIdMapping.remove(value);<commit_msg>correctly remove null values from index<commit_after>/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.util.indexing;

import com.intellij.openapi.diagnostic.Logger;
import gnu.trove.TIntHashSet;
import gnu.trove.TIntIterator;

import java.util.*;

/**
 * @author <NAME>
 *         Date: Dec 20, 2007
 */
class ValueContainerImpl<Value> extends UpdatableValueContainer<Value> implements Cloneable{
  private static final Logger LOG = Logger.getInstance("#com.intellij.util.indexing.ValueContainerImpl");

  private HashMap<Value, Object> myInputIdMapping;

  public ValueContainerImpl() {
    myInputIdMapping = new HashMap<Value, Object>(16, 0.98f);
  }

  @Override
  public void addValue(int inputId, Value value) {
    final Object input = myInputIdMapping.get(value);
    if (input == null) {
      //idSet = new TIntHashSet(3, 0.98f);
      myInputIdMapping.put(value, inputId);
    }
    else {
      final TIntHashSet idSet;
      if (input instanceof Integer) {
        idSet = new IdSet(3, 0.98f);
        idSet.add(((Integer)input).intValue());
        myInputIdMapping.put(value, idSet);
      }
      else {
        idSet = (TIntHashSet)input;
      }
      idSet.add(inputId);
    }
  }

  @Override
  public int size() {
    return myInputIdMapping.size();
  }

  @Override
  public void removeAssociatedValue(int inputId) {
    final List<Value> toRemove = new ArrayList<Value>(1);
    for (final Iterator<Value> valueIterator = getValueIterator(); valueIterator.hasNext();) {
      final Value value = valueIterator.next();
      if (isAssociated(value, inputId)) {
        LOG.assertTrue(toRemove.isEmpty(), "Expected only one value per-inputId");
        toRemove.add(value);
      }
    }

    if (!toRemove.isEmpty()) {
      for (Value value : toRemove) {
        removeValue(inputId, value);
      }
    }
  }

  @Override
  public boolean removeValue(int inputId, Value value) {
    final Object input = myInputIdMapping.get(value);
    if (input == null) {
      return false;
    }
    if (input instanceof TIntHashSet) {
      final TIntHashSet idSet = (TIntHashSet)input;
      final boolean reallyRemoved = idSet.remove(inputId);
      if (reallyRemoved) {
        idSet.compact();
      }
      if (!idSet.isEmpty()) {
        return reallyRemoved;
      }
    }
    else if (input instanceof Integer) {
      if (((Integer)input).intValue() != inputId) {
        return false;
      }
    }
    myInputIdMapping.remove(value);
{'fraction_non_alphanumeric': 0.10819272917029049, 'fraction_numerical': 0.010958427552617847, 'mean_word_length': 2.9847539847539846, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4825750', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1711, 'n_words': 491}