| 1 | # Copyright 2010 Kevin Ryde | ||||||||||
| 2 | |||||||||||
| 3 | # This file is part of Gtk2-Ex-TiedListColumn. | ||||||||||
| 4 | # | ||||||||||
| 5 | # Gtk2-Ex-TiedListColumn is free software; you can redistribute it and/or | ||||||||||
| 6 | # modify it under the terms of the GNU General Public License as published | ||||||||||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||||||||||
| 8 | # later version. | ||||||||||
| 9 | # | ||||||||||
| 10 | # Gtk2-Ex-TiedListColumn is distributed in the hope that it will be useful, | ||||||||||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||||||||||
| 13 | # Public License for more details. | ||||||||||
| 14 | # | ||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||
| 16 | # with Gtk2-Ex-TiedListColumn. If not, see <http://www.gnu.org/licenses/>. | ||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | package Gtk2::Ex::TiedTreePath; | ||||||||||
| 20 | 2 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 2 | our $VERSION = 5; | |||||||||
| 25 | |||||||||||
| 26 | # uncomment this to run the ### lines | ||||||||||
| 27 | #use Smart::Comments; | ||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 30 | 0 | my ($class, $path) = @_; | |||||||||
| 31 | 0 | tie (my @array, $class, $path); | |||||||||
| 32 | 0 | return \@array; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 0 | ▶ | sub TIEARRAY { | ||||||||
|
Subroutine
TIEARRAY: not called CC=1 SCAR=6.9
| |||||||||||
| 36 | 0 | my ($class, $path) = @_; | |||||||||
| 37 | 0 | return bless \$path, $class; | |||||||||
| 38 | } | ||||||||||
| 39 | # optional, not needed | ||||||||||
| 40 | # sub UNTIE { } | ||||||||||
| 41 | |||||||||||
| 42 | # tied object method | ||||||||||
| 43 | 0 | ▶ | sub path { | ||||||||
|
Subroutine
path: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 44 | 0 | return ${$_[0]}; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | # negative indices already normalized to >=0 by the time they get here | ||||||||||
| 48 | 0 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: not called CC=1 SCAR=6.9
| |||||||||||
| 49 | 0 | my ($self, $index) = @_; | |||||||||
| 50 | ### TiedTreePath FETCH: $index | ||||||||||
| 51 | 0 | return (($$self)->get_indices)[$index]; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | # negative indices already normalized to >=0 by the time they get here | ||||||||||
| 55 | 0 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: not called CC=5 SCAR=34.0
| |||||||||||
| 56 | 0 | my ($self, $index, $value) = @_; | |||||||||
| 57 | ### TiedTreePath STORE: [$index, $value] | ||||||||||
| 58 | 0 | my $path = $$self; | |||||||||
| 59 | 0 | my $depth = $path->get_depth; | |||||||||
| 60 | 0 | ▶ | if ($index >= $depth) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | foreach ($depth .. $index-1) { | |||||||||
| 62 | 0 | $path->append_index (0); | |||||||||
| 63 | } | ||||||||||
| 64 | 0 | $path->append_index ($value); | |||||||||
| 65 | } else { | ||||||||||
| 66 | 0 | my @array = $path->get_indices; | |||||||||
| 67 | 0 | foreach ($index .. $depth-1) { | |||||||||
| 68 | 0 | $path->up; | |||||||||
| 69 | } | ||||||||||
| 70 | 0 | $path->append_index ($value); | |||||||||
| 71 | 0 | foreach my $i ($index+1 .. $depth-1) { | |||||||||
| 72 | 0 | $path->append_index ($array[$i]); | |||||||||
| 73 | } | ||||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub _path_clear { | ||||||||
|
Subroutine
_path_clear: not called CC=2 SCAR=17.9
| |||||||||||
| 78 | 0 | my ($path) = @_; | |||||||||
| 79 | 0 | ▶ | while ($path->up) {} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | } | ||||||||||
| 81 | 0 | ▶ | sub _path_set_indices { | ||||||||
|
Subroutine
_path_set_indices: not called CC=2 SCAR=17.9
| |||||||||||
| 82 | 0 | my $path = shift; | |||||||||
| 83 | 0 | _path_clear ($path); | |||||||||
| 84 | 0 | ▶ | while (@_) { $path->append_index (shift @_); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub FETCHSIZE { | ||||||||
|
Subroutine
FETCHSIZE: not called CC=1 SCAR=6.9
| |||||||||||
| 88 | 0 | my ($self) = @_; | |||||||||
| 89 | ### TiedTreePath FETCHSIZE | ||||||||||
| 90 | 0 | return ($$self)->get_depth; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # big negative sizes normalized to 0 by the time they get here | ||||||||||
| 94 | 0 | ▶ | sub STORESIZE { | ||||||||
|
Subroutine
STORESIZE: not called CC=3 SCAR=24.8
| |||||||||||
| 95 | 0 | my ($self, $want_size) = @_; | |||||||||
| 96 | ### TiedTreePath STORESIZE: $want_size | ||||||||||
| 97 | 0 | my $path = $$self; | |||||||||
| 98 | 0 | my $depth = $path->get_depth; | |||||||||
| 99 | 0 | foreach ($want_size .. $depth-1) { # shorten | |||||||||
| 100 | 0 | $path->up; | |||||||||
| 101 | } | ||||||||||
| 102 | 0 | foreach ($depth .. $want_size-1) { # lengthen | |||||||||
| 103 | 0 | $path->append_index (0); | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub EXTEND { | ||||||||
|
Subroutine
EXTEND: not called CC=1 SCAR=0.0
| |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | # negative indices already normalized to >=0 by the time they get here | ||||||||||
| 111 | 0 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||
| 112 | 0 | my ($self, $index) = @_; | |||||||||
| 113 | ### TiedTreePath EXISTS: $index | ||||||||||
| 114 | 0 | return ($index < ($$self)->get_depth); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | # normalized to 0 <= $index <= FETCHSIZE-1 by the time get here | ||||||||||
| 118 | 0 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: not called CC=3 SCAR=24.8
| |||||||||||
| 119 | 0 | my ($self, $index) = @_; | |||||||||
| 120 | ### TiedTreePath DELETE: $index | ||||||||||
| 121 | 0 | my $path = $$self; | |||||||||
| 122 | 0 | my $ret; | |||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | if ($index < (my $depth = $path->get_depth)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | $ret = ($path->get_indices)[$index]; | |||||||||
| 126 | 0 | ▶ | if ($index == $depth-1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | $path->up; | |||||||||
| 128 | } else { | ||||||||||
| 129 | 0 | $self->STORE ($index, 0); | |||||||||
| 130 | } | ||||||||||
| 131 | } | ||||||||||
| 132 | 0 | return $ret; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | sub CLEAR { | ||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||
| 136 | 0 | my ($self) = @_; | |||||||||
| 137 | ### TiedTreePath CLEAR | ||||||||||
| 138 | 0 | _path_clear ($$self); | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 0 | ▶ | sub PUSH { | ||||||||
|
Subroutine
PUSH: not called CC=2 SCAR=17.9
| |||||||||||
| 142 | 0 | my $self = shift; | |||||||||
| 143 | 0 | my $path = $$self; | |||||||||
| 144 | 0 | ▶ | while (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 0 | $path->append_index (shift @_); | |||||||||
| 146 | } | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | sub POP { | ||||||||
|
Subroutine
POP: not called CC=1 SCAR=6.9
| |||||||||||
| 150 | 0 | my ($self) = @_; | |||||||||
| 151 | ### TiedTreePath POP | ||||||||||
| 152 | 0 | my $path = $$self; | |||||||||
| 153 | 0 | my $ret = ($path->get_indices)[-1]; | |||||||||
| 154 | 0 | $path->up; | |||||||||
| 155 | 0 | return $ret; | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 0 | ▶ | sub SHIFT { | ||||||||
|
Subroutine
SHIFT: not called CC=2 SCAR=17.9
| |||||||||||
| 159 | 0 | my ($self) = @_; | |||||||||
| 160 | ### TiedTreePath SHIFT | ||||||||||
| 161 | 0 | my $path = $$self; | |||||||||
| 162 | 0 | my @array = $path->get_indices; | |||||||||
| 163 | 0 | ▶ | if (! @array) { return; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 0 | my $ret = shift @array; | |||||||||
| 165 | ### $ret | ||||||||||
| 166 | 0 | _path_set_indices ($path, @array); | |||||||||
| 167 | 0 | return $ret; | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | # don't have to return the new size here, FETCHSIZE is called separately | ||||||||||
| 171 | 0 | ▶ | sub UNSHIFT { | ||||||||
|
Subroutine
UNSHIFT: not called CC=1 SCAR=6.9
| |||||||||||
| 172 | 0 | my $self = shift; | |||||||||
| 173 | ### TiedTreePath UNSHIFT | ||||||||||
| 174 | 0 | my $path = $$self; | |||||||||
| 175 | 0 | push @_, $path->get_indices; | |||||||||
| 176 | 0 | _path_set_indices ($path, @_); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 0 | ▶ | sub SPLICE { | ||||||||
|
Subroutine
SPLICE: not called CC=2 SCAR=17.9
| |||||||||||
| 180 | 0 | my $self = shift; | |||||||||
| 181 | 0 | my $offset = shift; | |||||||||
| 182 | 0 | my $length = shift; | |||||||||
| 183 | ### TiedTreePath SPLICE: [$offset,$length] | ||||||||||
| 184 | |||||||||||
| 185 | 0 | my $path = $$self; | |||||||||
| 186 | 0 | my @array = $path->get_indices; | |||||||||
| 187 | 0 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 0 | my @ret = splice @array, $offset, $length, @_; | |||||||||
| 189 | 0 | _path_set_indices ($path, @array); | |||||||||
| 190 | 0 | return @ret; | |||||||||
| 191 | } else { | ||||||||||
| 192 | 0 | my $ret = splice @array, $offset, $length, @_; | |||||||||
| 193 | 0 | _path_set_indices ($path, @array); | |||||||||
| 194 | 0 | return $ret; | |||||||||
| 195 | } | ||||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | 2 | 1; | |||||||||
| 199 | __END__ | ||||||||||