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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
# $Id: SparseMap.pm,v 1.4 2001/10/31 08:51:30 m-kasahr Exp $
#
# Copyright (c) 2001 Japan Network Information Center. All rights reserved.
#
# By using this file, you agree to the terms and conditions set forth bellow.
#
# LICENSE TERMS AND CONDITIONS
#
# The following License Terms and Conditions apply, unless a different
# license is obtained from Japan Network Information Center ("JPNIC"),
# a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
# Chiyoda-ku, Tokyo 101-0047, Japan.
#
# 1. Use, Modification and Redistribution (including distribution of any
# modified or derived work) in source and/or binary forms is under this
# License Terms and Conditions.
#
# 2. Redistribution of source code must retain the copyright notices as they
# appear in each source code file, this License Terms and Conditions.
#
# 3. Redistribution in binary form must reproduce the Copyright Notice,
# this License Terms and Conditions, in the documentation and/or other
# materials provided with the distribution. For the purposes of binary
# distribution the "Copyright Notice" refers to the following language:
# "Copyright (c) Japan Network Information Center. All rights reserved."
#
# 4. Neither the name of JPNIC may be used to endorse or promote products
# derived from this Software without specific prior written approval of
# JPNIC.
#
# 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#
# 6. Indemnification by Licensee
# Any person or entities using and/or redistributing this Software under
# this License Terms and Conditions shall defend indemnify and hold
# harmless JPNIC from and against any and all judgements damages,
# expenses, settlement liabilities, cost and other liabilities of any
# kind as a result of use and redistribution of this Software or any
# claim, suite, action, litigation or proceeding by any third party
# arising out of or relates to this License Terms and Conditions.
#
# 7. Governing Law, Jurisdiction and Venue
# This License Terms and Conditions shall be governed by and and
# construed in accordance with the law of Japan. Any person or entities
# using and/or redistributing this Software under this License Terms and
# Conditions hereby agrees and consent to the personal and exclusive
# jurisdiction and venue of Tokyo District Court of Japan.
#
package SparseMap;
use strict;
use Carp;
my $debug = 0;
sub new {
# common options are:
# BITS => [8, 7, 6], # 3-level map, 2nd level bits=7, 3rd = 6.
# MAX => 0x110000 # actually, max + 1.
my $class = shift;
my $self = {@_};
croak "BITS unspecified" unless exists $self->{BITS};
croak "BITS is not an array reference"
unless ref($self->{BITS}) eq 'ARRAY';
croak "MAX unspecified" unless exists $self->{MAX};
$self->{MAXLV} = @{$self->{BITS}} - 1;
$self->{FIXED} = 0;
my $lv0size = (indices($self, $self->{MAX} - 1))[0] + 1;
my @map = (undef) x $lv0size;
$self->{MAP} = \@map;
bless $self, $class;
}
sub add1 {
my ($self, $n, $val) = @_;
croak "Already fixed" if $self->{FIXED};
carp("data ($n) out of range"), return if $n >= $self->{MAX};
my @index = $self->indices($n);
my $r = $self->{MAP};
my $maxlv = $self->{MAXLV};
my $idx;
my $lv;
for ($lv = 0; $lv < $maxlv - 1; $lv++) {
$idx = $index[$lv];
$r->[$idx] = $self->create_imap($lv + 1, undef)
unless defined $r->[$idx];
$r = $r->[$idx];
}
$idx = $index[$lv];
$r->[$idx] = $self->create_dmap() unless defined $r->[$idx];
$self->add_to_dmap($r->[$idx], $index[$maxlv], $val);
}
sub fix {
my $self = shift;
my $map = $self->{MAP};
my $maxlv = $self->{MAXLV};
my @tmp;
my @zero;
carp "Already fixed" if $self->{FIXED};
$self->collapse_tree();
$self->fill_default();
$self->{FIXED} = 1;
}
sub indices {
my $self = shift;
my $v = shift;
my @bits = @{$self->{BITS}};
my @idx;
print "indices($v,", join(',', @bits), ") = " if $debug;
for (my $i = @bits - 1; $i >= 0; $i--) {
my $bit = $bits[$i];
unshift @idx, $v & ((1 << $bit) - 1);
$v = $v >> $bit;
}
print "(", join(',', @idx), ")\n" if $debug;
@idx;
}
sub get {
my $self = shift;
my $v = shift;
my $map = $self->{MAP};
my @index = $self->indices($v);
croak "Not yet fixed" unless $self->{FIXED};
my $lastidx = pop @index;
foreach my $idx (@index) {
return $map->{DEFAULT} unless defined $map->[$idx];
$map = $map->[$idx];
}
$map->[$lastidx];
}
sub indirectmap {
my $self = shift;
croak "Not yet fixed" unless $self->{FIXED};
my @maps = $self->collect_maps();
my $maxlv = $self->{MAXLV};
my @bits = @{$self->{BITS}};
my @indirect = ();
for (my $lv = 0; $lv < $maxlv; $lv++) {
my $offset;
my $chunksz;
my $mapsz = @{$maps[$lv]->[0]};
if ($lv < $maxlv - 1) {
# indirect map
$offset = @indirect + @{$maps[$lv]} * @{$maps[$lv]->[0]};
$chunksz = (1 << $bits[$lv + 1]);
} else {
# direct map
$offset = 0;
$chunksz = 1;
}
my $nextmaps = $maps[$lv + 1];
foreach my $mapref (@{$maps[$lv]}) {
croak "mapsize inconsistent ", scalar(@$mapref),
" should be ", $mapsz, " (lv $lv)\n" if @$mapref != $mapsz;
foreach my $m (@$mapref) {
my $idx;
for ($idx = 0; $idx < @$nextmaps; $idx++) {
last if $nextmaps->[$idx] == $m;
}
croak "internal error: map corrupted" if $idx >= @$nextmaps;
push @indirect, $offset + $chunksz * $idx;
}
}
}
@indirect;
}
sub cprog_imap {
my $self = shift;
my %opt = @_;
my $name = $opt{NAME} || 'map';
my @indirect = $self->indirectmap();
my $prog;
my $i;
my ($idtype, $idcol, $idwid);
my $max = 0;
$max < $_ and $max = $_ foreach @indirect;
if ($max < 256) {
$idtype = 'char';
$idcol = 8;
$idwid = 3;
} elsif ($max < 65536) {
$idtype = 'short';
$idcol = 8;
$idwid = 5;
} else {
$idtype = 'long';
$idcol = 4;
$idwid = 10;
}
$prog = "static const unsigned $idtype ${name}_imap[] = {\n";
$i = 0;
foreach my $v (@indirect) {
if ($i % $idcol == 0) {
$prog .= "\n" if $i != 0;
$prog .= "\t";
}
$prog .= sprintf "%${idwid}d, ", $v;
$i++;
}
$prog .= "\n};\n";
$prog;
}
sub cprog {
my $self = shift;
$self->cprog_imap(@_) . "\n" . $self->cprog_dmap(@_);
}
sub stat {
my $self = shift;
my @maps = $self->collect_maps();
my $elsize = $self->{ELSIZE};
my $i;
my $total = 0;
my @lines;
for ($i = 0; $i < $self->{MAXLV}; $i++) {
my $nmaps = @{$maps[$i]};
my $mapsz = @{$maps[$i]->[0]};
push @lines, "level $i: $nmaps maps (size $mapsz) ";
push @lines, "[", $nmaps * $mapsz * $elsize, "]" if $elsize;
push @lines, "\n";
}
my $ndmaps = @{$maps[$i]};
push @lines, "level $i: $ndmaps dmaps";
my $r = $maps[$i]->[0];
if (ref($r) eq 'ARRAY') {
push @lines, " (size ", scalar(@$r), ")";
}
push @lines, "\n";
join '', @lines;
}
sub collapse_tree {
my $self = shift;
my @tmp;
$self->_collapse_tree_rec($self->{MAP}, 0, \@tmp);
}
sub _collapse_tree_rec {
my ($self, $r, $lv, $refs) = @_;
my $ref = $refs->[$lv];
my $maxlv = $self->{MAXLV};
my $found;
return $r unless defined $r;
$ref = $refs->[$lv] = [] unless defined $ref;
if ($lv == $maxlv) {
$found = $self->find_dmap($ref, $r);
} else {
for (my $i = 0; $i < @$r; $i++) {
$r->[$i] = $self->_collapse_tree_rec($r->[$i], $lv + 1, $refs);
}
$found = $self->find_imap($ref, $r);
}
unless ($found) {
$found = $r;
push @$ref, $found;
}
return $found;
}
sub fill_default {
my $self = shift;
my $maxlv = $self->{MAXLV};
my $bits = $self->{BITS};
my @zeros;
$zeros[$maxlv] = $self->create_dmap();
for (my $lv = $maxlv - 1; $lv >= 0; $lv--) {
my $r = $zeros[$lv + 1];
$zeros[$lv] = $self->create_imap($lv, $r);
}
_fill_default_rec($self->{MAP}, 0, $maxlv, \@zeros);
}
sub _fill_default_rec {
my ($r, $lv, $maxlv, $zeros) = @_;
return if $lv == $maxlv;
for (my $i = 0; $i < @$r; $i++) {
if (defined($r->[$i])) {
_fill_default_rec($r->[$i], $lv + 1, $maxlv, $zeros);
} else {
$r->[$i] = $zeros->[$lv + 1];
}
}
}
sub create_imap {
my ($self, $lv, $v) = @_;
my @map;
@map = ($v) x (1 << $self->{BITS}->[$lv]);
\@map;
}
sub find_imap {
my ($self, $maps, $map) = @_;
my $i;
foreach my $el (@$maps) {
next unless @$el == @$map;
for ($i = 0; $i < @$el; $i++) {
last unless ($el->[$i] || 0) == ($map->[$i] || 0);
}
return $el if $i >= @$el;
}
undef;
}
sub collect_maps {
my $self = shift;
my @maps;
_collect_maps_rec($self->{MAP}, 0, $self->{MAXLV}, \@maps);
@maps;
}
sub _collect_maps_rec {
my ($r, $lv, $maxlv, $maps) = @_;
my $mapref = $maps->[$lv];
return unless defined $r;
foreach my $ref (@{$mapref}) {
return if $ref == $r;
}
push @{$maps->[$lv]}, $r;
if ($lv < $maxlv) {
_collect_maps_rec($_, $lv + 1, $maxlv, $maps) foreach @{$r};
}
}
sub add {confess "Subclass responsibility";}
sub create_dmap {confess "Subclass responsibility";}
sub add_to_dmap {confess "Subclass responsibility";}
sub find_dmap {confess "Subclass responsibility";}
sub cprog_dmap {confess "Subclass responsibility";}
1;
package SparseMap::Bit;
use strict;
use vars qw(@ISA);
use Carp;
#use SparseMap;
@ISA = qw(SparseMap);
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{DEFAULT} = 0;
bless $self, $class;
}
sub add {
my $self = shift;
$self->add1($_, undef) foreach @_;
}
sub create_dmap {
my $self = shift;
my $bmbits = $self->{BITS}->[-1];
my $s = "\0" x (1 << ($bmbits - 3));
\$s;
}
sub add_to_dmap {
my ($self, $map, $idx, $val) = @_;
vec($$map, $idx, 1) = 1;
}
sub find_dmap {
my ($self, $ref, $r) = @_;
foreach my $map (@$ref) {
return $map if $$map eq $$r;
}
return undef;
}
sub cprog_dmap {
my $self = shift;
my %opt = @_;
my $name = $opt{NAME} || 'map';
my @maps = $self->collect_maps();
my @bitmap = @{$maps[-1]};
my $prog;
my $bmsize = 1 << ($self->{BITS}->[-1] - 3);
$prog = <<"END";
static const struct {
unsigned char bm[$bmsize];
} ${name}_bitmap[] = {
END
foreach my $bm (@bitmap) {
my $i = 0;
$prog .= "\t{{\n";
foreach my $v (unpack 'C*', $$bm) {
if ($i % 16 == 0) {
$prog .= "\n" if $i != 0;
$prog .= "\t";
}
$prog .= sprintf "%3d,", $v;
$i++;
}
$prog .= "\n\t}},\n";
}
$prog .= "};\n";
$prog;
}
1;
package SparseMap::Int;
use strict;
use vars qw(@ISA);
use Carp;
#use SparseMap;
@ISA = qw(SparseMap);
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{DEFAULT} = 0 unless exists $self->{DEFAULT};
bless $self, $class;
}
sub add {
my $self = shift;
while (@_ > 0) {
my $n = shift;
my $val = shift;
$self->add1($n, $val);
}
}
sub create_dmap {
my $self = shift;
my $tblbits = $self->{BITS}->[-1];
my $default = $self->{DEFAULT};
my @tbl = ($default) x (1 << $tblbits);
\@tbl;
}
sub add_to_dmap {
my ($self, $map, $idx, $val) = @_;
$map->[$idx] = $val;
}
sub find_dmap {
my ($self, $ref, $r) = @_;
foreach my $map (@$ref) {
if (@$map == @$r) {
my $i;
for ($i = 0; $i < @$map; $i++) {
last if $map->[$i] != $r->[$i];
}
return $map if $i == @$map;
}
}
return undef;
}
sub cprog_dmap {
my $self = shift;
my %opt = @_;
my $name = $opt{NAME} || 'map';
my @maps = $self->collect_maps();
my @table = @{$maps[-1]};
my $prog;
my $i;
my ($idtype, $idcol, $idwid);
my $tblsize = 1 << $self->{BITS}->[-1];
my ($min, $max);
foreach my $a (@table) {
foreach my $v (@$a) {
$min = $v if !defined($min) or $min > $v;
$max = $v if !defined($max) or $max < $v;
}
}
if (exists $opt{MAPTYPE}) {
$idtype = $opt{MAPTYPE};
} else {
my $u = $min < 0 ? '' : 'unsigned ';
my $absmax = abs($max);
$absmax = abs($min) if abs($min) > $absmax;
if ($absmax < 256) {
$idtype = "${u}char";
} elsif ($absmax < 65536) {
$idtype = "${u}short";
} else {
$idtype = "${u}long";
}
}
$idwid = decimalwidth($max);
$idwid = decimalwidth($min) if decimalwidth($min) > $idwid;
$prog = <<"END";
static const struct {
$idtype tbl[$tblsize];
} ${name}_table[] = {
END
foreach my $a (@table) {
my $i = 0;
my $col = 0;
$prog .= "\t{{\n\t";
foreach my $v (@$a) {
my $s = sprintf "%${idwid}d, ", $v;
$col += length($s);
if ($col > 70) {
$prog .= "\n\t";
$col = length($s);
}
$prog .= $s;
}
$prog .= "\n\t}},\n";
}
$prog .= "};\n";
$prog;
}
sub decimalwidth {
my $n = shift;
my $neg = 0;
my $w;
if ($n < 0) {
$neg = 1;
$n = -$n;
}
if ($n < 100) {
$w = 2;
} elsif ($n < 10000) {
$w = 4;
} elsif ($n < 1000000) {
$w = 6;
} elsif ($n < 100000000) {
$w = 8;
} else {
$w = 10;
}
$w + $neg;
}
1;
|