<commit_before>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.core.optimizer

import com.kotlinnlp.simplednn.core.arrays.UpdatableArray
import com.kotlinnlp.simplednn.simplemath.ndarray.sparse.SparseNDArray
import java.io.Serializable

/**
 * A container of iterable parameters as [UpdatableArray]s, with some utilities methods to assign and copy them.
 */
abstract class IterableParams<SelfType: IterableParams<SelfType>>
  : Serializable, Iterable<UpdatableArray<*>> {

  companion object {

    /**
     * Private val used to serialize the class (needed by Serializable)
     */
    @Suppress("unused")
    private const val serialVersionUID: Long = 1L
  }

  /**
   * The iterator inner class which iterates over all the parameters of all the layers
   */
  private inner class ParamsIterator: Iterator<UpdatableArray<*>> {

    /**
     * The index of the next parameter.
     */
    private var nextParamIndex: Int = 0

    /**
     * The hasNext() method of the Iterator.
     */
    override fun hasNext(): Boolean = this.nextParamIndex < this@IterableParams.paramsList.size

    /**
     * The next() method of the Iterator.
     */
    override fun next(): UpdatableArray<*> = this@IterableParams.paramsList[this.nextParamIndex++]
  }

  /**
   * The list of all parameters.
   */
  abstract val paramsList: List<UpdatableArray<*>>

  /**
   * The amount of parameters into this [IterableParams].
   */
  val size: Int get() = this.paramsList.size

  /**
   * @param i the index of a parameter
   *
   * @return the parameter at the given index
   */
  operator fun get(i: Int): UpdatableArray<*> = this.paramsList[i]

  /**
   * The iterator to use to iterate over all the parameters of all the layers
   *
   * @return the iterator of all the parameters
   */
  override fun iterator(): Iterator<UpdatableArray<*>> = this.ParamsIterator()

  /**
   * Assign the values of each parameter of [x] to the parameters of this [IterableParams].
   *
   * @param x the [IterableParams] to assign to this
   */
  fun assignValues(x: SelfType) {

    this.zip(x).forEach {
      it.first.values.assignValues(it.second.values)
    }
  }

  /**
   * Sum the values of each parameter of [x] to the parameters of this [IterableParams].
   *
   * @param x the [IterableParams] to add to this
   */
  fun assignSum(x: SelfType) {

    this.zip(x).forEach {

      val first = it.first.values
      val second = it.second.values

      if (first is SparseNDArray && second is SparseNDArray) {
        first.assignSumMerging(second)
      } else {
        first.assignSum(second)
      }
    }
  }

  /**
   * Divide the values of each parameter by [n].
   *
   * @param n an integer number
   */
  fun assignDiv(n: Int) {

    if (n > 1) {<commit_msg>Replace the UpdatableArray with the ParamsArray<commit_after>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.core.optimizer

import com.kotlinnlp.simplednn.core.arrays.ParamsArray
import java.io.Serializable

/**
 * A container of iterable parameters as [ParamsArray]s, with some utilities methods to assign and copy them.
 */
abstract class IterableParams<SelfType: IterableParams<SelfType>>
  : Serializable, Iterable<ParamsArray> {

  companion object {

    /**
     * Private val used to serialize the class (needed by Serializable)
     */
    @Suppress("unused")
    private const val serialVersionUID: Long = 1L
  }

  /**
   * The iterator inner class which iterates over all the parameters of all the layers
   */
  private inner class ParamsIterator: Iterator<ParamsArray> {

    /**
     * The index of the next parameter.
     */
    private var nextParamIndex: Int = 0

    /**
     * The hasNext() method of the Iterator.
     */
    override fun hasNext(): Boolean = this.nextParamIndex < this@IterableParams.paramsList.size

    /**
     * The next() method of the Iterator.
     */
    override fun next(): ParamsArray = this@IterableParams.paramsList[this.nextParamIndex++]
  }

  /**
   * The list of all parameters.
   */
  abstract val paramsList: List<ParamsArray>

  /**
   * The amount of parameters into this [IterableParams].
   */
  val size: Int get() = this.paramsList.size

  /**
   * @param i the index of a parameter
   *
   * @return the parameter at the given index
   */
  operator fun get(i: Int): ParamsArray = this.paramsList[i]

  /**
   * The iterator to use to iterate over all the parameters of all the layers
   *
   * @return the iterator of all the parameters
   */
  override fun iterator(): Iterator<ParamsArray> = this.ParamsIterator()

  /**
   * Assign the values of each parameter of [x] to the parameters of this [IterableParams].
   *
   * @param x the [IterableParams] to assign to this
   */
  fun assignValues(x: SelfType) {

    this.zip(x).forEach {
      it.first.values.assignValues(it.second.values)
    }
  }

  /**
   * Sum the values of each parameter of [x] to the parameters of this [IterableParams].
   *
   * @param x the [IterableParams] to add to this
   */
  fun assignSum(x: SelfType) {

    this.zip(x).forEach {

      val first = it.first.values
      val second = it.second.values

      first.assignSum(second)
    }
  }

  /**
   * Divide the values of each parameter by [n].
   *
   * @param n an integer number
   */
  fun assignDiv(n: Int) {

    if (n > 1) {
{'fraction_non_alphanumeric': 0.12415994623655914, 'fraction_numerical': 0.003696236559139785, 'mean_word_length': 2.63209273947529, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '3142207', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1751, 'n_words': 594}