Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
Bitonic.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 "Bitonic.h"
27
32
34 if(log2n < 1)return; //safety
35
36 m_nInputs = 1 << log2n;
37 m_nDepth = log2n*(log2n + 1)/2;
39
40 std::vector<CComparator>* vecLevel = new std::vector<CComparator>[m_nDepth];
41
42 CreateComparators(vecLevel); //create with min-max and max-min comparators
43 MakeAllMinMax(vecLevel); //twist out any max-min comparators
44 CreateMatchArray(vecLevel); //convert to matching array
45
46 delete [] vecLevel;
47
50} //constructor
51
59
60void CBitonicSort::CreateComparators(std::vector<CComparator>* vecLevel){
61 UINT nCurLevel = 0; //current level
62
63 for(UINT i=2; i<=m_nInputs; i*=2)
64 for(UINT j=i/2; j>0; j/=2){
65 for(UINT nMin=0; nMin<m_nInputs; nMin++){ //channel for min
66 const UINT nMax = nMin ^ j; //channel for max
67
68 if(nMax > nMin)
69 vecLevel[nCurLevel].push_back(
70 (nMin & i)? CComparator(nMin, nMax): CComparator(nMax, nMin));
71 } //for
72
73 ++nCurLevel; //next level
74 } //for
75} //CreateComparators
76
81
82void CBitonicSort::MakeAllMinMax(std::vector<CComparator>* vecLevel){
83 for(UINT i=0; i<m_nDepth; i++) //for each level
84 for(auto& p: vecLevel[i]) //for each comparator at that level
85 if(p.m_nMax < p.m_nMin){
86 std::swap(p.m_nMin, p.m_nMax);
87 Twist(vecLevel, i + 1, p.m_nMin, p.m_nMax);
88 } //if
89} //MakeAllMinMax
90
99
100void CBitonicSort::Twist(std::vector<CComparator>* vecLevel,
101 UINT nLevel, UINT nMin, UINT nMax)
102{
103 for(UINT i=nLevel; i<m_nDepth; i++) //for each level
104 for(auto& p: vecLevel[i]){ //for each comparator at that level
105 //replace p.m_nMin if necessary
106 if(p.m_nMin == nMin)p.m_nMin = nMax; else
107 if(p.m_nMin == nMax)p.m_nMin = nMin;
108
109 //replace p.m_nMax if necessary
110 if(p.m_nMax == nMin)p.m_nMax = nMax; else
111 if(p.m_nMax == nMax)p.m_nMax = nMin;
112 } //for
113} //Twist
114
119
120void CBitonicSort::CreateMatchArray(std::vector<CComparator>* vecLevel){
122
123 for(UINT i=0; i<m_nDepth; i++) //for each level
124 for(auto& p: vecLevel[i]) //for each comparator at that level
125 InsertComparator(i, p.m_nMin, p.m_nMax); //insert into array
126} //CreateMatchArray
127
131
132const std::wstring CBitonicSort::GetName() const{
133 return std::wstring(L"Bitonic" + std::to_wstring(m_nInputs));
134} //GetName
Interface for Batcher's bitonic sorting network.
void Twist(std::vector< CComparator > *, UINT, UINT, UINT)
Twist channels.
Definition: Bitonic.cpp:100
void MakeAllMinMax(std::vector< CComparator > *)
Make all comparators min-max.
Definition: Bitonic.cpp:82
CBitonicSort(const UINT)
Constructor.
Definition: Bitonic.cpp:33
void CreateMatchArray(std::vector< CComparator > *)
Create match array.
Definition: Bitonic.cpp:120
void CreateComparators(std::vector< CComparator > *)
Create comparators.
Definition: Bitonic.cpp:60
const std::wstring GetName() const
Get name.
Definition: Bitonic.cpp:132
A min-max or max-min comparator used in bitonic sort.
Definition: Bitonic.h:39
void InsertComparator(UINT, UINT, UINT)
Insert comparator.
UINT m_nInputs
Number of inputs.
void CreateMatchArray(UINT, UINT)
Create match array.
void CreateUsageArray()
Make usage array.
void CreateValueArray()
Make value array.