Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
Main.cpp
Go to the documentation of this file.
1
3
4// MIT License
5//
6// Copyright (c) 2022 Ian Parberry
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to
10// deal in the Software without restriction, including without limitation the
11// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12// sell copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25
26//#ifdef _DEBUG
27 //#include <vld.h> //Visual Leak Detector from http://vld.codeplex.com/
28//#endif
29
30#include "Includes.h"
31
32#include "CMain.h"
33
34#include "OddEven.h"
35#include "Bitonic.h"
36#include "Pairwise.h"
37#include "Bubblesort.h"
38
39static CMain* g_pMain = nullptr;
40
41static const int g_nMinW = 320;
42static const int g_nMinH = 160;
43
52
53LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
54 UINT nMenuId = 0; //menu identifier for menu command messages
55 static bool bResizing = false;
56
57 switch(message){
58 case WM_CREATE: //window has been created
59 g_pMain = new CMain(hWnd); //create the main class
60 return 0;
61
62 case WM_DESTROY: //window has been removed from the screen
63 delete g_pMain; //delete the main class
64 PostQuitMessage(0); //ready to shut down
65 return 0;
66
67 case WM_SIZING: //user is resizing the window
68 MinDragRect(hWnd, wParam, (RECT*)lParam, g_nMinW, g_nMinH); //enforce minimum size
69 return 0;
70
71 case WM_PAINT: //window needs to be redrawn
73 return 0;
74
75 case WM_COMMAND: //user has selected a command from the menu
76 nMenuId = LOWORD(wParam); //menu id
77
78 switch(nMenuId){
79 case IDM_FILE_OPEN: //open and read a comparator network file
80 g_pMain->Read();
81 g_pMain->Draw();
82 InvalidateRect(hWnd, nullptr, FALSE);
83 break;
84
85 case IDM_FILE_EXPORT_PNG: //export to PNG file
86 g_pMain->Export(eExport::Png);
87 break;
88
89 case IDM_FILE_EXPORT_TEX: //export to TeX file
90 g_pMain->Export(eExport::TeX);
91 break;
92
93 case IDM_FILE_EXPORT_SVG: //export to SVG file
94 g_pMain->Export(eExport::Svg);
95 break;
96
97 case IDM_FILE_VERIFY: //verify that it sorts
98 if(g_pMain->Verify()){ //redundant comparators trigger redraw
99 g_pMain->Draw();
100 InvalidateRect(hWnd, nullptr, FALSE);
101 } //if
102 break;
103
104 case IDM_FILE_QUIT: //so long, farewell, auf weidersehn, goodbye!
105 SendMessage(hWnd, WM_CLOSE, 0, 0);
106 break;
107
108 //---------------------------------------------------
109
110 case IDM_GENERATE_MINBUBBLE: //generate min-bubblesort sorting network
112 g_pMain->Draw();
113 InvalidateRect(hWnd, nullptr, FALSE);
114 break;
115
116 case IDM_GENERATE_MAXBUBBLE: //generate max-bubblesort sorting network
118 g_pMain->Draw();
119 InvalidateRect(hWnd, nullptr, FALSE);
120 break;
121
122 case IDM_GENERATE_BUBBLE: //generate bubblesort sorting network
124 g_pMain->Draw();
125 InvalidateRect(hWnd, nullptr, FALSE);
126 break;
127
128 case IDM_GENERATE_ODDEVEN: //generate odd-even sorting network
130 g_pMain->Draw();
131 InvalidateRect(hWnd, nullptr, FALSE);
132 break;
133
134 case IDM_GENERATE_BITONIC: //generate bitonic sorting network
136 g_pMain->Draw();
137 InvalidateRect(hWnd, nullptr, FALSE);
138 break;
139
140 case IDM_GENERATE_PAIRWISE: //generate pairwise sorting network
142 g_pMain->Draw();
143 InvalidateRect(hWnd, nullptr, FALSE);
144 break;
145
146 //---------------------------------------------------
147
148 case IDM_VIEW_VERTICAL: //vertical view mode
149 g_pMain->SetDrawStyle(eDrawStyle::Vertical);
150 g_pMain->Draw();
151 InvalidateRect(hWnd, nullptr, FALSE);
152 break;
153
154 case IDM_VIEW_HORIZONTAL: //horizontal view mode
155 g_pMain->SetDrawStyle(eDrawStyle::Horizontal);
156 g_pMain->Draw();
157 InvalidateRect(hWnd, nullptr, FALSE);
158 break;
159
160 //---------------------------------------------------
161
162 case IDM_HELP_HELP: //show help
163 ShellExecute(0, 0,
164 "https://ian-parberry.github.io/sortingnetworkviewer/html",
165 0, 0, SW_SHOW);
166 break;
167
168 case IDM_HELP_ABOUT: //show ABout dialog box.
169 MessageBox(nullptr,
170 "Copyright © Ian Parberry, 2022.\nSource code available under the MIT License from https://github.com/Ian-Parberry/sortingnetworkviewer/.",
171 "About", MB_ICONINFORMATION | MB_OK);
172 break;
173 } //switch
174
175 return 0; //all is good
176
177 default:
178 return DefWindowProc(hWnd, message, wParam, lParam); //not my message
179 } //switch
180} //WndProc
181
190
191int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpStr, int nShow){
192 UNREFERENCED_PARAMETER(hPrev); //nope
193 UNREFERENCED_PARAMETER(lpStr); //nope nope
194
195 InitWindow(hInst, nShow, WndProc); //create and show a window
196
197 MSG msg; //current message
198
199 while(GetMessage(&msg, nullptr, 0, 0)){ //message pump
200 TranslateMessage(&msg);
201 DispatchMessage(&msg);
202 } //while
203
204 return (int)msg.wParam;
205} //wWinMain
Interface for Batcher's bitonic sorting network.
Interface for the bubblesort sorting network.
Interface for the main class CMain.
Useful includes.
static const int g_nMinH
Minimum window height.
Definition: Main.cpp:42
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Window procedure.
Definition: Main.cpp:53
static const int g_nMinW
Minimum window width.
Definition: Main.cpp:41
static CMain * g_pMain
Pointer to the main class.
Definition: Main.cpp:39
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpStr, int nShow)
Winmain.
Definition: Main.cpp:191
Interface for Batcher's odd-even sorting network.
Interface for the pairwise sorting network.
void MinDragRect(HWND hwnd, WPARAM wParam, RECT *pRect, int w, int h)
Enforce minimum drag rectangle.
void InitWindow(HINSTANCE hInst, INT nShow, WNDPROC WndProc)
Initialize window.
#define IDM_GENERATE_MINBUBBLE
Menu id for Generate min-bubblesort.
#define IDM_GENERATE_MAXBUBBLE
Menu id for Generate max-bubblesort.
#define IDM_FILE_EXPORT_PNG
Menu id for Export PNG.
#define IDM_FILE_OPEN
Menu id for Generate.
#define IDM_GENERATE_BUBBLE
Menu id for Generate min-bubblesort.
#define IDM_GENERATE_BITONIC
Menu id for Generate bitonic.
#define IDM_FILE_VERIFY
Menu id for Verify.
#define IDM_FILE_QUIT
Menu id for Quit.
#define IDM_FILE_EXPORT_TEX
Menu id for Export TeX.
#define IDM_HELP_HELP
Menu id for display help.
#define IDM_VIEW_HORIZONTAL
Menu id for horizontal view.
#define IDM_FILE_EXPORT_SVG
Menu id for Export SVG.
#define IDM_GENERATE_PAIRWISE
Menu id for Generate pairwise.
#define IDM_VIEW_VERTICAL
Menu id for vertical view.
#define IDM_GENERATE_ODDEVEN
Menu id for Generate odd-even.
#define IDM_HELP_ABOUT
Menu id for display About info.
Batcher's bitonic sorting network.
Definition: Bitonic.h:62
Bubblesort.
Definition: Bubblesort.h:65
Max-bubblesort.
Definition: Bubblesort.h:52
Min-bubblesort.
Definition: Bubblesort.h:37
The main class.
Definition: CMain.h:38
void SetDrawStyle(const eDrawStyle)
Set drawing style.
Definition: CMain.cpp:339
void Draw()
Draw comparator network to bitmap.
Definition: CMain.cpp:237
void Read()
Read comparator network from file.
Definition: CMain.cpp:169
void GeneratePowerOf2()
Generate sorting network.
Definition: CMain.cpp:215
HRESULT Export(const eExport)
Export image file.
Definition: CMain.cpp:246
void Generate()
Generate sorting network.
Definition: CMain.cpp:191
void OnPaint()
Paint the client area of the window.
Definition: CMain.cpp:74
bool Verify()
Verify that comparator network sorts.
Definition: CMain.cpp:266
Batcher's odd-even sorting network.
Definition: OddEven.h:40
The pairwise sorting network.
Definition: Pairwise.h:39