<commit_before>/*
 * Scala (https://www.scala-lang.org)
 *
 * Copyright EPFL and Lightbend, Inc.
 *
 * Licensed under Apache License 2.0
 * (http://www.apache.org/licenses/LICENSE-2.0).
 *
 * See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 */

package scala
package reflect
package internal
package util

/** A wrapper for a re-entrant, cached instance of a value of type `T`.
  *
  * Not thread safe.
  */
final class ReusableInstance[T <: AnyRef](make: () => T, enabled: Boolean) {
  private val cached = make()
  private var taken = false

  @inline def using[R](action: T => R): R =
    if (!enabled || taken) action(make())
    else try {
      taken = true
      action(cached)
    } finally taken = false<commit_msg>Reusable Instance: replace single-var with a mutable Stack.<commit_after>/*
 * Scala (https://www.scala-lang.org)
 *
 * Copyright EPFL and Lightbend, Inc.
 *
 * Licensed under Apache License 2.0
 * (http://www.apache.org/licenses/LICENSE-2.0).
 *
 * See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.
 */

package scala
package reflect
package internal
package util

import scala.collection.mutable.ArrayBuffer

/** A wrapper for a list of cached instances of a value of type `T`.
  * The wrapper is recursion-reentrant: several instances are kept, so
  * at each depth of reentrance we are reusing the instance for that.
  *
  * An instance is created upon creating this object, and more instances
  * are allocated dynamically, on demand, when reentrance occurs.
  *
  * Not thread safe.
  */
final class ReusableInstance[T <: AnyRef](make: () => T, enabled: Boolean) {
  private[this] val cached = new ArrayBuffer[T]
  cached += make()

  private var taken: Int = 0

  @inline def using[R](action: T => R): R =
    if (!enabled)
      action(make())
    else {
      if (taken == cached.size)
        cached += make()
      try {
        taken += 1
        action(cached(taken-1))
      } finally taken -= 1
    }
{'fraction_non_alphanumeric': 0.1118133203694701, 'fraction_numerical': 0.00583373845405931, 'mean_word_length': 3.149193548387097, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '1791239', 'n_tokens_mistral': 613, 'n_tokens_neox': 597, 'n_words': 227}