はてなキーワード: ARGVとは
というわけでサンプルコード書きなおしてみた。
public class Main { public static void main(String[] args) { int count= args.length==0?100000:Integer.parseInt(args[0]); long start=System.currentTimeMillis(); Main m=new Main(); double result=0; for(int i=0;i<count;i++){ result+=m.test((double)(0.1f*i)); } System.out.println(result); System.out.println(String.format("COMPLETE! %d msec",System.currentTimeMillis()-start)); } private double product=0; public double test(double rad){ return this.product+=Math.tan(rad); } }
<?php class Test{ private $product; public function test($rad) { return $this->product+=tan($rad); } } $count =is_null($argv[1])?100000:(int)$argv[1]; $start=microtime(true); $t=new Test(); $result=0; for($i=0;$i<$count;$i++){ $result+=$t->test(0.1*$i); } echo "{$result}\n"; printf("COMPLETE! %f msec", (microtime(true)-$start)*1000);
$java -jar test.jar 100000 -7.524990063072938E9 COMPLETE! 31 msec
$ php -f test.php 100000 -11900078829.3 COMPLETE! 209.314108 msec
うむ。有意な差はあるっぽいな。(結果が違い過ぎてるのはなんか問題があるかもしれん)
多分、変数の型が出鱈目で計算されるようなもんだとPHPは遅くなるのだと思われ。javaは強い型制約があるから、同じ型同士の計算だと速いとか?
サンプルコード。
package test; public class Main { public static void main(String[] args) { int count= args[0]==null?10000:Integer.parseInt(args[0]); long start=System.currentTimeMillis(); Main m=new Main(); for(int i=0;i<count;i++){ System.out.println(m.test()); } System.out.println(String.format("COMPLETE! %d msec",System.currentTimeMillis()-start)); } public String test(){ return "ぽぽぽぽーん"; } }
<?php class Test{ public function test(){ return "ぽぽぽぽーん"; } } $count =is_null($argv[1])?10000:(int)$argv[1]; $start=microtime(true); $t=new Test(); for($i=0;$i<$count;$i++){ echo $t->test()."\n"; } printf("COMPLETE! %f msec", (microtime(true)-$start)*1000);
ぽぽぽ...
COMPLETE! 1008 msec
ぽぽぽ...
COMPLETE! 988.869190 msec
どういうことなのか説明してもらおうか?
じゃぁ、問1をやってみた。
unsigned int f(unsigned int x) {
x = x - 1;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
return x + 1;
}
int main(int argc, const char * argv[])
{
int check = 1;
int cnt=0;
if(0!=f(0)){
cnt++;
}
for(unsigned int i=1;i!=0;i++){
if(check < i){
check <<=1;
}
if(check != f(i)){
cnt++;
}
if(i%0x10000==0){
printf("%x\n",i);
}
}
printf("cnt=%d\n",cnt);
return 0;
}
iよりも等しいか大きい最小の2の乗数
書いてみたけどぜんぜん面白くならんわ。このサイズで面白さを競ってもなあ。
どうやったら面白くなる?
#include "stdafx.h" #include "windows.h" int _tmain(int argc, _TCHAR* argv[]) { int i=2; int j=0; while(1){ i++; for(j=2;j<i;j++){ if((i % j)==0){ //printf("%d は素数ではない\n",i); break; } } if(j==i){ //printf("%d は素数\n",i); printf("%d\n",i); } //Sleep(1000); } return 0; }
最近の記事は http://anond.hatelabo.jp/20100727011929 だと画像が取れなくなっているのでちょっと直した.あと引数で記事を指定できるようにしたよ.<<を半角に直して使ってね
ruby -ropen-uri -rnokogiri -e "Nokogiri::HTML(open ARGV[0]).search('img.pict').map{|e| e['src']}.each{|http| File.open(http.match(/.+\/(.+\.jpg)/)[1],'w'){|f| f << open(http).read }}" "http://minkch.com/archives/51611588.html"
if も 3項演算子も for も do whileすらもない ifなしの Fizz Buzz
#include "stdio.h" #include "stdlib.h" int cnumber=0; void fizz(){ printf("fizz"); cnumber++; }; void nonfizz(){ }; void buzz(){ printf("buzz"); cnumber++; }; void nonbuzz(){ }; void number(int i){ printf("%d",i); cnumber = 0; } void nonnumber(int i){ cnumber = 0; } void myexit(void){ printf("\n Hit return key to exit\n"); getchar(); exit(1); } void noexit(void){ } void (*pfizz[3])() = {fizz,nonfizz,nonfizz}; void (*pbuzz[5])() = {buzz,nonbuzz,nonbuzz,nonbuzz,nonbuzz}; void (*pnumber[3])(int) = {number,nonnumber,nonnumber}; void (*pmyexit[2])() = {noexit,myexit}; int main(int argc, char* argv[]) { int loopmax = (111+222+333)*10; int i = 1; head: (*pfizz[i%3])(); (*pbuzz[i%5])(); (*pnumber[cnumber])(i); (*pmyexit[!(loopmax-i)])(); printf(","); i++; goto head; return 0; }
includeが<>を使ってないので必要ならパスは各自で通してねw
10/18 改訂
なお、取得した画像の著作権はグーグル他各社が保持しています。
ご利用は計画的に私的範囲でどうぞご利用ください。
#!/usr/bin/perl use strict; use warnings; use Getopt::Long; use LWP::UserAgent; use GD; my $cmdline = join(" ", $0, @ARGV); my $usage = "usage: $0 -sx=116423 -sy=51603 -ex=116426 -ey=51605 -dx=4 -dy=3 -z=17 -size=300 -get=30 -dir=cache -output=output.jpg -nodebug"; my ($sx, $sy) = (0, 0); my ($ex, $ey) = (0, 0); my ($dx, $dy) = (4, 3); my $z = 17; my $size = 300; my $get = 30; my $dir = "cache"; my $output = "output.jpg"; my $debug = 0; GetOptions("sx=i" => \$sx, "sy=i" => \$sy, "ex=i" => \$ex, "ey=i" => \$ey, "dx=i" => \$dx, "dy=i" => \$dy, "z=i" => \$z, "size=i" => \$size, "get=i" => $get, "dir=s" => \$dir, "output=s" => \$output, "debug!" => \$debug) or die "$usage\nDied"; if ($ex == 0) { $ex = $sx + $dx; } else { $ex++; $dx = $ex - $sx; } if ($ey == 0) { $ey = $sy + $dy; } else { $ey++; $dy = $ey - $sy; } $sx>0 and $dx>0 and $sy>0 and $dy>0 and $z>0 and $dir and $output or die "$usage\nBad arguments"; $dx*$dy > $size and die "Getting too large."; $debug and print "debug: mkdir $dir\n"; mkdir $dir; -d $dir or die "can't make dir $dir: $!"; my $base = sprintf("http://khm%d.google.co.jp/kh/v=46&z=%d", int(rand(4)), $z); my $ua = LWP::UserAgent->new; printf "now get %d images...\n", $dx*$dy; for (my $x=$sx; $x < $ex; $x++) { for (my $y=$sy; $y < $ey; $y++) { my $file = sprintf("%s/%02dz%06dx%06d.jpg", $dir, $z, $x, $y); $debug and print "debug: check of $file\n"; -s $file and next; --$get < 0 and last; my $req = HTTP::Request->new(GET=>+"$base&x=$x&y=$y"); $debug and print "debug: fetch from ".$req->uri."\n"; my $res = $ua->request($req); unless ($res->is_success) { print "fail fetch from $file: ", $res->status_line, "\n"; next; } if (open(my $fh, ">", $file)) { $debug and print "debug: write of $file\n"; binmode $fh; print $fh $res->content; close $fh; } else { print "fail open in $file: $!\n"; } } } $get < 0 and print "reach the getting limit, skip after all.\n"; printf "creating %dX%d image...\n", 256*$dx, 256*$dy; my $image = new GD::Image(256*$dx, 256*$dy); for (my $x=$sx; $x < $ex; $x++) { for (my $y=$sy; $y < $ey; $y++) { my $file = sprintf("%s/%02dz%06dx%06d.jpg", $dir, $z, $x, $y); $debug and print "debug: check of $file\n"; -s $file or next; $debug and print "debug: read of $file\n"; my $part = GD::Image->newFromJpeg($file); $debug and print "debug: image copy\n"; $image->copy($part, 256*($x-$sx), 256*($y-$sy), 0, 0, 256, 256); } } #$image->string(gdSmallFont, 0, 0, $cmdline, $image->colorAllocate(255, 255, 255)); open(my $fh, ">", $output) or die "fail open $output: $!"; $debug and print "debug: write of $output\n"; binmode $fh; print $fh $image->jpeg(); close $fh;
例えば秋葉原とか
perl gmwall.pl -sx=116423 -sy=51603 -ex=116427 -ey=51606
駅だけとか
perl gmwall.pl -sx=465701 -sy=206420 -ex=465705 -ey=206423 -z=19
使う数値はfirebugなどで拾ってください。
C++ の std::list にデフォルトコンストラクタが無いclassを使おうと思って気づいたのだけれど、コピーコンストラクタとか代入演算子を定義してやらないと駄目なのかな。
いやまぁ、それで動くんだからいいじゃんという話とか、ポインタにして自分で new すればいいじゃんというのもあるのだけれど、
class hoge { public: hoge(int dummy); hoge(const hoge &other); private: char buffer[1000]; }; std::list<hoge> hogeList; </pre>とか定義してる時に、
hogeList.push_back(hoge(0));って書いた時、auto変数の領域かなにかに領域が確保されて、hoge(int dummy) が呼び出されて何かの初期化が一回入って、その後、hogeListが確保した領域に、コピーコンストラクタでその値が上書きされるということになって、ぶっちゃけ最初のhoge(int dummy)呼び出しは無駄なんじゃないかなーというか、なにやらでかいバッファを持ってる奴とかだったら、それが全部copyされると考えるとなんか無駄だなーとか細かいことが気になった。
で、これでコピーコンストラクタを呼び出されないようにする方法っていって自分が考え付いたのは、
std::list<hoge *> hogePointerList; </pre>として、自前で new するってのなのだけれど、まぁアレですよ。できれば自前で new とかしないですむ「俺楽チン」なステキ手法はないものか、と愚痴ってみただけです。ハイ。
つか、それくらいやりかたありそうなもんだけど、無いのかなほんとに。そういうもん?
一応試してみたcodeはこんなん。
#include <stdio.h> #include <list> class hoge { public: hoge(int dummy); hoge(const hoge &other); private: char buffer[1000]; }; hoge::hoge(int dummy){ printf("hoge initialized. this: %p\n", this); } hoge::hoge(const hoge &other){ printf("hoge copyed. this: %p\n", this); } int main(int argc, char *argv[]){ std::list<hoge> hogeList; hogeList.push_back(hoge(0)); return 0; } </pre>結果はこんなん。
hoge initialized. this: 0x22c8c0 hoge copyed. this: 0x6c0210で、蛇足的に、
hogeList.push_back(0);みたいな書き方をしてみたら、
hoge initialized. this: 0x22c8c0 hoge copyed. this: 0x6c0210となった。あぁ、これは
hogeList.push_back(hoge(0));に読み替えてくれたんだね。賢いなぁC++は。
ということで、hoge(int dummy) の方に explicit をつけてみたら、muchするfunctionが無いよとcompile errorした。まぁそうだよねぇ。
セキュリティ目的ではない。ハッシュテーブルで使うような奴でキャッシュで使いたい。
手軽なほうが良い。軽いほうが良い。推測可能でよい。数十バイトくらいの文字列にしたい。
md5が一番汎用っぽいけど、無駄に重い気がする。crc32は軽そうだしそれなりに汎用っぽいけど、ハッシュ長が短いのがめんどい。
調べた→ http://anond.hatelabo.jp/20090327015620
ベンチ用スクリプト
#!/usr/local/bin/python from sys import argv, stderr from time import time from string import ascii_letters, join from random import choice from hashlib import md5 from binascii import crc32 from itertools import izip time_fmt = '%10s: %5d ms' shift = int(argv[1]) if len(argv)>1 and argv[1].isdigit() else 2 length = 0x100 << shift cycle = 0x10000 >> shift print >> stderr, 'string length: 0x%x, cycle: 0x%x' % (length, cycle) data = tuple(''.join(choice(ascii_letters) for i in xrange(length)) for j in xrange(cycle)) start = time() md5hex = tuple(md5(s).hexdigest() for s in data) print >> stderr, time_fmt % ('md5hex', (time() - start) * 1000) start = time() crc32x4 = tuple(''.join('%08x' % abs(crc32(s[i::4])) for i in (0, 1, 2, 3)) for s in data) print >> stderr, time_fmt % ('crc32x4', (time() - start) * 1000) start = time() startend = tuple(s[:16]+s[-16:] for s in data) print >> stderr, time_fmt % ('headtail', (time() - start) * 1000) start = time() skip = tuple(s[::(len(s)/32+1)] for s in data) print >> stderr, time_fmt % ('skipover', (time() - start) * 1000) for s in izip(data, md5hex, crc32x4, startend, skip): print join(s)
実行結果
% python hashbench.py 0 > hash0.txt string length: 0x100, cycle: 0x10000 md5hex: 199 ms crc32x4: 1081 ms headtail: 30 ms skipover: 41 ms % python hashbench.py 2 > hash1.txt string length: 0x400, cycle: 0x4000 md5hex: 83 ms crc32x4: 363 ms headtail: 10 ms skipover: 20 ms % python hashbench.py 4 > hash2.txt string length: 0x1000, cycle: 0x1000 md5hex: 52 ms crc32x4: 170 ms headtail: 2 ms skipover: 5 ms
いまさらだがFizzBuzz。
1から100まで、3の倍数5の倍数云々って、全部定数の計算じゃね?
というところに気付き、自称メタプログラマー(略してメタグラマー)俺の血が騒いだ。
定数計算なら、それは実行時ではなくコンパイル時に行なわれるべきだ……。
#include <iostream> const int FIZZ_NUM = 3; const int BUZZ_NUM = 5; const int BEGIN_NUM = 1; const int END_NUM = 101; template<int N> struct Fizz { enum {PRINT = 0, NEXT = N + 1}; static void print() {} }; template<int N> struct Buzz { enum {PRINT = 0, NEXT = N + 1}; static void print() {} }; template<int N, bool ForB> struct Number {static void print() {std::cout << N;}}; template<> struct Fizz<FIZZ_NUM> { enum {PRINT = 1, NEXT = 1}; static void print() {std::cout << "Fizz";} }; template<> struct Buzz<BUZZ_NUM> { enum {PRINT = 1, NEXT = 1}; static void print() {std::cout << "Buzz";} }; template<int N> struct Number<N, true> {static void print() {}}; template<int N, int F, int B> struct FizzBuzz { static void print() { typedef ::Fizz<F> Fizz; typedef ::Buzz<B> Buzz; Fizz::print(); Buzz::print(); Number<N, Fizz::PRINT || Buzz::PRINT>::print(); std::cout << std::endl; FizzBuzz<N + 1, Fizz::NEXT, Buzz::NEXT>::print(); } }; template<int F, int B> struct FizzBuzz<END_NUM, F, B> {static void print() {}}; int main(int argc, char **argv) { FizzBuzz<BEGIN_NUM, 1, 1>::print(); return 0; }
ifなし%なしループ系なし、しかも実行時オーバーヘッドなし!(多分)
ああ、久しぶりにC++を触ったけど、やっぱC++のテンプレートってダメダメだな。20世紀の遺物といわざるを得ない。
君がもし21世紀のモテ系イケメンメタグラマーなら、21世紀のプログラミング言語、D言語を使うべきだ!
驚くべきことに、D言語はコンパイル時に関数が実行でき、その結果をソースコードとして取り込める!
ただし実行できるのは簡単な関数だけだけど……。
import std.stdio; // これでFizzBuzzを全部出力するコードを作るぜ! string makeFizzBuzzCode() { string code; for(int i = 1; i <= 100; ++i) { // 効率? コンパイル時にそんな配慮は要らん! if(i % 3 == 0 && i % 5 == 0) { code ~= "writefln(\"FizzBuzz\");\n"; } else if(i % 3 == 0) { code ~= "writefln(\"Fizz\");\n"; } else if(i % 5 == 0) { code ~= "writefln(\"Buzz\");\n"; } else { code ~= "writefln(" ~ static_itoa(i) ~ ");\n"; } } return code; } int main(string[] args) { // おまけで生成されたコードも見せるよ。 pragma(msg, makeFizzBuzzCode()); // 生成したコードを埋め込む。コピペみたいな感覚。 mixin(makeFizzBuzzCode); return 0; } // 以下ユーティリティ。このぐらい標準で欲しいな……。 /// 整数→文字列変換(コンパイル時) string static_itoa(int n) { if(n == 0) { return "0"; } // 10で割りながら余りを文字にして追加。桁が逆転した文字列になる。 string s; for(; n; n /= 10) { s ~= ("0123456789")[n % 10]; } // 桁位置を正常にする。相変わらず効率無視。 return static_reverse(s); } /// 配列リバース(コンパイル時) /// 実行時ならarray.reverseが使えるんだけどね……。 T[] static_reverse(T)(T[] s) { T[] result; foreach_reverse(c; s) { result ~= c; } return result; } // 心配なので静的ユニットテスト(笑) unittest { static assert(static_itoa(0) == "0"); static assert(static_itoa(10) == "10"); static assert(static_itoa(999) == "999"); static assert(static_itoa(9999) == "9999"); static assert(static_itoa(12345) == "12345"); static assert(static_itoa(314159265) == "314159265"); }
コンパイル結果
$ dmd -unittest fizz_buzz.d writefln(1); writefln(2); writefln("Fizz"); writefln(4); writefln("Buzz"); writefln("Fizz"); writefln(7); writefln(8); writefln("Fizz"); writefln("Buzz"); writefln(11); writefln("Fizz"); writefln(13); writefln(14); writefln("FizzBu(ry
出力結果は略。
さすがD言語!C++やJavaやC#にできない事を平然とやってのけるッ
そこにシビれる!あこがれるゥ!
というか、
writefln(1);
writefln(2);
writefln("Fizz");
writefln(4);
もうwritefln(出力関数)要らなくね?
修正。
// これでFizzBuzzを全部出力するぜ! string makeFizzBuzzCode() { string code; for(int i = 1; i <= 100; ++i) { // 効率? コンパイル時にそんな配慮は要らん! if(i % 3 == 0 && i % 5 == 0) { code ~= "FizzBuzz\n"; } else if(i % 3 == 0) { code ~= "Fizz\n"; } else if(i % 5 == 0) { code ~= "Buzz\n"; } else { code ~= static_itoa(i) ~ "\n"; } } return code; } int main(string[] args) { // もうコンパイル時のメッセージしか出さない。(笑) pragma(msg, makeFizzBuzzCode()); return 0; }
コンパイル結果。
$ dmd -unittest fizz_buzz.d 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBu(ry
実行するまでもなく結果が出力された。つまり実行時間ゼロ、ということは……
世 界 最 速
みんな使おうD言語!
http://www.kmonos.net/alang/d/1.0/index.html(1.0。こっちの方が安定してる?)
http://www.kmonos.net/alang/d/2.0/index.html(もっと凄い2.0)
実行してみたら出力に変化がなかった。アルェー?
# tree -pugs | perl -e '@line=<>; $max=(sort {$b<=>$a} map {/^(.*?)\[/; length($1)} @line)[0]; map {s/^(.*?)\[/$1 . " "x($max-length($1))."\["/e} @line; print @line'
毎回正規表現考えるのも面倒かなーとかとか。
理想を語ると出力形式指定できれば最強だなーとか妄想してた。
この辺は、ファイルにして ~/bin に放りこんで、自分好みに育て上げる、ってのがエロゲ^H^H^Hギークの楽しみ方かと。
雛型的には↓かなぁ。どうだろ。弾・monger・コーガイ氏が登場したら……滅多切りさるかもw
#!/usr/bin/perl use strict; use warnings; use Getopt::Std; use vars qw($opt_t); # オプション処理 getopts('t'); # $opt_tがセットされていたらタブ処理、ということにする。 # コマンド起動 # 引数処理とかシグナルとか面倒なところは…… open TREE, "tree -pugs ".join(" ",@ARGV,"|") or die; my @line = <TREE>; # 富豪的にいく close TREE; # ここで子プロセスの処理だっけ? # それともwaitするんだっけ? # ツリー表示部分の最大長を求める。 my $max=(sort {$b<=>$a} map {/^(.*?)\[/; length($1)} @line)[0]; # 色々やる時はユーザ名とかグループ名とかファイル名とかの最大長がいるかも foreach (@line) { if ($opt_t) { # タブ(\t)で位置をそろえるとか…… } else { s/^(.*?)\[/$1 . " "x($max-length($1))."\["/e; } print; }
treeへのオプションの与え方とか、それによる出力の変化とか考えると、結構面倒だけど、自分用に決め打ちしとけば何とかなるでしょう。
あと、「[%p<>%u<>%g<>%s]」みたいなのはCPANに転がってたりしそうな気もするので、それを使うとか。
そんなところでなかろうか。
[追記]
http://search.cpan.org/~darren/String-Format-1.14/Format.pm というのがあった。
@data=(a,b,c);print@data
#!/usr/bin/perl use strict; my @data = qw(a b c); my $cx = scalar @data; my $si = 0; my $ax; LOOP: $ax = $data[$si]; print $ax; $si++; not --$cx or goto LOOP;
#!/usr/bin/perl use strict; my @data = qw(a b c); package AtoH; use base qw(Tie::Handle); sub TIEHANDLE { my $class = shift; return bless {data => [@_]}, $class; } sub READLINE { return shift @{shift->{data}}; } package main; tie *ARGV, 'AtoH', @data; while (<>) { print; }
http://anond.hatelabo.jp/20070711013155 こちらの宿題を作ってみました。
http://anond.hatelabo.jp/20070711080519 で参加を宣言した者です。
#include "stdafx.h" #include <time.h> #include <conio.h> #include <list> using namespace std; enum MMError { MME_None = 0, MME_SizeError, MME_MemoryAllocError, MME_NotInitialized, }; enum MMKind { MMK_None = 0, MMK_Space, // 通路 MMK_Filled, // 埋まってるところ。掘れる。 MMK_Wall, // 壁。掘れないところ。 }; // // 迷路実体管理用クラス定義 // class CMazeMatrix { public: CMazeMatrix(); virtual ~CMazeMatrix(); public: virtual bool Initialize(int nXSize, int nYSize); // 初期化すると同時に、外壁まで作ってしまう。 virtual MMKind GetAt(int nXPos, int nYPos); virtual bool SetAt(int nXPos, int nYPos, MMKind kind); MMError GetLastError() {return m_lastError;} protected: inline int calcIndex(int nXPos, int nYPos) {return nYPos * m_nXSize + nXPos;} bool finalize(); protected: MMKind *m_pMaze; int m_nXSize; int m_nYSize; MMError m_lastError; }; // // 実体管理用クラス実体 // CMazeMatrix::CMazeMatrix() { m_pMaze = NULL; m_nXSize = 0; m_nYSize = 0; m_lastError = MME_None; } CMazeMatrix::~CMazeMatrix() { finalize(); } bool CMazeMatrix::Initialize(int nXSize, int nYSize) { finalize(); int nSize = nXSize * nYSize; if ((__int64)nSize != (__int64)nXSize * (__int64)nYSize) { m_lastError = MME_SizeError; return false; } m_pMaze = new MMKind[nSize]; if (m_pMaze == NULL) { m_lastError = MME_MemoryAllocError; return false; } m_nXSize = nXSize; m_nYSize = nYSize; int nCnt; for (nCnt = 0; nCnt < nSize; nCnt++) m_pMaze[nCnt] = MMK_Filled; for (nCnt = 0; nCnt < m_nXSize; nCnt++) { m_pMaze[calcIndex(nCnt, 0)] = MMK_Wall; m_pMaze[calcIndex(nCnt, m_nYSize - 1)] = MMK_Wall; } for (nCnt = 0; nCnt < m_nYSize; nCnt++) { m_pMaze[calcIndex(0, nCnt)] = MMK_Wall; m_pMaze[calcIndex(m_nXSize - 1, nCnt)] = MMK_Wall; } return true; } MMKind CMazeMatrix::GetAt(int nXPos, int nYPos) { #ifdef _DEBUG if (nXPos < 0 || nXPos >= m_nXSize || nYPos < 0 || nYPos >= m_nYSize) { m_lastError = MME_SizeError; return MMK_None; } #endif return m_pMaze[calcIndex(nXPos, nYPos)]; } bool CMazeMatrix::SetAt(int nXPos, int nYPos, MMKind kind) { #ifdef _DEBUG if (nXPos < 0 || nXPos >= m_nXSize || nYPos < 0 || nYPos >= m_nYSize) { m_lastError = MME_SizeError; return false; } #endif m_pMaze[calcIndex(nXPos, nYPos)] = kind; return true; } bool CMazeMatrix::finalize() { if (m_pMaze != NULL) { delete [] m_pMaze; m_pMaze = NULL; } return true; } // // 迷路作成用クラス定義 // class CMazeMaker { public: CMazeMaker(); virtual ~CMazeMaker(); public: bool Initialize(int nXSize, int nYSize); // 力業。ループで回す。メモリは食わない。 // 美しくない。 bool Generate1(); // 掘った箇所をスタックに積んで、掘れなくなったらスタックを戻す。 // 綺麗だけれど、迷路のサイズを増やすとスタックオーバーフローが起こる。 bool Generate2(); // Generate2をlistに置き換えたもの。 // stdを使ってしまったのが心残り。 bool Generate3(); MMError GetLastError() {return m_lastError;} protected: bool finalize(); bool checkPos(int nXPos, int nYPos, int nXAdd, int nYAdd); int process(int nXPos, int nYPos); void dig(int nXPos, int nYPos); bool makeStartGoal(); virtual CMazeMatrix* matrixAllocate(); protected: int m_nXSize; int m_nYSize; CMazeMatrix *m_pMatrix; MMError m_lastError; }; CMazeMaker::CMazeMaker() { m_nXSize = 0; m_nYSize = 0; m_pMatrix = NULL; m_lastError = MME_None; } CMazeMaker::~CMazeMaker() { finalize(); } bool CMazeMaker::Initialize(int nXSize, int nYSize) { finalize(); m_pMatrix = matrixAllocate(); if (m_pMatrix == NULL) { m_lastError = MME_MemoryAllocError; return false; } if (m_pMatrix->Initialize(nXSize, nYSize) == false) { m_lastError = m_pMatrix->GetLastError(); return false; } m_nXSize = nXSize; m_nYSize = nYSize; return true; } CMazeMatrix* CMazeMaker::matrixAllocate() { return new CMazeMatrix; } bool CMazeMaker::finalize() { if (m_pMatrix != NULL) { delete m_pMatrix; m_pMatrix = NULL; } return true; } // スタート位置と、ゴールの位置を作成。外壁部分に穴を開ける。 // 今回のアルゴリズムでは、外壁のすぐ内側が通路になっていないことがあるので // その場合には箇所を移動させる。 // どこをとっても通路が見あたらない場合には、エラーとする。 // (乱数の発生具合がとても意地悪な場合を考えると、可能性は少なくとも0ではない。) // bool CMazeMaker::makeStartGoal() { // スタート地点を左の壁の上の方に int nCnt = 0; for (nCnt = 1; nCnt < m_nYSize - 1; nCnt++) { if (m_pMatrix->GetAt(1, nCnt) == MMK_Space) { m_pMatrix->SetAt(0, nCnt, MMK_Space); break; } } if (nCnt == m_nXSize - 1) { return false; } // ゴール地点を右の壁の下の方に for (nCnt = m_nYSize; nCnt > 0; nCnt--) { if (m_pMatrix->GetAt(m_nXSize - 2, nCnt) == MMK_Space) { m_pMatrix->SetAt(m_nXSize - 1, nCnt, MMK_Space); break; } } if (nCnt == 0) { return false; } return true; } // 現在位置nXPos, nYPosからみて、nXAdd、nYAddを足した位置に移動できるかをチェック // 移動先が埋まっている状態で、さらに三方が通路以外に覆われているなら、OKとする bool CMazeMaker::checkPos(int nXPos, int nYPos, int nXAdd, int nYAdd) { if (m_pMatrix->GetAt(nXPos + nXAdd, nYPos + nYAdd) != MMK_Filled) return false; if (nXAdd == 0) { if (m_pMatrix->GetAt(nXPos - 1, nYPos + nYAdd * 2) != MMK_Space && m_pMatrix->GetAt(nXPos , nYPos + nYAdd * 2) != MMK_Space && m_pMatrix->GetAt(nXPos + 1, nYPos + nYAdd * 2) != MMK_Space && m_pMatrix->GetAt(nXPos - 1, nYPos + nYAdd ) != MMK_Space && m_pMatrix->GetAt(nXPos + 1, nYPos + nYAdd ) != MMK_Space) { return true; } } else { if (m_pMatrix->GetAt(nXPos + nXAdd * 2, nYPos - 1) != MMK_Space && m_pMatrix->GetAt(nXPos + nXAdd * 2, nYPos ) != MMK_Space && m_pMatrix->GetAt(nXPos + nXAdd * 2, nYPos + 1) != MMK_Space && m_pMatrix->GetAt(nXPos + nXAdd , nYPos - 1) != MMK_Space && m_pMatrix->GetAt(nXPos + nXAdd , nYPos + 1) != MMK_Space) { return true; } } return false; } static const int moveInfo[4][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1}, }; int CMazeMaker::process(int nXPos, int nYPos) { int digCount=0; int aryMove[4] = {0}; if (m_pMatrix->GetAt(nXPos, nYPos) != MMK_Space) { return 0; } while (1) { int nMoveCount = 0; for (int nCnt = 0; nCnt < 4; nCnt++) { if (checkPos(nXPos, nYPos, moveInfo[nCnt][0], moveInfo[nCnt][1]) == true) { aryMove[nMoveCount] = nCnt; nMoveCount++; } } if (nMoveCount == 0) { break; } int nMove = ((rand() >> 1) % nMoveCount); nXPos = nXPos + moveInfo[aryMove[nMove]][0]; nYPos = nYPos + moveInfo[aryMove[nMove]][1]; m_pMatrix->SetAt(nXPos, nYPos, MMK_Space); digCount++; } return digCount; } bool CMazeMaker::Generate1() { // 開始点は1, 1から。(ループの先頭 m_pMatrix->SetAt(1, 1, MMK_Space); ::srand((unsigned int)time(NULL)); int nXCnt; int nYCnt; for (nXCnt = 1; nXCnt < m_nXSize - 1; nXCnt++) { for (nYCnt = 1; nYCnt < m_nYSize - 1; nYCnt++) { while (process(nXCnt, nYCnt) != 0) {} } } return makeStartGoal(); } void CMazeMaker::dig(int nXPos, int nYPos) { m_pMatrix->SetAt(nXPos, nYPos, MMK_Space); int aryMove[4] = {0}; while (1) { int nMoveCount = 0; for (int nCnt = 0; nCnt < 4; nCnt++) { if (checkPos(nXPos, nYPos, moveInfo[nCnt][0], moveInfo[nCnt][1]) == true) { aryMove[nMoveCount] = nCnt; nMoveCount++; } } if (nMoveCount == 0) { break; } int nMove = ((rand() >> 1) % nMoveCount); dig(nXPos + moveInfo[aryMove[nMove]][0], nYPos + moveInfo[aryMove[nMove]][1]); } } bool CMazeMaker::Generate2() { ::srand((unsigned int)time(NULL)); int nXStart = ((rand() >> 1) % (m_nXSize - 2)) + 1; int nYStart = ((rand() >> 1) % (m_nYSize - 2)) + 1; dig(nXStart, nYStart); return makeStartGoal(); } struct PosInfo { int xPos; int yPos; }; bool CMazeMaker::Generate3() { ::srand((unsigned int)time(NULL)); int nXStart = ((rand() >> 1) % (m_nXSize - 2)) + 1; int nYStart = ((rand() >> 1) % (m_nYSize - 2)) + 1; m_pMatrix->SetAt(nXStart, nYStart, MMK_Space); list<PosInfo> posList; PosInfo info = {nXStart, nYStart}; posList.push_back(info); while (posList.size() != 0) { int nXPos = (posList.rbegin())->xPos; int nYPos = (posList.rbegin())->yPos; int aryMove[4] = {0}; int nMoveCount = 0; for (int nCnt = 0; nCnt < 4; nCnt++) { if (checkPos(nXPos, nYPos, moveInfo[nCnt][0], moveInfo[nCnt][1]) == true) { aryMove[nMoveCount] = nCnt; nMoveCount++; } } if (nMoveCount == 0) { posList.pop_back(); continue; } int nMove = ((rand() >> 1) % nMoveCount); info.xPos = nXPos + moveInfo[aryMove[nMove]][0]; info.yPos = nYPos + moveInfo[aryMove[nMove]][1]; m_pMatrix->SetAt(info.xPos, info.yPos, MMK_Space); posList.push_back(info); } return makeStartGoal(); } // // コンソール出力用 class CMazeMakerConsole : public CMazeMaker { public: CMazeMakerConsole(){}; virtual ~CMazeMakerConsole(){}; public: void Output(); }; void CMazeMakerConsole::Output() { for (int nYCnt = 0; nYCnt < m_nYSize; nYCnt++) { for (int nXCnt = 0; nXCnt < m_nXSize; nXCnt++) { if (m_pMatrix->GetAt(nXCnt, nYCnt) == MMK_Space) { printf("."); } else { printf("#"); } } puts(""); } _getch(); } // // int _tmain(int argc, _TCHAR* argv[]) { CMazeMakerConsole maker; do { if (false == maker.Initialize(75, 50)) { puts("Initialize Error"); return 0; } } while (false == maker.Generate3()); //失敗するのは、スタート、ゴールが作れなかった場合。偶然そういうことになることもあるので、そうなったら作り直す。 maker.Output(); return 0; }
最初に昔の記憶を頼りにCMazeMaker::Generate1()を作ったけれど、美しくなかったのでGenerate2()を作成。迷路のサイズを増やすとスタックオーバーフローになるので、Generate3()を作成。一応、満足。
########################################################################### .....##...#.#.##.....##......#....#...#.#.#.........#.##.........##.#.....# #.#.##..#.#......###..###.##.#.##.#.###...#.#####.#....##.######....##.##.# #.#..##.#.#.##.###.##.....##...#....#...#.....##..#.##.#..##...####..#..#.# #.##..###.#..#..##..###.#..###.#.####.###.#####..####..#.##..#....##.##.#.# #..##.....##.##..##...#.##..#######...#.#.#...#.##....##.#..###.#..#.##.### #.####.#####.###.###.##..####.......###.###.#.###..##.#..#.##...##.#..#...# #....###......#..#.#....##....########..##..#..#..#####.######.##..##.##.## #.##..##.######.##...####..#####....#..##..###.#.##.##..#......#..###..#..# #..##.#...##....#..#.#..#.##.#...#.##.##..##.....#.....####.####.##.##.##.# ##..#...#..#.#######...##....#.#####..#..#########.##.##.#..##...##..#....# ###.########.##...##########.#...##..##.##.##...####..#....##..###..#####.# #.#..##...##..#.#.....##.....##.##..##..#...###.#.#..###.####.##...##.....# #.##.#..#..##.#.#####.#..#####..#..##..##.#.....#...##.#.#....####.#..##### #..#.#.###.#..#.....#...##.....##.##..##..##.##.#.###....####.#.#..#.###..# #.##.#.##....######.########.###..#..###.##..#....#.##.#...#..#...##...##.# #..#.#..###.##.#....#..#.....##..###..#...#.#######....#.#.#.##.#.####.##.# ##.#.##.##...#.#.#####...#####..##.##.##.##......#####.###.#.#..#..#.#..#.# ##....#..###...#....#..#.....#.##...#.#...#.#.##.....#..##.#...###...##...# #..#####...########.####.##.##....#...#######..#####.####..#####.########.# #.##.#.###..#.....#....###...###########...###..##...#....##.........##...# #..#....###...#.#.####..#..#.....#.#.....#..#..##..###.#.##..#######.#..### ##.####...#######...###.##.#.###.#...######.####..##...#.#..##...#...#.##.# #....####...#...###...#..#.#..####.#....#.....#..##..#.###.##..#.#.###.#..# ####...#..#.#.#..####.##.####......####.#####.##..#.##...#..####.#.#...#.## ##.###.#.##.#.##.#.#...#....##.######.#.##..#.###.######.##...#..#...###..# #..##..#.####.##.#...#.####..#........#..##....##...##....###...######...## #.##..##..##..#..##.####..##.#.######.##..####..#.#..##.###.#.#..#.##..#..# #.##.####.#..##.##..##...###.###...#...##.##.##.####.#..#...####.#..#.###.# #.#...#...#.###..##..#.#...#..#..#...#..#..#.##..##..#.##.#..##..##.#...### #...#...#.#...##..##...#.####.####.#######.#..##.#..##..#.##..##..#...#...# #.###########.###..#####....#...##.#..#....##..#...####.#..##..##.#######.# #....#....##..#.##....###.#####..#...##.######.#####.##.##.###....#.....#.# ###.##.#.##..##..####..#..#...##.###.##......#...##.....##..########.##.#.# #.#.#..#..#.####.#..##...##.#.#..#....######.#.#....#.#..##.#...##...#....# #...#.###...#......####.##..#...###.###...##.#.######.#.##..#.#..##.####.## #.#.#.#.#####.###.##....#..######.#.##..#..#.#..#.....#..##...##.#...#....# #.###.#...##...#..#..####.##.#.##.#.#..##.##.##.#.###.##..######.#.#.####.# #..##.#.#..#.#.####.###.#..#......#...#####..#..#...####.##..#.#.###.##...# ##..#...##.#.#.#........##.#.#.#.####..#....#######..##...##...#..#...###.# ###.######.#.#...####.#..#.#.###.#..##.#.####....###..###..##.###.###...#.# #...#...#..#####...##.####.#..#....##..#..#...##...##...##......#..####.#.# #.###.#...##...#####..##...##.#.##.#..###.#.######..##.###########.#....#.# #..#..##.##..#.#...#.##..######..###.##.#....###.##.##...#.......#.#.#.#### #.##.###..#.##...#.###..##....##........#.##..#...#..#.#.#.###.#.#.###....# #..#...##.#..#####..##.####.#.##.############...#.##.#.#.#...#.###..##.##.# ##.###..###.##...##.#..#....#..###....##....#######..#.###.###...##..###..# #...###..#...##.###.#.##.#####...#.##.#..##.#...#...##..#...###.####.#...## #.#...##...#......#......#.....#...#....##....#...#..##...#...#........#... ###########################################################################
ちなみに http://anond.hatelabo.jp/20070711194709 これを聞いたのは自分。
かなりたくさん書けることがわかりました。
yourfilehostぶっこぬき
http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv
こんなのからhogehoge.wmvをダウンロードするためのrubyスクリプト
これをbukkonuki.rbとして保存する
require 'rubygems' require 'scrapi' require 'open-uri' yourfilehost_src = Scraper.define do process "embed", :src => "@src" result :src end ARGV.each do |uri| p system "wget", yourfilehost_src.scrape(URI.parse(uri)) end
こんな感じで使うとDLしてくる
ruby bukkonuki.rb "http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv"
複数指定してもDLしてくる
ruby bukkonuki.rb "http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv1" "http://www.yourfilehost.com/media.php?cat=video&file=hogehoge.wmv2"
半日かかったぞ…ふざけやがって