-
Notifications
You must be signed in to change notification settings - Fork 2
/
nested.pl
executable file
·167 lines (118 loc) · 3.18 KB
/
nested.pl
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
164
165
166
167
package foo;
use warnings;
use strict;
use Carp;
#$SIG{'__WARN__'} = \&Carp::confess;
use Contextual::Return;
sub TIEHASH
{
print "TIEHASH: @_\n";
my ($class) = @_;
my $self = { '_pwlf' => {} };
return bless $self, $class;
}
sub FIRSTKEY
{
(undef) = scalar keys %{$_[0]->{'_pwlf'}};
return each %{$_[0]->{'_pwlf'}};
}
sub NEXTKEY
{
return each %{$_[0]->{'_pwlf'}};
}
sub EXISTS
{
print "EXISTS: @_\n";
my ($self, $key) = @_;
return exists $self->{'_pwlf'}{$key};
}
sub STORE
{
print "STORE: @_\n";
my ($self, $key, $val) = @_;
if ( ref $val )
{
tie %{ $val }, 'foo';
}
$self->{'_pwlf'}{ $key } = $val;
return $val;
}
sub FETCH
{
print "FETCH: @_\n";
my ($self, $key) = @_;
return
REF
{
## if in REF context and the key does not exist
## auto-vivify a nested tied hash
if ( ! exists $self->{'_pwlf'}{ $key } )
{
return STORE( $self, $key, {} );
}
else
{
## in REF context, if the key exists, we need to return an object
## that can unwind a series of STORE calls or a series of
## interp calls
my $lo_x = 0;
my $hi_x = 2;
my $lo_y = $self->{'_pwlf'}{$lo_x};
my $hi_y = $self->{'_pwlf'}{$hi_x};
print "y-s: $lo_y $hi_y\n";
my $interp = sub
{
my $x = shift();
$lo_y = ref $lo_y ? $lo_y->{$x} : $lo_y;
$hi_y = ref $hi_y ? $hi_y->{$x} : $hi_y;
return mx_plus_b( $key, $lo_x, $hi_x, $lo_y, $hi_y );
};
my $recurse = ref $lo_y || ref $hi_y;
return $recurse
? $interp
: $interp->($key);
}
}
DEFAULT
{
## if *not* in REF context and the key does not exist
## interpolate
my $lo_x = 0;
my $hi_x = 2;
my $lo_y = $self->{'_pwlf'}{$lo_x};
my $hi_y = $self->{'_pwlf'}{$hi_x};
print "y-s: $lo_y $hi_y\n";
my $interp = sub
{
my $x = shift();
$lo_y = ref $lo_y ? $lo_y->{$x} : $lo_y;
$hi_y = ref $hi_y ? $hi_y->{$x} : $hi_y;
return mx_plus_b( $key, $lo_x, $hi_x, $lo_y, $hi_y );
};
my $recurse = ref $lo_y || ref $hi_y;
return $recurse
? $interp
: $interp->($key);
};
}
sub mx_plus_b
{
my ( $x, $x_dn, $x_up, $y_dn, $y_up ) = @_;
my $slope = ( $y_up - $y_dn ) / ( $x_up - $x_dn );
my $intercept = $y_up - ( $slope * $x_up );
my $y = $slope * $x + $intercept;
return $y;
}
package main;
use warnings;
use strict;
my %foo;
tie %foo, 'foo';
$foo{0}{0} = 0;
$foo{0}{2} = 2;
$foo{2}{0} = 0;
$foo{2}{2} = 4;
print $foo{1} . "\n";
#use Data::Dumper;
#print Dumper \%foo;
#print $foo{1}{2}{3}{4} . "\n";