<commit_before>export { Base, Root };

/**
 * A pool for recycling (re-using) objects.
 *
 * It could be used to improve performance by reducing memory re-allocation.
 *
 *
 * @member {object[]} issuedObjectArray
 *   All objects returned by .get_or_create_by() will be recorded in this array.
 *
 * @member {Set} sessionKeptObjectSet
 *   Temporary object list for speeding up searching of whether kept (i.e. not recycled) an object.
 *
 * @member {object[]} movingObjectArray
 *   Temporary object list for moving kept (i.e. not recycled) objects to the parent session.
 *
 * @member {object[]} recycledObjectArray
 *   All objects passed into .recycle() will be recorded in this array.
 *
 * @member {Set} recycledObjectSet
 *   Same content as .recycledObjectArray for avoiding recycle object duplicately.
 *
 */
let Base = ( ParentClass = Object ) => class Base extends ParentClass {

  /**
   * @param {Class} objectClass
   *   The class for all newly created objects.
   *
   * @param {function} pfn_SetAsConstructor_ReturnObject
   *   A function set contents like its constructor and return an object. Before .get_or_create_by() returns a recycled object,
   * its .pfnSetAsConstructor() method will be called to re-initilaize it. Its return value will be the final returned object.
   */
  constructor( objectClass, pfn_SetAsConstructor_ReturnObject ) {
    this.objectClass = objectClass;
    this.pfn_SetAsConstructor_ReturnObject = pfn_SetAsConstructor_ReturnObject;

    this.issuedObjectArray = new Array();
    this.sessionKeptObjectSet = new Set(); // For reducing memory re-allocation.
    this.movingObjectArray = new Array(); // For reducing memory re-allocation.

//!!! ...unfinished... (2022/06/21)
// should be a MultiLayerMap whose leaf node is an Array object.
// All objects with the same specification will be recycled into the same Array.
// This is more useful for Float32Array because of reducing more memory re-allocation.
//

    this.recycledObjectArray = new Array(); // For fetching efficiently (without creating iterator).
    this.recycledObjectSet = new Set(); // For checking object recycled multiple times.
  }

  /**
   * @return {number} Return the quantity of issued objects.
   */
  get issuedCount() {
    return this.issuedObjectArray.length;
  }

  /**
   * @return {number} Return the quantity of recycled objects.
   */
  get recycledCount() {
    return this.recycledObjectSet.length;
  }

  /**
   *
   * @return {object}
   *   An obeject which is an instance of this.ObjectClass. If it is newly created, all RestArgs will be passed into its constructor.
   * If it is re-used (i.e. from recycled) object, all RestArgs will be passed into its .pfnSetAsConstructor() method.
   */
  get_or_create_by( ...restArgs ) {
    let returnedObject;
    if ( this.recycledObjectArray.length > 0 ) {
      let candicatedObject = this.recycledObjectArray.pop();
      this.recycledObjectSet.delete( candicatedObject ); // Removed from set.
      returnedObject =this.pfn_SetAsConstructor_ReturnObject.apply( candicatedObject, restArgs );
    } else {
      returnedObject = new ( this.objectClass )( ...restArgs );
    }
    return returnedObject;
  }

  /**
   * @param {Object} objectToBeRecycled
   *   The object (which should be an instance of this.ObjectClass) to be recycled.
   *
   * @return {boolean}
   *   Return true, if the object is recycled. Return false, if the object has already been recycled.
   */
  recycle( objectToBeRecycled ) {
    if ( !this.recycledObjectSet.has( objectToBeRecycled ) ) { // Avoiding duplicately.
      this.recycledObjectSet.add( objectToBeRecycled );
      this.recycledObjectArray.push( objectToBeRecycled );
      return true;
    }
    return false;
  }

  /**
   * Discard all recycled objects. (Note: The issued objects list are not influenced.)
   */
  recycledClear() {<commit_msg>Update Pool_Base.js<commit_after>export { Base, Root };

/**
 * A pool for recycling (re-using) objects.
 *
 * It could be used to improve performance by reducing memory re-allocation.
 *
 *
 * @member {object[]} issuedObjectArray
 *   All objects returned by .get_or_create_by() will be recorded in this array.
 *
 * @member {Set} sessionKeptObjectSet
 *   Temporary object list for speeding up searching of whether kept (i.e. not recycled) an object.
 *
 * @member {object[]} movingObjectArray
 *   Temporary object list for moving kept (i.e. not recycled) objects to the parent session.
 *
 * @member {object[]} recycledObjectArray
 *   All objects passed into .recycle() will be recorded in this array.
 *
 * @member {Set} recycledObjectSet
 *   Same content as .recycledObjectArray for avoiding recycle object duplicately.
 *
 */
let Base = ( ParentClass = Object ) => class Base extends ParentClass {

  /**
   * @param {Class} objectClass
   *   The class for all newly created objects.
   *
   * @param {function} pfn_SetAsConstructor_ReturnObject
   *   A function set contents like its constructor and return an object. Before .get_or_create_by() returns a recycled object,
   * its .pfnSetAsConstructor() method will be called to re-initilaize it. Its return value will be the final returned object.
   */
  constructor( objectClass, pfn_SetAsConstructor_ReturnObject ) {
    this.objectClass = objectClass;
    this.pfn_SetAsConstructor_ReturnObject = pfn_SetAsConstructor_ReturnObject;

    this.issuedObjectArray = new Array();
    this.sessionKeptObjectSet = new Set(); // For reducing memory re-allocation.
    this.movingObjectArray = new Array(); // For reducing memory re-allocation.

//!!! ...unfinished... (2022/06/21)
// should be a MultiLayerMap whose leaf node is an Array object.
// All objects with the same specification will be recycled into the same Array.
// This is more useful for Float32Array because of reducing more memory re-allocation.
//

    this.recycledObjectArray = new Array(); // For fetching efficiently (without creating iterator).
    this.recycledObjectSet = new Set(); // For checking object recycled multiple times.
  }

  /**
   * @return {number} Return the quantity of issued objects.
   */
  get issuedCount() {
    return this.issuedObjectArray.length;
  }

  /**
   * @return {number} Return the quantity of recycled objects.
   */
  get recycledCount() {
    return this.recycledObjectSet.length;
  }

  /**
   *
   * @return {object}
   *   An obeject which is an instance of this.ObjectClass. If it is newly created, all RestArgs will be passed into its constructor.
   * If it is re-used (i.e. from recycled) object, all RestArgs will be passed into its .pfnSetAsConstructor() method.
   */
  get_or_create_by( ...restArgs ) {
    let returnedObject;
    if ( this.recycledObjectArray.length > 0 ) {
      let candicatedObject = this.recycledObjectArray.pop();
      this.recycledObjectSet.delete( candicatedObject ); // Removed from set.
      returnedObject =this.pfn_SetAsConstructor_ReturnObject.apply( candicatedObject, restArgs );
    } else {
      returnedObject = new ( this.objectClass )( ...restArgs );
    }
    return returnedObject;
  }

  /**
   * @param {Object} objectToBeRecycled
   *   The object (which should be an instance of this.ObjectClass) to be recycled.
   *
   * @return {boolean}
   *   Return true, if the object is recycled. Return false, if the object has already been recycled.
   */
  recycle( objectToBeRecycled ) {

//!!! ...unfinished... (2022/06/22)
// What if the object to be recycled is inside .issuedObjectArray ?

    if ( !this.recycledObjectSet.has( objectToBeRecycled ) ) { // Avoiding duplicately.
      this.recycledObjectSet.add( objectToBeRecycled );
      this.recycledObjectArray.push( objectToBeRecycled );
      return true;
    }
    return false;
  }

  /**
   * Discard all recycled objects. (Note: The issued objects list are not influenced.)
   */
  recycledClear() {
{'fraction_non_alphanumeric': 0.09641312643093361, 'fraction_numerical': 0.003815822945815314, 'mean_word_length': 3.3466003316749586, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2235312', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2239, 'n_words': 777}