tag-release: a simple script to tag the repository for release
[nasm/avx512.git] / perllib / Graph / AdjacencyMap / Vertex.pm
blob72d8142792c31f2cd961115633b513308b0a0114
1 package Graph::AdjacencyMap::Vertex;
3 # THIS IS INTERNAL IMPLEMENTATION ONLY, NOT TO BE USED DIRECTLY.
4 # THE INTERFACE IS HARD TO USE AND GOING TO STAY THAT WAY AND
5 # ALMOST GUARANTEED TO CHANGE OR GO AWAY IN FUTURE RELEASES.
7 use strict;
9 # $SIG{__DIE__ } = sub { use Carp; confess };
10 # $SIG{__WARN__} = sub { use Carp; confess };
12 use Graph::AdjacencyMap qw(:flags :fields);
13 use base 'Graph::AdjacencyMap';
15 use Scalar::Util qw(weaken);
17 sub _new {
18 my ($class, $flags, $arity) = @_;
19 bless [ 0, $flags, $arity ], $class;
22 require overload; # for de-overloading
24 sub __set_path {
25 my $m = shift;
26 my $f = $m->[ _f ];
27 my $id = pop if ($f & _MULTI);
28 if (@_ != 1) {
29 require Carp;
30 Carp::confess(sprintf "Graph::AdjacencyMap::Vertex: arguments %d expected 1", scalar @_);
32 my $p;
33 $p = $m->[ _s ] ||= { };
34 my @p = $p;
35 my @k;
36 my $k = shift;
37 my $q = ref $k && ($f & _REF) && overload::Method($k, '""') ? overload::StrVal($k) : $k;
38 push @k, $q;
39 return (\@p, \@k);
42 sub __set_path_node {
43 my ($m, $p, $l) = splice @_, 0, 3;
44 my $f = $m->[ _f ];
45 my $id = pop if ($f & _MULTI);
46 unless (exists $p->[-1]->{ $l }) {
47 my $i = $m->_new_node( \$p->[-1]->{ $l }, $id );
48 $m->[ _i ]->{ defined $i ? $i : "" } = $_[0];
49 } else {
50 $m->_inc_node( \$p->[-1]->{ $l }, $id );
54 sub set_path {
55 my $m = shift;
56 my $f = $m->[ _f ];
57 my ($p, $k) = $m->__set_path( @_ );
58 return unless defined $p && defined $k;
59 my $l = defined $k->[-1] ? $k->[-1] : "";
60 my $set = $m->__set_path_node( $p, $l, @_ );
61 return $set;
64 sub __has_path {
65 my $m = shift;
66 my $f = $m->[ _f ];
67 if (@_ != 1) {
68 require Carp;
69 Carp::confess(sprintf
70 "Graph::AdjacencyMap: arguments %d expected 1\n",
71 scalar @_);
73 my $p = $m->[ _s ];
74 return unless defined $p;
75 my @p = $p;
76 my @k;
77 my $k = shift;
78 my $q = ref $k && ($f & _REF) && overload::Method($k, '""') ? overload::StrVal($k) : $k;
79 push @k, $q;
80 return (\@p, \@k);
83 sub has_path {
84 my $m = shift;
85 my ($p, $k) = $m->__has_path( @_ );
86 return unless defined $p && defined $k;
87 return exists $p->[-1]->{ defined $k->[-1] ? $k->[-1] : "" };
90 sub has_path_by_multi_id {
91 my $m = shift;
92 my $id = pop;
93 my ($e, $n) = $m->__get_path_node( @_ );
94 return undef unless $e;
95 return exists $n->[ _nm ]->{ $id };
98 sub _get_path_id {
99 my $m = shift;
100 my $f = $m->[ _f ];
101 my ($e, $n) = $m->__get_path_node( @_ );
102 return undef unless $e;
103 return ref $n ? $n->[ _ni ] : $n;
106 sub _get_path_count {
107 my $m = shift;
108 my $f = $m->[ _f ];
109 my ($e, $n) = $m->__get_path_node( @_ );
110 return 0 unless $e && defined $n;
111 return
112 ($f & _COUNT) ? $n->[ _nc ] :
113 ($f & _MULTI) ? scalar keys %{ $n->[ _nm ] } : 1;
116 sub __attr {
117 my $m = shift;
118 if (@_ && ref $_[0] && @{ $_[0] } != $m->[ _a ]) {
119 require Carp;
120 Carp::confess(sprintf "Graph::AdjacencyMap::Vertex: arguments %d expected %d",
121 scalar @{ $_[0] }, $m->[ _a ]);
125 sub _get_id_path {
126 my ($m, $i) = @_;
127 return defined $m->[ _i ] ? $m->[ _i ]->{ $i } : undef;
130 sub del_path {
131 my $m = shift;
132 my $f = $m->[ _f ];
133 my ($e, $n, $p, $k, $l) = $m->__get_path_node( @_ );
134 return unless $e;
135 my $c = ($f & _COUNT) ? --$n->[ _nc ] : 0;
136 if ($c == 0) {
137 delete $m->[ _i ]->{ ref $n ? $n->[ _ni ] : $n };
138 delete $p->[ -1 ]->{ $l };
140 return 1;
143 sub del_path_by_multi_id {
144 my $m = shift;
145 my $f = $m->[ _f ];
146 my $id = pop;
147 my ($e, $n, $p, $k, $l) = $m->__get_path_node( @_ );
148 return unless $e;
149 delete $n->[ _nm ]->{ $id };
150 unless (keys %{ $n->[ _nm ] }) {
151 delete $m->[ _i ]->{ $n->[ _ni ] };
152 delete $p->[-1]->{ $l };
154 return 1;
157 sub paths {
158 my $m = shift;
159 return map { [ $_ ] } values %{ $m->[ _i ] } if defined $m->[ _i ];
160 wantarray ? ( ) : 0;
164 =pod
166 =head1 NAME
168 Graph::AdjacencyMap - create and a map of graph vertices or edges
170 =head1 SYNOPSIS
172 Internal.
174 =head1 DESCRIPTION
176 B<This module is meant for internal use by the Graph module.>
178 =head2 Object Methods
180 =over 4
182 =item del_path(@id)
184 Delete a Map path by ids.
186 =item del_path_by_multi_id($id)
188 Delete a Map path by a multi(vertex) id.
190 =item has_path(@id)
192 Return true if the Map has the path by ids, false if not.
194 =item has_path_by_multi_id($id)
196 Return true ifd the a Map has the path by a multi(vertex) id, false if not.
198 =item paths
200 Return all the paths of the Map.
202 =item set_path(@id)
204 Set the path by @ids.
206 =back
208 =head1 AUTHOR AND COPYRIGHT
210 Jarkko Hietaniemi F<jhi@iki.fi>
212 =head1 LICENSE
214 This module is licensed under the same terms as Perl itself.
216 =cut