<commit_before>///<reference path="../reference.ts" />

module Plottable {
  interface IPerspective {
    dataSource: DataSource;
    accessor: IAccessor;
  }
  export class Scale extends Broadcaster {
    public _d3Scale: D3.Scale.Scale;
    public _autoDomain = true;
    private rendererID2Perspective: {[rendererID: string]: IPerspective} = {};
    private dataSourceID2ReferenceCount: {[datasourceID: string]: number} = {};
    private isAutorangeUpToDate = false;
    public _autoNice = false;
    public _autoPad  = false;
    /**
     * Creates a new Scale.
     *
     * @constructor
     * @param {D3.Scale.Scale} scale The D3 scale backing the Scale.
     */
    constructor(scale: D3.Scale.Scale) {
      super();
      this._d3Scale = scale;
    }

    public _getCombinedExtent(): any[] {
      var perspectives: IPerspective[] = d3.values(this.rendererID2Perspective);
      var extents = perspectives.map((p) => {
        var source = p.dataSource;
        var accessor = p.accessor;
        return source._getExtent(accessor);
      });
      return extents;
    }

    public autorangeDomain() {
        this.isAutorangeUpToDate = true;
        this.domain(this._getCombinedExtent());
        return this;
    }

    public _addPerspective(rendererIDAttr: string, dataSource: DataSource, accessor: IAccessor) {
      if (this.rendererID2Perspective[rendererIDAttr] != null) {
        this._removePerspective(rendererIDAttr);
      }
      this.rendererID2Perspective[rendererIDAttr] = {dataSource: dataSource, accessor: accessor};
      var dataSourceID = dataSource._plottableID;
      var currentRefCount = this.dataSourceID2ReferenceCount[dataSourceID];
      if (currentRefCount == null) {
        currentRefCount = 0;
      }
      if (currentRefCount === 0 ) {
        dataSource.registerListener(this, () => this.isAutorangeUpToDate = false );
      }
      this.dataSourceID2ReferenceCount[dataSourceID] = currentRefCount + 1;
      this.isAutorangeUpToDate = false;
      return this;
    }

    public _removePerspective(rendererIDAttr: string) {
      var dataSourceID = this.rendererID2Perspective[rendererIDAttr].dataSource._plottableID;
      this.dataSourceID2ReferenceCount[dataSourceID]--;
      if (this.dataSourceID2ReferenceCount[dataSourceID] === 0) {
        this.rendererID2Perspective[rendererIDAttr].dataSource.deregisterListener(this);
      }
      delete this.rendererID2Perspective[rendererIDAttr];
      this.isAutorangeUpToDate = false;
      return this;
    }

    /**
     * Returns the range value corresponding to a given domain value.
     *
     * @param value {any} A domain value to be scaled.
     * @returns {any} The range value corresponding to the supplied domain value.
     */
    public scale(value: any) {
      if (!this.isAutorangeUpToDate && this._autoDomain) {
        this.autorangeDomain();
      }
      return this._d3Scale(value);
    }

    /**
     * Retrieves the current domain, or sets the Scale's domain to the specified values.
     *
     * @param {any[]} [values] The new value for the domain.
     * @returns {any[]|Scale} The current domain, or the calling Scale (if values is supplied).
     */
    public domain(): any[];
    public domain(values: any[]): Scale;
    public domain(values?: any[]): any {
      if (values == null) {
        if (!this.isAutorangeUpToDate && this._autoDomain) {
          this.autorangeDomain();<commit_msg>Modify scale to use the IDCounter rather than local refernece counting logic<commit_after>///<reference path="../reference.ts" />

module Plottable {
  interface IPerspective {
    dataSource: DataSource;
    accessor: IAccessor;
  }
  export class Scale extends Broadcaster {
    public _d3Scale: D3.Scale.Scale;
    public _autoDomain = true;
    private rendererID2Perspective: {[rendererID: string]: IPerspective} = {};
    private dataSourceReferenceCounter = new Utils.IDCounter();
    private isAutorangeUpToDate = false;
    public _autoNice = false;
    public _autoPad  = false;
    /**
     * Creates a new Scale.
     *
     * @constructor
     * @param {D3.Scale.Scale} scale The D3 scale backing the Scale.
     */
    constructor(scale: D3.Scale.Scale) {
      super();
      this._d3Scale = scale;
    }

    public _getCombinedExtent(): any[] {
      var perspectives: IPerspective[] = d3.values(this.rendererID2Perspective);
      var extents = perspectives.map((p) => {
        var source = p.dataSource;
        var accessor = p.accessor;
        return source._getExtent(accessor);
      });
      return extents;
    }

    /**
    * Modify the domain on the scale so that it includes the extent of all perspectives it depends on.
    * Extent: The (min, max) pair for a QuantitiativeScale, all covered strings for an OrdinalScale.
    * Perspective: A combination of a DataSource and an Accessor that represents a view in to the data.
    */
    public autorangeDomain() {
        this.isAutorangeUpToDate = true;
        this.domain(this._getCombinedExtent());
        return this;
    }

    public _addPerspective(rendererIDAttr: string, dataSource: DataSource, accessor: IAccessor) {
      if (this.rendererID2Perspective[rendererIDAttr] != null) {
        this._removePerspective(rendererIDAttr);
      }
      this.rendererID2Perspective[rendererIDAttr] = {dataSource: dataSource, accessor: accessor};

      var dataSourceID = dataSource._plottableID;
      if (this.dataSourceReferenceCounter.increment(dataSourceID) === 1 ) {
        dataSource.registerListener(this, () => this.isAutorangeUpToDate = false );
      }

      this.isAutorangeUpToDate = false;
      return this;
    }

    public _removePerspective(rendererIDAttr: string) {
      var dataSource = this.rendererID2Perspective[rendererIDAttr].dataSource;
      var dataSourceID = dataSource._plottableID;
      if (this.dataSourceReferenceCounter.decrement(dataSourceID) === 0) {
        dataSource.deregisterListener(this);
      }

      delete this.rendererID2Perspective[rendererIDAttr];
      this.isAutorangeUpToDate = false;
      return this;
    }

    /**
     * Returns the range value corresponding to a given domain value.
     *
     * @param value {any} A domain value to be scaled.
     * @returns {any} The range value corresponding to the supplied domain value.
     */
    public scale(value: any) {
      if (!this.isAutorangeUpToDate && this._autoDomain) {
        this.autorangeDomain();
      }
      return this._d3Scale(value);
    }

    /**
     * Retrieves the current domain, or sets the Scale's domain to the specified values.
     *
     * @param {any[]} [values] The new value for the domain.
     * @returns {any[]|Scale} The current domain, or the calling Scale (if values is supplied).
     */
    public domain(): any[];
    public domain(values: any[]): Scale;
    public domain(values?: any[]): any {
      if (values == null) {
        if (!this.isAutorangeUpToDate && this._autoDomain) {
          this.autorangeDomain();
{'fraction_non_alphanumeric': 0.10408017179670723, 'fraction_numerical': 0.00572655690765927, 'mean_word_length': 2.9513574660633486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3995211', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2050, 'n_words': 519}