<commit_before>public struct OrderedMap<Key, Value> where Key: Hashable {

  public typealias Element = (key: Key, value: Value)
  public typealias Index = Int

  public init() {
    storage = []
    keyToIndex = [:]
  }

  public init<S>(_ elements: S) where S: Sequence, S.Element == (Key, Value) {
    storage = Array(elements)
    keyToIndex = Dictionary(
      uniqueKeysWithValues: storage.enumerated().map({ ($0.element.key, $0.offset) }))
  }

  public var last: Element? {
    return storage.last
  }

  private var storage: [Element]
  private var keyToIndex: [Key: Index]

}

extension OrderedMap: Collection {

  public var startIndex: Index { return 0 }
  public var endIndex  : Index { return storage.count }

  public func index(after i: Int) -> Int {
    return i + 1
  }

  public subscript(i: Int) -> Element {
    return storage[i]
  }

  public subscript(key: Key) -> Value? {
    get {
      return keyToIndex[key].map { storage[$0].value }
    }
    set {
      if let index = keyToIndex[key] {
        if let value = newValue {
          storage[index] = (key, value)
        } else {
          storage.remove(at: index)
          for i in index ..< storage.count {
            keyToIndex[storage[i].key] = i
          }
        }
      } else if let value = newValue {
        keyToIndex[key] = storage.count
        storage.append((key, value))
      }
    }
  }

  public var keys  : [Key]   { return map { $0.key } }
  public var values: [Value] { return map { $0.value } }

}

extension OrderedMap: ExpressibleByDictionaryLiteral {

  public init(dictionaryLiteral elements: (Key, Value)...) {
    self.init(elements)<commit_msg>Refactor the OrderedMap implementation<commit_after>public struct OrderedMap<Key, Value> where Key: Hashable {

  public init() {
    self.storage = [:]
    self.order = []
  }

  public init<S>(_ elements: S) where S: Sequence, S.Element == (Key, Value) {
    self.storage = [:]
    self.order = []
    for (key, value) in elements {
      storage[key] = value
      order.append(key)
    }
  }

  private var storage: [Key: Value]
  private var order: [Key]

  public var keys: [Key] {
    return order
  }

  public var values: [Value] {
    return order.map { storage[$0]! }
  }

  public func index(of key: Key) -> Index? {
    return order.firstIndex(of: key)
  }

  public mutating func insert(_ value: Value, forKey key: Key, at index: Index) {
    // Make sure we don't set a duplicate key.
    precondition(storage[key] == nil, "Key '\(key)' already exists")
    storage[key] = value
    order.insert(key, at: index)
  }

  public subscript(key: Key) -> Value? {
    get {
      return storage[key]
    }
    set {
      if let value = newValue {
        if storage[key] == nil {
          order.append(key)
        }
        storage[key] = value
      } else {
        order.removeAll { $0 == key }
        storage[key] = nil
      }
    }
  }

}

extension OrderedMap: MutableCollection {

  public typealias Index = Int
  public typealias Element = (key: Key, value: Value)

  public var startIndex: Index { return 0 }
  public var endIndex: Index { return order.count }

  public func index(after i: Index) -> Index {
    return i + 1
  }

  public subscript(index: Index) -> Element {
    get {
      return (key: order[index], value: storage[order[index]]!)
    } set {
      if let i = self.index(of: newValue.key) {
        // Make sure we don't set a duplicate key at a different index.
        precondition(i == index, "Key '\(newValue.key)' already exists at index \(i)")
      }
      order[index] = newValue.key
      storage[newValue.key] = newValue.value
    }
  }

}

extension OrderedMap: ExpressibleByDictionaryLiteral {

  public init(dictionaryLiteral elements: (Key, Value)...) {
    self.init(elements)
{'fraction_non_alphanumeric': 0.12424162490108151, 'fraction_numerical': 0.0029016090741229225, 'mean_word_length': 2.51436515291937, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, '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': '4307251', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1184, 'n_words': 351}