Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
SortingNetwork.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 deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// 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 all
16// 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 FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24// SOFTWARE.
25
26#include "SortingNetwork.h"
27
30
32 if(m_nValue){ //safety
33 for(UINT i=0; i<m_nDepth; i++)
34 delete [] m_nValue[i];
35 delete [] m_nValue;
36 } //if
37
38 if(m_bUsed){ //safety
39 for(UINT i=0; i<m_nDepth; i++)
40 delete [] m_bUsed[i];
41 delete [] m_bUsed;
42 } //if
43
44 delete m_pGrayCode;
45} //destructor
46
50
51void CSortingNetwork::initValues(const UINT firstlayer, const UINT lastlayer){
52 for(UINT i=firstlayer; i<=lastlayer; i++) //for each layer in range
53 for(UINT j=0; j<m_nInputs; j++) //for each channel
54 m_nValue[i][j] = 0; //set the value on this channel at that layer to zero
55} //initValues
56
59
61 const bool bFirstNormalForm = FirstNormalForm();
62
63 for(UINT j=0; j<m_nInputs; j++) //for each channel in first layer
64 m_bUsed[0][j] = bFirstNormalForm; //set the value on this channel at that layer to b
65
66 for(UINT i=1; i<m_nDepth; i++) //for each layer after the first
67 for(UINT j=0; j<m_nInputs; j++) //for each channel
68 m_bUsed[i][j] = false; //set the value on this channel at that layer to b
69 } //initUsage
70
74
76 delete m_pGrayCode;
78
79 m_pGrayCode->Initialize(m_nInputs); //initialize the Gray code to all zeros.
80 initValues(0, m_nDepth - 1); //initialize the network values to all zeros.
81 initUsage(); //mark all comparators unused
82} //initSortingTest
83
89
90UINT CSortingNetwork::flipinput(UINT j, const UINT firstlayer, const UINT lastlayer){
91 for(UINT i=firstlayer; i<=lastlayer; i++){ //for each layer in range
92 m_nValue[i][j] ^= 1; //flip the value on channel j at that level
93
94 UINT k = m_nMatch[i][j]; //find the channel to which it is joined via a comparator, if any
95
96 if(0 <= k && k < m_nInputs)
97 if((m_nValue[i][k] && j>k) || !(m_nValue[i][k] || j>k)){
98 m_bUsed[i][j] = m_bUsed[i][k] = true; //mark both channels used
99 j = k;
100 } //if
101 } //for
102
103 return j;
104} //flipinput
105
109
110bool CSortingNetwork::stillsorts(const int delta){
111 return flipinput(delta - 1, 0, m_nDepth - 1) ==
113} //stillsorts
114
118
120 UINT i=0; //index of bit to flip
121 m_bSorts = true; //assume it sorts until we find otherwise
122 initSortingTest(); //intialize input and values in comparator network to zero
123
124 while(m_bSorts && i<=m_nInputs){ //bail if it doesn't sort, or we've tried all binary inputs
125 i = m_pGrayCode->Next(); //next bit to flip in Gray code order
126 m_bSorts = m_bSorts && (i>m_nInputs || stillsorts(i)); //check whether it still sorts when this bit is flipped
127 } //while
128
129 return m_bSorts;
130} //sorts
131
135
136const UINT CSortingNetwork::GetUnused() const{
137 UINT count = 0;
138
139 if(m_bUsed){
140 for(UINT i=0; i<m_nDepth; i++) //for each level
141 for(UINT j=0; j<m_nInputs; j++) //for each channel
142 if(m_nMatch[i][j] > j && !m_bUsed[i][j])
143 count++;
144 } //if
145
146 return count;
147} //GetUnused
148
151
153 m_nValue = new UINT*[m_nDepth];
154
155 for(UINT i=0; i<m_nDepth; i++){
156 m_nValue[i] = new UINT[m_nInputs];
157
158 for(UINT j=0; j<m_nInputs; j++)
159 m_nValue[i][j] = 0;
160 } //for
161} //CreateValueArray
162
165
167 m_bUsed = new bool*[m_nDepth];
168
169 for(UINT i=0; i<m_nDepth; i++){
170 m_bUsed[i] = new bool[m_nInputs];
171
172 for(UINT j=0; j<m_nInputs; j++)
173 m_bUsed[i][j] = true;
174 } //for
175} //CreateUsageArray
176
182
183bool CSortingNetwork::Read(LPWSTR lpwstr){
184 const bool ok = CComparatorNetwork::Read(lpwstr); //read from file
185
186 if(ok){
187 CreateValueArray(); //create and initialize value array to all zeros
188 CreateUsageArray(); //create usage array
189 } //if
190
191 return ok;
192} //Read
Interface for the sorting network CSortingNetwork.
Binary reflected Gray code generator.
virtual UINT Next()
Get next code word.
virtual void Initialize(const UINT)
Get first code word.
UINT * m_nGrayCodeWord
Current code word.
UINT m_nZeros
Number of zeros in the code word.
UINT ** m_nMatch
Matchings at each level.
virtual bool Read(LPWSTR)
Read from file.
const bool FirstNormalForm() const
Test for first normal form.
bool m_bSorts
True if it sorts, false if it doesn't or unknown.
UINT m_nInputs
Number of inputs.
bool ** m_bUsed
Whether comparators are used when sorting.
void initUsage()
Initialize usage array.
UINT flipinput(UINT j, const UINT firstlayer, const UINT lastlayer)
Recompute network values when a bit is changed.
const UINT GetUnused() const
Get number of unused comparators.
bool stillsorts(const int delta)
Does it still sort when a bit is changed?
UINT ** m_nValue
Values at each level when sorting.
bool Read(LPWSTR)
Read from file.
void initSortingTest()
Initialize the sorting test.
void initValues(const UINT firstlayer, const UINT lastlayer)
Initialize the network values to the all zero input.
bool sorts()
Does it sort?
void CreateUsageArray()
Make usage array.
void CreateValueArray()
Make value array.
~CSortingNetwork()
Destructor.
CBinaryGrayCode * m_pGrayCode
Gray code generator.
Ternary reflected Gray code generator.