Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
Pairwise.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 "Pairwise.h"
27
32
34 if(log2n < 1)return; //safety
35
36 m_nInputs = 1 << log2n;
37 m_nDepth = log2n*(log2n + 1)/2;
38 m_nSize = m_nInputs*log2n*(log2n - 1)/4 + m_nInputs - 1;
39
44} //constructor
45
51
53 UINT nCurLevel = 0; //current level
54
55 for(UINT i=1; i<m_nInputs; i<<=1){ //each iteration constructs a level
56 for(UINT j=0; j<i; j++) //min-most channel used on this level
57 for(UINT nMin=j; nMin<m_nInputs; nMin+=i<<1) //min channel
58 InsertComparator(nCurLevel, nMin, nMin + i);
59
60 ++nCurLevel; //next level
61 } //for
62
63 UINT k = 1; //smallest value of j in second for-loop below
64
65 for(UINT i=m_nInputs>>2; i>0; i>>=1){
66 for(UINT j=k; j>0; j>>=1){ //each iteration constructs a level
67 const UINT nDelta = i*j; //distance between min and max channels
68 UINT nMax = i + nDelta; //max channel
69 UINT nCount = 0; //count mod i of how many times we have incremented nMax
70
71 while(nMax < m_nInputs){ //each iteration inserts a group of comparators
72 InsertComparator(nCurLevel, nMax - nDelta, nMax);
73 ++nMax; ++nCount; //next comparator
74
75 if(nCount >= i){ //skip over channels for next group of comparators
76 nCount = 0;
77 nMax += i;
78 } //if
79 } //while
80
81 ++nCurLevel; //next level
82 } //for
83
84 k = 2*k + 1;
85 } //for
86} //CreateComparators
87
91
92const std::wstring CPairwiseSort::GetName() const{
93 return std::wstring(L"Pairwise" + std::to_wstring(m_nInputs));
94} //GetName
Interface for the pairwise sorting network.
void InsertComparator(UINT, UINT, UINT)
Insert comparator.
UINT m_nInputs
Number of inputs.
void CreateMatchArray(UINT, UINT)
Create match array.
void CreateComparators()
Create comparators.
Definition: Pairwise.cpp:52
CPairwiseSort(const UINT)
Constructor.
Definition: Pairwise.cpp:33
const std::wstring GetName() const
Get name.
Definition: Pairwise.cpp:92
void CreateUsageArray()
Make usage array.
void CreateValueArray()
Make value array.