ã¯ããã«
ããã®é£è¼ã§ã¯ã並åå¦çãé«åº¦ã«æ½è±¡åããã¤ã³ãã«TBBãéãã¦ã並ååã®èãæ¹ãåå¾ãããã¨ãç®çã¨ãã¦ãã¾ããä»å¾ã並ååã¯å½ããåã®ãã®ã¨ãªãããã¾ãã¾ãªå½¢ã§ãµãã¼ããããããã«ãªãã¾ãã並ååå¦çã®æ ¹åºã«æµããèãæ¹ã身ã«ã¤ããã°ããã®å¤åã«å¯¾å¿ã§ãã¾ãã
ãä»åã¯ã¹ã¬ããã»ã¼ãããã³ã¤ã³ãã«TBBã«ç¨æããã¦ãã4åã®ã³ã³ããã®ä½¿ãæ¹ã¨æ³¨æããã¹ãç¹ã«ã¤ãã¦è§£èª¬ãã¾ãããã®é£è¼ã®ãµã³ãã«ã¯ããã¾ã§ãã¤ã³ãã«TBBã®ä½¿ãæ¹ã説æãããã®ã§ãããå®åãç¹å¥ã«æèãããã®ã§ã¯ããã¾ããããã®ç¹ããç解ãã ããã
対象èªè
ãçè ãæ³å®ãã¦ããèªè ã¯C++ã®åºæ¬çææ³ãç解ãã並ååããã°ã©ãã³ã°ã«èå³ãæã£ã¦ããæ¹ã§ããé«åº¦ãªC++ãã¯ããã¯ã極åãããåºæ¬çãªææ³ããåããã°èªããããã«æ¥µå注æãã¾ãã®ã§ã並ååã«èå³ãæã£ã¦ããæ¹ã¯ãã²ãã®é£è¼ã«ç®ãéãã¦ãã ããã
å¿ è¦ãªç°å¢
ãC++ã³ã³ãã¤ã©ãå¿ è¦ã§ãããæã¡ã§ãªãæ¹ã¯ç¡åã§æä¾ããã¦ãããã¤ã¯ãã½ãã社ã®ãVisual Studio C++ 2008 Express Editionãããã¦ã³ãã¼ããããªã©ãã¦å ¥æãã¦ãã ããã
ããã®é£è¼ã¯åºæ¬çã«Windowsç°å¢ãæ³å®ãã¦è§£èª¬ãã¾ãããã¤ã³ãã«TBBãã®ãã®ã¯ä»ã®OSä¸ã§ãåä½ãã¾ãã®ã§ãé©å®èªã¿æ¿ãã¦åèã«ãã¦ãã ããã
ããã®ã»ãã«ãã¤ã³ãã«TBBã使ãããã®æºåãå¿ è¦ãªã®ã§æ¬¡é ã§è§£èª¬ãã¾ãã
ã¤ã³ãã«TBBã®æºå
ãã¤ã³ãã«TBBã使ç¨ããæ¹æ³ã¯2ã¤ããã¾ãã1ã¤ç®ã¯ãã¤ã³ãã« Parallel Studioãã使ç¨ãããã¨ã§ããã¤ã³ãã« Parallel Studioã¯ã¤ã³ãã«TBBããããããç¨æããã¦ããããªãã·ã§ã³ãæå®ããã ãã§ã¤ã³ãã«TBBã使ç¨ã§ãã¾ãã詳ããã¯ãã¤ã³ãã« Parallel Studioã使ã£ã¦ä¸¦ååããã°ã©ãã³ã°ã試ãã¦ã¿ããï¼Codezineï¼ãåç §ãã ããã
ã2ã¤ç®ã®æ¹æ³ã¯ããªã¼ãã³ã½ã¼ã¹çã®ã¤ã³ãã«TBBãå ¬å¼ãã¼ã ãã¼ã¸ãããã¦ã³ãã¼ããã¦ãã¦ãã©ã¤ãã©ãªã¨ã¤ã³ã¯ã«ã¼ããã¡ã¤ã«ã¸ã®ãã¹ãæå®ãããã¨ã§ãããããããã®æé ã解説ãã¾ãããã ãããã®è¨äºã¯2009å¹´10æã«æ¸ããããã®ã§ãããä»å¾URLãå¤æ´ãããæããããã¾ãã®ã§ãã®ç¹ã«æ³¨æãã¦ãã ããã
ãã¾ãã¯å ¬å¼ãã¼ã ãã¼ã¸ã«ã¢ã¯ã»ã¹ããï¼»Downloadsï¼½ã¨æ¸ãããã¿ã°ãé¸æãã¾ããããã¦ãï¼»Stable Releaseï¼½ã¨ããè¦åºããã¯ãªãã¯ããã¨ãã¤ã³ãã«TBBã®ããã¤ãã®ãã¼ã¸ã§ã³ã表示ããã¾ãã
ãããã§ã¯ãtbb22_20090809ossããé¸æããã¯ãªãã¯ãããã®ã¨ãã¦è©±ãé²ãã¾ããç»é¢ä¸é¨ã®ãtbb22_20090809oss_win.zipãã¨æ¸ãããå³æ¨ªã«ããï¼»Downloadï¼½ãªã³ã¯ãã¯ãªãã¯ããã¨ããã¦ã³ãã¼ããå§ã¾ãã¾ãããã¦ã³ãã¼ãå¾ã¯ã好ããªå ´æã«è§£åãã¦ãã ããã
ãç¶ãã¦Visual Studioï¼ä»¥ä¸ãVSï¼ã§ãã¹ã®è¨å®ãè¡ãã¾ããï¼»ãã¼ã«ï¼½-ï¼»ãªãã·ã§ã³ï¼½-ï¼»ããã¸ã§ã¯ãããã³ã½ãªã¥ã¼ã·ã§ã³ï¼½-ï¼»VC++ãã£ã¬ã¯ããªã£ï¼½ãé¸æããã¨ãããã£ã¬ã¯ããªã£ã表示ããããã¸ã§ã¯ããã¨ããé ç®ãããã®ã§ãããã§ã¤ã³ã¯ã«ã¼ããã¡ã¤ã«ãé¸æãã¦ãããå ã»ã©ãã¦ã³ãã¼ãããTBBã®ãã¹ã追å ãã¾ããä¾ãã°ãCãã©ã¤ãã«è§£åããã¨ä»®å®ããã¨ãã¹ã¯ãC:\tbb22_20090809oss\includeãã«ãªãã¾ãããã®ãã¹ã追å ããããåæ§ã«ãã¦ãã©ã¤ãã©ãªãã¨ãã½ã¼ã¹ã³ã¼ããã®ãã¹ã追å ãã¦ãã ããã
ã以ä¸ã§è¨å®ã¯å®äºã§ããããã§ã¤ã³ãã«TBBã使ãæºåãæ´ãã¾ããã
並åå¦çã¨ã³ã³ããï¼ã¹ã¬ããã»ã¼ãã¨ã¯ä½ã
ã並åããã°ã©ãã³ã°ãè¡ãéã«ã¯ãå¾æ¥ã®é次ããã°ã©ãã³ã°ã§ã¯èããªãã£ããã¨ãèããªãã¦ã¯ãªãã¾ããããã®ãã¡ã®1ã¤ãã並åå¦çã§åæã«ã³ã³ãããæä½ããæã«èµ·ããåé¡ã«ã¤ãã¦ã§ããããã¯ããããã«ãã¹ã¬ããããã°ã©ãã³ã°ã§ã¯ã¹ã¬ããã»ã¼ããªã³ã³ããã使ãå¿ è¦ããããã¨è¡¨ç¾ããã¾ããããããã¹ã¬ããã»ã¼ãã¨ã¯ä½ãªã®ããããããããªã人ãå¤ããã¨æãã¾ããããã¯ç¡çããªããã¨ãªã®ã§ãæ°ã«ããå¿ è¦ã¯ããã¾ããããã®é ã§ã¯ãã®ãã¨ã«ã¤ãã¦è§£èª¬ãã¾ãã
ãã¹ã¬ããã»ã¼ãã§ãªãã³ã³ããã使ç¨ããã¨ä½ãåé¡ãªã®ããç解ããããã«ã¯ãå®éã«ã·ã³ãã«ãªã³ã³ãããä½ãã®ã1çªã§ããããããã°ãä½ãåé¡ãªã®ããåããã¾ããããããã¹ã¬ããã»ã¼ãã§ãªã極åã·ã³ãã«ã«ããListãå®è£
ãããµã³ãã«ããã¸ã§ã¯ãSimpleList
ãæ²è¼ãã¾ããã¾ãã¯ãã®ã³ã¼ããè¦ã¦ãã ããã
#include <iostream> using namespace std; /*------------------------------------------------------------------------------- 0以ä¸ã®æ°å¤ã®ã¿ãæ ¼ç´ããã·ã³ãã«ãªãªã¹ã ----------------------------------------------------------------------------------*/ class List { private: int size; int index; int* buffer; public: /*------------------------------------------------------- ãªã¹ãã®ãµã¤ãºãæå®ãã¦åæå ---------------------------------------------------------*/ List( int size ) { this->size = size; this->buffer = new int[ size ]; this->index = -1; for ( int i = 0; i < this->size; ++i ) this->buffer[ i ] = -1; } /*------------------------------------------------------- ãããã¡ãåé¤ãã ---------------------------------------------------------*/ ~List() { delete[] buffer; } /*------------------------------------------------------- ãªã¹ãã«åå¨ããè¦ç´ ã®åæ° ---------------------------------------------------------*/ int Count() { return this->index + 1; } /*------------------------------------------------------- ãã¼ã¿ã追å ãã æ»ãå¤ã¯æåæ0ã»å¤±ææ-1 ---------------------------------------------------------*/ int Add( int data ) { if ( index < size && data >= 0 ) { this->index++; this->buffer[ index ] = data; return 0; } else { return -1; } } /*------------------------------------------------------- æå®ããä½ç½®ã«ããè¦ç´ ãåé¤ãã æ»ãå¤ã¯æåæ0ã»å¤±ææ-1 ---------------------------------------------------------*/ int Remove( int pos ) { if ( pos <= index && pos >= 0 ) { //ãã¼ã¿ã1ã¤å·¦ã¸ç§»åãã¦è¦ç´ ãæ¶ã for ( int i = pos; i < index; ++i ) { this->buffer[ i ] = this->buffer[ i + 1 ]; } this->buffer[ this->index ] = -1; //æªä½¿ç¨é åãåæå this->index--; return 0; } else { return -1; } } //æå®ãããä½ç½®ã®è¦ç´ ãè¿ã int& operator[] ( int pos ) { if ( pos <= index && pos >= 0 ) { return this->buffer[ pos ]; } else { cout << "å¢çã¨ã©ã¼ãçºçãã¾ããã" << endl; exit( 1 ); } } }; /*------------------------------------------------------------------------------- ã¡ã¤ã³ããã°ã©ã ----------------------------------------------------------------------------------*/ int main(void) { //0ï½9ã®æ°å¤ãæ ¼ç´ãã¦è¡¨ç¤º int size = 10; List list( size ); for ( int i = 0; i < size; ++i ) { list.Add( i ); } cout << "ãªã¹ãã®ãµã¤ãºã¯" << list.Count() << "ã§ãã" << endl; cout << "æ ¼ç´ããã¦ããè¦ç´ ã¯ã»ã»ã»" << endl; for ( int i = 0; i < list.Count(); ++i ) { cout << list[ i ] << " "; } cout << endl << endl; //è¦ç´ ãã²ã¨ã¤åé¤ãã¦è¡¨ç¤º int pos = 0; cout << "ã¤ã³ããã¯" << pos << "ã®è¦ç´ ãåé¤ãã¾ãã" << endl; int result = list.Remove( pos ); if ( result == -1 ) { cout << "åå¨ããªãè¦ç´ ãåé¤ãããã¨ãã¾ãããã¤ã³ããã¯ã¹ã®æå®ãééã£ã¦ãã¾ãã" << endl; exit( 1 ); } cout << "ãªã¹ãã®ãµã¤ãºã¯" << list.Count() << "ã§ãã" << endl; cout << "æ ¼ç´ããã¦ããè¦ç´ ã¯ã»ã»ã»" << endl; for ( int i = 0; i < list.Count(); ++i ) { cout << list[ i ] << " "; } cout << endl << endl; return 0; }
ããµã³ãã«ã³ã¼ãSimpleList
ã¯ã0以ä¸ã®æ°å¤ããæ ¼ç´ã§ããªããªã¹ãã«å¯¾ãã¦ã0ï½10ã®æ°å¤ããªã¹ãã«è¿½å ãã¦ãã0ãåé¤ãã¦ãã¾ããåé¤ã¯ãããã¡å
ã®ãã¼ã¿ãå·¦ï¼ã¤ã³ããã¯ã¹ãä½ãæ¹ï¼ã«1ã¤ãããã¦ä¸æ¸ãããæå¾ã®ä½ç½®ã-1ã§åæåãããã¨ã«ããå®ç¾ãã¦ãã¾ãããµã³ãã«ãå³ç¤ºããã¨ä¸è¨ã®ã¨ããã«ãªãã¾ãã
ãSimpleList
ãµã³ãã«ããã¸ã§ã¯ããå®è¡ãã¦ã¿ã¦ãã ããããã®ããã«ãé次ããã°ã©ãã³ã°ããã¦ããæã¯ãåé¡ã¯çãã¾ããããããã並åããã°ã©ãã³ã°ãè¡ã£ãæãåé¡ãçãã¾ãããã®ãã¨ã次é
ã®SimpleList2
ãµã³ãã«ããã¸ã§ã¯ãã§ç¤ºãã¾ãã