<commit_before>/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * 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.nesscomputing.sequencer;

import java.util.Collections;
import java.util.List;

import javax.annotation.concurrent.NotThreadSafe;

import com.google.common.collect.Lists;

import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;

/**
 * Maps a sparse range of keys to a dense range (starting at 0).
 *
 * @param <K> the type of sparse keys to map
 */
@NotThreadSafe
public class HashSequencer<K> extends AbstractSequencer<K> {
    private static final long serialVersionUID = 1L;

    private final TObjectIntHashMap<K> keyToInt;
    private final List<K> intToKey;
    private int nextInt = 0;

    public HashSequencer() {
        keyToInt = new TObjectIntHashMap<K>();
        intToKey = Lists.newArrayList();
    }

    private HashSequencer(Sequencer<K> other) {
        keyToInt = new TObjectIntHashMap<K>(other.size());
        intToKey = Lists.newArrayListWithCapacity(other.size());

        this.nextInt = other.size();
        for (int i = 0; i < this.nextInt; i++) {
            K key = other.unsequence(i);
            this.intToKey.add(key);
            this.keyToInt.put(key, i);
        }
    }

    public static <K> HashSequencer<K> copyOf(Sequencer<K> other)
    {
        return new HashSequencer<K>(other);
    }

    @Override
    public List<K> getKeys() {
        return Collections.unmodifiableList(intToKey);
    }

    @Override
    public int sequenceOrAdd(K key) {
        int result = keyToInt.get(key);
        if (result == keyToInt.getNoEntryValue() && !keyToInt.containsKey(key)) {
            keyToInt.put(key, result = nextInt);
            intToKey.add(key);
            nextInt++;
        }

        return result;
    }

    @Override
    public boolean containsKey(Object key) {
        return keyToInt.containsKey(key);
    }

    @Override
    public int sequenceIfExists(K key) {
        int val = keyToInt.get(key);

        if (val == keyToInt.getNoEntryValue() && !keyToInt.containsKey(key))
            return -1;

        return val;
    }

    @Override
    public void sequenceExisting(Iterable<K> keys, TObjectIntMap<K> result)
    {
        for (K key : keys) {
            int val = keyToInt.get(key);
            if (val != keyToInt.getNoEntryValue() || keyToInt.containsKey(key)) {
                result.put(key, val);
            }
        }
    }

    @Override
    public K unsequence(int index) {
        return intToKey.get(index);
    }

    @Override
    public int size() {
        return nextInt;
    }
}<commit_msg>Improve HashSequencer performance by using -1 as the noEntryValue<commit_after>/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * 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.nesscomputing.sequencer;

import java.util.Collections;
import java.util.List;

import javax.annotation.concurrent.NotThreadSafe;

import com.google.common.collect.Lists;

import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;

/**
 * Maps a sparse range of keys to a dense range (starting at 0).
 *
 * @param <K> the type of sparse keys to map
 */
@NotThreadSafe
public class HashSequencer<K> extends AbstractSequencer<K> {
    private static final long serialVersionUID = 1L;
    private static final float LOAD_FACTOR = 0.5f;

    private final TObjectIntHashMap<K> keyToInt;
    private final List<K> intToKey;
    private int nextInt = 0;

    private HashSequencer(int startingSize) {
        keyToInt = new TObjectIntHashMap<K>(startingSize, LOAD_FACTOR, -1);
        intToKey = Lists.newArrayListWithCapacity(startingSize);
    }

    private HashSequencer(Sequencer<K> other) {
        this(other.size(), other.getKeys());
    }

    private HashSequencer(int size, Iterable<K> elements) {
        keyToInt = new TObjectIntHashMap<K>(size, LOAD_FACTOR, -1);
        intToKey = Lists.newArrayListWithCapacity(size);

        this.nextInt = other.size();
        for (int i = 0; i < this.nextInt; i++) {
            K key = other.unsequence(i);
            this.intToKey.add(key);
            this.keyToInt.put(key, i);
        }
    }

    public static <K> HashSequencer<K> copyOf(Sequencer<K> other)
    {
        return new HashSequencer<K>(other);
    }

    @Override
    public List<K> getKeys() {
        return Collections.unmodifiableList(intToKey);
    }

    @Override
    public int sequenceOrAdd(K key) {
        assert keyToInt.getNoEntryValue() == -1 : "noEntryValue must be == -1";

        int result = keyToInt.get(key);
        if (result == keyToInt.getNoEntryValue()) {
            keyToInt.put(key, result = nextInt);
            intToKey.add(key);
            nextInt++;
        }

        return result;
    }

    @Override
    public boolean containsKey(Object key) {
        return keyToInt.containsKey(key);
    }

    @Override
    public int sequenceIfExists(K key) {
        assert keyToInt.getNoEntryValue() == -1 : "noEntryValue must be == -1";
        return keyToInt.get(key);
    }

    @Override
    public void sequenceExisting(Iterable<K> keys, TObjectIntMap<K> result)
    {
        assert keyToInt.getNoEntryValue() == -1 : "noEntryValue must be == -1";
        for (K key : keys) {
            int val = keyToInt.get(key);
            if (val != keyToInt.getNoEntryValue()) {
                result.put(key, val);
            }
        }
    }

    @Override
    public K unsequence(int index) {
        return intToKey.get(index);
    }

    @Override
    public int size() {
        return nextInt;
    }
}
{'fraction_non_alphanumeric': 0.10518361795375548, 'fraction_numerical': 0.005440531963125284, 'mean_word_length': 2.644273127753304, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 34, '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': '1086098', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1978, 'n_words': 566}