Guest User

Untitled

a guest
May 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. diff --git a/kernel/bootstrap/hash.rb b/kernel/bootstrap/hash.rb
  2. index ca1a696..0ee3fd5 100644
  3. --- a/kernel/bootstrap/hash.rb
  4. +++ b/kernel/bootstrap/hash.rb
  5. @@ -106,7 +106,7 @@ class Hash
  6.  
  7. # Creates a fully-formed instance of Hash.
  8. #--
  9. - # @size is the number of pairs, equivalent to <code>hsh.size</code>.
  10. + # @count is the number of pairs, equivalent to <code>hsh.count</code>.
  11. # @records is the number of entries in +@bins+.
  12. # @bins is the vector of storage for the bucket chains.
  13. #++
  14. @@ -122,7 +122,7 @@ class Hash
  15. # We don't need the nanny checking our symbols
  16. set_instance_variable :@records, MIN_SIZE
  17. set_instance_variable :@bins, Tuple.new(MIN_SIZE)
  18. - set_instance_variable :@size, 0
  19. + set_instance_variable :@count, 0
  20. end
  21.  
  22. # Returns the storage vector for Hash. The object should provide
  23. @@ -131,17 +131,21 @@ class Hash
  24. @bins
  25. end
  26.  
  27. - # Returns the size of the storage vector (+@bins+).
  28. + # Returns the magnitude of the storage vector (+@bins+).
  29. def records
  30. @records
  31. end
  32.  
  33. - def size
  34. - @size
  35. + # Retuns the number of items in the Hash.
  36. + def count
  37. + @count
  38. end
  39.  
  40. - def size=(size)
  41. - @size = size
  42. + # Increments the number of items in the Hash and requests
  43. + # that the Hash be redistributed. The request will be
  44. + # honored if the Hash's density exceeds a threshold.
  45. + def count=(count)
  46. + @count = count
  47. redistribute false
  48. end
  49.  
  50. @@ -167,9 +171,9 @@ class Hash
  51. entry = @bins[bin]
  52. return entry if entry
  53.  
  54. - self.size += 1
  55. + self.count += 1
  56.  
  57. - # recalc the bin since #size may have invoked redistribute
  58. + # recalc the bin since #count may have invoked redistribute
  59. @bins[entry_bin(key_hash)] = Bucket.new key, nil, key_hash
  60. end
  61.  
  62. @@ -183,7 +187,7 @@ class Hash
  63. # Iterator instance will be invalid after a call to redistribute.
  64. # If +rehash+ is true, recalculate the key_hash for each key.
  65. def redistribute(rehash = true)
  66. - resize = @size >= MAX_DENSITY * @records
  67. + resize = @count >= MAX_DENSITY * @records
  68. return unless rehash or resize
  69.  
  70. i = to_iter
  71. diff --git a/kernel/common/hash.rb b/kernel/common/hash.rb
  72. index baec56f..45bca28 100644
  73. --- a/kernel/common/hash.rb
  74. +++ b/kernel/common/hash.rb
  75. @@ -56,7 +56,7 @@ class Hash
  76.  
  77. # Pickaxe claims that defaults are compared, but MRI 1.8.[46] doesn't actually do that
  78. # return false unless other.default == default
  79. - each { |k, v| return false unless other[k] == v }
  80. + each_item { |k, v| return false unless other[k] == v }
  81. true
  82. end
  83.  
  84. @@ -92,7 +92,7 @@ class Hash
  85.  
  86. hsh = key_hash key
  87. entry = self.entry key, hsh
  88. - self.size += 1 if entry.set(key, value, hsh)
  89. + self.count += 1 if entry.set(key, value, hsh)
  90.  
  91. value
  92. end
  93. @@ -140,7 +140,7 @@ class Hash
  94.  
  95. bins[bin] = entry.next if result.nil?
  96. unless result
  97. - self.size -= 1
  98. + self.count -= 1
  99. return entry.value
  100. end
  101. end
  102. @@ -149,8 +149,6 @@ class Hash
  103. end
  104.  
  105. def delete_if(&block)
  106. - raise LocalJumpError, "no block given" unless block_given? or empty?
  107. -
  108. select(&block).each { |k, v| delete k }
  109.  
  110. self
  111. @@ -164,29 +162,24 @@ class Hash
  112. end
  113.  
  114. def each_key
  115. - raise LocalJumpError, "no block given" unless block_given? or empty?
  116. -
  117. - each { |k, v| yield k }
  118. + each_item { |k, v| yield k }
  119.  
  120. self
  121. end
  122.  
  123. def each
  124. - raise LocalJumpError, "no block given" unless block_given? or empty?
  125. -
  126. - i = to_iter
  127. - while entry = i.next
  128. - begin
  129. - yield [entry.key, entry.value]
  130. - end while entry = entry.next
  131. - end
  132. + each_item { |k, v| yield [k, v] }
  133.  
  134. self
  135. end
  136.  
  137. - def each_pair
  138. - raise LocalJumpError, "no block given" unless block_given? or empty?
  139. -
  140. + # Yields key, value for each item in the Hash. This method
  141. + # is necessary to protect the essential iterator from subclasses
  142. + # (e.g. REXML::Attribute) that replace #each with a version
  143. + # that is incompatible with the dependencies here (e.g. defining
  144. + # #each -> #each_attribute -> #each_value, where we had been
  145. + # defining #each_value in terms of #each).
  146. + def each_item
  147. i = to_iter
  148. while entry = i.next
  149. begin
  150. @@ -197,20 +190,22 @@ class Hash
  151. self
  152. end
  153.  
  154. - def each_value
  155. - raise LocalJumpError, "no block given" unless block_given? or empty?
  156. + alias_method :each_pair, :each_item
  157.  
  158. - each { |k, v| yield v }
  159. + def each_value
  160. + each_item { |k, v| yield v }
  161.  
  162. self
  163. end
  164.  
  165. + # Returns +size+ == 0. Does not use #count so that a subclass's
  166. + # idea of size will be consistent with emptiness.
  167. def empty?
  168. size == 0
  169. end
  170.  
  171. def index(value)
  172. - each { |k, v| return k if v == value }
  173. + each_item { |k, v| return k if v == value }
  174. nil
  175. end
  176.  
  177. @@ -220,7 +215,7 @@ class Hash
  178.  
  179. out = []
  180. RecursionGuard.inspect(self) do
  181. - each do |key, value|
  182. + each_item do |key, value|
  183. str = key.inspect
  184. str << '=>'
  185. str << value.inspect
  186. @@ -233,7 +228,7 @@ class Hash
  187.  
  188. def invert
  189. inverted = {}
  190. - each { |key, value| inverted[value] = key }
  191. + each_item { |key, value| inverted[value] = key }
  192. inverted
  193. end
  194.  
  195. @@ -261,7 +256,7 @@ class Hash
  196.  
  197. def merge!(other)
  198. other = Type.coerce_to other, Hash, :to_hash
  199. - other.each do |k, v|
  200. + other.each_item do |k, v|
  201. if block_given? and key? k
  202. self[k] = yield k, self[k], v
  203. else
  204. @@ -281,8 +276,6 @@ class Hash
  205. end
  206.  
  207. def reject!
  208. - raise LocalJumpError, "no block given" unless block_given? or empty?
  209. -
  210. rejected = select { |k, v| yield k, v }
  211. return if rejected.empty?
  212.  
  213. @@ -295,7 +288,7 @@ class Hash
  214. return self if self.equal? other
  215.  
  216. clear
  217. - other.each { |k, v| self[k] = v }
  218. + other.each_item { |k, v| self[k] = v }
  219.  
  220. if other.default_proc
  221. @default = other.default_proc
  222. @@ -309,8 +302,6 @@ class Hash
  223. end
  224.  
  225. def select
  226. - raise LocalJumpError, "no block given" unless block_given? or empty?
  227. -
  228. selected = []
  229. i = to_iter
  230. while e = i.next
  231. @@ -328,12 +319,13 @@ class Hash
  232. i = to_iter
  233. if entry = i.next
  234. bins[i.index] = entry.next
  235. - self.size -= 1
  236. + self.count -= 1
  237. return entry.key, entry.value
  238. end
  239. end
  240.  
  241. - alias_method :length, :size
  242. + alias_method :length, :count
  243. + alias_method :size, :count
  244.  
  245. def sort(&block)
  246. to_a.sort(&block)
  247. @@ -352,7 +344,7 @@ class Hash
  248. end
  249.  
  250. def value?(value)
  251. - each { |k, v| return true if v == value }
  252. + each_item { |k, v| return true if v == value }
  253. false
  254. end
  255. alias_method :has_value?, :value?
  256. diff --git a/kernel/common/misc.rb b/kernel/common/misc.rb
  257. index 926133d..07cee07 100644
  258. --- a/kernel/common/misc.rb
  259. +++ b/kernel/common/misc.rb
  260. @@ -38,7 +38,7 @@ class NilClass
  261. alias_method :|, :^
  262.  
  263. def call(*a)
  264. - raise LocalJumpError, "not callable"
  265. + raise LocalJumpError, "no block given or not callable"
  266. end
  267. end
Add Comment
Please, Sign In to add comment