/[projects]/queensgui/src/queensmain.cpp
ViewVC logotype

Annotation of /queensgui/src/queensmain.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 655 - (hide annotations) (download)
Fri Apr 23 05:57:28 2010 UTC (14 years, 1 month ago) by torben
File size: 9599 byte(s)
Code sync towards a faster implementation. The key is to avoid virtual functions and inline as many functions as posible
1 torben 1 /***************************************************************************
2     * Copyright (C) 2005 by Torben Nielsen *
3     * torben@t-hoerup.dk *
4     * *
5     * This program is free software; you can redistribute it and/or modify *
6     * it under the terms of the GNU General Public License as published by *
7     * the Free Software Foundation; either version 2 of the License, or *
8     * (at your option) any later version. *
9     * *
10     * This program is distributed in the hope that it will be useful, *
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13     * GNU General Public License for more details. *
14     * *
15     * You should have received a copy of the GNU General Public License *
16     * along with this program; if not, write to the *
17     * Free Software Foundation, Inc., *
18     * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19     ***************************************************************************/
20     #include <qapplication.h>
21     #include <qlabel.h>
22     #include <qlayout.h>
23 torben 327 #include <q3listbox.h>
24     #include <q3popupmenu.h>
25 torben 1 #include <qpushbutton.h>
26     #include <qspinbox.h>
27     #include <qstring.h>
28    
29    
30 torben 2
31 torben 1 #include "queensmain.h"
32 torben 327 //Added by qt3to4:
33     #include <Q3HBoxLayout>
34     #include <QContextMenuEvent>
35     #include <Q3VBoxLayout>
36 torben 1 #include "board.h"
37     #include "queens.h"
38 torben 5
39 torben 2 #include "solution.h"
40 torben 5 #include "solutionmatrix.h"
41     #include "solutionint.h"
42 torben 1
43 torben 4 #include "containervector.h"
44     #include "containerlist.h"
45     #include "containerhash.h"
46     #include "containermnvector.h"
47 torben 13 #include "containerminimalvector.h"
48 torben 4
49 torben 1 #include "config.h"
50    
51 torben 2
52 torben 5
53 torben 1 QueensMain::QueensMain(QWidget *parent, const char *name)
54     : QDialog(parent, name)
55     {
56 torben 327 Q3VBoxLayout *mainlayout = new Q3VBoxLayout( this );
57 torben 1
58     m_board = new Board( this );
59    
60     m_start = new QPushButton("Start", this);
61     m_quit = new QPushButton("Quit", this);
62     m_stop = new QPushButton("Stop", this);
63     m_stop->setEnabled( false );
64    
65 torben 327 Q3HBoxLayout *upperlayout = new Q3HBoxLayout( mainlayout );
66     Q3VBoxLayout *left = new Q3VBoxLayout( upperlayout );
67 torben 1
68     upperlayout->add( m_board );
69 torben 327 m_list = new Q3ListBox( this );
70 torben 1
71     m_list->setMinimumWidth( 180 );
72     m_sizeSelector = new QSpinBox( this );
73     m_status = new QLabel( this );
74    
75     left->add( m_list );
76     left->add( m_sizeSelector );
77    
78 torben 327 Q3HBoxLayout *buttons = new Q3HBoxLayout( mainlayout );
79 torben 1 buttons->add( m_start );
80     buttons->add( m_stop );
81     buttons->add( m_quit );
82    
83     mainlayout->add( m_status );
84    
85     m_sizeSelector->setMinValue( MIN_SIZE );
86     m_sizeSelector->setMaxValue( MAX_SIZE );
87     m_sizeSelector->setValue( 8 );
88     m_board->setSize( 8 );
89    
90 torben 10 m_storage = StorageInt;
91 torben 17 m_sortalgo = SortMinimalVector;
92 torben 1 m_solutions = NULL;
93     m_queens = NULL;
94 torben 4 m_sol = NULL;
95 torben 1
96     connect(m_quit,
97     SIGNAL( clicked() ),
98     qApp,
99     SLOT( quit() )
100     );
101    
102     connect(m_sizeSelector,
103     SIGNAL( valueChanged(int) ),
104     this,
105     SLOT( resize(int) )
106     );
107    
108     connect(m_start,
109     SIGNAL( clicked() ),
110     this,
111     SLOT( start() )
112     );
113    
114     connect(m_stop,
115     SIGNAL( clicked() ),
116     this,
117     SLOT( stop() )
118     );
119    
120     connect(m_list,
121     SIGNAL( selectionChanged() ),
122     this,
123     SLOT( showSolution() )
124     );
125    
126     connect( qApp,
127     SIGNAL( lastWindowClosed() ),
128     qApp,
129     SLOT( quit() )
130     );
131     }
132    
133    
134     QueensMain::~QueensMain()
135     {
136     delete m_board;
137 torben 7
138     if (m_solutions != NULL)
139     delete m_solutions;
140     if (m_sol != NULL)
141     delete m_sol;
142 torben 1 }
143    
144     void QueensMain::start()
145     {
146     m_sizeSelector->setEnabled( false );
147     m_start->setEnabled( false );
148     m_stop->setEnabled( true );
149     m_list->clear();
150     m_status->setText( QString("Searching ...") );
151    
152 torben 6 m_board->setMatrix(0);
153    
154 torben 1 if (m_queens != NULL) {
155     m_queens->wait();
156     delete m_queens;
157 torben 7 m_queens = 0;
158 torben 1 }
159    
160     if (m_solutions != NULL)
161     delete m_solutions;
162     switch (m_sortalgo) {
163     case SortList:
164 torben 4 m_solutions = new ContainerList(this);
165 torben 1 break;
166     case SortVector:
167 torben 4 m_solutions = new ContainerVector(this);
168 torben 1 break;
169     case SortHash:
170 torben 4 m_solutions = new ContainerHash(this);
171 torben 1 break;
172     case SortMNVector:
173 torben 4 m_solutions = new ContainerMNVector(this);
174 torben 1 break;
175 torben 13 case SortMinimalVector:
176     m_solutions = new ContainerMinimalVector(this);
177     break;
178 torben 1 }
179 torben 2
180     if (m_sol != NULL)
181     delete m_sol;
182 torben 655 /*
183 torben 2 switch(this->m_storage) {
184     case StorageInt:
185 torben 5 m_sol = new SolutionInt(m_sizeSelector->value());
186 torben 2 break;
187     case StorageMatrix:
188 torben 5 m_sol = new SolutionMatrix(m_sizeSelector->value());
189 torben 2 break;
190 torben 655 }*/
191 torben 1
192 torben 655 m_sol = new Solution( m_sizeSelector->value() );
193 torben 2
194 torben 655
195 torben 1 m_elapsed.start();
196     m_time.start();
197 torben 4 m_queens = new Queens(this, m_sol, m_solutions, m_sizeSelector->value() ,false);
198 torben 1 m_queens->start();
199     }
200    
201     void QueensMain::stop()
202     {
203     m_solutions->halt();
204     m_queens->stop();
205     m_queens->wait();
206    
207     int num = m_solutions->numSolutions();
208     m_status->setText( QString("Aborted. Found ") + QString::number(num,10).append(" solutions") );
209     m_list->clear();
210     for (int i=1; i<=num; i++)
211     m_list->insertItem( QString("Solution no ") + QString::number(i,10), i);
212    
213     delete m_queens;
214     m_queens = NULL;
215    
216     m_list->setEnabled( true );
217     m_sizeSelector->setEnabled( true );
218     m_start->setEnabled( true );
219     m_stop->setEnabled( false );
220     }
221    
222     void QueensMain::foundSolution()
223     {
224 torben 18 int num = m_solutions->totalSolutions();
225 torben 1 if (m_elapsed.elapsed() > 500)
226     {
227     m_status->setText( QString("Searching ... found ") + QString::number(num,10).append(" solutions") );
228     m_elapsed.restart();
229     }
230     }
231    
232     void QueensMain::finishedSearch()
233     {
234     uniqueSolutions();
235     m_sizeSelector->setEnabled( true );
236     m_start->setEnabled( true );
237     m_stop->setEnabled( false );
238     }
239    
240    
241     void QueensMain::showSolution()
242     {
243     int index = m_list->currentItem();
244 torben 2 Solution* sol = m_solutions->solution( index );
245 torben 4 m_board->setMatrix( sol );
246 torben 1 }
247    
248     void QueensMain::resize(int size)
249     {
250     m_board->setSize( size );
251     m_board->repaint( true );
252     m_list->clear();
253     }
254    
255     QString QueensMain::elapsed()
256     {
257     int time = m_time.elapsed();
258     int msec = time % 1000;
259     int sec = time / 1000;
260     int min = sec / 60;
261     sec %= 60;
262     QString smsec = (QString::number(msec)).rightJustify(3, '0');
263     QString ssec = (QString::number(sec)).rightJustify(2,'0');
264    
265     QString res;
266     if (min!=0)
267     res = QString("%1:%2.%3").arg(min).arg(ssec).arg(smsec);
268     else
269     res = QString("%1.%2").arg(sec).arg(smsec);
270     return res;
271     }
272    
273     void QueensMain::uniqueSolutions()
274     {
275 torben 15 m_totalcount = m_solutions->totalSolutions();
276 torben 1 m_status->setText( QString("sorting ") + QString::number(m_totalcount,10).append(" solutions...") );
277    
278     m_elapsed.restart();
279     m_solutions->uniqueSolutions();
280    
281     int uniq = m_solutions->numSolutions();
282    
283    
284     QString msg;
285     msg = QString( "Found %1 unique solutions, of %2 total solutions. Time elapsed: %3" ).arg(uniq).arg(m_totalcount).arg( elapsed() );
286    
287     m_list->clear();
288     for (int i=1; i<=uniq; i++)
289     m_list->insertItem( QString("Unique solution no ") + QString::number(i,10), i);
290    
291     m_status->setText( msg );
292     }
293    
294     void QueensMain::contextMenuEvent( QContextMenuEvent *event)
295     {
296 torben 327 Q3PopupMenu *contextMenu = new Q3PopupMenu( this );
297 torben 1 contextMenu->setCheckable( true );
298 torben 327 contextMenu->insertItem( "---Container class---" );
299 torben 1
300     contextMenu->insertItem("List", this, SLOT( sortList() ), 0, 1);
301     contextMenu->insertItem("Vector", this, SLOT( sortVector() ), 0, 2);
302     contextMenu->insertItem("Hash", this, SLOT( sortHash() ), 0, 3);
303     contextMenu->insertItem("M*N Vector", this, SLOT(sortMNVector()), 0, 4);
304 torben 13 contextMenu->insertItem("Minimal Vector", this, SLOT(sortMinimalVector()), 0, 5);
305 torben 2
306 torben 327 contextMenu->insertItem( "---Solution Class---" );
307 torben 13 contextMenu->insertItem("Matrix Solution", this, SLOT( storageMatrix() ), 0, 6);
308     contextMenu->insertItem("Int Solution", this, SLOT( storageInt() ), 0, 7);
309 torben 1
310 torben 2
311 torben 1 switch (m_sortalgo) {
312     case SortList:
313     contextMenu->setItemChecked( 1, true);
314     break;
315     case SortVector:
316     contextMenu->setItemChecked( 2, true);
317     break;
318     case SortHash:
319     contextMenu->setItemChecked( 3, true);
320     break;
321     case SortMNVector:
322     contextMenu->setItemChecked( 4, true);
323     break;
324 torben 13 case SortMinimalVector:
325     contextMenu->setItemChecked( 5, true);
326     break;
327 torben 1 }
328    
329 torben 2 switch (m_storage) {
330     case StorageMatrix:
331 torben 13 contextMenu->setItemChecked(6, true);
332 torben 2 break;
333     case StorageInt:
334 torben 13 contextMenu->setItemChecked(7, true);
335 torben 2 break;
336     }
337    
338    
339 torben 1 contextMenu->exec( event->globalPos() );
340     delete contextMenu;
341     }
342    
343     void QueensMain::duplicateRemoved()
344     {
345     if (m_elapsed.elapsed() > 500)
346     {
347     int uniq = m_solutions->getUniqueRemoved();
348     QString status;
349     status = QString ("Sorting %1 solutions. So far identified %2 duplicates").arg(m_totalcount).arg(uniq);
350     m_status->setText(status);
351     m_elapsed.restart();
352     }
353     }
354    
355     void QueensMain::sortList()
356     {
357     m_sortalgo = SortList;
358     }
359    
360     void QueensMain::sortVector()
361     {
362     m_sortalgo = SortVector;
363     }
364    
365     void QueensMain::sortHash()
366     {
367     m_sortalgo = SortHash;
368     }
369    
370     void QueensMain::sortMNVector()
371     {
372     m_sortalgo = SortMNVector;
373     }
374    
375 torben 13 void QueensMain::sortMinimalVector()
376     {
377     m_sortalgo = SortMinimalVector;
378     }
379    
380    
381 torben 2 void QueensMain::storageInt()
382     {
383     m_storage = StorageInt;
384     }
385    
386     void QueensMain::storageMatrix()
387     {
388     m_storage = StorageMatrix;
389     }

Properties

Name Value
svn:eol-style native
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20