<commit_before>/// 
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/// Use of this file is governed by the BSD 3-clause license that
/// can be found in the LICENSE.txt file in the project root.
/// 

//
//  LookupDictionary.swift
//   antlr.swift
//
//  Created by janyou on 15/9/23.
//

import Foundation

public enum LookupDictionaryType: Int {
    case lookup = 0
    case ordered
}

public struct LookupDictionary {
    private let type: LookupDictionaryType
    private var cache = [Int: ATNConfig]()

    public init(type: LookupDictionaryType = LookupDictionaryType.lookup) {
        self.type = type
    }

    private func hash(_ config: ATNConfig) -> Int {
        if type == LookupDictionaryType.lookup {

            var hashCode: Int = 7
            hashCode = 31 * hashCode + config.state.stateNumber
            hashCode = 31 * hashCode + config.alt
            hashCode = 31 * hashCode + config.semanticContext.hashValue
            return hashCode

        } else {
            //Ordered
            return config.hashValue
        }<commit_msg>work around Swift crash where reading hashValue on SemanticContext.AND instance would call the empty constructor (possibly a Swift bug?)<commit_after>/// 
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/// Use of this file is governed by the BSD 3-clause license that
/// can be found in the LICENSE.txt file in the project root.
/// 

//
//  LookupDictionary.swift
//   antlr.swift
//
//  Created by janyou on 15/9/23.
//

import Foundation

public enum LookupDictionaryType: Int {
    case lookup = 0
    case ordered
}

public struct LookupDictionary {
    private let type: LookupDictionaryType
    private var cache = [Int: ATNConfig]()

    public init(type: LookupDictionaryType = LookupDictionaryType.lookup) {
        self.type = type
    }

    private func hash(_ config: ATNConfig) -> Int {
        if type == LookupDictionaryType.lookup {
            /* migrating to XCode 12.3/Swift 5.3 introduced a very weird bug
             where reading hashValue from a SemanticContext.AND instance woul:
                call the AND empty constructor
                NOT call AND.hash(into)
             Could it be a Swift compiler bug ?
             All tests pass when using Hasher.combine()
             Keeping the old code for reference:
             
                var hashCode: Int = 7
                hashCode = 31 * hashCode + config.state.stateNumber
                hashCode = 31 * hashCode + config.alt
                hashCode = 31 * hashCode + config.semanticContext.hashValue // <- the crash would occur here
                return hashCode
             
            */
            var hasher = Hasher()
            hasher.combine(7)
            hasher.combine(config.state.stateNumber)
            hasher.combine(config.alt)
            hasher.combine(config.semanticContext)
            return hasher.finalize()
        } else {
            //Ordered
            return config.hashValue
        }
{'fraction_non_alphanumeric': 0.07900826446280992, 'fraction_numerical': 0.01652892561983471, 'mean_word_length': 1.975417895771878, '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': '1689073', 'n_tokens_mistral': 859, 'n_tokens_neox': 788, 'n_words': 277}