Sha256: 01536c622d377fee33b3dfd435ec0366fabd92ad2f52fc99dc1a75c0beb3e043

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module DataStructures101
  module Hash
    # Utility class to manipulate (key, pairs) that have same hash code.
    # @see ChainedHashTable
    # @author Rene Hernandez
    # @since 0.2
    class Bucket
      attr_reader :table

      def initialize
        @table = []
      end

      def [](key)
        find(key)
      end

      def []=(key, value)
        insert(key, value)
      end

      def insert(key, value)
        idx = @table.find_index { |table_key, _| table_key == key }

        if idx.nil?
          @table << [key, value]
          return nil
        else
          value, @table[idx][1] = @table[idx][1], value
          return value
        end
      end

      def size
        @table.size
      end

      def find(key)
        pair = @table.find { |table_key, _| table_key == key }
        pair.nil? ? nil : pair.last
      end

      def delete(key)
        idx = @table.find_index { |table_key, _| table_key == key }
        return nil if idx.nil?

        value = @table[idx].last
        @table[idx] = @table.last if idx != @table.size - 1
        @table.pop
        value
      end

      def each
        return enum_for(:each) unless block_given?

        @table.each do |key, value|
          yield(key, value)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
data_structures_101-0.3.1 lib/data_structures_101/hash/bucket.rb
data_structures_101-0.3.0 lib/data_structures_101/hash/bucket.rb
data_structures_101-0.2.10 lib/data_structures_101/hash/bucket.rb
data_structures_101-0.2.9 lib/data_structures_101/hash/bucket.rb
data_structures_101-0.2.8 lib/data_structures_101/hash/bucket.rb
data_structures_101-0.2.7 lib/data_structures_101/hash/bucket.rb
data_structures_101-0.2.6 lib/data_structures_101/hash/bucket.rb