C++ (Qt)using namespace std::chrono; const unsigned Num = 1000000; QStringList list; initList<Num>(list); std::mt19937 gen; high_resolution_clock::time_point t1 = high_resolution_clock::now(); std::random_shuffle(list.begin(), list.end(), std::bind(std::uniform_int_distribution<int>(0, Num-1), gen)); high_resolution_clock::time_point t2 = high_resolution_clock::now(); duration<double> time_span = duration_cast<duration<double>>(t2 - t1); std::cout << "random_shuffle: " << time_span.count() << " seconds." << std::endl; t1 = high_resolution_clock::now(); for (int i = 0; i < list.size(); ++i) { list.swap(i, qrand() % list.size()); } t2 = high_resolution_clock::now(); time_span = duration_cast<duration<double>>(t2 - t1); std::cout << "igors: " << time_span.count() << " seconds." << std::endl;
Bashrandom_shuffle: 0.237838 seconds.igors: 0.27764 seconds.
C++ (Qt) // Slide slideShowButton = new QToolButton ( navToolbarBefore ); slideShowButton->setIcon( QIcon (":/icons/show.png") ); slideShowButton->setToolTip( tr( "Slideshow Play/Pause" ) ); slideShowButton->setCheckable(true); slideShowButton->setChecked(false); slideShowButton->setContextMenuPolicy( Qt::CustomContextMenu ); navToolbarBefore->addWidget( slideShowButton ); zeroActionSlideShow = navToolbarBefore->addWidget( slideShowButton ); zeroActionSlideShow->setVisible( cfg.preferences.slideshowButtonVisible ); slideShowTimerAction = new QAction( tr( "Set Timer" ), this ); slideShowRandomAction = new QAction( tr( "Random" ), this ); slideShowRandomAction->setCheckable(true); slideShowRandomAction->setChecked(cfg.preferences.slideShowRandom); slideShowPopupAction = new QAction( tr( "Popup Window" ), this ); slideShowPopupAction->setCheckable(true); slideShowPopupAction->setChecked(cfg.preferences.slideShowPopup); slideShowPopupOnTopAction = new QAction( tr( "Popup Window On Top" ), this ); slideShowPopupOnTopAction->setCheckable(true); slideShowPopupOnTopAction->setChecked(cfg.preferences.alwaysOnTopPopup); slideShowGrandAction = new QAction( tr( "Grand Window" ), this ); slideShowGrandAction->setCheckable(true); slideShowGrandAction->setChecked(cfg.preferences.slideShowGrand); sendToTranslateLineAction = new QAction( tr( "Send To Translate Line" ), this ); sendToTranslateLineAction->setCheckable(true); sendToTranslateLineAction->setChecked(cfg.preferences.sendToTranslateLine); slideShowGrandOnTopAction = new QAction( tr( "Grand Window On Top" ), this ); slideShowGrandOnTopAction->setCheckable(true); slideShowGrandOnTopAction->setChecked(cfg.preferences.alwaysOnTop); slideShowOpenAction = new QAction( tr( "Open" ), this ); slideShowQuitAction = new QAction( tr( "Stop" ), this ); slideShowPlay = false; slideShowPause = true; slideShowStop = false; connect( slideShowButton, SIGNAL( toggled( bool ) ), this, SLOT( slideShowOn( bool ) ) ); connect( slideShowButton, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( contextToolbarSlideShowButton( const QPoint & ) ) ); connect( slideShowTimerAction, SIGNAL( triggered() ), this, SLOT(slideShowTimer())); connect( slideShowRandomAction, SIGNAL( triggered( bool ) ), this, SLOT( slideShowRandomTriggered( bool ) ) ); connect( slideShowPopupAction, SIGNAL( triggered( bool ) ), this, SLOT( slideShowPopupTriggered( bool ) ) ); connect( slideShowPopupOnTopAction, SIGNAL( triggered( bool ) ), this, SLOT( slideShowOnTopTriggered( bool ) ) ); connect( slideShowGrandAction, SIGNAL( triggered( bool ) ), this, SLOT( slideShowGrandTriggered( bool ) ) ); connect( sendToTranslateLineAction, SIGNAL( triggered( bool ) ), this, SLOT( sendToTranslateLineTriggered( bool ) ) ); connect( slideShowGrandOnTopAction, SIGNAL( triggered( bool ) ), this, SLOT( on_alwaysOnTop_triggered( bool ) ) ); connect( slideShowOpenAction, SIGNAL( triggered() ), this, SLOT( openNewSlideShow() ) ); connect( slideShowQuitAction, SIGNAL( triggered() ), this, SLOT( slideShowQuit() ) );
C++ (Qt)void MainWindow::slideShowOn(bool on){ if(!on) { slideShowPause = true; return; } else if(slideShowPlay && slideShowPause) { slideShowPause = false; return; } else { if(!cfg.preferences.slideShowPopup && !cfg.preferences.slideShowGrand) { slideShowPopupAction->setChecked(true); cfg.preferences.slideShowPopup = true; } slideShowStop = false; slideShowPlay = true; slideShowPause = false; QString importPath; if( cfg.contentExportPath.isEmpty() ) importPath = QDir::homePath(); else { importPath = QDir::fromNativeSeparators( cfg.contentExportPath ); if( !QDir( importPath ).exists() ) importPath = QDir::homePath(); } QString fileName = QFileDialog::getOpenFileName( this, tr( "Slideshow content from file" ), importPath, tr( "Text files (*.txt);;All files (*.*)" ) ); if( fileName.size() == 0) { slideShowButton->setChecked(false); return; } QFileInfo fileInfo( fileName ); QString contentName = fileInfo.baseName(); QFile file( fileName ); slideShowOnTopTriggered( cfg.preferences.alwaysOnTopPopup ); int n(0); for(;;) { // Count n=0; if ( file.open( QFile::ReadOnly ) ) { char buf[1024]; qint64 lineLength; forever { lineLength = file.readLine(buf, sizeof(buf)); if (lineLength > 0) { ++n; } else if (lineLength == -1) { break; } } file.close(); } slideShowName.setNum(n); slideShowName = contentName + "/" + slideShowName; if(slideShowRandomAction->isChecked()) { if ( file.open( QFile::ReadOnly | QIODevice::Text ) ) { QTextStream fileStr( & file ); QStringList list; if(slideShowGrandAction->isChecked())setWindowTitle( slideShowName + "/Random" ); if(slideShowPopupAction->isChecked())winPopup->setWindowTitle( slideShowName + "/Random" ); while(!fileStr.atEnd()) { list.append(fileStr.readLine()); } for (int i = 0; i < list.size(); ++i) list.swap(qrand() % list.size(), qrand() % list.size()); foreach (QString itm, list) { do { QEventLoop loop; QTimer::singleShot(0, &loop, SLOT(quit())); loop.exec(); // Нормально стопим паузу для экстренного выхода программы if(slideShowStop)break; } while(slideShowPause); if(slideShowGrandAction->isChecked())showTranslationFor( itm ); if(sendToTranslateLineAction->isChecked())translateLine->setText( itm ); if(slideShowPopupAction->isChecked())winPopup->slideTranslationFor( itm ); for(int i=1; i<=cfg.preferences.slideShowTimer/1000; ++i) { QEventLoop loop3; QTimer::singleShot(1000, &loop3, SLOT(quit())); loop3.exec(); } if(!cfg.preferences.slideShowRandom)break; if(slideShowStop)break; } file.close(); } } if(!slideShowStop) { if ( file.open( QFile::ReadOnly | QIODevice::Text ) ) { QTextStream fileStream( & file ); QString itemStr; if(slideShowGrandAction->isChecked())setWindowTitle( slideShowName ); if(slideShowPopupAction->isChecked())winPopup->setWindowTitle( slideShowName ); do { do { QEventLoop loop; QTimer::singleShot(0, &loop, SLOT(quit())); loop.exec(); if(slideShowStop)break; } while(slideShowPause); itemStr = fileStream.readLine(); if( fileStream.status() >= QTextStream::ReadCorruptData ) break; trimmedStr = itemStr.trimmed(); if( trimmedStr.isEmpty() ) continue; if(slideShowGrandAction->isChecked())showTranslationFor( trimmedStr ); if(sendToTranslateLineAction->isChecked())translateLine->setText( trimmedStr ); if(slideShowPopupAction->isChecked())winPopup->slideTranslationFor( trimmedStr ); for(int i=1; i<=cfg.preferences.slideShowTimer/1000; i++) { QEventLoop loop3; QTimer::singleShot(1000, &loop3, SLOT(quit())); loop3.exec(); } if(cfg.preferences.slideShowRandom)break; if(slideShowStop)break; } while( !fileStream.atEnd() ); } file.close(); } if(slideShowStop)break; } }} void MainWindow::quitApp(){ slideShowStop = true; qApp->quit();} void MainWindow::openNewSlideShow(){ slideShowStop = true; slideShowOn(true); } void MainWindow::slideShowTimer(){ TimerDialog *dialog = new TimerDialog( cfg ); dialog->setWindowFlags(Qt::WindowStaysOnTopHint); dialog->show();} void MainWindow::slideShowRandomTriggered( bool checked ){ cfg.preferences.slideShowRandom = checked;} void MainWindow::slideShowPopupTriggered( bool checked ){ cfg.preferences.slideShowPopup = checked; if(cfg.preferences.slideShowRandom) { winPopup->setWindowTitle( slideShowName + "/Random"); } else { winPopup->setWindowTitle( slideShowName); }} void MainWindow::slideShowGrandTriggered( bool checked ){ cfg.preferences.slideShowGrand = checked; if(cfg.preferences.slideShowRandom) { setWindowTitle( slideShowName + "/Random"); } else { setWindowTitle( slideShowName); }} void MainWindow::sendToTranslateLineTriggered( bool checked ){ cfg.preferences.sendToTranslateLine = checked;} void MainWindow::slideShowQuit(){ slideShowButton->setChecked(false); slideShowPlay = false; slideShowPause = true; slideShowStop = true;} void MainWindow::contextToolbarSlideShowButton( const QPoint &pos){ QMenu * slideMenu = new QMenu( this ); slideMenu->addAction( slideShowTimerAction ); slideMenu->addAction( slideShowRandomAction ); slideMenu->addSeparator(); slideMenu->addAction( slideShowPopupAction ); slideMenu->addAction( slideShowPopupOnTopAction ); slideMenu->addSeparator(); slideMenu->addAction( slideShowGrandAction ); slideMenu->addAction( sendToTranslateLineAction ); slideMenu->addAction( slideShowGrandOnTopAction ); slideMenu->addSeparator(); slideMenu->addAction( slideShowOpenAction ); slideMenu->addAction( slideShowQuitAction ); slideMenu->exec( slideShowButton->mapToGlobal( pos ) );}
C++ (Qt)#include <QtGui> #include "timerslide.hh" TimerDialog::TimerDialog( Config::Class &cfg_, QWidget *parent ) : QDialog( parent ), cfg(cfg_) { setWindowTitle(tr("Timer")); setMinimumWidth(210); setHoursLabel = new QLabel(tr("Hours:")); setHoursSpinBox = new QSpinBox(); setHoursSpinBox->setValue(cfg.preferences.slideShowTimer/3600000); setHoursSpinBox->setMinimum(0); setHoursSpinBox->setMaximum(12); setMinutesLabel = new QLabel(tr("Minutes:")); setMinutesSpinBox = new QSpinBox(); setMinutesSpinBox->setValue(cfg.preferences.slideShowTimer%3600000/60000); setMinutesSpinBox->setMinimum(0); setMinutesSpinBox->setMaximum(60); setSecondsLabel = new QLabel(tr("Seconds:")); setSecondsSpinBox = new QSpinBox(); setSecondsSpinBox->setValue(cfg.preferences.slideShowTimer%3600000%60000/1000); setSecondsSpinBox->setMinimum(1); setSecondsSpinBox->setMaximum(60); setSecondsSpinBox->setFocus(); setOkButton = new QPushButton(tr("Ok")); setOkButton->setToolTip( tr( "Set Timer" ) ); QGridLayout *timerLayout = new QGridLayout; timerLayout->addWidget(setHoursLabel, 0, 0); timerLayout->addWidget(setHoursSpinBox, 0, 1); timerLayout->addWidget(setMinutesLabel, 1, 0); timerLayout->addWidget(setMinutesSpinBox, 1, 1); timerLayout->addWidget(setSecondsLabel, 2, 0); timerLayout->addWidget(setSecondsSpinBox, 2, 1); timerLayout->addWidget(setOkButton, 3, 0, 1, 2); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(timerLayout); setLayout(mainLayout); connect(setOkButton, SIGNAL(clicked()), this, SLOT(setOkButtonClicked()));} void TimerDialog::setOkButtonClicked(){ int valtHours = setHoursSpinBox->value(); int valMinutes = setMinutesSpinBox->value(); int valSeconds = setSecondsSpinBox->value(); cfg.preferences.slideShowTimer = valtHours*60*60*1000+valMinutes*60*1000+valSeconds*1000; close();}
C++ (Qt)QStringList list = in.readAll().split(QRegExp("[\\s\n]"), QString::SkipEmptyParts);
C++ (Qt)split("\n", QString::SkipEmptyParts)
do{ QEventLoop loop; QTimer::singleShot(0, &loop, SLOT(quit())); loop.exec(); // Нормально стопим паузу для экстренного выхода программы if(slideShowStop)break;} while(slideShowPause);
C++ (Qt)QStringList list = in.readAll().trimmed().split("\n", QString::SkipEmptyParts);