Skip to content
This repository
Brian Shirai September 20, 2013
file 856 lines (668 sloc) 16.434 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
if Rubinius::Config['hash.hamt']
class Hash
  include Enumerable

  Vector = Rubinius::Tuple

  class State
    attr_accessor :head
    attr_accessor :tail
    attr_accessor :size

    def self.from(state)
      new_state = new
      new_state.compare_by_identity if state and state.compare_by_identity?
      new_state
    end

    def initialize
      @size = 0
      @head = nil
      @tail = nil
      @compare_by_identity = false
    end

    def compare_by_identity?
      @compare_by_identity
    end

    def compare_by_identity
      @compare_by_identity = true
      class << self
        def match?(this_key, other_key)
          other_key.equal? this_key
        end
      end
    end

    def match?(this_key, other_key)
      other_key.eql? this_key
    end
  end

  class Item
    attr_accessor :state
    attr_accessor :previous
    attr_accessor :next
    attr_accessor :key
    attr_accessor :value

    def initialize(key, value, state)
      if key.kind_of?(String) and !key.frozen?
        key = key.dup
        key.freeze
      end

      @key = key
      @value = value
      @state = state

      if tail = state.tail
        @previous = tail
        state.tail = tail.next = self
      else
        state.head = state.tail = self
      end

      state.size += 1
    end

    def empty?
      not @state
    end

    def key_hash
      @key.hash
    end

    def lookup(key, key_hash)
      return self if @state.match? @key, key
    end

    def insert(key, key_hash, value)
      return unless @state.match? @key, key
      @value = value
      self
    end

    def delete(key, key_hash)
      if @state.match? @key, key

        if @previous
          @previous.next = @next
        else
          @state.head = @next
        end

        if @next
          @next.previous = @previous
        else
          @state.tail = @previous
        end

        @state.size -= 1
        @state = nil

        self
      end
    end
  end

  class List
    attr_accessor :state
    attr_accessor :key_hash
    attr_accessor :entries

    def initialize(state, key_hash)
      @state = state
      @key_hash = key_hash
      @entries = Vector.new 2
    end

    def empty?
      not @entries
    end

    def lookup(key, key_hash)
      @entries.each { |e| return e if e.lookup(key, key_hash) }
      nil
    end

    def insert(key, key_hash, value)
      @entries.each { |e| return e if e.insert(key, key_hash, value) }

      item = Item.new key, value, @state
      @entries = @entries.insert_at_index @entries.size, item

      item
    end

    def add(item, key, key_hash, value)
      @entries[0] = item
      @entries[1] = Item.new key, value, @state
    end

    def delete(key, key_hash)
      @entries.each_with_index do |e, i|
        if e.delete key, key_hash
          @entries = @entries.delete_at_index i
          return e
        end
      end
      nil
    end
  end

  # An Array Mapped Trie
  class Trie
    attr_accessor :bmp
    attr_accessor :entries
    attr_accessor :level
    attr_accessor :state

    def initialize(state, level)
      @state = state
      @level = level
      @bmp = 0
      @entries = nil
    end

    def empty?
      not @entries
    end

    def lookup(key, key_hash)
      return unless index = item_index(key_hash)
      @entries[index].lookup key, key_hash
    end

    def insert(key, key_hash, value)
      if index = item_index(key_hash)
        item = @entries[index]
        unless item.insert key, key_hash, value
          trie = Trie.new @state, @level + 1
          trie.add item, key, key_hash, value
          @entries[index] = trie
          return trie
        else
          return item
        end
      else
        @bmp = set_bitmap key_hash
        index = item_index key_hash
        item = Item.new key, value, @state
        @entries = @entries.insert_at_index index, item
        return item
      end
    end

    def add(item, key, key_hash, value)
      item_hash = item.key_hash
      bmp = @bmp = set_bitmap(item_hash)
      @bmp = set_bitmap key_hash

      if bmp == @bmp
        if item_hash == key_hash
          list = List.new @state, key_hash
          list.add item, key, key_hash, value
          @entries = Vector[list]
        else
          trie = Trie.new @state, @level + 1
          trie.add item, key, key_hash, value
          @entries = Vector[trie]
        end
      else
        @entries = Vector.new 2
        @entries[item_index(item_hash)] = item
        @entries[item_index(key_hash)] = Item.new key, value, @state
      end
    end

    def delete(key, key_hash)
      return unless index = item_index(key_hash)

      item = @entries[index]
      if de = item.delete(key, key_hash)
        if item.empty?
          @bmp = unset_bitmap key_hash

          @entries = @entries.delete_at_index index
        end

        return de
      end
    end

    def item_index(key_hash)
      Rubinius.invoke_primitive :vm_hash_trie_item_index, key_hash, @level, @bmp
    end

    def set_bitmap(key_hash)
      Rubinius.invoke_primitive :vm_hash_trie_set_bitmap, key_hash, @level, @bmp
    end

    def unset_bitmap(key_hash)
      Rubinius.invoke_primitive :vm_hash_trie_unset_bitmap, key_hash, @level, @bmp
    end
  end

  class Table
    attr_accessor :entries
    attr_accessor :state

    def initialize(state)
      @state = state
      @entries = Vector.new 64
    end

    def item_index(key_hash)
      key_hash & 0b111111
    end

    def lookup(key, key_hash)
      if item = @entries[item_index(key_hash)]
        item.lookup key, key_hash
      end
    end

    def insert(key, key_hash, value)
      index = item_index key_hash
      if item = @entries[index]
        unless item.insert key, key_hash, value
          trie = Trie.new @state, 0
          trie.add item, key, key_hash, value
          @entries[index] = trie
        end
      else
        item = Item.new key, value, @state
        @entries[index] = item
      end
    end

    def delete(key, key_hash)
      index = item_index key_hash
      return unless item = @entries[index]

      if de = item.delete(key, key_hash)
        if item.empty?
          @entries[index] = nil
        end
        return de
      end
    end
  end

  # An external iterator that returns entries in insertion order. While
  # somewhat following the API of Enumerator, it is named Iterator because it
  # does not provide <code>#each</code> and should not conflict with
  # +Enumerator+ in MRI 1.8.7+. Returned by <code>Hash#to_iter</code>.

  class Iterator
    def initialize(state)
      @state = state
    end

    # Returns the next object or +nil+.
    def next(item)
      if item
        return item if item = item.next
      else
        return @state.head
      end
    end
  end

  # Hash methods

  def self.[](*args)
    total = args.size
    if total == 1
      obj = args.first
      if obj.kind_of? Hash
        return allocate.replace(obj)
      elsif obj.respond_to? :to_hash
        return allocate.replace(Rubinius::Type.coerce_to(obj, Hash, :to_hash))
      elsif obj.kind_of? Array # See redmine # 1385
        h = allocate
        args.first.each do |arr|
          next unless arr.respond_to? :to_ary
          arr = arr.to_ary
          next unless (1..2).include? arr.size
          h[arr.at(0)] = arr.at(1)
        end
        return h
      end
    end

    return allocate if total == 0

    if total & 1 == 1
      raise ArgumentError, "Expected an even number, got #{total}"
    end

    hash = allocate
    i = 0

    while i < total
      hash[args[i]] = args[i+1]
      i += 2
    end

    hash
  end

  def self.try_convert(obj)
    Rubinius::Type.try_convert obj, Hash, :to_hash
  end

  def self.new_from_literal(size)
    allocate
  end

  def initialize(default=undefined, &block)
    Rubinius.check_frozen

    if !undefined.equal?(default) and block
      raise ArgumentError, "Specify a default value or a block, not both"
    end

    if block
      @default = nil
      @default_proc = block
    elsif !undefined.equal?(default)
      @default = default
      @default_proc = nil
    end

    self
  end
  private :initialize

  def hash
    val = size
    Thread.detect_outermost_recursion self do
      each_item do |item|
        val ^= item.key.hash
        val ^= item.value.hash
      end
    end

    return val
  end

  def initialize_copy(other)
    replace other
  end
  private :initialize_copy

  def [](key)
    if @table and item = @table.lookup(key, key.hash)
      return item.value
    end

    default key
  end

  def []=(key, value)
    Rubinius.check_frozen

    unless @table
      @state = State.new unless @state
      @table = Table.new @state
    end

    @table.insert key, key.hash, value

    value
  end

  alias_method :store, :[]=

  def assoc(key)
    each_item { |e| return e.key, e.value if key == e.key }
    nil
  end

  def clear
    @state = State.from @state
    @table = nil
    self
  end

  def compare_by_identity
    Rubinius.check_frozen

    @state = State.new unless @state
    @state.compare_by_identity
    self
  end

  def compare_by_identity?
    return false unless @state
    @state.compare_by_identity?
  end

  def default(key=undefined)
    if @default_proc and !undefined.equal?(key)
      @default_proc.call(self, key)
    else
      @default
    end
  end

  def default=(value)
    @default_proc = nil
    @default = value
  end

  def default_proc
    @default_proc
  end

  def default_proc=(prc)
    prc = Rubinius::Type.coerce_to prc, Proc, :to_proc

    if prc.lambda? and prc.arity != 2
      raise TypeError, "default proc must have arity 2"
    end

    @default = nil
    @default_proc = prc
  end

  def delete(key)
    Rubinius.check_frozen

    if @table
      if item = @table.delete(key, key.hash)
        return item.value
      end
    end

    return yield(key) if block_given?

    nil
  end

  def delete_if
    Rubinius.check_frozen

    return to_enum(:delete_if) unless block_given?

    each_item { |e| delete e.key if yield(e.key, e.value) }

    self
  end

  def each
    return to_enum(:each) unless block_given?

    if @state
      item = @state.head
      while item
        yield [item.key, item.value]
        item = item.next
      end
    end

    self
  end

  alias_method :each_pair, :each

  def each_item
    return unless @state

    item = @state.head
    while item
      yield item
      item = item.next
    end
  end

  def each_key
    return to_enum(:each_key) unless block_given?

    each_item { |e| yield e.key }

    self
  end

  def each_value
    return to_enum(:each_value) unless block_given?

    each_item { |e| yield e.value }

    self
  end

  def empty?
    size == 0
  end

  def eql?(other)
    return true if self.equal? other

    unless other.kind_of? Hash
      return false unless other.respond_to? :to_hash
      return other.eql?(self)
    end

    return false unless other.size == size

    Thread.detect_recursion self, other do
      other.each_item do |e|

        return false unless item = @table.lookup(e.key, e.key.hash)

        # Order of the comparison matters! We must compare our value with
        # the other Hash's value and not the other way around.
        return false unless item.value.eql?(e.value)
      end
    end

    true
  end

  alias_method :==, :eql?

  def fetch(key, default=undefined)
    unless empty?
      if item = @table.lookup(key, key.hash)
        return item.value
      end
    end

    return yield(key) if block_given?
    return default unless undefined.equal?(default)
    raise IndexError, 'key not found'
  end

  # Searches for an item matching +key+. Returns the item
  # if found. Otherwise returns +nil+. Called from the C-API.
  def find_item(key)
    unless empty?
      if item = @table.lookup(key, key.hash)
        return item
      end
    end

    nil
  end

  def flatten(level=1)
    to_a.flatten(level)
  end

  def initialize_copy(other)
    replace other
  end
  private :initialize_copy

  def inspect
    out = []
    return '{...}' if Thread.detect_recursion self do
      each_item do |e|
        str = e.key.inspect
        str << '=>'
        str << e.value.inspect
        out << str
      end
    end
    "{#{out.join ', '}}"
  end

  alias_method :to_s, :inspect

  def invert
    h = Hash.allocate
    each_item { |e| h[e.value] = e.key }
    h
  end

  def keep_if
    Rubinius.check_frozen

    return to_enum(:keep_if) unless block_given?

    each_item { |e| delete e.key unless yield(e.key, e.value) }

    self
  end

  def key(value)
    each_item { |e| return e.key if e.value == value }

    nil
  end

  alias_method :index, :key

  def key?(key)
    if @table and @table.lookup(key, key.hash)
      true
    else
      false
    end
  end

  alias_method :has_key?, :key?
  alias_method :include?, :key?
  alias_method :member?, :key?

  def keys
    ary = []
    each_item { |e| ary << e.key }
    ary
  end

  def merge(other, &block)
    dup.merge!(other, &block)
  end

  def merge!(other)
    Rubinius.check_frozen

    other = Rubinius::Type.coerce_to other, Hash, :to_hash

    return self if other.empty?

    unless @table
      @state = State.new unless @state
      @table = Table.new @state
    end

    if block_given?
      other.each_item do |e|
        key = e.key
        if item = @table.lookup(key, key.hash)
          item.value = yield(key, item.value, e.value)
        else
          @table.insert key, key.hash, e.value
        end
      end
    else
      other.each_item do |e|
        key = e.key
        @table.insert key, key.hash, e.value
      end
    end

    self
  end

  alias_method :update, :merge!

  def rassoc(value)
    each_item { |e| return e.key, e.value if value == e.value }
    nil
  end

  def rehash
    Rubinius.check_frozen

    return if empty?

    item = @state.head
    @state = State.from @state
    table = Table.new @state

    while item
      table.insert item.key, item.key.hash, item.value
      item = item.next
    end

    @table = table

    self
  end

  def reject(&block)
    return to_enum(:reject) unless block_given?

    hsh = dup.delete_if(&block)
    hsh.taint if tainted?
    hsh
  end

  def reject!(&block)
    Rubinius.check_frozen

    return to_enum(:reject!) unless block_given?

    unless empty?
      size = @state.size
      delete_if(&block)
      return self if size != @state.size
    end

    nil
  end

  def replace(other)
    Rubinius.check_frozen

    other = Rubinius::Type.coerce_to other, Hash, :to_hash
    return self if self.equal? other

    @state = State.from @state
    @state.compare_by_identity if other.compare_by_identity?
    @table = Table.new @state

    other.each_item do |e|
      key = e.key
      @table.insert key, key.hash, e.value
    end

    @default = other.default
    @default_proc = other.default_proc

    self
  end

  def select
    Rubinius.check_frozen

    return to_enum(:select) unless block_given?

    hsh = Hash.allocate

    each_item do |e|
      key = e.key
      value = e.value
      hsh[key] = value if yield(key, value)
    end

    hsh
  end

  def select!
    Rubinius.check_frozen

    return to_enum(:select!) unless block_given?

    return nil if empty?

    size = @state.size
    each_item { |e| delete e.key unless yield(e.key, e.value) }
    return nil if size == @state.size

    self
  end

  def shift
    Rubinius.check_frozen

    return default(nil) if empty?

    item = @state.head
    @table.delete item.key, item.key.hash

    return item.key, item.value
  end

  def size
    @state ? @state.size : 0
  end

  alias_method :length, :size

  def to_a
    ary = []
    each_item { |e| ary << [e.key, e.value] }
    ary
  end

  # Returns an external iterator for the bins. See +Iterator+
  def to_iter
    Iterator.new @state
  end

  def to_hash
    self
  end

  def value?(value)
    each_item { |e| return true if value == e.value }
    false
  end

  alias_method :has_value?, :value?

  def values
    ary = []
    each_item { |e| ary << e.value }
    ary
  end

  def values_at(*args)
    if empty?
      args.map { |key| default key }
    else
      args.map do |key|
        if item = @table.lookup(key, key.hash)
          item.value
        else
          default key
        end
      end
    end
  end

  # Used internally in Rubinius to get around subclasses redefining #[]=
  alias_method :__store__, :[]=
end
end
Something went wrong with that request. Please try again.