Sorting Network Search
Backtracking for Small Sorting Networks
1NF.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 "Defines.h"
27#include "TernaryGrayCode.h"
28
29#include "1NF.h"
30
34
36 delete m_pGrayCode;
38
39 //first layer is the identity matching
40
41 const size_t n = evenfloor(m_nWidth);
42
43 for(size_t i=0; i<n; i++)
44 m_nComparator[0][i] = i ^ 1;
45} //constructor
46
50
52 m_pGrayCode->Initialize(); //initialize the Gray code to all zeros.
53 InitValues(1, m_nDepth - 1); //initialize the network values to all zeros.
54 m_nZeros = m_nWidth; //all zeros
55} //initialize
56
60
61bool C1NF::StillSorts(const size_t delta){
62 const size_t nTarget = m_nValue[1][delta]? m_nZeros: m_nZeros - 1;
63 return FlipInput(delta, 1, m_nDepth - 1) == nTarget;
64} //StillSorts
65
72
74 size_t i = 0; //index of bit to flip
75 bool bSorts = true; //assume it sorts until we find otherwise
76
77 while(bSorts && i < m_nWidth){ //bail if it doesn't sort, or we've tried all binary inputs
78 i = m_pGrayCode->Next(); //next bit to flip in Gray code order
79 bSorts = bSorts && (i >= m_nWidth || StillSorts(i)); //check whether it still sorts when this bit is flipped
80 } //while
81
82 return bSorts;
83} //EvenSorts
84
90
92 //first handle the case where n is even, and the case where n is odd
93 //and fails to sort an input that ends with a zero
94 Initialize(); //set all channels to zero
95 if(!EvenSorts())return false; //test inputs ending in zero
96
97 //if odd number of inputs, check input that end with a one
98 if(odd(m_nWidth)){
99 Initialize(); //set all channels to zero
100
101 for(int j=0; j<m_nDepth; j++) //set all values on last channel to one
102 m_nValue[j][m_nWidth - 1] = 1;
103
104 m_nZeros = m_nWidth - 1; //correct the count of zeros
105
106 if(!EvenSorts())return false; //test inputs ending with one
107 } //if
108
109 return true; //Oh, we made it this far? Then I must be a sorting network. Hurray!
110} //sorts
Interface for the first normal form sorting network C1NF.
Useful definitions.
#define evenfloor(n)
If odd, round down to make even.
Definition: Defines.h:36
#define odd(n)
Oddness test.
Definition: Defines.h:32
Interface for the ternary reflected Gray code generator CTernaryGrayCode.
void Initialize()
Initialize the sorting test.
Definition: 1NF.cpp:51
bool Sorts()
Does it sort all inputs?
Definition: 1NF.cpp:91
bool EvenSorts()
Does it sort if there are an odd number of inputs and we fix the value on the last channel?
Definition: 1NF.cpp:73
C1NF()
Constructor.
Definition: 1NF.cpp:35
bool StillSorts(const size_t)
Does it still sort when a bit is changed?
Definition: 1NF.cpp:61
virtual size_t Next()
Get next code word.
virtual void Initialize()
Get first code word.
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
Sorting network.
size_t FlipInput(size_t, const size_t, const size_t)
Recompute network values when a bit is changed.
size_t m_nZeros
Number of zeros in the input.
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.
CBinaryGrayCode * m_pGrayCode
Gray code generator.
Ternary reflected Gray code generator.