<commit_before>package net.dryanhild.jcollada.schema14.geometry;

import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

import net.dryanhild.jcollada.ParsingException;
import net.dryanhild.jcollada.data.geometry.DataType;

import org.collada.x2005.x11.colladaSchema.InputLocal;
import org.collada.x2005.x11.colladaSchema.InputLocalOffset;
import org.collada.x2005.x11.colladaSchema.PolylistDocument.Polylist;
import org.collada.x2005.x11.colladaSchema.VerticesDocument.Vertices;

import com.google.common.collect.ImmutableSet;

public class PolylistParser {

    private final Vertices vertices;
    private final Polylist polys;
    private final IndexReorganizer reorganizer;

    public PolylistParser(Polylist polyList, Vertices vertices, IndexReorganizer reorganizer) {
        polys = polyList;
        this.vertices = vertices;
        this.reorganizer = reorganizer;
    }

    public Set<DataType> getDataTypes() {
        TObjectIntMap<SourceReference> offsets = getOffsets(polys.getInputArray());

        ImmutableSet.Builder<DataType> types = ImmutableSet.builder();

        for (SourceReference ref : offsets.keySet()) {
            types.add(ref.type);
        }
        return types.build();
    }

    public int[] getTriangleIndices() {
        TObjectIntMap<SourceReference> offsets = getOffsets(polys.getInputArray());

        final int vertexStride = vertexStride(offsets);

        TIntList elementIndices = getTriangleIndices(polys.getVcount(), polys.getP(), vertexStride);

        TIntList indices = new TIntArrayList();
        TObjectIntMap<SourceReference> sourceToIndex = new TObjectIntHashMap<>();

        for (int baseOffset = 0; baseOffset < elementIndices.size(); baseOffset += vertexStride) {
            for (SourceReference ref : offsets.keySet()) {
                int elementIndex = elementIndices.get(baseOffset + offsets.get(ref));
                sourceToIndex.put(ref, elementIndex);
            }
            indices.add(reorganizer.convertToSingleIndex(sourceToIndex));
        }

        return indices.toArray();
    }

    private TObjectIntMap<SourceReference> getOffsets(InputLocalOffset[] inputs) {
        TObjectIntMap<SourceReference> offsets = new TObjectIntHashMap<>();

        for (InputLocalOffset input : inputs) {
            if ("VERTEX".equals(input.getSemantic())) {
                for (InputLocal local : vertices.getInputArray()) {
                    DataType type = DataType.valueOf(local.getSemantic());
                    SourceReference ref = new SourceReference(type, local.getSource());
                    assert !offsets.containsKey(ref) : "Two input elements reference the same source!";
                    BigInteger offset = input.getOffset();
                    offsets.put(ref, offset == null ? 0 : offset.intValue());
                }
            } else {
                DataType type = DataType.valueOf(input.getSemantic());
                SourceReference ref = new SourceReference(type, input.getSource());
                assert !offsets.containsKey(ref) : "Two input elements reference the same source!";
                BigInteger offset = input.getOffset();
                offsets.put(ref, offset == null ? 0 : offset.intValue());
            }
        }
        return offsets;
    }<commit_msg>XMLBeans inserts default values: removed a check to insert the defaults after parsing.<commit_after>package net.dryanhild.jcollada.schema14.geometry;

import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;

import net.dryanhild.jcollada.ParsingException;
import net.dryanhild.jcollada.data.geometry.DataType;

import org.collada.x2005.x11.colladaSchema.InputLocal;
import org.collada.x2005.x11.colladaSchema.InputLocalOffset;
import org.collada.x2005.x11.colladaSchema.PolylistDocument.Polylist;
import org.collada.x2005.x11.colladaSchema.VerticesDocument.Vertices;

import com.google.common.collect.ImmutableSet;

public class PolylistParser {

    private final Vertices vertices;
    private final Polylist polys;
    private final IndexReorganizer reorganizer;

    public PolylistParser(Polylist polyList, Vertices vertices, IndexReorganizer reorganizer) {
        polys = polyList;
        this.vertices = vertices;
        this.reorganizer = reorganizer;
    }

    public Set<DataType> getDataTypes() {
        TObjectIntMap<SourceReference> offsets = getOffsets(polys.getInputArray());

        ImmutableSet.Builder<DataType> types = ImmutableSet.builder();

        for (SourceReference ref : offsets.keySet()) {
            types.add(ref.type);
        }
        return types.build();
    }

    public int[] getTriangleIndices() {
        TObjectIntMap<SourceReference> offsets = getOffsets(polys.getInputArray());

        final int vertexStride = vertexStride(offsets);

        TIntList elementIndices = getTriangleIndices(polys.getVcount(), polys.getP(), vertexStride);

        TIntList indices = new TIntArrayList();
        TObjectIntMap<SourceReference> sourceToIndex = new TObjectIntHashMap<>();

        for (int baseOffset = 0; baseOffset < elementIndices.size(); baseOffset += vertexStride) {
            for (SourceReference ref : offsets.keySet()) {
                int elementIndex = elementIndices.get(baseOffset + offsets.get(ref));
                sourceToIndex.put(ref, elementIndex);
            }
            indices.add(reorganizer.convertToSingleIndex(sourceToIndex));
        }

        return indices.toArray();
    }

    private TObjectIntMap<SourceReference> getOffsets(InputLocalOffset[] inputs) {
        TObjectIntMap<SourceReference> offsets = new TObjectIntHashMap<>();

        for (InputLocalOffset input : inputs) {
            if ("VERTEX".equals(input.getSemantic())) {
                for (InputLocal local : vertices.getInputArray()) {
                    DataType type = DataType.valueOf(local.getSemantic());
                    SourceReference ref = new SourceReference(type, local.getSource());
                    assert !offsets.containsKey(ref) : "Two input elements reference the same source!";
                    BigInteger offset = input.getOffset();
                    offsets.put(ref, offset.intValue());
                }
            } else {
                DataType type = DataType.valueOf(input.getSemantic());
                SourceReference ref = new SourceReference(type, input.getSource());
                assert !offsets.containsKey(ref) : "Two input elements reference the same source!";
                BigInteger offset = input.getOffset();
                offsets.put(ref, offset.intValue());
            }
        }
        return offsets;
    }
{'fraction_non_alphanumeric': 0.09981356661408289, 'fraction_numerical': 0.008030976624121612, 'mean_word_length': 2.9091928251121075, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, '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': '3115037', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1914, 'n_words': 384}