Skip to content

Instantly share code, notes, and snippets.

@dshimy
Created April 21, 2011 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshimy/933771 to your computer and use it in GitHub Desktop.
Save dshimy/933771 to your computer and use it in GitHub Desktop.
Efficient Memory Deduplication
# 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