HaxeIterator.hx 585 B

1234567891011121314151617181920212223242526272829303132333435
  1. package python;
  2. import python.lib.Exceptions.StopIteration;
  3. import python.NativeIterator;
  4. class HaxeIterator<T> {
  5. var it:NativeIteratorRaw<T>;
  6. var x:Null<T> = null;
  7. var has = false;
  8. var checked = false;
  9. public function new(it:NativeIteratorRaw<T>) {
  10. this.it = it;
  11. }
  12. public inline function next():T {
  13. if (!checked) hasNext();
  14. checked = false;
  15. return x;
  16. }
  17. public function hasNext ():Bool {
  18. if (!checked) {
  19. try {
  20. x = it.__next__();
  21. has = true;
  22. } catch (s:StopIteration) {
  23. has = false;
  24. x = null;
  25. }
  26. checked = true;
  27. }
  28. return has;
  29. }
  30. }