映射两组元素
有一组索引为 [0, n] 的元素。在任何时候,A 集中最多 m 个元素可以处于活动状态(使用中),明显的条件是 0 <= m <= n。我想在本地数组中索引这些活动元素。可以在程序执行时动态停用元素,并且我希望在激活新元素时可以重用其索引。
我想以最有效的方式将元素索引映射到其本地索引,因为我使用本地索引来快速查找活动元素数据。
简单散列函数(元素索引 == 本地索引)和通过关联列表进行暴力破解的可能解决方案对于大 n 来说不能很好地扩展。
数据结构的动态扩展/收缩是一个明显的优点。
谢谢
There is an A set of elements indexed [0, n]. At any time at most m elements from an A set can be active (in use), with an obvious condition that 0 <= m <= n. I would like to index those active elements inside a local array. An element can be deactivated dynamically while the program is executing and I would prefer that its index could be reused, when new elements are activated.
I would like to map an element index to it's local index in the most efficient way as I'm using the local index for fast lookup of active element data.
Possible solutions of trivial hash function (element index == local index) and brute forcing through an associative list do not scale well with large n.
Dynamic expansion/shrinking of data structure is an obvious plus.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有最快的代码(tanstatfc) - Michael Abrash
有您的问题有多种解决方案。
第一步是选择数据结构和哈希函数。
数据结构可以是:
1 普通数组
这是最简单的解决方案。如果您的分配很混乱(这意味着它们在空间中聚集在一起),这甚至可能是一个很好的解决方案。
它的工作原理如下:
您可以在此结构中容纳 2GB / 4 字节每个条目 = 5 亿个条目。
这最适合分组在靠近的集群中的数据。
如果索引是随机的,这将是低效的。
下面是 Delphi 伪代码:
使用虚拟内存的直接列表的示例代码
2 带链表的数组
这最适合具有非常随机索引的数据,碰撞变化很小。
成功与否很大程度上取决于您的哈希函数,它需要尽可能多地选择不同的数组条目,太多
冲突
,您将一直在同一个链表中行走。链表数组的示例代码
使用索引中的位来遍历树的 3 个二叉树
使用二叉树的示例代码
推荐阅读:
Knuth:TAOCP I:基础算法,第 2 章 ISBN 0-201-03809-9
科门、莱瑟森和Rivest:算法导论,第三章数据结构 ISBN 0-07-013143-0
There ain't no such thing as the fastest code (tanstatfc) - Michael Abrash
There are many solutions to your problem.
First step is to choose a datastructure and a hash function.
Datastructure can be:
1 Plain array
This is the simplest solution. And if your allocations are blobby (that means they are clustered together in space) this might even be a good solution.
Here's how it works:
You can fit 2GB / 4bytes per entry = 500 million entries in this structure.
This works best for data that grouped in clusters that are close together.
If the indexes are random, this will be inefficient.
Here's Delphi pseudo code:
Example code for straight list using Virtual memory
2 Array with linked list
This works best for data that has a very random index, with little change of colliding.
The success depends critically upon your hash function, it needs to select a different array entry as much as possible, too many
collisions
and you will just be walking the same linked list all the time.Example code for array of linked lists
3 binary tree using the bit in the index to traverse the tree
Example code using a binary tree
Recommend reading:
Knuth: TAOCP I: fundamental algorithms, chapter 2 ISBN 0-201-03809-9
Cormen, Leiserson & Rivest: Introduction to Algorithms, chapter III Data structures ISBN 0-07-013143-0