Sha256: 7bcc7f5ebe29a47685b569dc859c85df8428351f6b63bbb72081cd45a1dc247a

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module DataStructures101
    module Hash
        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 {|_key, _| _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 {|_key, _| _key == key}
                pair.nil? ? nil : pair.last
            end

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

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

        end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_structures_101-0.2.1 lib/data_structures_101/hash/bucket.rb