<commit_before>/**
 * This file is part of Erjang - A JVM-based Erlang VM
 *
 * Copyright (c) 2009 by Trifork
 *
 * 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 erjang.m.ets;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.trifork.clj_ds.IMapEntry;
import com.trifork.clj_ds.IPersistentCollection;
import com.trifork.clj_ds.IPersistentMap;
import com.trifork.clj_ds.IPersistentSet;
import com.trifork.clj_ds.ISeq;
import com.trifork.clj_ds.PersistentHashSet;
import com.trifork.clj_ds.PersistentHashMap;
import com.trifork.clj_ds.Seqable;
import erjang.EAtom;
import erjang.ECons;
import erjang.EInteger;
import erjang.EInternalPID;
import erjang.EList;
import erjang.EObject;
import erjang.EProc;
import erjang.EPseudoTerm;
import erjang.ERT;
import erjang.ESeq;
import erjang.ETuple;
import erjang.ETuple2;
import erjang.NotImplemented;

/**
 * 
 */
public class ETableBag extends ETable {

	/** holds an Integer with the bag size */
	AtomicInteger sizeRef;
	
	ETableBag(EProc owner, 
			  EAtom type, 
			  EInteger tid, 
			  EAtom aname, 
			  EAtom access,
			  int keypos, 
			  boolean writeConcurrency,
			  boolean isNamed, 
			  EInternalPID heirPid, 
			  EObject heirData)
	{
		super(owner, type, tid, aname, access, keypos, isNamed, heirPid,
			  heirData, PersistentHashMap.EMPTY);
		try {
			sizeRef = new AtomicInteger(0);
		} catch (Exception e) {
			throw new Error(e);
		}
	}
	
	//
	// A bag is implemented as a map (of keys), that refer to 
	// a collection of values with that key.  This function creates
	// an empty one of those nested collections depending on
	// the ets type.
	//
	
	IPersistentCollection empty() {
		return type==Native.am_bag 

			// normal bag; there can be several elements with the
			// same key, but no duplicates (i.e. == each other)
			// maybe use EPersistentInsertionOrderedSet.EMPTY
			? PersistentHashSet.EMPTY 
					
			// duplicate bag, allows duplicate elements with the
			// same key; just hold values as a list
			: PersistentBag.EMPTY;
	}

	@Override
	protected void insert_many(final ESeq values) {
		in_tx(new WithMap<Object>() {
			@Override
			protected Object run(IPersistentMap map) {
				int count = 0;
				for (ESeq seq = values; !seq.isNil(); seq = seq.tail()) {
					ETuple value = seq.head().testTuple();
					if (value == null) throw ERT.badarg(values);
					EObject key = get_key(value);
					IPersistentCollection c =
						(IPersistentCollection) map.valAt(key, empty());

					// Insert element - noting whether the map grows
					int sizeBefore = c.count();
					c = c.cons(value);
					int sizeAfter = c.count();<commit_msg>Use insertion-ordered set for [bag].<commit_after>/**
 * This file is part of Erjang - A JVM-based Erlang VM
 *
 * Copyright (c) 2009 by Trifork
 *
 * 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 erjang.m.ets;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.trifork.clj_ds.IMapEntry;
import com.trifork.clj_ds.IPersistentCollection;
import com.trifork.clj_ds.IPersistentMap;
import com.trifork.clj_ds.IPersistentSet;
import com.trifork.clj_ds.ISeq;
import com.trifork.clj_ds.PersistentHashSet;
import com.trifork.clj_ds.PersistentHashMap;
import com.trifork.clj_ds.Seqable;
import erjang.EAtom;
import erjang.ECons;
import erjang.EInteger;
import erjang.EInternalPID;
import erjang.EList;
import erjang.EObject;
import erjang.EProc;
import erjang.EPseudoTerm;
import erjang.ERT;
import erjang.ESeq;
import erjang.ETuple;
import erjang.ETuple2;
import erjang.NotImplemented;

/**
 * 
 */
public class ETableBag extends ETable {

	/** holds an Integer with the bag size */
	AtomicInteger sizeRef;
	
	ETableBag(EProc owner, 
			  EAtom type, 
			  EInteger tid, 
			  EAtom aname, 
			  EAtom access,
			  int keypos, 
			  boolean writeConcurrency,
			  boolean isNamed, 
			  EInternalPID heirPid, 
			  EObject heirData)
	{
		super(owner, type, tid, aname, access, keypos, isNamed, heirPid,
			  heirData, PersistentHashMap.EMPTY);
		try {
			sizeRef = new AtomicInteger(0);
		} catch (Exception e) {
			throw new Error(e);
		}
	}
	
	//
	// A bag is implemented as a map (of keys), that refer to 
	// a collection of values with that key.  This function creates
	// an empty one of those nested collections depending on
	// the ets type.
	//
	
	IPersistentCollection empty() {
		return type==Native.am_bag 

			// normal bag; there can be several elements with the
			// same key, but no duplicates (i.e. == each other)
			? EPersistentInsertionOrderedSet.EMPTY 
					
			// duplicate bag, allows duplicate elements with the
			// same key; just hold values as a list
			: PersistentBag.EMPTY;
	}

	@Override
	protected void insert_many(final ESeq values) {
		in_tx(new WithMap<Object>() {
			@Override
			protected Object run(IPersistentMap map) {
				int count = 0;
				for (ESeq seq = values; !seq.isNil(); seq = seq.tail()) {
					ETuple value = seq.head().testTuple();
					if (value == null) throw ERT.badarg(values);
					EObject key = get_key(value);
					IPersistentCollection c =
						(IPersistentCollection) map.valAt(key, empty());

					// Insert element - noting whether the map grows
					int sizeBefore = c.count();
					c = c.cons(value);
					int sizeAfter = c.count();
{'fraction_non_alphanumeric': 0.09551718675748455, 'fraction_numerical': 0.003484872485347695, 'mean_word_length': 3.430877192982456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3508130', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2050, 'n_words': 650}