Perl 数据结构具有移位/推送和快速查找功能?

发布于 2024-12-15 07:32:57 字数 381 浏览 0 评论 0原文

我正在接收事件,并且我想跟踪最后 N 个事件,并且对于任何新事件,计算过去发生类似事件的次数(这通常不会经常发生)。

因此,我需要具有队列语义(移位/推送)的东西以及对非插入顺序的键的快速查找。

当我们只关心计数时,我当前的解决方案是保留事件数组和计数散列,以便我可以在推送时增加适当的计数器并在轮班时减少它。但这不允许我跟踪更多的数据,而且很难概括。

另一种可能性是将事件放置在包含 N/K 个事件的 K+1 代哈希表中,跟踪插入顺序,并在最新的表已满时丢弃最旧的表。这让我在具有恒定移位/推送和线性查找(当 K=N 时)的队列与以最多两倍内存为代价的更快查找(K=1)之间进行权衡。

我认为像链接哈希表这样的东西会有更好的行为。 Perl 中是否存在这个或具有等效语义的任何内容?

I'm receiving events, and I want to keep track of the last N events, and for any new event, count how many times similar events have occurred in the past (this will in general not happen often).

Thus, I need something with queue semantics (shift/push) as well as fast lookups on a key that is NOT the insertion order.

My current solution, when we care about counting only, is to keep both an array of events and a hash of counts, so that I can increment the appropriate counter on pushes and decrement it on shifts. But that doesn't allow me to keep track of more than counts, and is awkward to generalize.

Another possibility was to place events in generations of K+1 hash tables containing N/K events, keeping track of the order of insertion, and discard the oldest table when the newest one is full. This gives me a tradeoff between a queue with constant shift/push and linear lookups (when K=N), and faster lookups at the expense of at most twice the memory (K=1).

I think something like a linked hash table would have a slightly better behaviour. Does this, or anything with equivalent semantics, exist in Perl?

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

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

发布评论

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

评论(1

瞄了个咪的 2024-12-22 07:32:57

如果您只想要有序的哈希,请查看 Tie::IxHash。它至少应该替换您同时保留数组和哈希的解决方案。

If you just want an ordered hash, take a look at Tie::IxHash. It should at least replace your solution where you keep both an array and a hash.

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