• KOBAYASHI Shuji's avatar
    Fix that Hash may not contain any empty buckets · dd81de49
    KOBAYASHI Shuji authored
    The Hash implementation assumed that there were always empty buckets, but
    sometimes there were only active or deleted buckets (no empty buckets).
    Therefore, fix it so that this situation does not occur.
    
    ### Example
    
    ```ruby
    # example.rb
    class A
      attr_reader :v
      def initialize(v) @v = v end
      def ==(o) @v == o.v end
      def hash; @v end
      def to_s; "#{self.class}[#{@v}]" end
      alias eql? ==
      alias inspect to_s
    end
    
    keys = (0..31).map{A.new(_1)}
    h = {}
    (0..16).each{h[keys[_1]] = _1}
    (17..31).each do
      k = keys[_1]
      h[k] = _1
      h.delete(k)
    end
    p h.keys
    ```
    
    #### Before this patch:
    
    ```console
    $ bin/mruby example.rb
    [A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9], A[10], A[11], A[12], A[13], A[14], A[15], A[16], A[30], A[31]]
    ```
    
    #### After this patch:
    
    ```console
    $ bin/mruby example.rb
    [A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9], A[10], A[11], A[12], A[13], A[14], A[15], A[16]]
    ```
    dd81de49
hash.c 50.6 KB