Perl 数据结构具有移位/推送和快速查找功能?
我正在接收事件,并且我想跟踪最后 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想要有序的哈希,请查看 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.