<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 org.eclipse.imp.pdb.facts.impl.hash;

import java.util.Iterator;

import org.eclipse.imp.pdb.facts.ITuple;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.impl.Value;
import org.eclipse.imp.pdb.facts.type.TupleType;
import org.eclipse.imp.pdb.facts.type.TypeFactory;
import org.eclipse.imp.pdb.facts.visitors.IValueVisitor;
import org.eclipse.imp.pdb.facts.visitors.VisitorException;

class Tuple extends Value implements ITuple {
    protected final IValue[] fElements;

    /*package*/ Tuple(IValue... elements) {
	super(TypeFactory.getInstance().tupleType(elements));
		this.fElements= elements;
    }
    
    private Tuple(Tuple other) {
    	super(other);
    	fElements = other.fElements;
    }

	public int arity() {
        return fElements.length;
    }

    public IValue get(int i) {
        return fElements[i];
    }
    
    public IValue get(String label) {
		return fElements[((TupleType) fType).getFieldIndex(label)];
	}


    public Iterator<IValue> iterator() {
        return new Iterator<IValue>() {
            private int count= 0;

            public boolean hasNext() {
                return count < arity();
            }

            public IValue next() {
                return get(count++);
            }

            public void remove() {
                throw new UnsupportedOperationException("Can not remove elements from a tuple");
            }
        };
    }

    public String toString() {
        StringBuffer b= new StringBuffer();
        b.append('<');
        for(Object o: this) {
            b.append(o.toString());
            b.append(',');
        }
        b.deleteCharAt(b.length() - 1);
        b.append('>');
        return b.toString();
    }

    @Override
    public boolean equals(Object o) {
        ITuple peer= (ITuple) o;
        int arity= arity();
        if (arity() != peer.arity()) {
            return false;
        }
        for(int i= 0; i < arity; i++) {
            if (!get(i).equals(peer.get(i))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        return 1;
    }
    
    public <T> T accept(IValueVisitor<T> v) throws VisitorException {
    	return v.visitTuple(this);
    }
    
    @Override<commit_msg>fixed implementation of equals<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 org.eclipse.imp.pdb.facts.impl.hash;

import java.util.Iterator;

import org.eclipse.imp.pdb.facts.ITuple;
import org.eclipse.imp.pdb.facts.IValue;
import org.eclipse.imp.pdb.facts.impl.Value;
import org.eclipse.imp.pdb.facts.type.TupleType;
import org.eclipse.imp.pdb.facts.type.TypeFactory;
import org.eclipse.imp.pdb.facts.visitors.IValueVisitor;
import org.eclipse.imp.pdb.facts.visitors.VisitorException;

class Tuple extends Value implements ITuple {
    protected final IValue[] fElements;

    /*package*/ Tuple(IValue... elements) {
	super(TypeFactory.getInstance().tupleType(elements));
		this.fElements= elements;
    }
    
    private Tuple(Tuple other) {
    	super(other);
    	fElements = other.fElements;
    }

	public int arity() {
        return fElements.length;
    }

    public IValue get(int i) {
        return fElements[i];
    }
    
    public IValue get(String label) {
		return fElements[((TupleType) fType).getFieldIndex(label)];
	}


    public Iterator<IValue> iterator() {
        return new Iterator<IValue>() {
            private int count= 0;

            public boolean hasNext() {
                return count < arity();
            }

            public IValue next() {
                return get(count++);
            }

            public void remove() {
                throw new UnsupportedOperationException("Can not remove elements from a tuple");
            }
        };
    }

    public String toString() {
        StringBuffer b= new StringBuffer();
        b.append('<');
        for(Object o: this) {
            b.append(o.toString());
            b.append(',');
        }
        b.deleteCharAt(b.length() - 1);
        b.append('>');
        return b.toString();
    }

    @Override
    public boolean equals(Object o) {
    	if (o instanceof ITuple) {
    		ITuple peer= (ITuple) o;
    		int arity= arity();
    		if (arity() != peer.arity()) {
    			return false;
    		}
    		for(int i= 0; i < arity; i++) {
    			if (!get(i).equals(peer.get(i))) {
    				return false;
    			}
    		}
    		return true;
    	}
    	return false;
    }

    @Override
    public int hashCode() {
        return 1;
    }
    
    public <T> T accept(IValueVisitor<T> v) throws VisitorException {
    	return v.visitTuple(this);
    }
    
    @Override
{'fraction_non_alphanumeric': 0.1684192078513845, 'fraction_numerical': 0.004206098843322818, 'mean_word_length': 2.405131264916468, 'pattern_counts': {'":': 0, '<': 21, '<?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': '3865266', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1628, 'n_words': 402}