-
Notifications
You must be signed in to change notification settings - Fork 2
/
GenericNode.pm
165 lines (106 loc) · 2.07 KB
/
GenericNode.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package GenericNode;
use strict;
use warnings;
use Carp;
my $ID_counter = 0;
sub new {
my $packagename = shift;
my ($value) = @_;
my $self = {
_value => $value,
_prev => {},
_next => {},
_ID => ++$ID_counter,
_colors => {},
};
bless ($self, $packagename);
return($self);
}
sub get_ID {
my $self = shift;
my $id = sprintf("%x", $self->{_ID});
return("$id");
}
sub get_value {
my $self = shift;
return($self->{_value});
}
sub set_value {
my ($self) = shift;
my ($value) = @_;
$self->{_value} = $value;
}
sub add_prev_node {
my $self = shift;
my ($prev_node) = @_;
unless (ref $prev_node) {
croak "Error, prev_node should be a node object";
}
$self->{_prev}->{$prev_node} = $prev_node;
return;
}
sub add_next_node {
my $self = shift;
my ($next_node) = @_;
unless (ref $next_node) {
croak "Error, next_node should be a node object";
}
$self->{_next}->{$next_node} = $next_node;
}
sub has_next_node {
my $self = shift;
my $next_node = shift;
if (exists $self->{_next}->{$next_node}) {
return(1);
}
else {
return(0);
}
}
sub has_prev_node {
my $self = shift;
my $prev_node = shift;
if (exists $self->{_prev}->{$prev_node}) {
return(1);
}
else {
return(0);
}
}
sub get_all_prev_nodes {
my $self = shift;
my @prev_nodes = grep { ref($_) } values %{$self->{_prev}};
#print "Prev: " . join(",", @prev_nodes) . "\n";
return(@prev_nodes);
}
sub get_all_next_nodes {
my $self = shift;
my @next_nodes = grep { ref($_) } values %{$self->{_next}};
#print "Next: " . join(",", @next_nodes) . "\n";
return(@next_nodes);
}
sub delete_prev_node {
my $self = shift;
my $node = shift;
my $prev_nodes_href = $self->{_prev};
delete $prev_nodes_href->{$node};
return;
}
sub delete_next_node {
my $self = shift;
my $node = shift;
my $next_nodes_href = $self->{_next};
delete $next_nodes_href->{$node};
return;
}
sub add_color {
my $self = shift;
my $color = shift;
$self->{_colors}->{$color}++;
return;
}
sub get_colors {
my $self = shift;
return(keys %{$self->{_colors}});
}
1; #EOM