Initial commit
[tie-hash-sorted-xs.git] / lib / Tie / Hash / Sorted / XS.pm
CommitLineData
f0a05402
MG
1package Tie::Hash::Sorted::XS;
2
3use 5.014000;
4use strict;
5use warnings;
6
7use Tree::SizeBalanced;
8
9our $VERSION = '0.000_001';
10
11sub TIEHASH {
12 my ($class, $tree) = @_;
13 $tree //= Tree::SizeBalanced::str_any->new;
14 bless \$tree, $class
15}
16
17sub FETCH {
18 my ($self, $key) = @_;
19 my ($k, $v) = $$self->find($key);
20 $v
21}
22
23sub STORE {
24 my ($self, $key, $value) = @_;
25 $$self->delete($key);
26 $$self->insert($key, $value);
27}
28
29sub DELETE {
30 my ($self, $key) = @_;
31 $$self->delete($key);
32}
33
34# sub CLEAR unimplemented
35
36sub EXISTS {
37 my ($self, $key) = @_;
38 my @list = $$self->find($key);
39 @list > 0;
40}
41
42sub FIRSTKEY {
43 my ($self) = @_;
44 $$self->find_min
45}
46
47sub NEXTKEY {
48 my ($self, $lastkey) = @_;
49 $$self->find_gt($lastkey);
50}
51
52sub SCALAR {
53 my ($self) = @_;
54 $$self->size
55}
56
571;
58__END__
59
60=encoding utf-8
61
62=head1 NAME
63
64Tie::Hash::Sorted::XS - hash with ordered keys backed by binary search tree
65
66=head1 SYNOPSIS
67
68 use Tie::Hash::Sorted::XS;
69
70 tie my %hash, 'Tie::Hash::Sorted::XS';
71
72 $hash{Jim} = 5;
73 $hash{Bob} = 3;
74 $hash{Anna} = 7;
75
76 my $keys = join ' ', keys %hash;
77
78 is $keys, 'Anna Bob Jim', 'keys are ordered';
79 is $hash{Bob}, 3, 'retrieval works';
80
81=head1 DESCRIPTION
82
83This module is not yet fully implemented. Current limitations include
84the CLEAR function not being implemented (meaning it is impossible to
85assign a list to a tied hash) and iteration being slow (O(n log n) to
86iterate over the whole hash). The latter is due to lack of suitable
87methods in the underlying L<Tree::SizeBalanced>.
88
89L<Tree::SizeBalanced> is an implementation of a size-balanced tree, a
90kind of self-balanced binary search tree. This is a data structure
91similar to a Perl hash that permits O(log n) insertion, deletion, and
92random access while keeping the keys sorted.
93
94This module is a C<tie> interface to L<Tree::SizeBalanced>. It allows
95one to create a hash that is implemented by a L<Tree::SizeBalanced>.
96These hashes should work similarly to regular Perl hashes except that
97keys will be ordered, keys can be any objects (not just strings), and
98they have different performance characteristics.
99
100The module is used by calling the tie function:
101
102=over
103
104=item B<tie> my %hash, 'Tie::Hash::Sorted::XS'[, I<$tree>]
105
106This ties a brand new hash to a given L<Tree::SizeBalanced> object (or
107if none is given, C<< Tree::SizeBalanced::str_any->new >> is used).
108
109Whenever this hash is iterated, the keys will come ordered.
110
111=back
112
113=head1 SEE ALSO
114
115L<Tree::SizeBalanced>, L<http://wcipeg.com/wiki/Size_Balanced_Tree>
116
117=head1 AUTHOR
118
119Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
120
121=head1 COPYRIGHT AND LICENSE
122
123Copyright (C) 2018 by Marius Gavrilescu
124
125This library is free software; you can redistribute it and/or modify
126it under the same terms as Perl itself, either Perl version 5.24.3 or,
127at your option, any later version of Perl 5 you may have available.
128
129
130=cut
This page took 0.016512 seconds and 4 git commands to generate.