Sorting Network Search
Backtracking for Small Sorting Networks
SortingNetwork.cpp
Go to the documentation of this file.
1
3
4// MIT License
5//
6// Copyright (c) 2023 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#include "SortingNetwork.h"
27
29
32} //constructor
33
35
37 delete m_pGrayCode;
38} //destructor
39
43
44void CSortingNetwork::InitValues(const size_t first, const size_t last){
45 for(size_t i=first; i<=last; i++) //for each level in range
46 for(size_t j=0; j<m_nWidth; j++) //for each channel
47 m_nValue[i][j] = 0; //set the value on this channel at that level to zero
48} //InitValues
49
53
55 m_pGrayCode->Initialize(); //initialize the Gray code to all zeros.
56 InitValues(0, m_nDepth - 1); //initialize the network values to all zeros.
57 m_nZeros = m_nWidth - 1; //all zeros
58} //initialize
59
74
75size_t CSortingNetwork::FlipInput(size_t j, const size_t first, const size_t last){
76 const size_t nBit = m_nValue[first][j] ^ 1;
77 m_nZeros += nBit? -1: 1; //if nBit has flipped to 1, one less zero, else one more
78
79 for(size_t i=first; i<=last; i++){ //for each layer in range
80 m_nValue[i][j] = nBit; //flip the value on channel j at that level
81
82 const size_t k = m_nComparator[i][j]; //channel joined via a comparator
83
84 if(xor(m_nValue[i][k], j > k))
85 j = k;
86 } //for
87
88 return j;
89} //FlipInput
90
95
96bool CSortingNetwork::StillSorts(const size_t delta){
97 const size_t nTarget = m_nValue[0][delta]? m_nZeros: m_nZeros - 1;
98 return FlipInput(delta, 0, m_nDepth - 1) == nTarget;
99} //StillSorts
100
103
105 size_t i = 0; //index of bit to flip
106 bool bSorts = true; //assume it sorts until we find otherwise
107 Initialize(); //intialize input and values in comparator network to zero
108
109 while(bSorts && i < m_nWidth){ //bail if it doesn't sort, or we've tried all binary inputs
110 i = m_pGrayCode->Next(); //next bit to flip in Gray code order
111 bSorts = bSorts && (i >= m_nWidth || StillSorts(i)); //check whether it still sorts when this bit is flipped
112 } //while
113
114 return bSorts;
115} //Sorts
#define xor(i, j)
Exclusive-or.
Definition: Defines.h:33
Interface for the sorting network CSortingNetwork.
Binary reflected Gray code generator.
virtual size_t Next()
Get next code word.
virtual void Initialize()
Get first code word.
Comparator network.
size_t m_nComparator[MAXDEPTH][MAXINPUTS]
Comparator array.
static size_t m_nWidth
Comparator network width.
Definition: Settings.h:39
static size_t m_nDepth
Comparator network depth.
Definition: Settings.h:40
virtual void Initialize()
Initialize the sorting test.
size_t FlipInput(size_t, const size_t, const size_t)
Recompute network values when a bit is changed.
virtual bool Sorts()
Does it sort?
virtual bool StillSorts(const size_t)
Does it still sort when a bit is changed?
size_t m_nZeros
Number of zeros in the input.
CSortingNetwork()
Constructor.
void InitValues(const size_t, const size_t)
Initialize the network values to the all zero input.
size_t m_nValue[MAXDEPTH][MAXINPUTS]
Values at each level when sorting.
~CSortingNetwork()
Destructor.
CBinaryGrayCode * m_pGrayCode
Gray code generator.