lib/rufus/lru.rb in rufus-lru-1.0.3 vs lib/rufus/lru.rb in rufus-lru-1.0.4
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2007-2010, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2007-2012, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -23,119 +23,127 @@
#++
module Rufus
module Lru
- VERSION = '1.0.3'
-end
-end
+ VERSION = '1.0.4'
-#
-# A Hash that has a max size. After the maxsize has been reached, the
-# least recently used entries (LRU hence), will be discared to make
-# room for the new entries.
-#
-# require 'rubygems'
-# require 'rufus/lru'
-#
-# h = LruHash.new(3)
-#
-# 5.times { |i| h[i] = "a" * i }
-#
-# puts h.inspect # >> {2=>"aa", 3=>"aaa", 4=>"aaaa"}
-#
-# h[:newer] = "b"
-#
-# puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
-#
-#
-class LruHash < Hash
+ #
+ # A Hash that has a max size. After the maxsize has been reached, the
+ # least recently used entries (LRU hence), will be discared to make
+ # room for the new entries.
+ #
+ # require 'rubygems'
+ # require 'rufus/lru'
+ #
+ # h = LruHash.new(3)
+ #
+ # 5.times { |i| h[i] = "a" * i }
+ #
+ # puts h.inspect # >> {2=>"aa", 3=>"aaa", 4=>"aaaa"}
+ #
+ # h[:newer] = "b"
+ #
+ # puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
+ #
+ # Nota bene: this class is not threadsafe.
+ #
+ class Hash < ::Hash
- attr_reader :maxsize
+ attr_reader :maxsize
+ attr_reader :lru_keys
- # Initializes a LruHash with a given maxsize.
- #
- def initialize (maxsize)
+ # Initializes a LruHash with a given maxsize.
+ #
+ def initialize(maxsize)
- super()
+ super()
- @maxsize = maxsize
- @lru_keys = []
- end
+ @maxsize = maxsize
+ @lru_keys = []
+ end
- def maxsize= (s)
+ def maxsize=(i)
- @maxsize = s
- remove_lru
- end
+ @maxsize = i
+ remove_lru
- def clear
+ i
+ end
- super
- @lru_keys.clear
- end
+ def clear
- # Returns the keys with the lru in front.
- #
- def ordered_keys
+ @lru_keys.clear
- @lru_keys
- end
+ super
+ end
- def [] (key)
+ # Returns the keys with the lru in front.
+ #
+ alias ordered_keys lru_keys
- value = super
- return nil unless value
- touch(key)
+ def [](key)
- value
- end
+ return nil unless has_key?(key)
- def []= (key, value)
+ touch(key)
- remove_lru
- super
- touch(key)
+ super
+ end
- value
- end
+ def []=(key, value)
- def merge! (hash)
+ remove_lru
+ touch(key)
- hash.each { |k, v| self[k] = v }
+ super
+ end
- # not using 'super', but in order not guaranteed at all...
- end
+ def merge!(hash)
- def delete (key)
+ hash.each { |k, v| self[k] = v }
- value = super
- @lru_keys.delete(key)
+ # not using 'super', but in order not guaranteed at all...
+ end
- value
- end
+ def delete(key)
- protected
+ @lru_keys.delete(key)
- # Puts the key on top of the lru 'stack'.
- # The bottom being the lru place.
- #
- def touch (key)
+ super
+ end
- @lru_keys.delete(key)
- @lru_keys << key
- end
+ # Returns a regular Hash with the entries in this hash.
+ #
+ def to_h
- # Makes sure that the hash fits its maxsize. If not, will remove
- # the least recently used items.
- #
- def remove_lru
+ {}.merge!(self)
+ end
- while size >= @maxsize
+ protected
- key = @lru_keys.delete_at(0)
- delete(key)
+ # Puts the key on top of the lru 'stack'.
+ # The bottom being the lru place.
+ #
+ def touch(key)
+
+ @lru_keys.delete(key)
+ @lru_keys << key
end
+
+ # Makes sure that the hash fits its maxsize. If not, will remove
+ # the least recently used items.
+ #
+ def remove_lru
+
+ while size >= @maxsize
+ delete(@lru_keys.delete_at(0))
+ end
+ end
end
+end
+end
+
+class LruHash < Rufus::Lru::Hash
end