<commit_before>#if cpp
import nme.display.BitmapData;
import nme.display.Bitmap;
import nme.display.Sprite;
import nme.display.Loader;
import nme.net.URLRequest;
import nme.events.Event;
import nme.geom.Rectangle;
import nme.geom.Point;
import nme.display.Shape;
import cpp.vm.Thread;

typedef ReloadedData = {
  var waiters: Array<ReloadedGraphic>;
  var loader: Loader;
  var url: String;
}

class ReloadedGraphic extends Bitmap {
  var url: String;
  var loader: Loader;
  var maskedShape: Shape;

  static var urlData: SuperObjectHash<String, ReloadedData> = null;

  public function new(url: String) {
  	super();
    this.url = url;

    if (urlData == null) {
      urlData = new SuperObjectHash();

      constantlyReload();
    }

    maskedShape = new Shape();
    maskedShape.graphics.beginFill(0);
    maskedShape.graphics.drawRect(0, 0, 25, 25);
    maskedShape.graphics.endFill();
    this.mask = maskedShape;

    if (urlData.exists(url)) {
      urlData.get(url).waiters.push(this);
    } else {
      loader = new Loader();
      urlData.set(url, {url: url, waiters: [this], loader: loader});
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
    }
  }

  public function setTile(tileX: Int, tileY: Int): Void {
    if (this.parent != null && this.mask.parent == null) {
      this.parent.addChild(maskedShape);
    }

    // Let the mask do the hard work for us.
    this.x = -tileX * 25;
    this.y = -tileY * 25;
  }

  // Returns true if the two BitmapDatas are equal, false otherwise.
  // BitmapData#compare exists in AS3 but not elsewhere.
  // Could be moved into some utility mixin.
  function compare(b1:BitmapData, b2:BitmapData): Bool {
#if flash
    return b1.compare(b2) == 0;
#end

    if (b1.width != b2.width || b1.height != b2.height) return false;

    for (i in 0...b1.width) {
      for (j in 0...b1.height) {
        if (b1.getPixel(i, j) != b2.getPixel(i, j)) return false;
      }
    }

    return true;
  }

  static function constantlyReload() {
    Thread.create(function() {

      for (val in urlData.values()) {
        val.loader.load(new URLRequest(val.url));
      }

      haxe.Timer.delay(function() {
        constantlyReload();
      }, 1000);
    });
  }

  function loadComplete(e:Event) {
    var loadedBitmap:Bitmap = cast(e.currentTarget.loader.content, Bitmap);
    var rect:Rectangle = new Rectangle(0, 0, loadedBitmap.width, loadedBitmap.height);
    var point:Point = new Point(0, 0);

    for (waiter in urlData.get(url).waiters) {
      if (waiter.bitmapData == null || !compare(waiter.bitmapData, loadedBitmap.bitmapData)) {
        waiter.bitmapData = loadedBitmap.bitmapData;
      }
    }
  }
}
<commit_msg>Rewrite a bit of ReloadedGraphic.<commit_after>#if cpp
import nme.display.BitmapData;
import nme.display.Bitmap;
import nme.display.Sprite;
import nme.display.Loader;
import nme.net.URLRequest;
import nme.events.Event;
import nme.geom.Rectangle;
import nme.geom.Point;
import nme.display.Shape;
import cpp.vm.Thread;

typedef ReloadedData = {
  var waiters: Array<ReloadedGraphic>;
  var loader: Loader;
  var url: String;
}

class ReloadedGraphic extends Bitmap {
  var url: String;
  var loader: Loader;
  var tileWidth: Int = 25;
  var tileHeight: Int = 25;
  var tileX: Int = 0;
  var tileY: Int = 0;
  var masterBitmapData:BitmapData;

  static var urlData: SuperObjectHash<String, ReloadedData> = null;

  public function new(url: String) {
  	super();
    this.url = url;

    if (urlData == null) {
      urlData = new SuperObjectHash();

      constantlyReload();
    }

    if (urlData.exists(url)) {
      urlData.get(url).waiters.push(this);
    } else {
      loader = new Loader();
      urlData.set(url, {url: url, waiters: [this], loader: loader});
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
    }
  }

  public function setTile(tileX: Int, tileY: Int): Void {
    this.tileX = tileX;
    this.tileY = tileY;

    if (masterBitmapData != null) {
      var region:Rectangle = new Rectangle(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight);
      bitmapData = new BitmapData(tileWidth, tileHeight);
      bitmapData.copyPixels(masterBitmapData, region, new Point(0, 0), null, null, true);
    }
  }

  // Returns true if the two BitmapDatas are equal, false otherwise.
  // BitmapData#compare exists in AS3 but not elsewhere.
  // Could be moved into some utility mixin.
  function compare(b1:BitmapData, b2:BitmapData): Bool {
#if flash
    return b1.compare(b2) == 0;
#end

    if (b1.width != b2.width || b1.height != b2.height) return false;

    for (i in 0...b1.width) {
      for (j in 0...b1.height) {
        if (b1.getPixel(i, j) != b2.getPixel(i, j)) return false;
      }
    }

    return true;
  }

  static function constantlyReload() {
    Thread.create(function() {

      for (val in urlData.values()) {
        val.loader.load(new URLRequest(val.url));
      }

      haxe.Timer.delay(function() {
        constantlyReload();
      }, 1000);
    });
  }

  function loadComplete(e:Event) {
    var loadedBitmap:Bitmap = cast(e.currentTarget.loader.content, Bitmap);
    var rect:Rectangle = new Rectangle(0, 0, loadedBitmap.width, loadedBitmap.height);
    var point:Point = new Point(0, 0);

    for (waiter in urlData.get(url).waiters) {
      if (waiter.bitmapData == null || !compare(waiter.bitmapData, loadedBitmap.bitmapData)) {
        waiter.masterBitmapData = loadedBitmap.bitmapData;
        waiter.setTile(waiter.tileX, waiter.tileY);
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.128736046092906, 'fraction_numerical': 0.012063377745768815, 'mean_word_length': 3.090574374079529, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3179476', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1869, 'n_words': 459}