<commit_before>require 'prime'

class HashMap

    attr_reader :weight, :rehash_threshold, :grow_multiplier

    def initialize(params = {})
        @data = Array.new(params.fetch(:capacity, 10))
        @weight = params.fetch(:weight, 0.8)
        @rehash_threshold = params.fetch(:rehash_threshold, 2.0)
        @grow_multiplier = params.fetch(:grow_multiplier, 2)
        @prime = Prime.first(100).sample
        @ops = 0
    end

    def empty?
        size == 0
    end

    def size
        @data.map{|bucket| bucket.nil? ? 0 : bucket.size}.inject(0){|sum,bucket_size| sum+bucket_size}
    end

    def keys
      @data.map{|bucket| bucket.nil? ? nil : bucket.keys}.reject{|keys| keys.nil?}.flatten
    end

    def values
      @data.map{|bucket| bucket.nil? ? nil : bucket.values}.reject{|values| values.nil?}.flatten
    end

    def underlying_size
        @data.length
    end

    def reweight_needed
        size >= (@weight * underlying_size)
    end

    def reweight
        return unless reweight_needed
        new_size = underlying_size * @grow_multiplier
        olddata = @data
        @data = Array.new(new_size)
        @ops = 0
        olddata.each do |bucket|
            while not bucket.nil?
                put(bucket.key, bucket.value)
                bucket = bucket.next_bucket
            end
        end
    end

    def rehash_needed
        average_get_operations > @rehash_threshold
    end

    def rehash
        return unless rehash_needed
        @ops = 0
        @prime = Prime.first(100).sample
        olddata = @data
        @data = Array.new(underlying_size)
        olddata.each do |bucket|
            while not bucket.nil?
                put(bucket.key, bucket.value)<commit_msg>Stop stack too deep error when rehashing<commit_after>require 'prime'

class HashMap

    attr_reader :weight, :rehash_threshold, :grow_multiplier

    def initialize(params = {})
        @data = Array.new(params.fetch(:capacity, 10))
        @weight = params.fetch(:weight, 0.8)
        @rehash_threshold = params.fetch(:rehash_threshold, 2.0)
        @grow_multiplier = params.fetch(:grow_multiplier, 2)
        @prime = Prime.first(100).sample
        @ops = 0
    end

    def empty?
        size == 0
    end

    def size
        @data.map{|bucket| bucket.nil? ? 0 : bucket.size}.inject(0){|sum,bucket_size| sum+bucket_size}
    end

    def keys
      @data.map{|bucket| bucket.nil? ? nil : bucket.keys}.reject{|keys| keys.nil?}.flatten
    end

    def values
      @data.map{|bucket| bucket.nil? ? nil : bucket.values}.reject{|values| values.nil?}.flatten
    end

    def underlying_size
        @data.length
    end

    def reweight_needed
        size >= (@weight * underlying_size)
    end

    def reweight
        return unless reweight_needed
        new_size = underlying_size * @grow_multiplier
        olddata = @data
        @data = Array.new(new_size)
        @ops = 0
        olddata.each do |bucket|
            while not bucket.nil?
                put(bucket.key, bucket.value)
                bucket = bucket.next_bucket
            end
        end
    end

    def rehash_needed
        ! @rehashing && average_get_operations > @rehash_threshold
    end

    def rehash
        return unless rehash_needed
        @rehashing = true
        @ops = 0
        @prime = Prime.first(100).sample
        olddata = @data
        @data = Array.new(underlying_size)
        olddata.each do |bucket|
            while not bucket.nil?
                put(bucket.key, bucket.value)
{'fraction_non_alphanumeric': 0.10156028368794326, 'fraction_numerical': 0.01078014184397163, 'mean_word_length': 2.0634231103388356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2828189', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1055, 'n_words': 246}