Sorting Network Search
Backtracking for Small Sorting Networks
Nearsort2.cpp
Go to the documentation of this file.
1
4
5// MIT License
6//
7// Copyright (c) 2023 Ian Parberry
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to
11// deal in the Software without restriction, including without limitation the
12// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13// sell copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25// IN THE SOFTWARE.
26
27#include "Nearsort2.h"
28
32
33CNearsort2::CNearsort2(CMatching& L2Matching, const size_t index):
34 CNearsort(L2Matching, index){
35} //constructor
36
40
42 size_t i = 0;
43 bool bNearSorts = true;
44
45 while(bNearSorts && i<m_nWidth){
46 i = m_pGrayCode->Next();
47 bNearSorts = bNearSorts && (i>=m_nWidth || StillNearsorts2(i));
48 } //while
49
51
52 return bNearSorts;
53} //EvenNearsorts2
54
58
61 InitValues(1, m_nDepth - 4);
62 m_nZeros = m_nWidth; //all zeros
63
64 for(int i=0; i<m_nWidth; i++){
65 for(int j=0; j<m_nWidth; j++)
66 m_bReachableFrom[i][j] = m_bReachableTo[i][j] = m_bReachable[i][j] = false;
67
69 } //for
70
71 if(!EvenNearsorts2())
72 return false;
73
74 if(m_nWidth & 1){ //odd number of inputs, handle the last one independently
76 InitValues(1, m_nDepth - 4);
77 m_nZeros = m_nWidth - 1;
78
79 for(int j=1; j<m_nDepth; j++)
80 m_nValue[j][m_nWidth - 1] = 1;
81
82 if(!EvenNearsorts2())
83 return false;
84 } //if
85
86 return true;
87} //Nearsorts2
88
93
94bool CNearsort2::StillNearsorts2(const size_t delta){
95 size_t k = m_nValue[1][delta]? m_nZeros: m_nZeros - 1; //destination channel
96 const size_t j = FlipInput(delta, 1, m_nDepth - 4);
97
98 if(j == k)return true; //self
99
100 //reachability heuristic: size of "from" <= 7
101
102 if(!m_bReachableFrom[j][k]){
103 if(m_nReachCountFrom[j] >= 7) return false; //not there and no room
105 m_bReachableFrom[j][k] = true;
106 } //if
107
108 //reachability heuristic: size of "to" <= 7
109
110 if(!m_bReachableTo[j][k]){
111 if(m_nReachCountTo[k] >= 7) return false; //not there and no room
112 m_nReachCountTo[k]++;
113 m_bReachableTo[j][k] = true;
114 } //if
115
116 //reachability heuristic: size of "from" union "to" <= 9
117
118 if(!m_bReachable[j][k]){
119 if(m_nReachCount[j] >= 9 || m_nReachCount[k] >= 9) return false; //not there and no room
120 m_nReachCount[j]++; m_nReachCount[k]++;
121 m_bReachable[j][k] = m_bReachable[k][j] = true;
122 } //if
123
124 return true;
125} //StillNearsorts2
126
132
134 if(Nearsorts2()){
136 bool unfinished = true;
137
138 while(unfinished){
140 unfinished = m_cMatching[m_nDepth - 3].Next();
141 if(unfinished)
143 } //while
144 } //if
145} //Process
146
148
150 m_nToS = (int)m_nDepth - 4;
151} //SetToS
virtual size_t Next()
Get next code word.
virtual void Initialize()
Get first code word.
Perfect matching.
Definition: Matching.h:39
bool Next()
Advance to next matching.
Definition: Matching.cpp:62
bool StillNearsorts2(const size_t delta)
Does it still nearsort with this input change?
Definition: Nearsort2.cpp:94
void SetToS()
Set top of stack.
Definition: Nearsort2.cpp:149
void Process()
Process a candidate comparator network.
Definition: Nearsort2.cpp:133
CNearsort2(CMatching &, const size_t)
Constructor.
Definition: Nearsort2.cpp:33
bool EvenNearsorts2()
Does it nearly sort, even number of inputs?
Definition: Nearsort2.cpp:41
bool Nearsorts2()
Does it nearly sort?
Definition: Nearsort2.cpp:59
Searchable sorting network with nearsort.
Definition: Nearsort.h:38
bool m_bReachableTo[MAXINPUTS][MAXINPUTS]
Reachable to.
Definition: Nearsort.h:43
bool m_bReachable[MAXINPUTS][MAXINPUTS]
Reachable from or to.
Definition: Nearsort.h:46
int m_nReachCountTo[MAXINPUTS]
Count of channels reachable to.
Definition: Nearsort.h:44
int m_nReachCount[MAXINPUTS]
Count of channels reachable from or to.
Definition: Nearsort.h:47
void Process()
Process a candidate comparator network.
Definition: Nearsort.cpp:134
int m_nReachCountFrom[MAXINPUTS]
Count of channels reachable from.
Definition: Nearsort.h:41
bool m_bReachableFrom[MAXINPUTS][MAXINPUTS]
Reachable from.
Definition: Nearsort.h:40
void InitMatchingRepresentations(size_t)
Initialize the two different matching representations.
Definition: Searchable.cpp:120
CMatching m_cMatching[MAXDEPTH]
Matchings that make up comparator network in a form that makes searching faster.
Definition: Searchable.h:43
void SynchMatchingRepresentations(size_t)
Synchronize the two different matching representations.
Definition: Searchable.cpp:102
int m_nToS
Top of stack.
Definition: Searchable.h:46
static size_t m_nWidth
Comparator network width.
Definition: Settings.h:39
static size_t m_nDepth
Comparator network depth.
Definition: Settings.h:40
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.