| 1 | # | ||
| 2 | # $Id: Iolist.pm,v 1.1.1.1 1998/02/25 21:13:00 schwartz Exp $ | ||
| 3 | # | ||
| 4 | # Iolist | ||
| 5 | # | ||
| 6 | # Copyright (C) 1997, 1998 Martin Schwartz | ||
| 7 | # | ||
| 8 | # (POD documentation at end of file) | ||
| 9 | # | ||
| 10 | # Contact: schwartz@cs.tu-berlin.de | ||
| 11 | # | ||
| 12 | |||
| 13 | package OLE::Storage::Iolist; | ||
| 14 | use strict; | ||
| 15 | my $VERSION=do{my@R=('$Revision: 1.1.1.1 $'=~/\d+/g);sprintf"%d."."%d"x$#R,@R}; | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ($proto, $oR, $lR) = @_; | ||
| 19 | my $class = ref($proto) || $proto; | ||
| 20 | my $S = { | ||
| 21 | O => $oR || [], | ||
| 22 | L => $lR || [] | ||
| 23 | }; | ||
| 24 | bless ($S, $class); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub dump { | ||
| 28 | my ($S) = @_; | ||
| 29 | if (@{$S->{O}}) { | ||
| 30 | print "Iolist = \n"; | ||
| 31 | for (0..$#{$S->{O}}) { | ||
| 32 | printf " %03x: O=%6x L=%x\n", $_, $S->{O}->[$_], $S->{L}->[$_]; | ||
| 33 | } | ||
| 34 | } else { | ||
| 35 | print "No Iolist.\n"; | ||
| 36 | } | ||
| 37 | print "\n"; | ||
| 38 | 1} | ||
| 39 | |||
| 40 | sub append { | ||
| 41 | my ($S, $o, $l) = @_; | ||
| 42 | my $max = max($S); | ||
| 43 | my ($o1, $l1) = entry($S, $max) if $max!=-1; | ||
| 44 | if (($max==-1) || (($o1+$l1) != $o)) { | ||
| 45 | push ( @{$$S{O}}, $o ); | ||
| 46 | push ( @{$$S{L}}, $l ); | ||
| 47 | } else { | ||
| 48 | entry($S, $max, $o1, $l1+$l); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | sub push { | ||
| 53 | my ($S, $sR) = @_; | ||
| 54 | if ($sR && $S) { | ||
| 55 | push ( @{$$S{O}}, @{$$sR{O}} ); | ||
| 56 | push ( @{$$S{L}}, @{$$sR{L}} ); | ||
| 57 | } | ||
| 58 | 1} | ||
| 59 | |||
| 60 | sub entry { | ||
| 61 | my ($S, $i) = (shift, shift); | ||
| 62 | ($$S{O}[$i], $$S{L}[$i]) = (shift, shift) if @_; | ||
| 63 | ($$S{O}[$i], $$S{L}[$i]); | ||
| 64 | } | ||
| 65 | sub length {my ($S, $i) = (shift, shift); $$S{L}[$i] = shift if @_; $$S{L}[$i]} | ||
| 66 | sub offset {my ($S, $i) = (shift, shift); $$S{O}[$i] = shift if @_; $$S{O}[$i]} | ||
| 67 | sub max {my $S = shift; $#{$$S{O}} } | ||
| 68 | |||
| 69 | # | ||
| 70 | # ----- aggregate methods ----- | ||
| 71 | # | ||
| 72 | |||
| 73 | sub sumlen { | ||
| 74 | my $S = shift; | ||
| 75 | my $size = 0; | ||
| 76 | for (@{$$S{L}}) { | ||
| 77 | $size += $_; | ||
| 78 | } | ||
| 79 | $size; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub aggregate { | ||
| 83 | # | ||
| 84 | # $iolistO = aggregate (method) | ||
| 85 | # | ||
| 86 | # method: | ||
| 87 | # 1 @offsets shall be sorted, no overlap allowed | ||
| 88 | # 2 @offsets shall be sorted, overlap is allowed | ||
| 89 | # 3 @offsets are sorted, no overlap allowed | ||
| 90 | # 4 @offsets are sorted, overlap is allowed | ||
| 91 | # | ||
| 92 | my ($S, $method) = @_; | ||
| 93 | my $empty = $S->new(); | ||
| 94 | return $empty if ($method<1)||($method>4); # Don't know method! | ||
| 95 | |||
| 96 | my ($o, $o1, $l, $l1); | ||
| 97 | my %o_in = (); | ||
| 98 | my $o_in = $S->new(); | ||
| 99 | my $o_out = $S->new(); | ||
| 100 | |||
| 101 | # | ||
| 102 | # Sort | ||
| 103 | # | ||
| 104 | if ( ($method==1) || ($method==2)) { | ||
| 105 | # sort offsets | ||
| 106 | for (0 .. $S->max()) { | ||
| 107 | ($o, $l) = $S->entry($_); | ||
| 108 | next if !$l; | ||
| 109 | if (defined $o_in{$o}) { | ||
| 110 | return $empty if $method==1; # Data chunks overlap! | ||
| 111 | $o_in{$o}=$_ if $l>$o_in{$o}; | ||
| 112 | } else { | ||
| 113 | $o_in{$o}=$_; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | for (sort {$a <=> $b} keys %o_in) { | ||
| 117 | $o_in->append($S->entry($o_in{$_})); | ||
| 118 | } | ||
| 119 | } else { | ||
| 120 | $o_in = $S; | ||
| 121 | } | ||
| 122 | |||
| 123 | # | ||
| 124 | # Aggregate | ||
| 125 | # | ||
| 126 | ($o, $l) = $o_in->entry(0); | ||
| 127 | |||
| 128 | for (1 .. $o_in->max()+1) { | ||
| 129 | ($o1, $l1) = $o_in->entry($_); | ||
| 130 | |||
| 131 | if ( ($_==($o_in->max()+1)) | ||
| 132 | || ( $o1 < $o ) | ||
| 133 | || ( $o1 > ($o+$l) ) | ||
| 134 | ) { | ||
| 135 | $o_out->append($o, $l); | ||
| 136 | ($o, $l) = ($o1, $l1); | ||
| 137 | } elsif ( $o1 < ($o+$l) ) { | ||
| 138 | return $empty if ($method==1 || $method==3); # Data chunks overlap! | ||
| 139 | if ( ($o1+$l1) > ($o+$l) ) { | ||
| 140 | $l=$o1+$l1-$o; | ||
| 141 | } | ||
| 142 | } else { | ||
| 143 | $l += $l1; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | $o_out; | ||
| 147 | } | ||
| 148 | |||
| 149 | "Atomkraft? Nein, danke!" | ||
| 150 | |||
| 151 | __END__ |