<commit_before>//
//  deferred.swift
//  async-deferred
//
//  Created by <NAME> on 2015-07-09.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Dispatch

/**
  The possible states of a `Deferred`.

  Must be a top-level type because Deferred is generic.
*/

public enum DeferredState: Int32 { case Waiting = 0, Executing = 1, Determined = 2 }
private let transientState = Int32.max

/**
  These errors can be thrown by a `Deferred`.

  Must be a top-level type because Deferred is generic.
*/

public enum DeferredError: ErrorType
{
  case AlreadyDetermined(String)
  case CannotDetermine(String)
}

/**
  An asynchronous computation.

  A `Deferred` starts out undetermined, in the `.Waiting` state.
  It may then enter the `.Executing` state, and will eventually become `.Determined`.
  Once it is `.Determined`, it is ready to supply a result.

  The `value` property will return the result, blocking until it becomes determined.
  If the result is ready when `value` is called, it will return immediately.

  A closure supplied to the `notify` method will be called after the `Deferred` has become determined.
*/

public class Deferred<T>
{
  private var v: T! = nil
  private let group = dispatch_group_create()

  private var currentState: Int32 = DeferredState.Waiting.rawValue

  // MARK: Initializers

  private init()
  {
    dispatch_group_enter(group)
  }

  deinit
  {
    if v == nil { dispatch_group_leave(group) }
  }

  /// Initialize to an already determined state
  ///
  /// - parameter value: the value of this `Deferred`

  public init(value: T)
  {
    v = value
    currentState = DeferredState.Determined.rawValue
  }

  /// Initialize with a computation task to be performed in the background
  ///
  /// - parameter queue: the `dispatch_queue_t` onto which the computation task should be queued
  /// - parameter task:  the computation to be performed
<commit_msg>Layout tweak<commit_after>//
//  deferred.swift
//  async-deferred
//
//  Created by <NAME> on 2015-07-09.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Dispatch

/**
  The possible states of a `Deferred`.

  Must be a top-level type because Deferred is generic.
*/

public enum DeferredState: Int32 { case Waiting = 0, Executing = 1, Determined = 2 }
private let transientState = Int32.max

/**
  These errors can be thrown by a `Deferred`.

  Must be a top-level type because Deferred is generic.
*/

public enum DeferredError: ErrorType
{
  case AlreadyDetermined(String)
  case CannotDetermine(String)
}

/**
  An asynchronous computation.

  A `Deferred` starts out undetermined, in the `.Waiting` state.
  It may then enter the `.Executing` state, and will eventually become `.Determined`.
  Once it is `.Determined`, it is ready to supply a result.

  The `value` property will return the result, blocking until it becomes determined.
  If the result is ready when `value` is called, it will return immediately.

  A closure supplied to the `notify` method will be called after the `Deferred` has become determined.
*/

public class Deferred<T>
{
  private var v: T! = nil
  private let group = dispatch_group_create()

  private var currentState: Int32 = DeferredState.Waiting.rawValue

  // MARK: Initializers

  private init()
  {
    dispatch_group_enter(group)
  }

  deinit
  {
    if v == nil
    {
      dispatch_group_leave(group)
    }
  }

  /// Initialize to an already determined state
  ///
  /// - parameter value: the value of this `Deferred`

  public init(value: T)
  {
    v = value
    currentState = DeferredState.Determined.rawValue
  }

  /// Initialize with a computation task to be performed in the background
  ///
  /// - parameter queue: the `dispatch_queue_t` onto which the computation task should be queued
  /// - parameter task:  the computation to be performed

{'fraction_non_alphanumeric': 0.09159905783826224, 'fraction_numerical': 0.010991886940591469, 'mean_word_length': 3.1543478260869566, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5498767', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1103, 'n_words': 451}