在java中将两个long编码为另一个原语

发布于 2024-11-27 06:37:37 字数 266 浏览 2 评论 0 原文

我有一个 Tuple 对象,它包含 3 个基元:Tuple(double, long, long)。为了避免创建大量的元组,我正在考虑使用 Trove 库的原始 MAP,它将两个原始作为键和值。就我而言,它将是 Map

我的问题:是否可以有效地将两个 long 编码为一个可以存储在地图中的原语,然后对它们进行解码?

I have a Tuple object that holds 3 primitives: Tuple(double, long, long). To avoid creating a huge amount of Tuple, I'm thinking using Trove library's primitive MAP, which would take two primitive as key and value. In my case, it would be Map<double, some primitive>.

My question: is it possible efficiently to encode the two long into a single primitive that I can store in the map, and later decode them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↙温凉少女 2024-12-04 06:37:37

是否可以有效地将两个 long 编码为单个原语

否,仅仅因为 long 是 64 位,并且没有 Java 原语比这更长。您需要一个 128 位原语来将两个长整型编码到其中。

is it possible efficiently to encode the two long into a single primitive

No, simply because longs are 64-bit, and no Java primitive is longer than that. You would need a 128-bit primitive to encode two longs into it.

℡Ms空城旧梦 2024-12-04 06:37:37

没错,您不能将两个 64 位原语打包到另一个原语中,而另一个原语的大小最多为 64 位。按照标准,doublelong 均由 64 个二进制数字映射。

问题是,您是否可以对您正在处理的号码施加一些限制。如果您知道,您将始终拥有偶数数字或非偶数数字,或者第一个组件将具有整数范围,或者您正在处理 1000 的众多,您可以在这里赢得一些比特。

实际上,您永远不会利用所有

2^64 x 2^64 组合

长值对的

。另一方面,处理值对上的映射也没什么大不了的。这就是让 Java 这样的面向对象语言不仅能够处理 C 中的 struct 等数据类型,而且能够将方法绑定到数据上的全部努力。
您可以在网络中找到 Pair 类的良好实现,例如 angelikalanger.com。或者您可以轻松地自己编写一个实现,特别是因为您只需要一对 Long 值。
还可以考虑使用 Pair> 或立即实现一个 Tuple 类而不是 Map,即键值组合,遵循以下轮廓配对实现。

最后,您甚至可以使用 内存数据库,例如 H2 来保存您的 元组(double, long, long) 条目。将其作为 Java 库包含在您的项目中并正确配置它就足够了。

顺便说一句,3元组称为三元组。因此,您可以正确地调用您的类 Triple(double, long, long) 或更好的 Triple(Double, Long, Long)

It's right, you cannot pack two 64-bit primitives into another primitive, which is at most 64 bits of size. Both, double and long by standard are mapped by 64 binary digits.

The question is, whether you can impose some restrictions on the numbers you are dealing with. If you know, you will always have even numbers or uneven numbers or the first component will have integer range or you are dealing with multitudes of 1000, you can win some bits here.

Practically speaking, you will never make use of all

2^64 x 2^64 combinations

of pairs of long values.

On the other hand, it's no big deal to handle maps on pairs of values. That was the whole effort to make object-oriented languages like Java to not only deal with data types like struct in C, but also to bind methods to the data.
You can find good implementations of a Pair class in the web, e.g. angelikalanger.com. Or you can easily code an implementation yourself, especially, since you only need a pair of Long values.
Also consider to use Pair<Double, Pair<Long, Long>> or implement a Tuple<M,N,T> class right away instead of a Map, i.e. key-value combination, following the outline of the Pair<M,N> implementation.

Finally, you could even employ an in-memory database like H2 to hold your Tuple(double, long, long) entries. It is enough to enclose it in your project as a Java library and configure it properly.

By the way, a 3-tuple is called a triple. Therefore, you could correctly call your class Triple(double, long, long) or better Triple(Double, Long, Long).

忆沫 2024-12-04 06:37:37

您可以使用 Trove 的双对象映射并将两个 long 编码为 BigInteger,但如果您的目标是严格遵守原始类型,那么这显然没有任何帮助。

You could use Trove's double-Object map and encode the two longs into a BigInteger, but if your objective is to stay strictly with primitive types, that obviously isn't any help.

一腔孤↑勇 2024-12-04 06:37:37

正如 Joonas 所说,没有任何一个原语可以容纳 128 位。可能满足您需求的是使用数组来保存两个长整型:Map。虽然 Doublelong[] 并不是严格意义上可能适合的原语。请记住,您不能将 double (small-d) 放入 Map 中,因为 Map 只能包含引用类型,而不能包含基元。

或者,Map(Double, Pair) 怎么样,其中 Pair 是一个保存两个 long 的小类?大多数图书馆都有类似的东西。

As Joonas says, there is no single primitive that will hold 128 bits. What might meet your need is to use an array to hold the two longs: Map<Double, long[]>. While Double and long[] are not strictly primitives that might suit. Remember that you cannot put double (small-d) into a Map as Maps can only contain reference types, not primitives.

Alternatively, how about Map(Double, Pair), where Pair is a small class to hold two longs? Most libraries have something like that lying around somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文