Skip to content

Instantly share code, notes, and snippets.

@kLabz
Last active May 6, 2021 08:27
Show Gist options
  • Select an option

  • Save kLabz/8b13c92b97c88c7d87fdf6ddad2b2012 to your computer and use it in GitHub Desktop.

Select an option

Save kLabz/8b13c92b97c88c7d87fdf6ddad2b2012 to your computer and use it in GitHub Desktop.
Haxe C# dictionary iteration
import cs.system.collections.generic.IEnumerator_1;
import cs.system.collections.generic.KeyValuePair_2;
import cs.system.collections.generic.Dictionary_2;
using Main;
@:nativeGen class Main {
public static function main() {
var dict = new Dictionary_2<String, String>();
dict.Add('hello', 'world');
dict.Add('foo', 'foobar');
dict.Add('bar', 'bar');
for (k => v in dict) trace(k, v);
for (v in dict) trace(v);
for (k in dict.keys()) trace(k);
}
}
class DictionaryHelper {
public static inline function keys<K, V>(
dict:Dictionary_2<K, V>
):Iterator<K> {
return new EnumeratorIterator<K>(dict.Keys.GetEnumerator());
}
public static inline function iterator<K, V>(
dict:Dictionary_2<K, V>
):Iterator<V> {
return new EnumeratorIterator<V>(dict.Values.GetEnumerator());
}
public static inline function keyValueIterator<K, V>(
dict:Dictionary_2<K, V>
):KeyValuePairIterator<K, V> {
return new KeyValuePairIterator<K, V>(dict.GetEnumerator());
}
}
class KeyValuePairIterator<K, V> {
final enumerator:IEnumerator_1<KeyValuePair_2<K, V>>;
var _hasNext:Bool;
public inline function new(enumerator:IEnumerator_1<KeyValuePair_2<K, V>>) {
this.enumerator = enumerator;
moveNext();
}
inline function moveNext():Void _hasNext = enumerator.MoveNext();
public inline function hasNext():Bool return _hasNext;
public inline function next():{key:K, value:V} {
var current = enumerator.Current;
moveNext();
return {key: current.Key, value: current.Value};
}
}
class EnumeratorIterator<T> {
final enumerator:IEnumerator_1<T>;
var _hasNext:Bool;
public inline function new(enumerator:IEnumerator_1<T>) {
this.enumerator = enumerator;
moveNext();
}
inline function moveNext():Void _hasNext = enumerator.MoveNext();
public inline function hasNext():Bool return _hasNext;
public inline function next():T {
var current = enumerator.Current;
moveNext();
return current;
}
}
// Generated by Haxe 4.2.1+bf9ff69
#pragma warning disable 109, 114, 219, 429, 168, 162
public class EntryPoint__Main {
public static void Main() {
global::cs.Boot.init();
global::Main.main();
}
}
public class Main {
public Main() {
}
public static void main() {
unchecked {
global::System.Collections.Generic.Dictionary<string, string> dict = new global::System.Collections.Generic.Dictionary<string, string>();
dict.Add(((string) ("hello") ), ((string) ("world") ));
dict.Add(((string) ("foo") ), ((string) ("foobar") ));
dict.Add(((string) ("bar") ), ((string) ("bar") ));
{
global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, string>> _g_enumerator = ((global::System.Collections.Generic.IEnumerator<global::System.Collections.Generic.KeyValuePair<string, string>>) (dict.GetEnumerator()) );
bool _g__hasNext = ( _g_enumerator as global::System.Collections.IEnumerator ).MoveNext();
while (_g__hasNext) {
global::System.Collections.Generic.KeyValuePair<string, string> current = ((global::System.Collections.Generic.KeyValuePair<string, string>) (( _g_enumerator as global::System.Collections.IEnumerator ).Current) );
_g__hasNext = ( _g_enumerator as global::System.Collections.IEnumerator ).MoveNext();
string _g1_key = current.Key;
string _g1_value = current.Value;
string k = _g1_key;
string v = _g1_value;
global::haxe.Log.trace.__hx_invoke2_o(default(double), k, default(double), new global::haxe.lang.DynamicObject(new int[]{302979532, 1547539107, 1648581351, 1830310359}, new object[]{"main", "Main", "Main.hx", new global::Array<object>(new object[]{v})}, new int[]{1981972957}, new double[]{((double) (14) )}));
}
}
{
global::System.Collections.Generic.IEnumerator<string> _g_enumerator1 = ((global::System.Collections.Generic.IEnumerator<string>) (dict.Values.GetEnumerator()) );
bool _g__hasNext1 = ( _g_enumerator1 as global::System.Collections.IEnumerator ).MoveNext();
while (_g__hasNext1) {
string current1 = global::haxe.lang.Runtime.toString(( _g_enumerator1 as global::System.Collections.IEnumerator ).Current);
_g__hasNext1 = ( _g_enumerator1 as global::System.Collections.IEnumerator ).MoveNext();
string v1 = current1;
global::haxe.Log.trace.__hx_invoke2_o(default(double), v1, default(double), new global::haxe.lang.DynamicObject(new int[]{302979532, 1547539107, 1648581351}, new object[]{"main", "Main", "Main.hx"}, new int[]{1981972957}, new double[]{((double) (15) )}));
}
}
{
global::System.Collections.Generic.IEnumerator<string> _g1_enumerator = ((global::System.Collections.Generic.IEnumerator<string>) (dict.Keys.GetEnumerator()) );
bool _g1__hasNext = ( _g1_enumerator as global::System.Collections.IEnumerator ).MoveNext();
while (_g1__hasNext) {
string current2 = global::haxe.lang.Runtime.toString(( _g1_enumerator as global::System.Collections.IEnumerator ).Current);
_g1__hasNext = ( _g1_enumerator as global::System.Collections.IEnumerator ).MoveNext();
string k1 = current2;
global::haxe.Log.trace.__hx_invoke2_o(default(double), k1, default(double), new global::haxe.lang.DynamicObject(new int[]{302979532, 1547539107, 1648581351}, new object[]{"main", "Main", "Main.hx"}, new int[]{1981972957}, new double[]{((double) (16) )}));
}
}
}
}
}
Main.hx:14: hello, world
Main.hx:14: foo, foobar
Main.hx:14: bar, bar
Main.hx:15: world
Main.hx:15: foobar
Main.hx:15: bar
Main.hx:16: hello
Main.hx:16: foo
Main.hx:16: bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment