Sorting Network Search
Backtracking for Small Sorting Networks
Autocomplete.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 "Autocomplete.h"
27
28#include "Defines.h"
29
33
34CAutocomplete::CAutocomplete(CMatching& L2Matching, const size_t index):
35 C2NF(L2Matching, index){
36} //constructor
37
43
44bool CAutocomplete::StillSorts(const size_t delta){
45 size_t k = m_nValue[1][delta]? m_nZeros: m_nZeros - 1; //destination channel
46 size_t j = FlipInput(delta, 1, m_nDepth - 2);
47
48 //Build last layer, if necessary. Changed channel is currently j.
49
50 if(j == k)
51 return true; //success
52
53 else{
54 size_t& cj = m_nComparator[m_nDepth - 1][j]; //one end of comparator
55 size_t& ck = m_nComparator[m_nDepth - 1][k]; //other end of comparator
56
57 if(cj == k && ck == j)
58 return true; //comparator already exists
59
60 else if(cj == j && ck == k){ //both channels free
61 cj = k;
62 ck = j; //insert comparator
63
64 return true;
65 } //else if
66
67 else return false; //can't put a comparator in, so fail
68 } //else
69} //StillSorts
70
75
77 m_pGrayCode->Initialize(); //initialize the Gray code to all zeros.
78 InitValues(1, m_nDepth - 2); //initialize the network values to all zeros.
79 m_nZeros = m_nWidth; //all zeros
80} //initialize
81
84
86 for(int j=0; j<m_nWidth; j++) //for each channel
87 m_nComparator[m_nDepth - 1][j] = j;
88} //initLastLevel
89
95
97 initLastLevel(); //set last level to be empty, will be constructed on-the-fly
98
99 //first handle the case where n is even, and the case where n is odd
100 //and fails to sort an input that ends with a zero
101
102 Initialize(); //set all channels to zero
103 if(!EvenSorts())return false; //test inputs ending in zero
104
105 //if odd number of inputs, check input that end with a one
106 if(odd(m_nWidth)){
107 Initialize(); //set all channels to zero
108
109 for(int j=0; j<m_nDepth; j++) //set all values on last channel to one
110 m_nValue[j][m_nWidth - 1] = 1;
111
112 m_nZeros = m_nWidth - 1; //correct the count of zeros
113
114 if(!EvenSorts())return false; //test inputs ending with one
115 } //if
116
117 return true; //Oh, we made it this far? Then I must be a sorting network. Hurray!
118} //sorts
119
121
123 m_nToS = (int)m_nDepth - 2;
124} //SetToS
Interface for the fast searchable sorting network CAutocomplete.
Useful definitions.
#define odd(n)
Oddness test.
Definition: Defines.h:32
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
Second normal form searchable sorting network.
Definition: 2NF.h:38
bool StillSorts(const size_t)
Does it still sort when a bit is changed?
void Initialize()
Initialize the sorting test.
void SetToS()
Set top of stack.
bool Sorts()
Does it sort?
void initLastLevel()
Initialize the last level of the comparator network.
CAutocomplete(CMatching &, const size_t)
Constructor.
virtual void Initialize()
Get first code word.
size_t m_nComparator[MAXDEPTH][MAXINPUTS]
Comparator array.
Perfect matching.
Definition: Matching.h:39
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.