Sorting Network Search
Backtracking for Small Sorting Networks
Public Member Functions | Protected Attributes | List of all members
CTernaryGrayCode Class Reference

Ternary reflected Gray code generator. More...

#include <TernaryGrayCode.h>

Inheritance diagram for CTernaryGrayCode:
CBinaryGrayCode CSettings

Public Member Functions

void Initialize ()
 Get first code word. More...
 
size_t Next ()
 Get next code word. More...
 
- Public Member Functions inherited from CBinaryGrayCode
virtual void Initialize ()
 Get first code word. More...
 
virtual size_t Next ()
 Get next code word. More...
 
void Print ()
 Debug print. More...
 

Protected Attributes

int m_nDirection [MAXINPUTS+3]
 Direction of ternary change. More...
 
- Protected Attributes inherited from CBinaryGrayCode
size_t m_nBit [MAXINPUTS+3] = {0}
 Current code word. More...
 
size_t m_nStack [MAXINPUTS+3] = {0}
 Stack to remove recursion. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from CSettings
static void SetWidth (const size_t)
 Set width. More...
 
static void SetDepth (const size_t)
 Set depth. More...
 
- Static Protected Attributes inherited from CSettings
static size_t m_nWidth = 9
 Comparator network width. More...
 
static size_t m_nDepth = 6
 Comparator network depth. More...
 

Detailed Description

A ternary Gray code generates all strings of \(n\) bits in such a way that it is made up of the following bit pairs: 00 01 11, with an additional single bit if \(n\) is odd, and each string differs from the previous one in exactly one bit. For example, the following is the ternary reflected Gray code on 4 bits, with the ternary version followed by the binary equivalent, then by the index of the changed bit (from left to right starting at zero). The ternary digit 0 corresponds to the binary string 00, the ternary digit 1 corresponds to 10, and the ternary digit 2 corresponds to 11. There is no need for the bit pair 10 since each bit pair is input into a comparator on the first level of a first normal form comparator network.

00 0000
10 0100 1
20 1100 0
21 1101 3
11 0101 0
01 1000 1
02 0011 2
12 1011 1
22 1111 0

This class implements a nonrecursive version of the algorithm from the paper.

Definition at line 63 of file TernaryGrayCode.h.

Member Function Documentation

◆ Initialize()

void CTernaryGrayCode::Initialize ( )
virtual

Reset Gray code generator to the first word in Gray code order, the all-zero word.

Reimplemented from CBinaryGrayCode.

Definition at line 31 of file TernaryGrayCode.cpp.

◆ Next()

size_t CTernaryGrayCode::Next ( )
virtual

Get the next binary word in ternary reflected Gray code order, which will differ from the previous one in exactly one bit.

Returns
Index of changed bit. Out of range means we're finished.

Reimplemented from CBinaryGrayCode.

Definition at line 42 of file TernaryGrayCode.cpp.

Member Data Documentation

◆ m_nDirection

int CTernaryGrayCode::m_nDirection[MAXINPUTS+3]
protected

Definition at line 65 of file TernaryGrayCode.h.