forked from ggcrunchy/number_sequences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gray.lua
145 lines (121 loc) · 3.74 KB
/
gray.lua
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
--- Some [Gray code](http://en.wikipedia.org/wiki/Gray_code) utilities.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
-- Standard library imports --
local frexp = math.frexp
-- Modules --
local operators = require("bitwise_ops.operators")
-- Imports --
local band = operators.band
local bor = operators.bor
local bxor = operators.bxor
local rshift = operators.rshift
-- Cached module references --
local _BinaryToGray_
local _GrayToBinary_
-- Exports --
local M = {}
--- Converts an unsigned integer (binary) to a Gray code.
--
-- This is an inverse of @{GrayToBinary}.
-- @uint n Binary value, ≥ 0.
-- @treturn uint Gray code.
function M.BinaryToGray (n)
return bxor(rshift(n, 1), n)
end
-- Helper to iterate Gray codes
local function AuxFirstN (n, prev)
if prev then
local index = _GrayToBinary_(prev) + 1
if index <= n then
return _BinaryToGray_(index), index
end
else
return 0, 0
end
end
--
local function FirstNBody (n, prev, iter)
n = (n or 2^32) - 1
if prev then
n = _GrayToBinary_(prev) + n + 1
end
return iter, n, prev or false
end
--- Iterates over a series of Gray codes.
-- @uint[opt=2^32] n Number of iterations.
-- @uint[opt] prev If present, iteration starts at the next Gray code, i.e. at `Next(prev)`.
-- If absent, it begins at 0.
-- @treturn iterator Supplies the following, in order, at each iteration:
--
-- * Gray code.
-- * Binary value, i.e. `GrayToBinary(gray)`.
function M.FirstN (n, prev)
return FirstNBody(n, prev, AuxFirstN, false)
end
--
local function Change (index, prev)
local gray = _BinaryToGray_(index)
local diff = gray - prev
local added = diff > 0
local _, exp = frexp(added and diff or -diff)
return gray, exp, added
end
--
local function AuxFirstN_Change (n, prev)
local index = _GrayToBinary_(prev) + 1
if index <= n then
return Change(index, prev)
end
end
--- DOCME
function M.FirstN_Change (n, prev)
return FirstNBody(n, prev or 0, AuxFirstN_Change)
end
--- Converts a Gray code to an unsigned inte
--
-- This is an inverse of @{BinaryToGray}.
-- @uint gray Gray code.
-- @treturn uint Binary value.
function M.GrayToBinary (gray)
gray = bxor(gray, rshift(gray, 16))
gray = bxor(gray, rshift(gray, 8))
gray = bxor(gray, rshift(gray, 4))
gray = bxor(gray, rshift(gray, 2))
return bxor(gray, rshift(gray, 1))
end
--- Successor.
-- @uint gray Gray code.
-- @treturn uint Next Gray code, i.e. `BinaryToGray(GrayToBinary(gray) + 1)`.
function M.Next (gray)
return _BinaryToGray_(_GrayToBinary_(gray) + 1)
end
--- DOCME
function M.Next_Change (gray)
return Change(_GrayToBinary_(gray) + 1)
end
-- Cache module members.
_BinaryToGray_ = M.BinaryToGray
_GrayToBinary_ = M.GrayToBinary
-- Export the module.
return M