Sha256: 9b49337fe8ba5694005723e71b05c261f27518f6ae774c97fcfac190892238a6

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module MemDump
    def self.remove_node(dump, removed_node)
        remaining_records = Hash.new
        non_roots = Set.new
        dump.each_record do |r|
            address = (r['address'] || r['root'])
            remaining_records[address] = r

            if refs = r['references']
                refs.each do |ref_address|
                    non_roots << ref_address
                end
            end
        end

        roots = remaining_records.each_key.
            find_all { |a| !non_roots.include?(a) }

        queue = roots.dup
        selected_records = Hash.new
        while !queue.empty?
            address = queue.shift
            next if address == removed_node

            if record = remaining_records.delete(address)
                selected_records[address] = record
                if refs = record['references']
                    refs.each do |ref_address|
                        queue << ref_address
                    end
                end
            end
        end

        selected_records.values.reverse.map do |r|
            if refs = r['references']
                refs.delete_if { |a| !selected_records.has_key?(a) }
            end
            r
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memdump-0.2.0 lib/memdump/remove_node.rb
memdump-0.1.0 lib/memdump/remove_node.rb