Skip Menu |

This queue is for tickets about the data-trie CPAN distribution.

Report information
The Basics
Id: 15511
Status: new
Priority: 0/
Queue: data-trie

People
Owner: Nobody in particular
Requestors: grousse [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Allo storage of multiple values
The following patch allow the storage of multiple items for each word. I used a global variable, as there is no way currently to store preferences for a single trie, as it is only represented as a collection of nodes. Unless each node keep a pointer to its parent or to the root, it would need to duplicate options endlessly in each node.
diff -Naur Data-Trie-0.01/test.pl Data-Trie/test.pl --- Data-Trie-0.01/test.pl 2004-08-09 19:19:51.000000000 +0200 +++ Data-Trie/test.pl 2005-11-03 21:44:56.000000000 +0100 @@ -1,7 +1,7 @@ use strict; -use Test; +use Test::More; -BEGIN { plan tests => 7 } +BEGIN { plan tests => 9 } #find the module use Data::Trie; @@ -22,3 +22,15 @@ #print "Step 7: deleting the lemonade\n"; ok($t->remove('lemonade')); +my $t1 = Data::Trie->new(); +$t1->add('word', 'foo'); +$t1->add('word', 'bar'); +my ($result1, $data1) = $t1->lookup('word'); +is($data1, 'bar', 'univalued trie keep only last data'); + +$Data::Trie::MULTIVALUED = 1; +my $t2 = Data::Trie->new(); +$t2->add('word', 'foo'); +$t2->add('word', 'bar'); +my ($result2, $data2) = $t2->lookup('word'); +is_deeply($data2, [ 'foo', 'bar' ], 'multivalued trie keep all data'); diff -Naur Data-Trie-0.01/Trie.pm Data-Trie/Trie.pm --- Data-Trie-0.01/Trie.pm 2004-08-09 19:55:54.000000000 +0200 +++ Data-Trie/Trie.pm 2005-11-03 21:42:50.000000000 +0100 @@ -4,14 +4,16 @@ package Data::Trie; +our $MULTIVALUED = 0; + #creates a new Trie node and initializes its value and daughters to zero sub new { - my $self = {}; my $class = shift; - #does this node terminate a word? - $self->{value} = 0; - #is this node terminate a prefix of further words? - $self->{daughters} = {}; + + my $self = { + value => 0, #does this node terminate a word? + daughters => {}, #does this node terminate a prefix of further words? + }; bless $self, $class; } @@ -70,7 +72,11 @@ } else { #set the value to 1 and store the data $daughter->{value} = 1; - $daughter->{data} = $data; + if ($MULTIVALUED) { + push(@{$daughter->{data}}, $data); + } else { + $daughter->{data} = $data; + } } return 1; }