Sorting Network Search
Backtracking for Small Sorting Networks
Searchable.cpp
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 "Searchable.h"
27
29
32 for(size_t i = oddfloor(m_nWidth); i>1; i-=2)
33 m_nNumMatchings *= i;
34} //constructor
35
41
43 std::string filename =
44 "w" + std::to_string(m_nWidth) +
45 "d" + std::to_string(m_nDepth) +
46 "n" + std::to_string(m_nCount) + ".txt";
47
48 CComparatorNetwork::Save(filename); //save to file with that name
49} //Save
50
52
54 m_nToS = (int)m_nDepth - 1;
55} //SetToS
56
59
61 if(Sorts()){ //if it sorts
62 Save(); //save it
63 m_nCount++; //add 1 to the total
64 } //if
65} //Process
66
69
71 bool unfinished = true; //assume we're not finished
72
73 while(unfinished){ //until we're finished
74 Process(); //process the current comparator network, that is, see if it sorts
75 unfinished = NextComparatorNetwork(); //get the next comparator network, we're finished if this function says so
76 } //while
77} //Search
78
81
83 m_nCount = 0; //we've found none so far
84 FirstComparatorNetwork(1); //assuming first normal form here
85 Search(); //perform the search
86} //Backtrack
87
90
92 m_nTop = (int)toplevel; //save value of toplevel for later use
93
94 for(size_t i=toplevel; i<m_nDepth; i++) //for each level in range
95 InitMatchingRepresentations(i); //initialize both matching representations
96} //FirstComparatorNetwork
97
101
103 for(size_t j=0; j<m_nWidth; j+=2){ //for each pair of channels
104 size_t x = m_cMatching[level][j]; //channel at left end of comparator
105 size_t y = m_cMatching[level][j + 1]; //channel at the other end
106
107 if(y == m_nWidth) //if the rightmost channel is the last one in a comparator network with an odd number of inputs
108 m_nComparator[level][x] = x; //it's empty
109
110 else{ //make the testable representation
111 m_nComparator[level][x] = y; //x goes to y
112 m_nComparator[level][y] = x; //y goes to x
113 } //else
114 } //for
115} //SynchMatchingRepresentations
116
119
121 m_cMatching[level].Initialize(); //initialize the generatable form
122 m_nStack[level] = 0; //and its stack
123
124 for(size_t j=0; j<m_nWidth; j++) //initialize the testable form
125 m_nComparator[level][j] = j^1;
126
127 if(odd(m_nWidth)) //one extra one if n is odd
128 m_nComparator[level][m_nWidth - 1] = m_nWidth - 1;
129} //InitMatchingRepresentations
130
134
136 SetToS(); //set top of stack
137
138 m_nStack[m_nToS]++;
139
140 if(m_cMatching[m_nToS].Next())
142
143 while((m_nToS >= m_nTop) && (m_nStack[m_nToS] == m_nNumMatchings)){
145
146 if(--m_nToS >= m_nTop){
147 m_nStack[m_nToS]++;
148
151 } //if
152 } //while
153
154 return m_nToS >= m_nTop; //there are no more if we blow the top of the stack
155} //NextComparatorNetwork
156
159
160const size_t CSearchable::GetCount() const{
161 return m_nCount;
162} //GetCount
#define oddfloor(n)
If even, round down to make odd.
Definition: Defines.h:35
#define odd(n)
Oddness test.
Definition: Defines.h:32
Interface for the searchable sorting network CSearchable.
Sorting network in first normal form.
Definition: 1NF.h:62
bool Sorts()
Does it sort all inputs?
Definition: 1NF.cpp:91
size_t m_nComparator[MAXDEPTH][MAXINPUTS]
Comparator array.
void Save(const std::string &)
Save to file.
void Initialize()
Initialize.
Definition: Matching.cpp:50
size_t m_nTop
Topmost level.
Definition: Searchable.h:49
size_t m_nCount
Number of comparator networks found that sort.
Definition: Searchable.h:41
size_t m_nNumMatchings
Number of matchings of this size.
Definition: Searchable.h:48
virtual void Backtrack()
Backtracking search.
Definition: Searchable.cpp:82
void InitMatchingRepresentations(size_t)
Initialize the two different matching representations.
Definition: Searchable.cpp:120
CSearchable()
Constructor.
Definition: Searchable.cpp:30
virtual void Save()
Save comparator network.
Definition: Searchable.cpp:42
virtual void SetToS()
Set top of stack.
Definition: Searchable.cpp:53
int m_nStack[MAXDEPTH]
Stack to remove recursion from search.
Definition: Searchable.h:45
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
bool NextComparatorNetwork()
Change to next comparator network.
Definition: Searchable.cpp:135
void SynchMatchingRepresentations(size_t)
Synchronize the two different matching representations.
Definition: Searchable.cpp:102
int m_nToS
Top of stack.
Definition: Searchable.h:46
void FirstComparatorNetwork(size_t)
Set to first comparator network.
Definition: Searchable.cpp:91
void Search()
Do the actual search.
Definition: Searchable.cpp:70
const size_t GetCount() const
Get count.
Definition: Searchable.cpp:160
static size_t m_nWidth
Comparator network width.
Definition: Settings.h:39
static size_t m_nDepth
Comparator network depth.
Definition: Settings.h:40