Sorting Network Search
Backtracking for Small Sorting Networks
Nearsort.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 "Nearsort.h"
28
32
33CNearsort::CNearsort(CMatching& L2Matching, const size_t index):
34 CAutocomplete(L2Matching, index){
35} //constructor
36
39
41 bool bNearSorts = true;
42 size_t i = m_pGrayCode->Next();
43
44 do{
45 bNearSorts = StillNearsorts(i);
46 i = m_pGrayCode->Next();
47 }while(bNearSorts && i < m_nWidth);
48
50
51 return bNearSorts;
52} //EvenNearsorts
53
57
60 InitValues(1, m_nDepth - 3);
61 m_nZeros = m_nWidth; //all zeros
62
63 for(int i=0; i<m_nWidth; i++){
64 for(int j=0; j<m_nWidth; j++)
65 m_bReachableFrom[i][j] = m_bReachableTo[i][j] = m_bReachable[i][j] = false;
66
68 } //for
69
70 if(!EvenNearsorts())
71 return false;
72
73 //if odd number of inputs, handle the last one
74
75 if(odd(m_nWidth)){
77 InitValues(1, m_nDepth - 3);
78 m_nZeros = m_nWidth - 1; //all zeros
79
80 for(int j=1; j<m_nDepth; j++)
81 m_nValue[j][m_nWidth - 1] = 1;
82
83 if(!EvenNearsorts())
84 return false;
85 } //if
86
87 return true;
88} //Nearsorts
89
94
95bool CNearsort::StillNearsorts(const size_t delta){
96 size_t k = m_nValue[1][delta]? m_nZeros: m_nZeros - 1; //destination channel
97 size_t j = FlipInput(delta, 1, m_nDepth - 3); //source channel into level d-2
98
99 if(j == k)return true; //self
100
101 //reachability heuristic: size of "from" <= 3
102
103 if(!m_bReachableFrom[j][k]){
104 if(m_nReachCountFrom[j] >= 3)return false; //not there and no room
106 m_bReachableFrom[j][k] = true;
107 } //if
108
109 //reachability heuristic: size of "to" <= 3
110
111 if(!m_bReachableTo[j][k]){
112 if(m_nReachCountTo[k] >= 3)return false; //not there and no room
113 m_nReachCountTo[k]++;
114 m_bReachableTo[j][k] = true;
115 } //if
116
117 //reachability heuristic: size of "from" union "to" <= 5
118
119 if(!m_bReachable[j][k]){
120 if(m_nReachCount[j] >= 5 || m_nReachCount[k] >= 5) return false; //not there and no room
121 m_nReachCount[j]++; m_nReachCount[k]++;
122 m_bReachable[j][k] = m_bReachable[k][j] = true;
123 } //if
124
125 return true;
126} //stillnearsorts
127
133
135 if(Nearsorts()){
137 bool unfinished = true;
138
139 while(unfinished){
141 unfinished = m_cMatching[m_nDepth - 2].Next();
142
143 if(unfinished)SynchMatchingRepresentations(m_nDepth - 2);
144 } //while
145 } //if
146} //Process
147
149
151 m_nToS = (int)m_nDepth - 3;
152} //SetToS
#define odd(n)
Oddness test.
Definition: Defines.h:32
Interface for the searchable sorting network with the nearsort heuristic CNearsort.
Searchable second normal form sorting network with autocomplete.
Definition: Autocomplete.h:41
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 Nearsorts()
Does it nearly sort?
Definition: Nearsort.cpp:58
bool m_bReachableTo[MAXINPUTS][MAXINPUTS]
Reachable to.
Definition: Nearsort.h:43
void SetToS()
Set top of stack.
Definition: Nearsort.cpp:150
bool m_bReachable[MAXINPUTS][MAXINPUTS]
Reachable from or to.
Definition: Nearsort.h:46
bool EvenNearsorts()
Does it nearly sort, even number of inputs?
Definition: Nearsort.cpp:40
CNearsort(CMatching &, const size_t)
Constructor.
Definition: Nearsort.cpp:33
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
bool StillNearsorts(const size_t)
Does it still nearsort with this input change?
Definition: Nearsort.cpp:95
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
virtual void Process()
Process a candidate comparator network.
Definition: Searchable.cpp:60
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.