<commit_before>/*******************************************************************************
* Copyright (c) 2009 Centrum Wiskunde en Informatica (CWI)
* 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> - interfaces and implementation
*******************************************************************************/
package org.rascalmpl.value.impl.fast;

import java.util.Iterator;

import org.rascalmpl.value.IList;
import org.rascalmpl.value.IListWriter;
import org.rascalmpl.value.IValue;
import org.rascalmpl.value.impl.util.collections.ShareableValuesList;
import org.rascalmpl.value.type.Type;
import org.rascalmpl.value.type.TypeFactory;

// TODO Add checking.
/**
 * Implementation of IListWriter.
 * 
 * @author <NAME>
 */
/*package*/ class ListWriter implements IListWriter{
	protected Type elementType;
	protected final boolean inferred;
	
	protected final ShareableValuesList data;
	
	protected IList constructedList;
	
	/*package*/ ListWriter(Type elementType){
		super();
		
		this.elementType = elementType;
		this.inferred = false;
		
		data = new ShareableValuesList();
		
		constructedList = null;
	}
	
	/*package*/ ListWriter(){
		super();
		
		this.elementType = TypeFactory.getInstance().voidType();
		this.inferred = true;
		data = new ShareableValuesList();
		
		constructedList = null;
	}
	
	/*package*/ ListWriter(Type elementType, ShareableValuesList data){
		super();
		
		this.elementType = elementType;
		this.inferred = false;
		this.data = data;
		
		constructedList = null;
	}
	
	public void append(IValue element){
		checkMutation();
		
		updateType(element);
		data.append(element);
	}
	
	private void updateType(IValue element) {
		if (inferred) {
			elementType = elementType.lub(element.getType());
		}
	}

	public void append(IValue... elems){
		checkMutation();
		
		for(IValue elem : elems){
			updateType(elem);
			data.append(elem);
		}
	}
	
	public void appendAll(Iterable<? extends IValue> collection){
		checkMutation();
		
		Iterator<? extends IValue> collectionIterator = collection.iterator();
		while(collectionIterator.hasNext()){
			IValue next = collectionIterator.next();
			updateType(next);
			data.append(next);
		}
	}
	
	public void insert(IValue elem){
		checkMutation();
		updateType(elem);
		data.insert(elem);
	}
	
	public void insert(IValue... elements){<commit_msg>Use a faster path for array of IValues being inserted<commit_after>/*******************************************************************************
* Copyright (c) 2009 Centrum Wiskunde en Informatica (CWI)
* 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> - interfaces and implementation
*******************************************************************************/
package org.rascalmpl.value.impl.fast;

import java.util.Iterator;

import org.rascalmpl.value.IList;
import org.rascalmpl.value.IListWriter;
import org.rascalmpl.value.IValue;
import org.rascalmpl.value.impl.util.collections.ShareableValuesList;
import org.rascalmpl.value.type.Type;
import org.rascalmpl.value.type.TypeFactory;

// TODO Add checking.
/**
 * Implementation of IListWriter.
 * 
 * @author <NAME>
 */
/*package*/ class ListWriter implements IListWriter{
	protected Type elementType;
	protected final boolean inferred;
	
	protected final ShareableValuesList data;
	
	protected IList constructedList;
	
	/*package*/ ListWriter(Type elementType){
		super();
		
		this.elementType = elementType;
		this.inferred = false;
		
		data = new ShareableValuesList();
		
		constructedList = null;
	}
	
	/*package*/ ListWriter(){
		super();
		
		this.elementType = TypeFactory.getInstance().voidType();
		this.inferred = true;
		data = new ShareableValuesList();
		
		constructedList = null;
	}
	
	/*package*/ ListWriter(Type elementType, ShareableValuesList data){
		super();
		
		this.elementType = elementType;
		this.inferred = false;
		this.data = data;
		
		constructedList = null;
	}
	
	public void append(IValue element){
		checkMutation();
		
		updateType(element);
		data.append(element);
	}
	
	private void updateType(IValue element) {
		if (inferred) {
			elementType = elementType.lub(element.getType());
		}
	}

	public void append(IValue... elems){
		checkMutation();
		
		for(IValue elem : elems){
			updateType(elem);
		}
		data.appendAll(elems);
	}
	
	public void appendAll(Iterable<? extends IValue> collection){
		checkMutation();
		
		Iterator<? extends IValue> collectionIterator = collection.iterator();
		while(collectionIterator.hasNext()){
			IValue next = collectionIterator.next();
			updateType(next);
			data.append(next);
		}
	}
	
	public void insert(IValue elem){
		checkMutation();
		updateType(elem);
		data.insert(elem);
	}
	
	public void insert(IValue... elements){
{'fraction_non_alphanumeric': 0.16935327248009321, 'fraction_numerical': 0.003107399495047582, 'mean_word_length': 4.484558040468584, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5829582', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1456, 'n_words': 368}