Created
April 21, 2011 05:33
-
-
Save dshimy/933771 to your computer and use it in GitHub Desktop.
Efficient Memory Deduplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A memory efficient deduplication class used in cases where a | |
# duplicate object can only occur within n objects of each other. | |
class MemoryDedup | |
# Returns a new deduper. | |
# | |
# == Options | |
# * <tt>:size</tt> - the number of objects to store in the cache | |
def initialize(options = {}) | |
@size = options{:size} || 1000 | |
@current = {} | |
@next = {} | |
end | |
# Returns true if the object has been seen in the past :size unique | |
# objects, false otherwise. | |
def exists?(obj) | |
if (2 * @current.size) > @size | |
@current = @next | |
@next = {} | |
end | |
@next[obj] = true | |
if @current.has_key?(obj) | |
return true | |
else | |
@current[obj] = true | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment