首页 \ 问答 \ 为什么Guava不为小的ImmutableLists使用专门的类?(Why does Guava not use specialized classes for small ImmutableLists?)

为什么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 overloaded of() 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
更新时间:2022-06-01 10:06

最满意答案

我的意思是没有冒犯,但我花了一段时间来理解你的代码。 当您打算让其他人阅读时,您可能需要更明确地命名您的方法和参数。

作为一个局外人,我花了一段时间来理解你的代码中的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 the target_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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。