Sorting Network Verify and Draw
Check Whether Comparator Networks Sort and Draw Them
BinaryGrayCode.cpp
Go to the documentation of this file.
1
3
4// MIT License
5//
6// Copyright (c) 2022 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 deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// 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 all
16// 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 FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24// SOFTWARE.
25
26#include "BinaryGrayCode.h"
27
29
31 delete [] m_nGrayCodeWord;
32 delete [] m_nGrayCodeStack;
33} //destructor
34
38
39void CBinaryGrayCode::Initialize(const UINT n){
40 if(m_nSize != n){
41 delete [] m_nGrayCodeWord;
42 delete [] m_nGrayCodeStack;
43
44 const UINT nSize = n + 3U; //we need 3 extra entries
45
46 m_nGrayCodeWord = new UINT[nSize];
47 m_nGrayCodeStack = new UINT[nSize];
48
49 m_nSize = n;
50 } //if
51
52 m_nZeros = n; //all zeros
53
54 for(UINT i=0; i<=n+2; i++){
55 m_nGrayCodeWord[i] = 0; //all zeros
56 m_nGrayCodeStack[i] = i + 1; //recursion stack initial conditions
57 } //for
58} //Initialize
59
64
66 const UINT i = m_nGrayCodeStack[0]; //bit to change
67
68 m_nGrayCodeStack[0] = 1;
69 m_nGrayCodeWord[i] ^= 1; //change bit
71 m_nGrayCodeStack[i] = i + 1;
72 m_nZeros += 1 - 2*m_nGrayCodeWord[i]; //adjust zero count
73
74 return i; //return bit changed
75} //Next
Interface for the binary reflected Gray code generator CBinaryGrayCode.
virtual UINT Next()
Get next code word.
virtual void Initialize(const UINT)
Get first code word.
UINT * m_nGrayCodeWord
Current code word.
UINT * m_nGrayCodeStack
Stack to remove recursion.
UINT m_nZeros
Number of zeros in the code word.
UINT m_nSize
Size of the code word.
~CBinaryGrayCode()
Destructor.