<commit_before>public struct Deque<Element> {
  private var front: List<Element> { didSet { check() } }
  private var back : List<Element> { didSet { check() } }
  private var fCount, bCount: Int
  
  public init() {
    (front, back)    = (.Nil, .Nil)
    (fCount, bCount) = (0, 0)
  }
}

extension Deque {
  private init(front: List<Element>, back: List<Element>, fCount: Int, bCount: Int) {
    (self.front, self.back)    = (front, back)
    (self.fCount, self.bCount) = (fCount, bCount)
  }
  private init(front: [Element], noReverseBack: [Element]) {
    (self.front, back) = (List(seq: front), List(seq: noReverseBack))
    (fCount, bCount)   = (front.count, noReverseBack.count)
    check()
  }
}

extension Deque {
  private mutating func check() {
    if fCount == 1 || bCount == 1 { return }
    switch (front, back) {
    case (.Nil, let .Cons(head, tail)):
      (front, back)    = (tail.reverse(), [head])
      (fCount, bCount) = (bCount - 1, 1)
    case (let .Cons(head, tail), .Nil):
      (back, front)    = (tail.reverse(), [head])
      (bCount, fCount) = (fCount - 1, 1)
    default:
      return
    }
  }
}

extension Deque {
  public mutating func prepend(with: Element) {
    ++fCount
    front = with |> front
  }
  public mutating func append(with: Element) {
    ++bCount
    back = with |> front
  }
}

public struct DequeGenerator<Element> : GeneratorType {
  private var front, back: List<Element>
  public mutating func next() -> Element? {
    switch (front, back) {
    case (let .Cons(head, tail), _):
      front = tail
      return head
    case (_, let .Cons(head, tail)):
      back = tail
      return head
    default:
      return nil
    }
  }
}

extension DequeGenerator : SequenceType {
  public func generate() -> DequeGenerator {
    return self
  }
}

extension Deque : SequenceType {
  public func generate() -> DequeGenerator<Element> {
    return DequeGenerator(front: front, back: back.reverse())
  }
}

extension Deque : CustomDebugStringConvertible {
  public var debugDescription: String {
    
    return "[\(fCount), \(bCount)]: " +
      ", ".join(front.map { String(reflecting: $0) }) + " | " +
      ", ".join(back.reverse().map { String(reflecting: $0) })
  }
}

extension Deque {
  public init(ar: [Element]) {
    fCount = ar.endIndex / 2
    front = List(seq: ar[0..<fCount])
    bCount = ar.endIndex - fCount
    back  = List(seq: ar[fCount..<ar.endIndex].reverse())
  }
}

extension Deque : ArrayLiteralConvertible {
  public init(arrayLiteral: Element...) {
    self = Deque(ar: arrayLiteral)
  }
}

extension Deque {
  public mutating func removeLast() -> Element? {
    switch back {
    case .Nil: return nil
    case let .Cons(head, tail):
      --bCount
      back = tail
      return head
    }
  }
  public mutating func removeFirst() -> Element? {
    switch front {
    case .Nil: return nil
    case let .Cons(head, tail):
      --fCount
      front = tail
      return head
    }
  }
}

extension Deque {
  public var first: Element? {
    switch front {
    case .Nil: return nil
    case .Cons(let head, _): return head
    }
  }
  public var last: Element? {
    switch back {
    case .Nil: return nil
    case .Cons(let head, _): return head
    }
  }
}

extension Deque {
  public var tail: Deque<Element> {
    switch front {
    case .Nil: return Deque()
    case .Cons(_, let tail):
      var ret = Deque(front: tail, back: back, fCount: fCount - 1, bCount: bCount)
      ret.check()
      return ret
    }
  }
  public var initial: Deque<Element> {
    switch back {
    case .Nil: return Deque()
    case .Cons(_, let tail):
      var ret = Deque(front: front, back: tail, fCount: fCount, bCount: bCount - 1)
      ret.check()
      return ret
    }
  }
}

extension Deque : Indexable {
  public var startIndex: Int { return 0 }
  public var count     : Int { return fCount + bCount }
  public var endIndex  : Int { return count }
  public subscript(n: Int) -> Element {
    get { return n < fCount ? front[n] : back[bCount - (n - fCount) - 1] }
    set(v) { n < fCount ? (front[n] = v) : (back[bCount - (n - fCount) - 1] = v) }
  }
}

extension Deque {
  public func map<T>(@noescape transform: Element -> T) -> Deque<T> {
    return Deque<T>(
      front: front.map(transform),
      back: back.map(transform),
      fCount: fCount, bCount: bCount
    )
  }
}

extension Deque {
  public func reverse() -> Deque<Element> {
    return Deque(front: back, back: front, fCount: bCount, bCount: fCount)
  }
}

extension Deque {
  public func filter(@noescape includeElement: Element -> Bool) -> Deque<Element> {
    var (nFCount, nBCount) = (0, 0)
    let nFront = front.filter { includeElement($0) ? {++nFCount; return true}() : false }
    let nBack  = back .filter { includeElement($0) ? {++nBCount; return true}() : false }
    var ret = Deque(front: nFront, back: nBack, fCount: nFCount, bCount: nBCount)
    ret.check()
    return ret
  }
}

extension Deque {
  public func flatMap<S : SequenceType>(@noescape transform: Element -> S) -> Deque<S.Generator.Element> {
    let frontAr: [S.Generator.Element] = front.flatMap(transform)
    let backAr : [S.Generator.Element] = back .flatMap(transform)
    return Deque<S.Generator.Element>(front: frontAr, noReverseBack: backAr)
  }
  public func flatMap<T>(@noescape transform: Element -> T?) -> Deque<T> {
    let frontAr: [T] = front.flatMap(transform)
    let backAr : [T] = back .flatMap(transform)
    return Deque<T>(front: frontAr, noReverseBack: backAr)
  }
}<commit_msg>removed deque counting and indexing<commit_after>public struct Deque<Element> {
  private var front, back: List<Element>
  public init() {
    (front, back)    = (.Nil, .Nil)
  }
}

extension Deque {
  private init(front: List<Element>, back: List<Element>) {
    (self.front, self.back) = (front, back)
  }
  private init(front: [Element], noReverseBack: [Element]) {
    (self.front, back) = (List(seq: front), List(seq: noReverseBack))
    check()
  }
}

extension Deque {
  private mutating func check() {
    switch (front, back) {
    case (.Nil, let .Cons(head, tail)) where !tail.isEmpty:
      (front, back) = (tail.reverse(), [head])
    case (let .Cons(head, tail), .Nil) where !tail.isEmpty:
      (back, front) = (tail.reverse(), [head])
    default:
      return
    }
  }
}

extension Deque {
  public mutating func prepend(with: Element) {
    front = with |> front
  }
  public mutating func append(with: Element) {
    back = with |> front
  }
}

public struct DequeGenerator<Element> : GeneratorType {
  private var front, back: List<Element>
  public mutating func next() -> Element? {
    switch (front, back) {
    case (let .Cons(head, tail), _):
      front = tail
      return head
    case (_, let .Cons(head, tail)):
      back = tail
      return head
    default:
      return nil
    }
  }
}

extension DequeGenerator : SequenceType {
  public func generate() -> DequeGenerator {
    return self
  }
}

extension Deque : SequenceType {
  public func generate() -> DequeGenerator<Element> {
    return DequeGenerator(front: front, back: back.reverse())
  }
}

extension Deque : CustomDebugStringConvertible {
  public var debugDescription: String {
    
    return
      ", ".join(front.map { String(reflecting: $0) }) + " | " +
      ", ".join(back.reverse().map { String(reflecting: $0) })
  }
}

extension Deque {
  public init(ar: [Element]) {
    let half = ar.endIndex / 2
    front = List(seq: ar[0..<half])
    back  = List(seq: ar[half..<ar.endIndex].reverse())
  }
}

extension Deque : ArrayLiteralConvertible {
  public init(arrayLiteral: Element...) {
    self = Deque(ar: arrayLiteral)
  }
}

extension Deque {
  public mutating func removeLast() -> Element? {
    switch back {
    case .Nil: return nil
    case let .Cons(head, tail):
      back = tail
      return head
    }
  }
  public mutating func removeFirst() -> Element? {
    switch front {
    case .Nil: return nil
    case let .Cons(head, tail):
      front = tail
      return head
    }
  }
}

extension Deque {
  public var first: Element? {
    switch front {
    case .Nil: return nil
    case .Cons(let head, _): return head
    }
  }
  public var last: Element? {
    switch back {
    case .Nil: return nil
    case .Cons(let head, _): return head
    }
  }
}

extension Deque {
  public var tail: Deque<Element> {
    switch front {
    case .Nil: return Deque()
    case .Cons(_, let tail):
      var ret = Deque(front: tail, back: back)
      ret.check()
      return ret
    }
  }
  public var initial: Deque<Element> {
    switch back {
    case .Nil: return Deque()
    case .Cons(_, let tail):
      var ret = Deque(front: front, back: tail)
      ret.check()
      return ret
    }
  }
}

extension Deque {
  public func map<T>(@noescape transform: Element -> T) -> Deque<T> {
    return Deque<T>(
      front: front.map(transform),
      back: back.map(transform)
    )
  }
}

extension Deque {
  public func reverse() -> Deque<Element> {
    return Deque(front: back, back: front)
  }
}

extension Deque {
  public func filter(@noescape includeElement: Element -> Bool) -> Deque<Element> {
    var ret = Deque(front: front.filter(includeElement), back: back.filter(includeElement))
    ret.check()
    return ret
  }
}

extension Deque {
  public func flatMap<S : SequenceType>(@noescape transform: Element -> S) -> Deque<S.Generator.Element> {
    let frontAr: [S.Generator.Element] = front.flatMap(transform)
    let backAr : [S.Generator.Element] = back .flatMap(transform)
    return Deque<S.Generator.Element>(front: frontAr, noReverseBack: backAr)
  }
  public func flatMap<T>(@noescape transform: Element -> T?) -> Deque<T> {
    let frontAr: [T] = front.flatMap(transform)
    let backAr : [T] = back .flatMap(transform)
    return Deque<T>(front: frontAr, noReverseBack: backAr)
  }
}
{'fraction_non_alphanumeric': 0.1376575843838959, 'fraction_numerical': 0.0025416836112240747, 'mean_word_length': 2.81426909654905, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 77, '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': '5969695', 'n_tokens_mistral': 3393, 'n_tokens_neox': 3280, 'n_words': 925}