HaxeIterator.hx 614 B

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