Skip to content

Instantly share code, notes, and snippets.

@kLabz
Last active February 13, 2021 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kLabz/bd71cc3566d66e9dea39933fe27b3402 to your computer and use it in GitHub Desktop.
Save kLabz/bd71cc3566d66e9dea39933fe27b3402 to your computer and use it in GitHub Desktop.
List<T> modification
class ListUtils {
public static function kvIt<T>(list:List<T>):ListKeyItemIterator<T> {
return new ListKeyItemIterator(@:privateAccess list.h);
}
}
private typedef ListNode<T> = {item:T, next:ListNode<T>};
private class ListKeyItemIterator<T> {
var idx:Int;
var head:ListNode<T>;
public inline function new(head:ListNode<T>) {
this.head = head;
this.idx = 0;
}
public inline function hasNext():Bool {
return head != null;
}
public inline function next():{key:Int, value:ListNode<T>} {
var val = head.item;
var ret = {value: head, key: idx++};
head = head.next;
return ret;
}
}
using ListUtils;
class Test {
static function main() {
var list = new List<Int>();
for (i in 0...10) list.add(i);
for (i => item in list.kvIt()) {
if (i == 4) {
item.item = 42;
break;
}
}
for (i in list) trace(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment