是否有 Perl 模块用于记住散列中键的顺序?

发布于 2024-12-17 01:29:09 字数 648 浏览 0 评论 0原文

是否存在具有 O(log(n)) 访问时间的基于树的有序键值存储的“规范”(纯)perl 实现?

我尝试在 CPAN 上搜索“tree”,但没有从中获得太多见解。

这个问题激发了我发布自己的问题。

编辑:所以问题表述不清楚。

我正在寻找的是一种存储,它几乎不能完成 %hash 的功能,但以更糟糕的算法复杂性为代价维持其键的顺序。到目前为止,我所知道的所有具有此类属性的结构都是基于某种树(B 树、红黑树等),因此得名。

这是我想看到的伪代码示例:

my $set = Some::Module->new();
$set->store( foo=>1 );
$set->store( bar=>2 );
$set->fetch( "foo" ); # 1
$set->keys(); # bar, foo and not foo, bar
$set->keysBetween( undef, "baz" ); # bar only

Is there a "canonical" (pure-)perl implementation of a tree-based ordered key-value storage with O(log(n)) access time?

I've tried searching for "tree" on CPAN but haven't got much insight from that.

This question inspired me to post my own.

EDIT: So the question was unclearly stated.

What I'm looking for is a storage that barely does what a %hash would do, but maintains the order of its keys at a price of worse algorithmic complexity. All structures with such properties I know so far are based on some kind of tree (B-tree, red-black tree etc), hence the title.

Here's a pseudocode example of what I'd like to see:

my $set = Some::Module->new();
$set->store( foo=>1 );
$set->store( bar=>2 );
$set->fetch( "foo" ); # 1
$set->keys(); # bar, foo and not foo, bar
$set->keysBetween( undef, "baz" ); # bar only

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

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

发布评论

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

评论(2

翻了热茶 2024-12-24 01:29:09

Tie::IxHashperlfaq4 建议:

use Tie::IxHash;
tie my %myhash, 'Tie::IxHash';

for ( my $i=0; $i<20; $i++ ) {

    $myhash{$i} = 2*$i;
}

my @keys = keys %myhash;
# @keys = (0,1,2,3,...)

描述

这个 Perl 模块实现了保留顺序的 Perl 散列
添加了哈希元素。订单不受影响
IxHash 中现有键对应的值已更改。这
元素也可以设置为任意提供的顺序。熟悉的
Perl 数组操作也可以在 IxHash 上执行。

如果这个模块不能满足要求,也可以尝试在 CPAN 上搜索“tie hash order”。

Tie::IxHash is the perlfaq4 recommendation:

use Tie::IxHash;
tie my %myhash, 'Tie::IxHash';

for ( my $i=0; $i<20; $i++ ) {

    $myhash{$i} = 2*$i;
}

my @keys = keys %myhash;
# @keys = (0,1,2,3,...)

DESCRIPTION

This Perl module implements Perl hashes that preserve the order in
which the hash elements were added. The order is not affected when
values corresponding to existing keys in the IxHash are changed. The
elements can also be set to any arbitrary supplied order. The familiar
perl array operations can also be performed on the IxHash.

Also try searching for "tie hash order" on CPAN if this module doesn't cut the mustard.

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