什么是快速的数据结构来实现对存储,作为指定在这里? [英] What is a fast data-structure to implement a pair storage, as specified here?

查看:186
本文介绍了什么是快速的数据结构来实现对存储,作为指定在这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的存储值对的。它必须公开一个简单的API: newPair,获取,删除,isMember 。值可以由任何一个 4位int 或指向另一对。因此,例如:

I am trying to implement a storage of pairs of values. It must expose a simple API: newPair, get, remove, isMember. A value can consist of either a 4-bit int or a pointer to another pair. So, for example:

Pointer a = API.newPair(13,12);
Pointer b = API.newPair(a,7);
Pointer c = API.newPair(2,b);
Pointer d = API.newPair(a,7);

等。只是有一点需要注意,虽然:一对绝不能存储两次。如果试图添加已经存在一对,它只是返回现有的指针。因此,例如,在code以上, B = D

我的幼稚的做法涉及栈对。一对新加入每一次,我只是重复堆栈,如果我发现了一对在那里,我回到自己的立场。如果不是这样,我加入了新的元素添加到堆栈和返回堆栈大小。现在,我需要使它尽可能快。我现在的想法是只使用,而不是层叠的哈希表。

My naive implementation involved a stack of pairs. Each time a new pair was added, I just iterated the stack and, if I found the pair there, I returned its position. If not, I added a new element to the stack and returned the stack size. Now I need to make it as fast as possible. My current idea is to just use a hash-table instead of a stack.

所以,问题是:什么是实施这是一个快速的方法

So, the question is: what is a fast way to implement this?

推荐答案

一个双射的N×N - >ñ给出的康托尔配对函数的

A bijection NxN -> N given by Cantor Pairing Function

可以使用,如果N是无穷集。它的工作原理是N×N的连续分配的元素为N这样的:

can be used if N is an infinite set. It works by consecutive assigning elements of NxN to N this way:

当x,y是非负整数,可以使用在 ElegantPairing 的引入的映射

When x,y are non negative integers the mapping introduced in ElegantPairing may be used

x >= y ? x * x + x + y : x + y * y;  where x, y >= 0

它通过沿squere的边缘赋值。

which works by assigning values along the edges of a squere.

int cantor_pairing( int k1, int k2) {
    return 0.5 * ( k1 + k2) * ( k1 + k2 + 1) + k2;
}

int szuzik_pairing( int x, int y) {
    if( x < 0 || y < 0) return -1;
    return x >= y ? x * x + x + y : x + y * y;
}


即使你的整数是关节,你不可能一直使用只是映射均匀分布


Even if your integers were joint uniformly distributed you couldn't have been used just the mapping

(X,Y) - > X

(x,y) -> x

即。完全无视他们的第二个。这是因为,在方形的N×N个函数f(x,y)的将是1 / N ^ 2,但边缘分布f_y(X)= 1 / N,这将是一碰撞的概率。 毕竟,最有可能不是这种情况下,因为用户会更频繁地选择小的数字再大。

i.e. completely ignoring the second of them. This is because on a square NxN f(x,y) would be 1/N^2 but marginal distribution f_y(x) = 1/N and this would be a probability of a collision. After all, most probably this is not the case, because user will more frequently choose small numbers then large.

这篇关于什么是快速的数据结构来实现对存储,作为指定在这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆