Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
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

Public Member Functions

 ~CTernaryGrayCode ()
 Destructor. More...
 
void Initialize (const UINT)
 Get first code word. More...
 
UINT Next ()
 Get next code word. More...
 
- Public Member Functions inherited from CBinaryGrayCode
 ~CBinaryGrayCode ()
 Destructor. More...
 
virtual void Initialize (const UINT)
 Get first code word. More...
 
virtual UINT Next ()
 Get next code word. More...
 

Protected Attributes

int * m_nDirection = nullptr
 Direction of ternary change. More...
 

Additional Inherited Members

- Public Attributes inherited from CBinaryGrayCode
UINT m_nZeros = 0
 Number of zeros in the code word. More...
 
UINT m_nSize = 0
 Size of the code word. More...
 
UINT * m_nGrayCodeWord = nullptr
 Current code word. More...
 
UINT * m_nGrayCodeStack = nullptr
 Stack to remove recursion. 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:

‍I. Parberry, "A computer assisted optimal depth lower bound for nine-Input sorting networks", Proceedings of Supercomputing '89, pp. 152-161, Reno, Nevada, 1989.

I. Parberry, A computer assisted optimal depth lower bound for nine-Input sorting networks". Mathematical Systems Theory, Vol. 24, pp. 101-116, 1991.

Definition at line 68 of file TernaryGrayCode.h.

Constructor & Destructor Documentation

◆ ~CTernaryGrayCode()

CTernaryGrayCode::~CTernaryGrayCode ( )

Definition at line 6 of file TernaryGrayCode.cpp.

Member Function Documentation

◆ Initialize()

void CTernaryGrayCode::Initialize ( const UINT  n)
virtual

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

Parameters
nNumber of bits in the Gray code word.

Reimplemented from CBinaryGrayCode.

Definition at line 14 of file TernaryGrayCode.cpp.

◆ Next()

UINT 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 the bit that has changed, in the range 1..INPUTS. Out of range means we're finished.

Reimplemented from CBinaryGrayCode.

Definition at line 29 of file TernaryGrayCode.cpp.

Member Data Documentation

◆ m_nDirection

int* CTernaryGrayCode::m_nDirection = nullptr
protected

Definition at line 70 of file TernaryGrayCode.h.