-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrayCodeGenerator.h
More file actions
89 lines (75 loc) · 2.69 KB
/
Copy pathGrayCodeGenerator.h
File metadata and controls
89 lines (75 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
/*
Copyright © 2024 Claus Vind-Andreasen
This program is free software; you can redistribute it and /or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 - 1307 USA
This General Public License does not permit incorporating your program into proprietary programs.If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library.
If this is what you want to do, use the GNU Library General Public License instead of this License.
*/
#ifdef OS_WINDOWS // windows
#define WIN
typedef unsigned __int64 u64;
typedef __int64 s64;
#else //linux
#define NOTWIN
#include <stdlib.h>
#include <cstdint>
typedef uint64_t u64;
typedef int64_t s64;
#endif
class GrayCodeGeneratorClass
{
public:
GrayCodeGeneratorClass(): mask(0), state(0) {};
~GrayCodeGeneratorClass() {};
inline void Init(unsigned int _width, u64 FirstValue = 0) {
if (0 < _width && _width < 65) {
switch (_width)
{
case 64: mask = 0xFFFFFFFFFFFFFFFF;
break;
default: mask = ((u64)1) << _width;
mask--;
break;
}
state = (FirstValue & mask);
/*
'state' is now initialized to
Firstvalue ^ [Firstvalue/2] ^ [Firstvalue/4] ^ [Firstvalue/8] ^.......
an infinite sum, where the terms eventurally become 0, so we can stop there
(note: [X] is the integer part of X )
The output : state ^ (state >>1) then becomes
Firstvalue ^ [Firstvalue/2] ^ [Firstvalue/4] ^ [Firstvalue/8] ^.......
^ [Firstvalue/2] ^ [Firstvalue/4] ^ [Firstvalue/8] ^.......
where all terms except the first cancel..
*/
u64 temp = (FirstValue & mask) >> 1;
while (temp) {
state ^= temp;
temp = temp >> 1;
}
}
else {
mask = 0;
state = 0;
}
};
/*
the order 'state' is incremented or decremented below is a choice.
Not sure there is a 'right' way....
*/
inline u64 Next() {
u64 res = state ^ (state >> 1) ;
state = (state + 1) & mask;
return mask & res;
};
inline u64 Previous() {
if (state) state = (state - 1) & mask;
else state = mask;
return mask & (state ^ (state >> 1));
};
private:
u64 state;
u64 mask;
};