<commit_before>extension Document: RandomAccessCollection {
    public subscript(position: DocumentIndex) -> (String, Primitive) {
        let type = typeIdentifier(at: position.offset)!

        return (
            key(at: position.offset)!,
            value(forType: type, at: position.offset)!
        )
    }
    
    public typealias Iterator = DocumentIterator
    public typealias SubSequence = DocumentSlice
    public typealias Index = DocumentIndex
    
    public var count: Int {
        var offset = 4
        var count = 0

        while skipKeyValuePair(at: &offset) {
            count += 1
        }

        return count
    }
    
    public var startIndex: DocumentIndex {
        return DocumentIndex(offset: 0)
    }<commit_msg>Fix random access using DocumentIndex<commit_after>extension Document: RandomAccessCollection {
    public subscript(position: DocumentIndex) -> (String, Primitive) {
        var offset = 0
        for _ in 0..<position.offset {
            guard self.skipKeyValuePair(at: &offset) else {
                fatalError("DocumentIndex exceeded Document bounds")
            }
        }
        
        let type = typeIdentifier(at: offset)!

        return (
            key(at: offset)!,
            value(forType: type, at: offset)!
        )
    }
    
    public typealias Iterator = DocumentIterator
    public typealias SubSequence = DocumentSlice
    public typealias Index = DocumentIndex
    
    public var count: Int {
        var offset = 4
        var count = 0

        while skipKeyValuePair(at: &offset) {
            count += 1
        }

        return count
    }
    
    public var startIndex: DocumentIndex {
        return DocumentIndex(offset: 0)
    }
{'fraction_non_alphanumeric': 0.07594202898550724, 'fraction_numerical': 0.005797101449275362, 'mean_word_length': 1.9913344887348354, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '5443044', 'n_tokens_mistral': 460, 'n_tokens_neox': 434, 'n_words': 128}