<commit_before>/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> (<EMAIL>) - initial API and implementation

 *******************************************************************************/

package io.usethesource.vallang.type;

import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;

import org.checkerframework.checker.nullness.qual.Nullable;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IListWriter;
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.exceptions.IllegalOperationException;

/*package*/ class TupleType extends DefaultSubtypeOfValue {
    protected final Type[] fFieldTypes; // protected access for the benefit of inner classes
    protected int fHashcode = -1;

    /**
     * Creates a tuple type with the given field types. Does not copy the array..
     */
    /*package*/ TupleType(Type[] fieldTypes) {
        fFieldTypes = fieldTypes; // fieldTypes.clone(); was safer, but it ended up being a bottleneck
    }

    public static class Info implements TypeFactory.TypeReifier {
        @Override
        public Type getSymbolConstructorType() {
            return symbols().typeSymbolConstructor("tuple", TF.listType(symbols().symbolADT()), "symbols");
        }

        @Override
        public Type fromSymbol(IConstructor symbol, TypeStore store, Function<IConstructor, Set<IConstructor>> grammar) {
            return symbols().fromSymbols((IList) symbol.get("symbols"), store, grammar);
        }

        @Override
        public IConstructor toSymbol(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar,
                Set<IConstructor> done) {
            IListWriter w = vf.listWriter();

            for (Type f : type) {
                w.append(f.asSymbol(vf, store, grammar, done));
            }

            return vf.constructor(getSymbolConstructorType(), w.done());
        }

        @Override
        public void asProductions(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar,
                Set<IConstructor> done) {
            for (Type f : type) {
                f.asProductions(vf, store, grammar, done);
            }
        }

        @Override
        public boolean isRecursive() {
            return true;
        }

        @Override
        public Type randomInstance(Supplier<Type> next, TypeStore store, Random rnd) {
            return randomInstance(next, store, rnd, rnd.nextInt(5));
        }

        public Type randomInstance(Supplier<Type> next, TypeStore store, Random rnd, int arity) {
            Type[] types = new Type[arity];

            for (int i = 0; i < arity; i++) {
                types[i] = next.get();
            }

            return tf().tupleType(types);
        }
    }

    @Override
    public TypeFactory.TypeReifier getTypeReifier() {
        return new Info();
    }

    @Override
    public boolean isFixedWidth() {
        return true;
    }

    @Override
    public Type getFieldType(int i) {
        return fFieldTypes[i];
    }

    @Override
    public int getArity() {
        return fFieldTypes.length;
    }<commit_msg>fixed reification of labeled tuple types<commit_after>/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> (<EMAIL>) - initial API and implementation

 *******************************************************************************/

package io.usethesource.vallang.type;

import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;

import org.checkerframework.checker.nullness.qual.Nullable;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IListWriter;
import io.usethesource.vallang.ISetWriter;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.exceptions.FactTypeUseException;
import io.usethesource.vallang.exceptions.IllegalOperationException;

/*package*/ class TupleType extends DefaultSubtypeOfValue {
    protected final Type[] fFieldTypes; // protected access for the benefit of inner classes
    protected int fHashcode = -1;

    /**
     * Creates a tuple type with the given field types. Does not copy the array..
     */
    /*package*/ TupleType(Type[] fieldTypes) {
        fFieldTypes = fieldTypes; // fieldTypes.clone(); was safer, but it ended up being a bottleneck
    }

    public static class Info implements TypeFactory.TypeReifier {
        @Override
        public Type getSymbolConstructorType() {
            return symbols().typeSymbolConstructor("tuple", TF.listType(symbols().symbolADT()), "symbols");
        }

        @Override
        public Type fromSymbol(IConstructor symbol, TypeStore store, Function<IConstructor, Set<IConstructor>> grammar) {
            return symbols().fromSymbols((IList) symbol.get("symbols"), store, grammar);
        }

        @Override
        public IConstructor toSymbol(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar,
                Set<IConstructor> done) {
            IListWriter w = vf.listWriter();

            if (type.hasFieldNames()) {
                for (int i = 0; i < type.getArity(); i++) {
                    w.append(symbols().labelSymbol(vf, type.getFieldType(i).asSymbol(vf, store, grammar, done), type.getFieldName(i)));
                }
            }
            else {
                for (Type f : type) {
                    w.append(f.asSymbol(vf, store, grammar, done));
                }
            }

            return vf.constructor(getSymbolConstructorType(), w.done());
        }

        @Override
        public void asProductions(Type type, IValueFactory vf, TypeStore store, ISetWriter grammar,
                Set<IConstructor> done) {
            for (Type f : type) {
                f.asProductions(vf, store, grammar, done);
            }
        }

        @Override
        public boolean isRecursive() {
            return true;
        }

        @Override
        public Type randomInstance(Supplier<Type> next, TypeStore store, Random rnd) {
            return randomInstance(next, store, rnd, rnd.nextInt(5));
        }

        @SuppressWarnings("deprecation")
        public Type randomInstance(Supplier<Type> next, TypeStore store, Random rnd, int arity) {
            Type[] types = new Type[arity];

            for (int i = 0; i < arity; i++) {
                types[i] = next.get();
            }

            if (rnd.nextBoolean()) {
                return tf().tupleType(types);
            }

            String[] labels = new String[arity];
            for (int i = 0; i < arity; i++) {
                labels[i] = randomLabel(rnd);
            }

            return tf().tupleType(types, labels);
        }
    }

    @Override
    public TypeFactory.TypeReifier getTypeReifier() {
        return new Info();
    }

    @Override
    public boolean isFixedWidth() {
        return true;
    }

    @Override
    public Type getFieldType(int i) {
        return fFieldTypes[i];
    }

    @Override
    public int getArity() {
        return fFieldTypes.length;
    }
{'fraction_non_alphanumeric': 0.13573989368277908, 'fraction_numerical': 0.0029669922116454446, 'mean_word_length': 2.592362344582593, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, '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': '4330064', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2138, 'n_words': 582}