-
-
Save kLabz/bd71cc3566d66e9dea39933fe27b3402 to your computer and use it in GitHub Desktop.
List<T> modification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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