オブジェクト指向プログラミング

概要

  • 3x3のビットパターンを表現するクラスを作成し、これを使って回転しても重複しないパターンを探索する。

  • ソース
    fileBitPattern.zip

BitPattern.pm

# BitPattern.pm
# ビットパターンクラス
# 数値に対応するNxNのマトリックスを表現するクラス
#
# N=3 (default)
# | bit0 | bit1 | bit2 |
# | bit3 | bit4 | bit5 |
# | bit6 | bit7 | bit8 |

package BitPattern;

use strict;
use warnings;
use utf8;

sub new {
	my $pkg = shift;
	bless {
		number	=> 0,
		unit	=> 3,
		pattern	=> [ [0,0,0], [0,0,0], [0,0,0], ],
	}, $pkg;
}

sub toString {
	my $self = shift;
	my $unit = $self->{unit};
	my $pattern = "[ ";
	for( my $i=0; $i<$unit; $i++ ){
		$pattern .= "[ ";
		for( my $j=0; $j<$unit; $j++ ){
			$pattern .= ${$self->{pattern}}[ $i ][ $j ] . " ";
		}
		$pattern .= "] ";
	}
	$pattern .= "]";

	return sprintf( "%04x / %s", $self->{number}, $pattern );
}

sub clone {
	my $self = shift;
	my $unit = $self->{unit};
	my $tmp = BitPattern->new();
	# $tmp->{number}		= $self->{number};
	# $tmp->{unit}			= $self->{unit};
	# @{$tmp->{pattern}}	= @{$self->{pattern}};	# rotateで失敗する?
	$tmp->set( $self->{number}, $self->{unit} );
	return $tmp;
}

sub equals {
	my $self = shift;
	my $target = shift;
	return ( $self->{number} == $target->{number} );
}

sub set {
	my $self = shift;
	my $num = $self->{number} = int( shift );
	my $unit = $self->{unit} = int( shift || $self->{unit} || 3 );	# default=3
	my @pattern = ();
	for( my $i=0; $i<$unit; $i++ ){
		for( my $j=0; $j<$unit; $j++ ){
			if ( $num & 1 ){
				$pattern[ $i ][ $j ] = 1;
			} else {
				$pattern[ $i ][ $j ] = 0;
			}
			$num >>= 1;
		}
	}
	@{$self->{pattern}} = @pattern;
	return $self;
}

sub getNumber {
	my $self = shift;
	return $self->{number};
}

sub getPatternArray {
	my $self = shift;
	return wantarray ? @{$self->{pattern}} : $self->{pattern};
}

sub getPattern {
	my $self = shift;
	my $unit = $self->{unit};
	my $pattern = "";
	for( my $i=0; $i<$unit; $i++ ){
		for( my $j=0; $j<$unit; $j++ ){
			$pattern .= ${$self->{pattern}}[ $i ][ $j ] ? "*" : "-" ;
		}
		$pattern .= "\n";
	}
	return $pattern;
}

# 時計回りに回転
sub rotate {
	my $self = shift;
	my $unit = $self->{unit};
	my $tmp = $self->clone();
	for( my $i=0; $i<$unit; $i++ ){
		for( my $j=0; $j<$unit; $j++ ){
			${$self->{pattern}}[ $i ][ $j ] = 
				${$tmp->{pattern}}[ $unit - 1 - $j ][ $i ];
		}
	}

	my $num = 0;
	for( my $i=$unit-1; $i>=0; $i-- ){
		for( my $j=$unit-1; $j>=0; $j-- ){
			$num <<= 1;
			$num += ${$self->{pattern}}[ $i ][ $j ];
		}
	}
	$self->{number} = $num;

	return $self;
}

1;

# EOF

testBitPattern.pl

ソース

# testBitPattern.pl
# ビットパターンクラスの動作テスト

use strict;
use warnings;
use utf8;
use Data::Dump qw(dump);

use BitPattern;

my $unit = 3;

my $pattern = BitPattern->new();
my( @pat1, @pat2, $pattern2 );

$pattern->set( 0, $unit );

foreach ( 1, 2, 4, 8, 16, 32, 64, 128, 256, 170, 325 ){
	$pattern->set( $_ );
	@pat1 = split( "\n", $pattern->getPattern() );
	$pattern2 = $pattern->clone();
	$pattern2->rotate();
	@pat2 = split( "\n", $pattern2->getPattern() );
	printf( 
		"%s\n%d\n%s\neq: %b\n", 
		$pattern->toString(), $pattern->getNumber(), 
		dump( $pattern->getPatternArray() ), $pattern->equals($pattern2) 
	);
	for( my $i=0; $i<$unit; $i++ ){
		printf( "%s  ->  %s\n", $pat1[ $i ], $pat2[ $i ] );
	}
	print "\n";
}

# EOF

実行結果

0001 / [ [ 1 0 0 ] [ 0 0 0 ] [ 0 0 0 ] ]
1
([1, 0, 0], [0, 0, 0], [0, 0, 0])
eq: 0
*--  ->  --*
---  ->  ---
---  ->  ---

0002 / [ [ 0 1 0 ] [ 0 0 0 ] [ 0 0 0 ] ]
2
([0, 1, 0], [0, 0, 0], [0, 0, 0])
eq: 0
-*-  ->  ---
---  ->  --*
---  ->  ---

0004 / [ [ 0 0 1 ] [ 0 0 0 ] [ 0 0 0 ] ]
4
([0, 0, 1], [0, 0, 0], [0, 0, 0])
eq: 0
--*  ->  ---
---  ->  ---
---  ->  --*

0008 / [ [ 0 0 0 ] [ 1 0 0 ] [ 0 0 0 ] ]
8
([0, 0, 0], [1, 0, 0], [0, 0, 0])
eq: 0
---  ->  -*-
*--  ->  ---
---  ->  ---

0010 / [ [ 0 0 0 ] [ 0 1 0 ] [ 0 0 0 ] ]
16
([0, 0, 0], [0, 1, 0], [0, 0, 0])
eq: 1
---  ->  ---
-*-  ->  -*-
---  ->  ---

0020 / [ [ 0 0 0 ] [ 0 0 1 ] [ 0 0 0 ] ]
32
([0, 0, 0], [0, 0, 1], [0, 0, 0])
eq: 0
---  ->  ---
--*  ->  ---
---  ->  -*-

0040 / [ [ 0 0 0 ] [ 0 0 0 ] [ 1 0 0 ] ]
64
([0, 0, 0], [0, 0, 0], [1, 0, 0])
eq: 0
---  ->  *--
---  ->  ---
*--  ->  ---

0080 / [ [ 0 0 0 ] [ 0 0 0 ] [ 0 1 0 ] ]
128
([0, 0, 0], [0, 0, 0], [0, 1, 0])
eq: 0
---  ->  ---
---  ->  *--
-*-  ->  ---

0100 / [ [ 0 0 0 ] [ 0 0 0 ] [ 0 0 1 ] ]
256
([0, 0, 0], [0, 0, 0], [0, 0, 1])
eq: 0
---  ->  ---
---  ->  ---
--*  ->  *--

00aa / [ [ 0 1 0 ] [ 1 0 1 ] [ 0 1 0 ] ]
170
([0, 1, 0], [1, 0, 1], [0, 1, 0])
eq: 1
-*-  ->  -*-
*-*  ->  *-*
-*-  ->  -*-

0145 / [ [ 1 0 1 ] [ 0 0 0 ] [ 1 0 1 ] ]
325
([1, 0, 1], [0, 0, 0], [1, 0, 1])
eq: 1
*-*  ->  *-*
---  ->  ---
*-*  ->  *-*

checkPattern.pl

ソース

# checkPattern.pl
# ビットパターンのチェック
# 回転しても重複しないビットパターンを探索する

use strict;
use warnings;
use utf8;

use BitPattern;

my $pattern = BitPattern->new();
my @check = ();
my $count = 0;

for( my $i=1; $i<512; $i++ ){
	if ( !defined( $check[ $i ] ) ){
		$count++;
		$pattern->set( $i );
		printf( "%s\n", $pattern->toString() );
		$check[ $pattern->rotate()->getNumber() ] = 0;
		$check[ $pattern->rotate()->getNumber() ] = 0;
		$check[ $pattern->rotate()->getNumber() ] = 0;
		$check[ $i ] = 1;
	}
}
printf( "count: %d\n", $count );

# EOF

実行結果

0001 / [ [ 1 0 0 ] [ 0 0 0 ] [ 0 0 0 ] ]
0002 / [ [ 0 1 0 ] [ 0 0 0 ] [ 0 0 0 ] ]
0003 / [ [ 1 1 0 ] [ 0 0 0 ] [ 0 0 0 ] ]
0005 / [ [ 1 0 1 ] [ 0 0 0 ] [ 0 0 0 ] ]
0006 / [ [ 0 1 1 ] [ 0 0 0 ] [ 0 0 0 ] ]
0007 / [ [ 1 1 1 ] [ 0 0 0 ] [ 0 0 0 ] ]
000a / [ [ 0 1 0 ] [ 1 0 0 ] [ 0 0 0 ] ]
000b / [ [ 1 1 0 ] [ 1 0 0 ] [ 0 0 0 ] ]
000c / [ [ 0 0 1 ] [ 1 0 0 ] [ 0 0 0 ] ]
000d / [ [ 1 0 1 ] [ 1 0 0 ] [ 0 0 0 ] ]
000e / [ [ 0 1 1 ] [ 1 0 0 ] [ 0 0 0 ] ]
000f / [ [ 1 1 1 ] [ 1 0 0 ] [ 0 0 0 ] ]
0010 / [ [ 0 0 0 ] [ 0 1 0 ] [ 0 0 0 ] ]
0011 / [ [ 1 0 0 ] [ 0 1 0 ] [ 0 0 0 ] ]
0012 / [ [ 0 1 0 ] [ 0 1 0 ] [ 0 0 0 ] ]
0013 / [ [ 1 1 0 ] [ 0 1 0 ] [ 0 0 0 ] ]
0015 / [ [ 1 0 1 ] [ 0 1 0 ] [ 0 0 0 ] ]
0016 / [ [ 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集 凍結 差分 添付 コピー 名前変更   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: Sun, 04 Dec 2011 19:22:25 JST (2335d)