Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
OddEven.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 "OddEven.h"
27#include "Helpers.h"
28
31
33 m_nLog2n(log2n)
34{
35 if(log2n < 1)return; //safety
36
37 m_nInputs = 1 << log2n;
38 m_nDepth = log2n*(log2n + 1)/2;
39 m_nSize = m_nInputs*log2n*(log2n - 1)/4 + m_nInputs - 1;
40
45} //constructor
46
53
55 UINT nCurLevel = 0; //current level
56
57 for(UINT i=1; i<=m_nLog2n; i++){
58 const UINT p = 1<<(i - 1);
59
60 for(UINT j=p; j>0; j/=2){
61 for(UINT k=j%p; k<m_nInputs-j; k+=2*j){
62 const UINT nUpperMin = min(j + k, m_nInputs - j);
63
64 for(UINT nMin=k; nMin<nUpperMin; nMin++){ //channel for min
65 const UINT nMax = nMin + j; //channel for max
66
67 if((nMin >> i) == (nMax >> i))
68 InsertComparator(nCurLevel, nMin, nMax);
69 } //for
70 } //for
71
72 ++nCurLevel; //next level
73 } //for
74 } //for
75} //CreateComparators
76
80
81const std::wstring COddEvenSort::GetName() const{
82 return std::wstring(L"OddEven" + std::to_wstring(m_nInputs));
83} //GetName
Header for useful helper functions.
Interface for Batcher's odd-even sorting network.
void InsertComparator(UINT, UINT, UINT)
Insert comparator.
UINT m_nInputs
Number of inputs.
void CreateMatchArray(UINT, UINT)
Create match array.
UINT m_nLog2n
Log base 2 of the number of inputs.
Definition: OddEven.h:42
void CreateComparators()
Create comparators.
Definition: OddEven.cpp:54
const std::wstring GetName() const
Get name.
Definition: OddEven.cpp:81
COddEvenSort(const UINT)
Constructor.
Definition: OddEven.cpp:32
void CreateUsageArray()
Make usage array.
void CreateValueArray()
Make value array.