Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
Public Member Functions | Public Attributes | List of all members
CBinaryGrayCode Class Reference

Binary reflected Gray code generator. More...

#include <BinaryGrayCode.h>

Inheritance diagram for CBinaryGrayCode:
CTernaryGrayCode

Public Member Functions

 ~CBinaryGrayCode ()
 Destructor. More...
 
virtual void Initialize (const UINT)
 Get first code word. More...
 
virtual UINT Next ()
 Get next code word. More...
 

Public Attributes

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 binary Gray code generates all strings of a fixed number of bits in such a way that each string differs from the previous one in exactly one bit. For example, the following is a binary reflected Gray code on 4 bits with each bit string followed by the index of the changed bit (from left to right starting at zero).

0000
1000 0
1100 1
0100 0
0110 2
1110 0
1010 1
0010 0
0011 3
1011 0
1111 1
0111 0
0101 2
1101 0
1001 1
0001 0

This class implements a nonrecursive version of the binary reflected Gray code generation algorithm from the following paper:

‍Bitner, Ehrlich, and Reingold, "Efficient generation of the Binary Reflected Gray Code and its applications", Communications of the ACM, Vol. 19, No. 9, pp 517-521, 1976.

Definition at line 66 of file BinaryGrayCode.h.

Constructor & Destructor Documentation

◆ ~CBinaryGrayCode()

CBinaryGrayCode::~CBinaryGrayCode ( )

Delete the memory in the Gray code word and stack.

Definition at line 30 of file BinaryGrayCode.cpp.

Member Function Documentation

◆ Initialize()

void CBinaryGrayCode::Initialize ( const UINT  n)
virtual

Initialize and set the Gray code generator to the first word in Gray code order, which is the all-zero word.

Parameters
nNumber of bits in the Gray code word.

Reimplemented in CTernaryGrayCode.

Definition at line 39 of file BinaryGrayCode.cpp.

◆ Next()

UINT CBinaryGrayCode::Next ( )
virtual

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

Returns
Index of the changed bit in the range 1 through m_nSize. Out of range means we're finished.

Reimplemented in CTernaryGrayCode.

Definition at line 65 of file BinaryGrayCode.cpp.

Member Data Documentation

◆ m_nGrayCodeStack

UINT* CBinaryGrayCode::m_nGrayCodeStack = nullptr

Definition at line 71 of file BinaryGrayCode.h.

◆ m_nGrayCodeWord

UINT* CBinaryGrayCode::m_nGrayCodeWord = nullptr

Definition at line 70 of file BinaryGrayCode.h.

◆ m_nSize

UINT CBinaryGrayCode::m_nSize = 0

Definition at line 69 of file BinaryGrayCode.h.

◆ m_nZeros

UINT CBinaryGrayCode::m_nZeros = 0

Definition at line 68 of file BinaryGrayCode.h.