package org.metaborg.meta.lang.dynsem.interpreter.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.github.krukow.clj_ds.PersistentMap;
import com.github.krukow.clj_ds.TransientMap;
import com.github.krukow.clj_lang.PersistentHashMap;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

public class MapUtils {

	@TruffleBoundary
	public static <K, V> PersistentMap<K, V> plus(PersistentMap<K, V> one, PersistentMap<K, V> other) {
		// PersistentMap<K, V> nmap = one;
		// for (K key : other.keySet()) {
		// nmap = nmap.plus(key, other.get(key));
		// }
		// return nmap;

		TransientMap<K, V> tmp = ((PersistentHashMap) one).asTransient();

		Iterator<Map.Entry<K, V>> it = (Iterator<Entry<K, V>>) ((PersistentHashMap) other).entrySet();
		while (it.hasNext()) {
			Entry<K, V> tuple = it.next();
			tmp.plus(tuple.getKey(), tuple.getValue());
		}

		return tmp.persist();
	}

	@TruffleBoundary
	public static <K, V> PersistentMap<K, V> add(PersistentMap<K, V> map, K key, V val) {

		return map.plus(key, val);

	}

	@TruffleBoundary
	public static <K, V> V get(PersistentMap<K, V> map, K key) {
		return map.get(key);
	}

}

@@ -490,151 +490,8 @@
 ) %7B%0A
-%09%09// PersistentMap%3CK, V%3E nmap = one;%0A%09%09// for (K key : other.keySet()) %7B%0A%09%09// nmap = nmap.plus(key, other.get(key));%0A%09%09// %7D%0A%09%09// return nmap;%0A%0A
 %09%09Tr
@@ -650,16 +650,27 @@
 trySet()
+.iterator()
 ;%0A%09%09whil

Fix bug in map utility caused by lack of attention.