为什么Guava不为小的ImmutableLists使用专门的类?(Why does Guava not use specialized classes for small ImmutableLists?)
Guava的
ImmutableList
有一系列重载of()
方法。 正如在这个解决的问题的上下文中所讨论的那样,存在这些以避免在将varargs与泛型混合时发生的警告。但除此之外,0和1参数方法都依赖于专门的列表实现。 似乎可以对2..11参数方法做同样的事情,从而减少这些列表的内存消耗 - 沿着
final class ImmutableListWith2Elements<E> extends ImmutableList<E> { final E e1; final E e2; ...
相反,它们使用基于数组的实现,这意味着除了内容引用之外,还存储数组对象和对数组的引用。 你能帮我理解这里涉及的权衡吗?
Guava's
ImmutableList
has a series of overloadedof()
methods. As discussed in the context of this solved question, these exist to avoid the warnings that occur when mixing varargs with generics.But in addition to that, the 0 and 1 parameter methods each rely on a specialized list implementation. It would seem that the same thing could be done for the 2..11 parameter methods, thereby reducing memory consumption of these lists - along the lines of
final class ImmutableListWith2Elements<E> extends ImmutableList<E> { final E e1; final E e2; ...
Instead, they use an array-based implementation, which means storing an array object and a reference to the array in addition to the content references. Can you help me understand the trade-offs involved here?
原文:https://stackoverflow.com/questions/10568440
最满意答案
我的意思是没有冒犯,但我花了一段时间来理解你的代码。 当您打算让其他人阅读时,您可能需要更明确地命名您的方法和参数。
作为一个局外人,我花了一段时间来理解你的代码中的
s
实际上代表了target_number
。 评论您的方法以指示如何使用它们以及预期的返回值应该是什么,这也是一个好主意。integers = [1,2,3,4,5,6,7,8,9,3,4,5,6,7,8,4,3,2,1] target_number = 10 $current_index = [0] def set_right_index(integers, leftmost_index, leftmost_int, target_number) integers.each_with_index do |int, i| next if i <= leftmost_index return i if int == target_number - leftmost_int end nil end def find_pairs(integers, target_number) sums = {} integers.each_with_index do |int, i| next if i < $current_index.max # debug = [] # debug << set_right_index(integers, i, int, target_number) right_index = set_right_index(integers, i, int, target_number) if right_index.nil? return sums if i == integers.length - 1 next end sums[right_index] = [int, (target_number - int)] $current_index << i # sums.merge! sum_pairs(integers[0..right_index], target_number) #<= calling this returns a `stack overflow error` :P because the methods just keeps calling itself sums end end
现在,如果你打电话
find_pairs(integers, target_number)
返回值将如下所示:
{8=>[1, 9], 7=>[2, 8], 6=>[3, 7], 5=>[4, 6], 11=>[5, 5], 10=>[6, 4], 9=>[7, 3], 17=>[8, 2], 18=>[9, 1], 13=>[3, 7], 12=>[4, 6], 15=>[6, 4], 16=>[7, 3]}
你说:
问题是它返回一个散列,最左边的索引作为键,而对作为值。 我试图从另一个函数(#sum_pairs)调用这个递归函数,这样我就可以格式化我需要的输出
请告诉我你到底是什么意思,
so that I can then format the output I need
你想要最终结果怎么样?编辑#1
出于调试目的,我将所有结果放在一个数组中,结果如下所示
integers = [1,2,3,4,5,6,7,8,9,3,4,5,6,7,8,4,3,2,1] target_number = 10 => [{:left_index=>0, :right_index=>8, :values=>[1, 9]}, {:left_index=>1, :right_index=>7, :values=>[2, 8]}, {:left_index=>2, :right_index=>6, :values=>[3, 7]}, {:left_index=>3, :right_index=>5, :values=>[4, 6]}, {:left_index=>4, :right_index=>11, :values=>[5, 5]}, {:left_index=>5, :right_index=>10, :values=>[6, 4]}, {:left_index=>6, :right_index=>9, :values=>[7, 3]}, {:left_index=>7, :right_index=>17, :values=>[8, 2]}, {:left_index=>8, :right_index=>18, :values=>[9, 1]}, {:left_index=>9, :right_index=>13, :values=>[3, 7]}, {:left_index=>10, :right_index=>12, :values=>[4, 6]}, {:left_index=>12, :right_index=>15, :values=>[6, 4]}, {:left_index=>13, :right_index=>16, :values=>[7, 3]}, {:left_index=>14, :right_index=>17, :values=>[8, 2]}]
请告诉我您希望最终结果如何,以便我可以尝试一下。
I mean no offense but it took me a while to understand your code. You may need to name your methods and parameters more explicitly when you intend other people to read.
As an outsider, It took me a while to understand the
s
throughout your code actually represented thetarget_number
. It's also a good idea to comment your methods indicating how to use them and what the expected return value should be.integers = [1,2,3,4,5,6,7,8,9,3,4,5,6,7,8,4,3,2,1] target_number = 10 $current_index = [0] def set_right_index(integers, leftmost_index, leftmost_int, target_number) integers.each_with_index do |int, i| next if i <= leftmost_index return i if int == target_number - leftmost_int end nil end def find_pairs(integers, target_number) sums = {} integers.each_with_index do |int, i| next if i < $current_index.max # debug = [] # debug << set_right_index(integers, i, int, target_number) right_index = set_right_index(integers, i, int, target_number) if right_index.nil? return sums if i == integers.length - 1 next end sums[right_index] = [int, (target_number - int)] $current_index << i # sums.merge! sum_pairs(integers[0..right_index], target_number) #<= calling this returns a `stack overflow error` :P because the methods just keeps calling itself sums end end
Now, if you call
find_pairs(integers, target_number)
the return value will look like this:
{8=>[1, 9], 7=>[2, 8], 6=>[3, 7], 5=>[4, 6], 11=>[5, 5], 10=>[6, 4], 9=>[7, 3], 17=>[8, 2], 18=>[9, 1], 13=>[3, 7], 12=>[4, 6], 15=>[6, 4], 16=>[7, 3]}
and you said:
problem is it returns a hash with the leftmost index as key and the pair as value. I'm trying to call this recursive function from another function (#sum_pairs) so that I can then format the output I need
Please tell me exactly what you mean by
so that I can then format the output I need
How would you like the final result to be like?EDIT#1
For debugging purposes, I put all the results in an array instead and the result looks like this
integers = [1,2,3,4,5,6,7,8,9,3,4,5,6,7,8,4,3,2,1] target_number = 10 => [{:left_index=>0, :right_index=>8, :values=>[1, 9]}, {:left_index=>1, :right_index=>7, :values=>[2, 8]}, {:left_index=>2, :right_index=>6, :values=>[3, 7]}, {:left_index=>3, :right_index=>5, :values=>[4, 6]}, {:left_index=>4, :right_index=>11, :values=>[5, 5]}, {:left_index=>5, :right_index=>10, :values=>[6, 4]}, {:left_index=>6, :right_index=>9, :values=>[7, 3]}, {:left_index=>7, :right_index=>17, :values=>[8, 2]}, {:left_index=>8, :right_index=>18, :values=>[9, 1]}, {:left_index=>9, :right_index=>13, :values=>[3, 7]}, {:left_index=>10, :right_index=>12, :values=>[4, 6]}, {:left_index=>12, :right_index=>15, :values=>[6, 4]}, {:left_index=>13, :right_index=>16, :values=>[7, 3]}, {:left_index=>14, :right_index=>17, :values=>[8, 2]}]
Please let me know how you want the end result to be like so I can give it a try.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
将递归函数的返回值存储在ruby中的变量中(Storing the return value of a recursive function in a variable in ruby)[2023-05-10]
我的意思是没有冒犯,但我花了一段时间来理解你的代码。 当您打算让其他人阅读时,您可能需要更明确地命名您的方法和参数。 作为一个局外人,我花了一段时间来理解你的代码中的s实际上代表了target_number 。 评论您的方法以指示如何使用它们以及预期的返回值应该是什么,这也是一个好主意。 integers = [1,2,3,4,5,6,7,8,9,3,4,5,6,7,8,4,3,2,1] target_number = 10 $current_index = [0] def set_right_index ... -
Ruby函数可以检测请求的返回值数量吗?(Possible for a Ruby function to detect number of return values requested?)[2023-02-13]
不。Ruby环境不会提供这些信息。 你可以使用一个异常和ParseTree找出调用代码的样子,但这是过度杀伤:) No. The Ruby environment doesn't give you that information. You could use an exception and ParseTree to find out what the calling code looks like, but that is overkill :) -
何时返回递归函数?(When to Return a Recursive Function?)[2023-12-03]
我建议更仔细地学习return关键字,并多练习递归。 return display_r(node->left_); // this following code would not be executed in your example, // you've already returned out of the function! std::cout << node->value_ << std::endl; return display_r(node->right_); 这里的回报是必要的: if( ... -
打破循环和返回值,angularJs中的递归函数(break for loop and return value , recursive function in angularJs)[2021-01-17]
尝试以下操作。 它会在每次迭代结束时检查是否已设置密钥,如果已设置密钥,则返回密钥。 这也会从较低的递归水平“起泡”。 搜索顺序是深度优先。 $scope.findSelected = function (object, name){ var key; for (var i = 0; i < object.length; i++){ var children =object[i]; var vName = children.Value.Name; ... -
你不能从那样的异步调用return ...你需要利用回调: app.get('/sitemap',function(request,response){ var query = JSON.stringify(request.query.transaction_id).replace(/(^"|"$)/g, '').replace(/\s+/, "") ; var transaction_details = function(callback) { MongoClient. ...
-
共识是尽可能避免全局变量。 我会像这样递归地构建集合: def recursive(url) ### ... result = [] hash["values"].each do |x| result << x["links"]["self"]["href"] end if hash["next"] result += recursive(hash["next"]) end result end 或将集合移交给函数: def recursive(url, ...
-
在你的第一段代码中,我希望你的输入中看到一个nil ,在你的第二个版本中你会得到对象Simple.new('Z',[]) 如果那是你的问题,那是因为项目g有子项,第一个会设置一个值recusrively,但第二个将取消设置值,所以第二次通过循环, holder被设置为nil 。 编辑:实际上我对上面例子的结果的分析是错误的 ,因为顶级列表中的最后一项确实包含搜索的项目。 然而,两种解决方案之间的问题分析和行为差异仍然存在。 你可能只想要这个: def looper(d) holder = nil ...
-
首先,代码中存在一些冗余。 diff已分配但从未使用过。 你可以摆脱它: def run_app(name, startr) constant_var = name if startr == 1 some_name = modify name compare(some_name, constant_var) # recursive call run_app(some_name, 0) end 解决这个问题的标准方法是添加一个额外的参数来传递额外的信息: def run_app(na ...