<commit_before>//
//  Document+SequenceType.swift
//  BSON
//
//  Created by <NAME> on 03-02-16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

extension Document : Sequence {
    /// As required by and documented in `SequenceType`
    public var startIndex: Int {
        return elements.startIndex
    }

    /// As required by and documented in `SequenceType`
    public var endIndex: Int {
        return elements.endIndex
    }
    
    /// As required by and documented in `SequenceType`
    public func indexForKey(key: String) -> Int? {
        return elements.index(where:)(where: { $0.0 == key })
    }
    
    /// As required by and documented in `SequenceType`
    public subscript (key: String) -> BSONElement? {
        get {
            return elements.filter({ $0.0 == key }).first?.1
        }
        set {
            if let newValue = newValue {
                self.updateValue(newValue, forKey: key)
            }
        }
    }
    
    public subscript (key: Int) -> BSONElement? {
        get {
            return elements[key].1
        }
        set {
            guard let newValue = newValue else {
                let shouldKeepArray = self.validatesAsArray()
                
                elements.remove(at: key)
                
                if shouldKeepArray {
                    self.enforceArray()
                }
                
                return
            }
            <commit_msg>document["key"] = nil now removes the key from the document<commit_after>//
//  Document+SequenceType.swift
//  BSON
//
//  Created by <NAME> on 03-02-16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation

extension Document : Sequence {
    /// As required by and documented in `SequenceType`
    public var startIndex: Int {
        return elements.startIndex
    }

    /// As required by and documented in `SequenceType`
    public var endIndex: Int {
        return elements.endIndex
    }
    
    /// As required by and documented in `SequenceType`
    public func indexForKey(key: String) -> Int? {
        return elements.index(where:)(where: { $0.0 == key })
    }
    
    /// As required by and documented in `SequenceType`
    public subscript (key: String) -> BSONElement? {
        get {
            return elements.filter({ $0.0 == key }).first?.1
        }
        set {
            if let newValue = newValue {
                self.updateValue(newValue, forKey: key)
            } else {
                self.removeValueForKey(key)
            }
        }
    }
    
    public subscript (key: Int) -> BSONElement? {
        get {
            return elements[key].1
        }
        set {
            guard let newValue = newValue else {
                let shouldKeepArray = self.validatesAsArray()
                
                elements.remove(at: key)
                
                if shouldKeepArray {
                    self.enforceArray()
                }
                
                return
            }
            
{'fraction_non_alphanumeric': 0.09595793624712455, 'fraction_numerical': 0.010515938218862965, 'mean_word_length': 1.4177918983320095, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '5376989', 'n_tokens_mistral': 829, 'n_tokens_neox': 790, 'n_words': 232}