X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FCollation.pm;h=f66e902856eabb828e025cb9cc7073105246cb23;hb=32014ec936b48809e9f2dae8f20a01c887253427;hp=aa905d2223d09022af844e2368d8768e69bc3004;hpb=e290206835dd4bc540d751ea2d1849255dd192f2;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Collation.pm b/lib/Text/Tradition/Collation.pm index aa905d2..f66e902 100644 --- a/lib/Text/Tradition/Collation.pm +++ b/lib/Text/Tradition/Collation.pm @@ -3,6 +3,8 @@ package Text::Tradition::Collation; use Graph::Easy; use IPC::Run qw( run binary ); use Text::Tradition::Collation::Reading; +use Text::Tradition::Collation::Path; +use XML::LibXML; use Moose; has 'graph' => ( @@ -17,6 +19,7 @@ has 'graph' => ( path => 'edge', readings => 'nodes', paths => 'edges', + relationships => 'edges', }, default => sub { Graph::Easy->new( undirected => 0 ) }, ); @@ -34,16 +37,9 @@ has 'svg' => ( predicate => 'has_svg', ); -has 'graphviz' => ( - is => 'ro', - isa => 'Str', - writer => '_save_graphviz', - predicate => 'has_graphviz', - ); - has 'graphml' => ( is => 'ro', - isa => 'XML::LibXML::Document', + isa => 'Str', writer => '_save_graphml', predicate => 'has_graphml', ); @@ -57,10 +53,34 @@ has 'lemmata' => ( ); has 'wit_list_separator' => ( - is => 'rw', - isa => 'Str', - default => ', ', - ); + is => 'rw', + isa => 'Str', + default => ', ', + ); + +has 'baselabel' => ( + is => 'rw', + isa => 'Str', + default => 'base text', + ); + +has 'collapsed' => ( + is => 'rw', + isa => 'Bool', + ); + +has 'linear' => ( + is => 'rw', + isa => 'Bool', + default => 1, + ); + +has 'ac_label' => ( + is => 'rw', + isa => 'Str', + default => ' (a.c.)', + ); + # The collation can be created two ways: # 1. Collate a set of witnesses (with CollateX I guess) and process @@ -80,13 +100,56 @@ has 'wit_list_separator' => ( sub BUILD { my( $self, $args ) = @_; $self->graph->use_class('node', 'Text::Tradition::Collation::Reading'); + $self->graph->use_class('edge', 'Text::Tradition::Collation::Path'); # Pass through any graph-specific options. my $shape = exists( $args->{'shape'} ) ? $args->{'shape'} : 'ellipse'; $self->graph->set_attribute( 'node', 'shape', $shape ); } -# Wrappes around merge_nodes +# Wrapper around add_path + +around add_path => sub { + my $orig = shift; + my $self = shift; + + # Make sure there are three arguments + unless( @_ == 3 ) { + warn "Call add_path with args source, target, witness"; + return; + } + # Make sure the proposed path does not yet exist + my( $source, $target, $wit ) = @_; + $source = $self->reading( $source ) + unless ref( $source ) eq 'Text::Tradition::Collation::Reading'; + $target = $self->reading( $target ) + unless ref( $target ) eq 'Text::Tradition::Collation::Reading'; + foreach my $path ( $source->edges_to( $target ) ) { + if( $path->label eq $wit && $path->class eq 'edge.path' ) { + return; + } + } + # Do the deed + $self->$orig( @_ ); +}; + +# Wrapper around paths +around paths => sub { + my $orig = shift; + my $self = shift; + + my @result = grep { $_->class eq 'edge.path' } $self->$orig( @_ ); + return @result; +}; + +around relationships => sub { + my $orig = shift; + my $self = shift; + my @result = grep { $_->class eq 'edge.relationship' } $self->$orig( @_ ); + return @result; +}; + +# Wrapper around merge_nodes sub merge_readings { my $self = shift; @@ -97,6 +160,63 @@ sub merge_readings { return $self->graph->merge_nodes( @_ ); } +# Extra graph-alike utility +sub has_path { + my( $self, $source, $target, $label ) = @_; + my @paths = $source->edges_to( $target ); + my @relevant = grep { $_->label eq $label } @paths; + return scalar @paths; +} + +## Dealing with relationships between readings. This is a different +## sort of graph edge. + +sub add_relationship { + my( $self, $type, $source, $target, $global ) = @_; + + # Make sure there is not another relationship between these two + # readings already + $source = $self->reading( $source ) + unless ref( $source ) eq 'Text::Tradition::Collation::Reading'; + $target = $self->reading( $target ) + unless ref( $target ) eq 'Text::Tradition::Collation::Reading'; + foreach my $rel ( $source->edges_to( $target ) ) { + if( $rel->label eq $type && $rel->class eq 'edge.relationship' ) { + return; + } + } + + my $rel = Text::Tradition::Collation::Relationship->new( + 'sort' => $type, + 'global' => $global, + 'orig_relation' => [ $source, $target ], + ); + print STDERR sprintf( "Setting relationship %s between readings %s (%s)" + . " and %s (%s)\n", $type, + $source->label, $source->name, + $target->label, $target->name ); + $self->graph->add_edge( $source, $target, $rel ); + if( $global ) { + # Look for all readings with the source label, and if there are + # colocated readings with the target label, join them too. + foreach my $r ( $self->readings() ) { + next unless $r->label eq $source->label; + my @colocated = grep { $_->label eq $target->label } + $self->same_position_as( $r ); + if( @colocated ) { + warn "Multiple readings with same label at same position!" + if @colocated > 1; + my $dup_rel = Text::Tradition::Collation::Relationship->new( + 'sort' => $type, + 'global' => $global, + 'orig_relation' => [ $source, $target ], + ); + $self->graph->add_edge( $r, $colocated[0], $dup_rel ); + } + } + } +} + =head2 Output method(s) =over @@ -116,17 +236,55 @@ sub as_svg { my( $self, $recalc ) = @_; return $self->svg if $self->has_svg; - $self->_save_graphviz( $self->graph->as_graphviz() ) - unless( $self->has_graphviz && !$recalc ); + $self->collapse_graph_paths(); my @cmd = qw/dot -Tsvg/; my( $svg, $err ); - my $in = $self->graphviz; + my $in = $self->as_dot(); run( \@cmd, \$in, ">", binary(), \$svg ); - $self->{'svg'} = $svg; + $self->_save_svg( $svg ); + $self->expand_graph_paths(); return $svg; } +=item B + +print $graph->as_dot( $view, $recalculate ); + +Returns a string that is the collation graph expressed in dot +(i.e. GraphViz) format. The 'view' argument determines what kind of +graph is produced. + * 'path': a graph of witness paths through the collation (DEFAULT) + * 'relationship': a graph of how collation readings relate to + each other + +=cut + +sub as_dot { + my( $self, $view ) = @_; + $view = 'path' unless $view; + # TODO consider making some of these things configurable + my $dot = sprintf( "digraph %s {\n", $self->tradition->name ); + $dot .= "\tedge [ arrowhead=open ];\n"; + $dot .= "\tgraph [ rankdir=LR ];\n"; + $dot .= sprintf( "\tnode [ fontsize=%d, fillcolor=%s, style=%s, shape=%s ];\n", + 11, "white", "filled", $self->graph->get_attribute( 'node', 'shape' ) ); + + foreach my $reading ( $self->readings ) { + next if $reading->name eq $reading->label; + $dot .= sprintf( "\t\"%s\" [ label=\"%s\" ]\n", $reading->name, $reading->label ); + } + + my @edges = $view eq 'relationship' ? $self->relationships : $self->paths; + foreach my $edge ( @edges ) { + $dot .= sprintf( "\t\"%s\" -> \"%s\" [ color=\"%s\", fontcolor=\"%s\", label=\"%s\" ]\n", + $edge->from->name, $edge->to->name, '#000000', '#000000', $edge->label ); + } + + $dot .= "}\n"; + return $dot; +} + =item B print $graph->as_graphml( $recalculate ) @@ -156,51 +314,56 @@ sub as_graphml { $root->setAttributeNS( $xsi_ns, 'schemaLocation', $graphml_schema ); # Add the data keys for nodes - my @node_data = ( 'name', 'token', 'identical', 'position' ); - foreach my $ndi ( 0 .. $#node_data ) { + my @node_data = ( 'name', 'reading', 'identical', 'position' ); + # HACKY HACKY HACK Relationship data + my %node_data_keys; + my $ndi = 0; + foreach my $datum ( @node_data ) { + $node_data_keys{$datum} = 'dn'.$ndi++; my $key = $root->addNewChild( $graphml_ns, 'key' ); - $key->setAttribute( 'attr.name', $node_data[$ndi] ); + $key->setAttribute( 'attr.name', $datum ); $key->setAttribute( 'attr.type', 'string' ); $key->setAttribute( 'for', 'node' ); - $key->setAttribute( 'id', 'd'.$ndi ); + $key->setAttribute( 'id', $node_data_keys{$datum} ); } - # Add the data keys for edges - my %wit_hash; - my $wit_ctr = 0; - foreach my $wit ( $self->getWitnessList ) { - my $wit_key = 'w' . $wit_ctr++; - $wit_hash{$wit} = $wit_key; + # Add the data keys for edges, i.e. witnesses + my $edi = 0; + my %edge_data_keys; + foreach my $edge_key( qw/ witness_main witness_ante_corr relationship / ) { + $edge_data_keys{$edge_key} = 'de'.$edi++; my $key = $root->addNewChild( $graphml_ns, 'key' ); - $key->setAttribute( 'attr.name', $wit ); + $key->setAttribute( 'attr.name', $edge_key ); $key->setAttribute( 'attr.type', 'string' ); $key->setAttribute( 'for', 'edge' ); - $key->setAttribute( 'id', $wit_key ); + $key->setAttribute( 'id', $edge_data_keys{$edge_key} ); } - + # Add the graph, its nodes, and its edges my $graph = $root->addNewChild( $graphml_ns, 'graph' ); $graph->setAttribute( 'edgedefault', 'directed' ); $graph->setAttribute( 'id', 'g0' ); # TODO make this meaningful $graph->setAttribute( 'parse.edgeids', 'canonical' ); - $graph->setAttribute( 'parse.edges', $self->edges() ); + $graph->setAttribute( 'parse.edges', scalar($self->paths) ); $graph->setAttribute( 'parse.nodeids', 'canonical' ); - $graph->setAttribute( 'parse.nodes', $self->nodes() ); + $graph->setAttribute( 'parse.nodes', scalar($self->readings) ); $graph->setAttribute( 'parse.order', 'nodesfirst' ); my $node_ctr = 0; my %node_hash; - foreach my $n ( $self->readings ) { + foreach my $n ( sort { $a->name cmp $b->name } $self->readings ) { my %this_node_data = (); - foreach my $ndi ( 0 .. $#node_data ) { - my $value; - $this_node_data{'d'.$ndi} = $n->name if $node_data[$ndi] eq 'name'; - $this_node_data{'d'.$ndi} = $n->label - if $node_data[$ndi] eq 'token'; - $this_node_data{'d'.$ndi} = $n->primary->name if $n->has_primary; - $this_node_data{'d'.$ndi} = - $self->{'positions'}->node_position( $n ) - if $node_data[$ndi] eq 'position'; + foreach my $datum ( @node_data ) { + my $key = $node_data_keys{$datum}; + if( $datum eq 'name' ) { + $this_node_data{$key} = $n->name; + } elsif( $datum eq 'reading' ) { + $this_node_data{$key} = $n->label; + } elsif( $datum eq 'identical' && $n->has_primary ) { + $this_node_data{$key} = $n->primary->name; + } elsif( $datum eq 'position' ) { + $this_node_data{$key} = $n->position; + } } my $node_el = $graph->addNewChild( $graphml_ns, 'node' ); my $node_xmlid = 'n' . $node_ctr++; @@ -210,24 +373,127 @@ sub as_graphml { foreach my $dk ( keys %this_node_data ) { my $d_el = $node_el->addNewChild( $graphml_ns, 'data' ); $d_el->setAttribute( 'key', $dk ); - $d_el->appendTextChild( $this_node_data{$dk} ); + $d_el->appendText( $this_node_data{$dk} ); } } - foreach my $e ( $self->edges() ) { - my( $name, $from, $to ) = ( $e->name, - $node_hash{ $e->from()->name() }, - $node_hash{ $e->to()->name() } ); + # Add the path edges + my $edge_ctr = 0; + foreach my $e ( sort { $a->from->name cmp $b->from->name } $self->graph->edges() ) { + my( $name, $from, $to ) = ( 'e'.$edge_ctr++, + $node_hash{ $e->from->name() }, + $node_hash{ $e->to->name() } ); my $edge_el = $graph->addNewChild( $graphml_ns, 'edge' ); $edge_el->setAttribute( 'source', $from ); $edge_el->setAttribute( 'target', $to ); $edge_el->setAttribute( 'id', $name ); - # TODO Got to add the witnesses + if( $e->class() eq 'edge.path' ) { + # It's a witness path, so add the witness + my $base = $e->label; + my $key = $edge_data_keys{'witness_main'}; + # TODO kind of hacky + if( $e->label =~ /^(.*?)\s+(\(a\.c\.\))$/ ) { + $base = $1; + $key = $edge_data_keys{'witness_ante_corr'}; + } + my $wit_el = $edge_el->addNewChild( $graphml_ns, 'data' ); + $wit_el->setAttribute( 'key', $key ); + $wit_el->appendText( $base ); + } else { + # It's a relationship + my $rel_el = $edge_el->addNewChild( $graphml_ns, 'data' ); + $rel_el->setAttribute( 'key', $edge_data_keys{'relationship'} ); + $rel_el->appendText( $e->label() ); + } } # Return the thing - $self->_save_graphml( $graphml ); - return $graphml; + $self->_save_graphml( $graphml->toString(1) ); + return $graphml->toString(1); +} + +sub _make_xml_attr { + my $str = shift; + $str =~ s/\s/_/g; + $str =~ s/\W//g; + $str =~ "a$str" if $str =~ /^\d/; + return $str; +} + +sub collapse_graph_paths { + my $self = shift; + # Our collation graph has an path per witness. This is great for + # calculation purposes, but terrible for display. Thus we want to + # display only one path between any two nodes. + + return if $self->collapsed; + + print STDERR "Collapsing witness paths in graph...\n"; + + # Don't list out every witness if we have more than half to list. + my $majority = int( scalar( @{$self->tradition->witnesses} ) / 2 ) + 1; + # But don't compress if there are only a few witnesses. + $majority = 4 if $majority < 4; + foreach my $node( $self->readings ) { + my $newlabels = {}; + # We will visit each node, so we only look ahead. + foreach my $edge ( $node->outgoing() ) { + next unless $edge->class eq 'edge.path'; + add_hash_entry( $newlabels, $edge->to->name, $edge->name ); + $self->del_path( $edge ); + } + + foreach my $newdest ( keys %$newlabels ) { + my $label; + my @compressed_wits = (); + if( @{$newlabels->{$newdest}} < $majority ) { + $label = join( ', ', sort( @{$newlabels->{$newdest}} ) ); + } else { + ## TODO FIX THIS HACK + my @aclabels; + foreach my $wit ( @{$newlabels->{$newdest}} ) { + if( $wit =~ /^(.*?)(\s*\(?a\.\s*c\.\)?)$/ ) { + push( @aclabels, $wit ); + } else { + push( @compressed_wits, $wit ); + } + } + $label = join( ', ', 'majority', sort( @aclabels ) ); + } + + my $newpath = + $self->add_path( $node, $self->reading( $newdest ), $label ); + if( @compressed_wits ) { + $newpath->hidden_witnesses( \@compressed_wits ); + } + } + } + + $self->collapsed( 1 ); +} + +sub expand_graph_paths { + my $self = shift; + # Our collation graph has only one path between any two nodes. + # This is great for display, but not so great for analysis. + # Expand this so that each witness has its own path between any + # two reading nodes. + return unless $self->collapsed; + + print STDERR "Expanding witness paths in graph...\n"; + foreach my $path( $self->paths ) { + my $from = $path->from; + my $to = $path->to; + my @wits = split( /, /, $path->label ); + if( $path->has_hidden_witnesses ) { + push( @wits, @{$path->hidden_witnesses} ); + } + $self->del_path( $path ); + foreach ( @wits ) { + $self->add_path( $from, $to, $_ ); + } + } + $self->collapsed( 0 ); } =back @@ -268,11 +534,11 @@ assume that the path is that of the base text (if any.) sub reading_sequence { my( $self, $start, $end, $witness, $backup ) = @_; - $witness = 'base text' unless $witness; + $witness = $self->baselabel unless $witness; my @readings = ( $start ); my %seen; my $n = $start; - while( $n ne $end ) { + while( $n && $n ne $end ) { if( exists( $seen{$n->name()} ) ) { warn "Detected loop at " . $n->name(); last; @@ -299,7 +565,7 @@ sub reading_sequence { my $next_reading = $graph->next_reading( $reading, $witpath ); Returns the reading that follows the given reading along the given witness -path. TODO These are badly named. +path. =cut @@ -314,7 +580,7 @@ sub next_reading { my $prior_reading = $graph->prior_reading( $reading, $witpath ); Returns the reading that precedes the given reading along the given witness -path. TODO These are badly named. +path. =cut @@ -338,7 +604,7 @@ sub _find_linked_reading { my $base_le; my $alt_le; foreach my $le ( @linked_paths ) { - if( $le->name eq 'base text' ) { + if( $le->name eq $self->baselabel ) { $base_le = $le; } else { my @le_wits = $self->witnesses_of_label( $le->name ); @@ -367,7 +633,7 @@ sub _find_linked_reading { # Some set logic. sub _is_within { my( $set1, $set2 ) = @_; - my $ret = 1; + my $ret = @$set1; # will be 0, i.e. false, if set1 is empty foreach my $el ( @$set1 ) { $ret = 0 unless grep { /^\Q$el\E$/ } @$set2; } @@ -395,16 +661,7 @@ sub walk_witness_paths { $wit->path( \@wit_path ); # Detect the common readings. - if( @common_readings ) { - my @cn; - foreach my $n ( @wit_path ) { - push( @cn, $n ) if grep { $_ eq $n } @common_readings; - } - @common_readings = (); - push( @common_readings, @cn ); - } else { - push( @common_readings, @wit_path ); - } + @common_readings = _find_common( \@common_readings, \@wit_path ); } # Mark all the nodes as either common or not. @@ -420,44 +677,62 @@ sub walk_witness_paths { return @common_readings; } +sub _find_common { + my( $common_readings, $new_path ) = @_; + my @cr; + if( @$common_readings ) { + foreach my $n ( @$new_path ) { + push( @cr, $n ) if grep { $_ eq $n } @$common_readings; + } + } else { + push( @cr, @$new_path ); + } + return @cr; +} + +sub _remove_common { + my( $common_readings, $divergence ) = @_; + my @cr; + my %diverged; + map { $diverged{$_->name} = 1 } @$divergence; + foreach( @$common_readings ) { + push( @cr, $_ ) unless $diverged{$_->name}; + } + return @cr; +} + + # An alternative to walk_witness_paths, for use when a collation is -# constructed from a base text and an apparatus. Also modifies the -# collation graph to remove all 'base text' paths and replace them -# with real witness paths. +# constructed from a base text and an apparatus. We have the +# sequences of readings and just need to add path edges. -sub walk_and_expand_base { - my( $self, $end ) = @_; +sub make_witness_paths { + my( $self ) = @_; + my @common_readings; foreach my $wit ( @{$self->tradition->witnesses} ) { - my $sig = $wit_sigil; - my $post_sig; - $post_sig = $wit->post_correctione - if $wit->has_post_correctione; - my @wit_path = ( $self->start ); - my @wit_pc_path; - my $curr_rdg = $self->start; - my %seen; - while( $curr_rdg ne $end ) { - if( $seen{$curr_reading->name} ) { - warn "Detected loop in walk_and_expand_base with witness " - . "$sig on reading " . $curr_reading->name . "\n"; - last; - } - my $next_rdg = $self->next_reading( $curr_reading, $sig ); - unless( $self->has_explicit_path( $curr_reading, - $next_reading, $sig ) ) { - $self->add_path( $curr_reading, $next_reading, $sig ); - } - push( @wit_path, $next_reading ); - $seen{$curr_reading->name} = 1; - } - $wit->path( \@wit_path ); + print STDERR "Making path for " . $wit->sigil . "\n"; + $self->make_witness_path( $wit ); + @common_readings = _find_common( \@common_readings, $wit->path ); + @common_readings = _find_common( \@common_readings, $wit->uncorrected_path ); + } + return @common_readings; +} - # Now go through this path and look for p.c. divergences. - # TODO decide how to handle p.c. paths - # BIG TODO handle case where p.c. follows the base and a.c. doesn't! - - +sub make_witness_path { + my( $self, $wit ) = @_; + my @chain = @{$wit->path}; + my $sig = $wit->sigil; + foreach my $idx ( 0 .. $#chain-1 ) { + $self->add_path( $chain[$idx], $chain[$idx+1], $sig ); + } + @chain = @{$wit->uncorrected_path}; + foreach my $idx( 0 .. $#chain-1 ) { + my $source = $chain[$idx]; + my $target = $chain[$idx+1]; + $self->add_path( $source, $target, "$sig (a.c.)" ) + unless $self->has_path( $source, $target, $sig ); + } } sub common_readings { @@ -479,50 +754,66 @@ sub calculate_positions { my $node_pos = {}; foreach my $wit ( @{$self->tradition->witnesses} ) { - # First we walk each path, making a matrix for each witness that - # corresponds to its eventual position identifier. Common nodes - # always start a new row, and are thus always in the first column. - - my $wit_matrix = []; - my $cn = 0; # We should hit the common readings in order. - my $row = []; - foreach my $wn ( @{$wit->path} ) { - if( $wn eq $ordered_common[$cn] ) { - # Set up to look for the next common node, and - # start a new row of words. - $cn++; - push( @$wit_matrix, $row ) if scalar( @$row ); - $row = []; - } - push( @$row, $wn ); - } - push( @$wit_matrix, $row ); # Push the last row onto the matrix - - # Now we have a matrix per witness, so that each row in the - # matrix begins with a common node, and continues with all the - # variant words that appear in the witness. We turn this into - # real positions in row,cell format. But we need some - # trickery in order to make sure that each node gets assigned - # to only one position. - - foreach my $li ( 1..scalar(@$wit_matrix) ) { - foreach my $di ( 1..scalar(@{$wit_matrix->[$li-1]}) ) { - my $reading = $wit_matrix->[$li-1]->[$di-1]; - my $position = "$li,$di"; - # If we have seen this node before, we need to compare - # its position with what went before. - unless( $reading->has_position && - _cmp_position( $position, $reading->position ) < 1 ) { - # The new position ID replaces the old one. - $reading->position( $position ); - } # otherwise, the old position needs to stay. - } - } + print STDERR "Calculating positions in " . $wit->sigil . "\n"; + _update_positions_from_path( $wit->path, @ordered_common ); + _update_positions_from_path( $wit->uncorrected_path, @ordered_common ) + if $wit->has_ante_corr; + } + + # DEBUG + foreach my $r ( $self->readings() ) { + print STDERR "Reading " . $r->name . "/" . $r->label . " has no position\n" + unless( $r->has_position ); } $self->init_lemmata(); } +sub _update_positions_from_path { + my( $path, @ordered_common ) = @_; + + # First we walk the given path, making a matrix for the witness + # that corresponds to its eventual position identifier. Common + # nodes always start a new row, and are thus always in the first + # column. + my $wit_matrix = []; + my $cn = 0; # We should hit the common readings in order. + my $row = []; + foreach my $wn ( @{$path} ) { + if( $wn eq $ordered_common[$cn] ) { + # Set up to look for the next common node, and + # start a new row of words. + $cn++; + push( @$wit_matrix, $row ) if scalar( @$row ); + $row = []; + } + push( @$row, $wn ); + } + push( @$wit_matrix, $row ); # Push the last row onto the matrix + + # Now we have a matrix per witness, so that each row in the + # matrix begins with a common node, and continues with all the + # variant words that appear in the witness. We turn this into + # real positions in row,cell format. But we need some + # trickery in order to make sure that each node gets assigned + # to only one position. + + foreach my $li ( 1..scalar(@$wit_matrix) ) { + foreach my $di ( 1..scalar(@{$wit_matrix->[$li-1]}) ) { + my $reading = $wit_matrix->[$li-1]->[$di-1]; + my $position = "$li,$di"; + + # If we have seen this node before, we need to compare + # its position with what went before. + unless( $reading->has_position && + _cmp_position( $position, $reading->position ) < 1 ) { + # The new position ID replaces the old one. + $reading->position( $position ); + } # otherwise, the old position needs to stay. + } + } +} + sub _cmp_position { my( $a, $b ) = @_; if ( $a && $b ) { @@ -726,5 +1017,14 @@ sub unique_list { return values( %h ); } +sub add_hash_entry { + my( $hash, $key, $entry ) = @_; + if( exists $hash->{$key} ) { + push( @{$hash->{$key}}, $entry ); + } else { + $hash->{$key} = [ $entry ]; + } +} + no Moose; __PACKAGE__->meta->make_immutable;