<commit_before>extension OrderedDictionary: Encodable /* where Key: Encodable, Value: Encodable */ {
    
    /// __inheritdoc__
    public func encode(to encoder: Encoder) throws {
        assertTypeIsEncodable(Key.self, in: type(of: self))
        assertTypeIsEncodable(Value.self, in: type(of: self))
        
        var container = encoder.unkeyedContainer()
        
        for (key, value) in self {
            // Using the magic desribed here:
            // https://github.com/apple/swift/blob/master/stdlib/public/core/Codable.swift#L4114-L4116
            let keyEncoder = container.superEncoder()
            try (key as! Encodable).encode(to: keyEncoder)
            
            let valueEncoder = container.superEncoder()
            try (value as! Encodable).encode(to: valueEncoder)
        }
    }
    
    /// This assertion is used for checking the conformance to `Encodable` of `Key` and `Value`
    /// types in `OrderedDictionary`. This workaround is necessary due to the limitation of missing
    /// conditional protocol conformance in Swift.
    ///
    /// The code is take from the Swift repository:
    /// https://github.com/apple/swift/blob/master/stdlib/public/core/Codable.swift#L3963-L3981
    private func assertTypeIsEncodable<T>(_ type: T.Type, in wrappingType: Any.Type) {
        guard T.self is Encodable.Type else {
            if T.self == Encodable.self || T.self == Codable.self {
                preconditionFailure("\(wrappingType) does not conform to Encodable because Encodable does not conform to itself. You must use a concrete type to encode or decode.")
            } else {
                preconditionFailure("\(wrappingType) does not conform to Encodable because \(T.self) does not conform to Encodable.")
            }
        }
    }
    
}

extension OrderedDictionary: Decodable /* where Key: Decodable, Value: Decodable */ {
    
    /// __inheritdoc__
    public init(from decoder: Decoder) throws {
        self.init()
        
        assertTypeIsDecodable(Key.self, in: type(of: self))
        assertTypeIsDecodable(Value.self, in: type(of: self))
        
        var container = try decoder.unkeyedContainer()
        
        let keyMetaType = (Key.self as! Decodable.Type)
        let valueMetaType = (Value.self as! Decodable.Type)
        
        while !container.isAtEnd {
            // Using the magic desribed here:
            // https://github.com/apple/swift/blob/master/stdlib/public/core/Codable.swift#L4181-L4184
            let keyDecoder = try container.superDecoder()
            let key = try keyMetaType.init(from: keyDecoder) as! Key
            
            guard !container.isAtEnd else {
                let error = DecodingError.Context(codingPath: decoder.codingPath,
                                                  debugDescription: "Unkeyed container reached end before value in key-value pair.")
                throw DecodingError.dataCorrupted(error)
            }
            
            let valueDecoder = try container.superDecoder()
            let value = try valueMetaType.init(from: valueDecoder) as! Value
            
            self[key] = value
        }
    }
    
    /// This assertion is used for checking the conformance to `Decodable` of `Key` and `Value`
    /// types in `OrderedDictionary`. This workaround is necessary due to the limitation of missing
    /// conditional protocol conformance in Swift.
    ///
    /// The code is take from the Swift repository:
    /// https://github.com/apple/swift/blob/master/stdlib/public/core/Codable.swift#L3963-L3981
    private func assertTypeIsDecodable<T>(_ type: T.Type, in wrappingType: Any.Type) {
        guard T.self is Decodable.Type else {
            if T.self == Decodable.self || T.self == Codable.self {
                preconditionFailure("\(wrappingType) does not conform to Decodable because Decodable does not conform to itself. You must use a concrete type to encode or decode.")
            } else {
                preconditionFailure("\(wrappingType) does not conform to Decodable because \(T.self) does not conform to Decodable.")
            }
        }
    }
    
}
<commit_msg>Added conditional Codable conformace for Swift 4.1<commit_after>#if swift(>=4.1)

extension OrderedDictionary: Encodable where Key: Encodable, Value: Encodable {
    
    /// __inheritdoc__
    public func encode(to encoder: Encoder) throws {
        // Encode the ordered dictionary as an array of alternating key-value pairs.
        var container = encoder.unkeyedContainer()
        
        for (key, value) in self {
            try container.encode(key)
            try container.encode(value)
        }
    }
    
}

extension OrderedDictionary: Decodable where Key: Decodable, Value: Decodable {
    
    /// __inheritdoc__
    public init(from decoder: Decoder) throws {
        // Decoding the ordered dictionary from an array of alternating key-value pairs.
    
        self.init()
    
        var container = try decoder.unkeyedContainer()
        
        while !container.isAtEnd {
            let key = try container.decode(Key.self)
            guard !container.isAtEnd else { throw DecodingError.unkeyedContainerReachedEndBeforeValue(decoder.codingPath) }
            let value = try container.decode(Value.self)
            
            self[key] = value
        }
    }
    
}
    
#else

extension OrderedDictionary: Encodable {

    /// __inheritdoc__
    public func encode(to encoder: Encoder) throws {
        // Since Swift 4.0 lacks the protocol conditional conformance support, we have to make the
        // whole OrderedDictionary type conform to Encodable and assert that the key and value
        // types conform to Encodable. Furthermore, we leverage a trick of super encoders to be
        // able to encode objects without knowing their exact types. This trick was used in the
        // standard library for encoding/decoding Dictionary before Swift 4.1.
        
        _assertTypeIsEncodable(Key.self, in: type(of: self))
        _assertTypeIsEncodable(Value.self, in: type(of: self))

        var container = encoder.unkeyedContainer()

        for (key, value) in self {
            let keyEncoder = container.superEncoder()
            try (key as! Encodable).encode(to: keyEncoder)

            let valueEncoder = container.superEncoder()
            try (value as! Encodable).encode(to: valueEncoder)
        }
    }

    private func _assertTypeIsEncodable<T>(_ type: T.Type, in wrappingType: Any.Type) {
        guard T.self is Encodable.Type else {
            if T.self == Encodable.self || T.self == Codable.self {
                preconditionFailure("\(wrappingType) does not conform to Encodable because Encodable does not conform to itself. You must use a concrete type to encode or decode.")
            } else {
                preconditionFailure("\(wrappingType) does not conform to Encodable because \(T.self) does not conform to Encodable.")
            }
        }
    }

}

extension OrderedDictionary: Decodable {

    /// __inheritdoc__
    public init(from decoder: Decoder) throws {
        // Since Swift 4.0 lacks the protocol conditional conformance support, we have to make the
        // whole OrderedDictionary type conform to Decodable and assert that the key and value
        // types conform to Decodable. Furthermore, we leverage a trick of super decoders to be
        // able to decode objects without knowing their exact types. This trick was used in the
        // standard library for encoding/decoding Dictionary before Swift 4.1.
        
        self.init()

        _assertTypeIsDecodable(Key.self, in: type(of: self))
        _assertTypeIsDecodable(Value.self, in: type(of: self))

        var container = try decoder.unkeyedContainer()

        let keyMetaType = (Key.self as! Decodable.Type)
        let valueMetaType = (Value.self as! Decodable.Type)

        while !container.isAtEnd {
            let keyDecoder = try container.superDecoder()
            let key = try keyMetaType.init(from: keyDecoder) as! Key

            guard !container.isAtEnd else { throw DecodingError.unkeyedContainerReachedEndBeforeValue(decoder.codingPath) }

            let valueDecoder = try container.superDecoder()
            let value = try valueMetaType.init(from: valueDecoder) as! Value

            self[key] = value
        }
    }

    private func _assertTypeIsDecodable<T>(_ type: T.Type, in wrappingType: Any.Type) {
        guard T.self is Decodable.Type else {
            if T.self == Decodable.self || T.self == Codable.self {
                preconditionFailure("\(wrappingType) does not conform to Decodable because Decodable does not conform to itself. You must use a concrete type to encode or decode.")
            } else {
                preconditionFailure("\(wrappingType) does not conform to Decodable because \(T.self) does not conform to Decodable.")
            }
        }
    }

}

#endif

fileprivate extension DecodingError {
    
    fileprivate static func unkeyedContainerReachedEndBeforeValue(_ codingPath: [CodingKey]) -> DecodingError {
        return DecodingError.dataCorrupted(
            DecodingError.Context(
                codingPath: codingPath,
                debugDescription: "Unkeyed container reached end before value in key-value pair."
            )
        )
    }
    
}

{'fraction_non_alphanumeric': 0.08416897802432259, 'fraction_numerical': 0.004693834008960956, 'mean_word_length': 2.2529493407356003, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1996163', 'n_tokens_mistral': 2590, 'n_tokens_neox': 2492, 'n_words': 796}