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

Binary reflected Gray code generator. More...

#include <BinaryGrayCode.h>

Inheritance diagram for CBinaryGrayCode:
CSettings CTernaryGrayCode

Public Member Functions

virtual void Initialize ()
 Get first code word. More...
 
virtual size_t Next ()
 Get next code word. More...
 
void Print ()
 Debug print. More...
 

Protected Attributes

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 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.

Member Function Documentation

◆ Initialize()

void CBinaryGrayCode::Initialize ( )
virtual

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

Reimplemented in CTernaryGrayCode.

Definition at line 33 of file BinaryGrayCode.cpp.

◆ Next()

size_t 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 bit that has changed. Out of range means we're finished.

Reimplemented in CTernaryGrayCode.

Definition at line 45 of file BinaryGrayCode.cpp.

◆ Print()

void CBinaryGrayCode::Print ( )

Print to the console a sequence of Gray code changes, that is, the index of the bit that flips to get the next binary string.

Definition at line 59 of file BinaryGrayCode.cpp.

Member Data Documentation

◆ m_nBit

size_t CBinaryGrayCode::m_nBit[MAXINPUTS+3] = {0}
protected

Definition at line 68 of file BinaryGrayCode.h.

◆ m_nStack

size_t CBinaryGrayCode::m_nStack[MAXINPUTS+3] = {0}
protected

Definition at line 69 of file BinaryGrayCode.h.