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
|
#!perl
###############################################################################
## ##
## Copyright (c) 2000 - 2013 by Steffen Beyer. ##
## All rights reserved. ##
## ##
## This program is free software; you can redistribute it ##
## and/or modify it under the same terms as Perl itself. ##
## ##
###############################################################################
######################################################################
# #
# How to emulate the Set::Object module using Bit::Vector (example): #
# #
######################################################################
package InheritMe;
use strict;
use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION );
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw( RegisterMe ID2Obj Obj2ID Registered );
%EXPORT_TAGS = (all => [@EXPORT_OK]);
$VERSION = 1.0;
my $ID = 0;
my(%id2obj) = ();
my(%obj2id) = ();
sub RegisterMe
{
my($self) = @_;
if (exists $obj2id{$self})
{
warn "Object '$self' has already been registered!\n";
return $obj2id{$self};
}
else
{
$id2obj{$ID} = $self;
$obj2id{$self} = $ID;
return $ID++;
}
}
sub ID2Obj
{
my($self,$id) = @_;
return exists($id2obj{$id}) ? $id2obj{$id} : undef;
}
sub Obj2ID
{
my($self) = @_;
return exists($obj2id{$self}) ? $obj2id{$self} : undef;
}
sub Registered
{
return $ID;
}
package main;
use strict;
use Date::Calc::Object;
use Bit::Vector::Overload;
push( @Date::Calc::ISA, 'InheritMe' ); # (make Date::Calc a subclass)
InheritMe->import(':all'); # (as a convenience for dealing with non-objects)
Date::Calc->date_format(2);
my $today = Date::Calc->today();
my $yesterday = $today - 1;
my $tomorrow = $today + 1;
print "\nDates:\n";
print "'$yesterday' has got ID #", $yesterday->RegisterMe(), "\n";
print "'$today' has got ID #", $today->RegisterMe(), "\n";
print "'$tomorrow' has got ID #", $tomorrow->RegisterMe(), "\n";
my $pears = 50;
my $apples = 1;
my $oranges = 'out of stock';
print "\nFruit:\n";
print "\$pears ('$pears') has got ID #", RegisterMe(\$pears), "\n";
print "\$apples ('$apples') has got ID #", RegisterMe(\$apples), "\n";
print "\$oranges ('$oranges') has got ID #", RegisterMe(\$oranges), "\n";
my $set1 = Bit::Vector->new(&Registered);
my $set2 = Bit::Vector->new(&Registered);
$set1->Bit_On( $today->Obj2ID() );
$set1->Bit_On( Obj2ID(\$pears) );
$set1->Bit_On( Obj2ID(\$apples) );
$set1->Bit_On( Obj2ID(\$oranges) );
$set2->Bit_On( $yesterday->Obj2ID() );
$set2->Bit_On( $today->Obj2ID() );
$set2->Bit_On( $tomorrow->Obj2ID() );
$set2->Bit_On( Obj2ID(\$oranges) );
print "\nSet #1:\n";
print "<",
join
(
'> <',
map
(
ref($_) =~ /^[A-Z]+$/ ? "${$_}" : "$_",
map
(
InheritMe->ID2Obj($_),
$set1->Index_List_Read()
)
)
),
">\n";
print "\nSet #2:\n";
print "<",
join
(
'> <',
map
(
ref($_) =~ /^[A-Z]+$/ ? "${$_}" : "$_",
map
(
InheritMe->ID2Obj($_),
$set2->Index_List_Read()
)
)
),
">\n";
my $intersection = $set1 & $set2;
print "\nIntersection:\n";
print "<",
join
(
'> <',
map
(
ref($_) =~ /^[A-Z]+$/ ? "${$_}" : "$_",
map
(
InheritMe->ID2Obj($_),
$intersection->Index_List_Read()
)
)
),
">\n";
__END__
|