<commit_before>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2015 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// A type that represents the difference between two ordered collection states.
@available(swift, introduced: 5.1)
public struct CollectionDifference<ChangeElement> {
  /// A type that represents a single change to a collection.
  ///
  /// The `offset` of each `insert` refers to the offset of its `element` in
  /// the final state after the difference is fully applied. The `offset` of
  /// each `remove` refers to the offset of its `element` in the original
  /// state. Non-`nil` values of `associatedWith` refer to the offset of the
  /// complementary change.
  public enum Change {
    case insert(offset: Int, element: ChangeElement, associatedWith: Int?)
    case remove(offset: Int, element: ChangeElement, associatedWith: Int?)

    // Internal common field accessors
    var offset: Int {
      get {
        switch self {
        case .insert(offset: let o, element: _, associatedWith: _):
          return o
        case .remove(offset: let o, element: _, associatedWith: _):
          return o
        }
      }
    }
    var element: ChangeElement {
      get {
        switch self {
        case .insert(offset: _, element: let e, associatedWith: _):
          return e
        case .remove(offset: _, element: let e, associatedWith: _):
          return e
        }
      }
    }
    var associatedOffset: Int? {
      get {
        switch self {
        case .insert(offset: _, element: _, associatedWith: let o):
          return o
        case .remove(offset: _, element: _, associatedWith: let o):
          return o
        }
      }
    }
  }

  // The public initializer calls this function to ensure that its parameter
  // meets the conditions set in its documentation.
  private static func validateChanges<C>(_ changes : C) -> Bool where C:Collection, C.Element == Change {
    if changes.count == 0 { return true }

    var insertAssocToOffset = Dictionary<Int,Int>()
    var removeOffsetToAssoc = Dictionary<Int,Int>()
    var insertOffset = Set<Int>()
    var removeOffset = Set<Int>()

    for c in changes {
      let offset = c.offset
      if offset < 0 { return false }

      switch c {
      case .remove(_, _, _):
        if removeOffset.contains(offset) { return false }
        removeOffset.insert(offset)
      case .insert(_, _, _):
        if insertOffset.contains(offset) { return false }
        insertOffset.insert(offset)
      }

      if let assoc = c.associatedOffset {
        if assoc < 0 { return false }
        switch c {
        case .remove(_, _, _):
          if removeOffsetToAssoc[offset] != nil { return false }
          removeOffsetToAssoc[offset] = assoc
        case .insert(_, _, _):
          if insertAssocToOffset[assoc] != nil { return false }
          insertAssocToOffset[assoc] = offset
        }
      }
    }

    return removeOffsetToAssoc == insertAssocToOffset
  }

  /// Creates an instance from a collection of changes.
  ///
  /// For clients interested in the difference between two collections, see
  /// `BidirectionalCollection.difference(from:)`.
  ///
  /// To guarantee that instances are unambiguous and safe for compatible base
  /// states, this initializer will fail unless its parameter meets to the
  /// following requirements:
  ///
  /// 1) All insertion offsets are unique
  /// 2) All removal offsets are unique
  /// 3) All offset associations between insertions and removals are symmetric
  ///
  /// - Parameter changes: A collection of changes that represent a transition
  ///   between two states.
  ///
  /// - Complexity: O(*n* * log(*n*)), where *n* is the length of the
  ///   parameter.
  public init?<C>(_ c: C) where C:Collection, C.Element == Change {
    if !CollectionDifference<ChangeElement>.validateChanges(c) {
      return nil
    }

    self.init(validatedChanges: c)
  }

  // Internal initializer for use by algorithms that cannot produce invalid
  // collections of changes. These include the Myers' diff algorithm and
  // the move inferencer.
  init<C>(validatedChanges c: C) where C:Collection, C.Element == Change {
    let changes = c.sorted { (a, b) -> Bool in<commit_msg>Header docs for internal methods validateChanges(_:) and init(validatedChanges:)<commit_after>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2015 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// A type that represents the difference between two ordered collection states.
@available(swift, introduced: 5.1)
public struct CollectionDifference<ChangeElement> {
  /// A type that represents a single change to a collection.
  ///
  /// The `offset` of each `insert` refers to the offset of its `element` in
  /// the final state after the difference is fully applied. The `offset` of
  /// each `remove` refers to the offset of its `element` in the original
  /// state. Non-`nil` values of `associatedWith` refer to the offset of the
  /// complementary change.
  public enum Change {
    case insert(offset: Int, element: ChangeElement, associatedWith: Int?)
    case remove(offset: Int, element: ChangeElement, associatedWith: Int?)

    // Internal common field accessors
    var offset: Int {
      get {
        switch self {
        case .insert(offset: let o, element: _, associatedWith: _):
          return o
        case .remove(offset: let o, element: _, associatedWith: _):
          return o
        }
      }
    }
    var element: ChangeElement {
      get {
        switch self {
        case .insert(offset: _, element: let e, associatedWith: _):
          return e
        case .remove(offset: _, element: let e, associatedWith: _):
          return e
        }
      }
    }
    var associatedOffset: Int? {
      get {
        switch self {
        case .insert(offset: _, element: _, associatedWith: let o):
          return o
        case .remove(offset: _, element: _, associatedWith: let o):
          return o
        }
      }
    }
  }

  /// The public initializer calls this function to ensure that its parameter
  /// meets the conditions set in its documentation.
  ///
  /// - Parameter changes: a collection of `CollectionDifference.Change`
  ///   instances intended to represent a valid state transition for
  ///   `CollectionDifference`.
  ///
  /// - Returns: whether the parameter meets the following criteria:
  ///
  ///   1. All insertion offsets are unique
  ///   2. All removal offsets are unique
  ///   3. All associations between insertions and removals are symmetric
  ///
  /// Complexity: O(`changes.count`)
  private static func validateChanges<C>(_ changes : C) -> Bool where C:Collection, C.Element == Change {
    if changes.count == 0 { return true }

    var insertAssocToOffset = Dictionary<Int,Int>()
    var removeOffsetToAssoc = Dictionary<Int,Int>()
    var insertOffset = Set<Int>()
    var removeOffset = Set<Int>()

    for c in changes {
      let offset = c.offset
      if offset < 0 { return false }

      switch c {
      case .remove(_, _, _):
        if removeOffset.contains(offset) { return false }
        removeOffset.insert(offset)
      case .insert(_, _, _):
        if insertOffset.contains(offset) { return false }
        insertOffset.insert(offset)
      }

      if let assoc = c.associatedOffset {
        if assoc < 0 { return false }
        switch c {
        case .remove(_, _, _):
          if removeOffsetToAssoc[offset] != nil { return false }
          removeOffsetToAssoc[offset] = assoc
        case .insert(_, _, _):
          if insertAssocToOffset[assoc] != nil { return false }
          insertAssocToOffset[assoc] = offset
        }
      }
    }

    return removeOffsetToAssoc == insertAssocToOffset
  }

  /// Creates an instance from a collection of changes.
  ///
  /// For clients interested in the difference between two collections, see
  /// `BidirectionalCollection.difference(from:)`.
  ///
  /// To guarantee that instances are unambiguous and safe for compatible base
  /// states, this initializer will fail unless its parameter meets to the
  /// following requirements:
  ///
  /// 1. All insertion offsets are unique
  /// 2. All removal offsets are unique
  /// 3. All associations between insertions and removals are symmetric
  ///
  /// - Parameter c: A collection of changes that represent a transition
  ///   between two states.
  ///
  /// - Complexity: O(*n* * log(*n*)), where *n* is the length of the
  ///   parameter.
  public init?<C>(_ c: C) where C:Collection, C.Element == Change {
    if !CollectionDifference<ChangeElement>.validateChanges(c) {
      return nil
    }

    self.init(validatedChanges: c)
  }

  /// Internal initializer for use by algorithms that cannot produce invalid
  /// collections of changes. These include the Myers' diff algorithm and
  /// the move inferencer.
  ///
  /// If parameter validity cannot be guaranteed by the caller then
  /// `CollectionDifference.init?(_:)` should be used instead.
  ///
  /// - Parameter c: A valid collection of changes that represent a transition
  ///   between two states.
  ///
  /// - Complexity: O(*n* * log(*n*)), where *n* is the length of the
  ///   parameter.
  init<C>(validatedChanges c: C) where C:Collection, C.Element == Change {
    let changes = c.sorted { (a, b) -> Bool in
{'fraction_non_alphanumeric': 0.1325585975970061, 'fraction_numerical': 0.0038408508961985423, 'mean_word_length': 2.241302266198532, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, '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': '6637931', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2613, 'n_words': 1007}