IndexBuffer.hx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package hxd;
  2. private typedef InnerData = Array<hxd.impl.UInt16>;
  3. private class InnerIterator {
  4. var b : InnerData;
  5. var len : Int;
  6. var pos : Int;
  7. public inline function new( b : InnerData ) {
  8. this.b = b;
  9. this.len = this.b.length;
  10. this.pos = 0;
  11. }
  12. public inline function hasNext() {
  13. return pos < len;
  14. }
  15. public inline function next() : Int {
  16. return b[pos++];
  17. }
  18. }
  19. abstract IndexBuffer(InnerData) {
  20. public var length(get, never) : Int;
  21. public inline function new(length = 0) {
  22. #if js
  23. this = js.Syntax.construct(Array, length);
  24. #else
  25. this = new InnerData();
  26. if( length > 0 ) grow(length);
  27. #end
  28. }
  29. public inline function push( v : Int ) {
  30. this.push(v);
  31. }
  32. public inline function grow( v : Int ) {
  33. #if js
  34. while( this.length < v ) this.push(0);
  35. #else
  36. if( v > this.length ) this[v - 1] = 0;
  37. #end
  38. }
  39. @:arrayAccess inline function arrayRead(key:Int) : Int {
  40. return this[key];
  41. }
  42. @:arrayAccess inline function arrayWrite(key:Int, value : Int) : Int {
  43. return this[key] = value;
  44. }
  45. public inline function getNative() : InnerData {
  46. return this;
  47. }
  48. public inline function iterator() {
  49. return new InnerIterator(this);
  50. }
  51. inline function get_length() : Int {
  52. return this.length;
  53. }
  54. }