Skip to content

Commit 464f2d8

Browse files
authored
perf(shuffle): better bitwise xor for random integer (#44)
feat(policy): add `reverse_order` policy
1 parent 2df5906 commit 464f2d8

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ It use offline github actions to weekly collect and update the most popular Vim/
3232
3333
It install color plugins via git submodules instead of copy-paste source code, so you get continuously updates from original authors instead of me, e.g. it only transport and manage, not produce.
3434

35-
It allow you play them with multiple playback settings:
35+
It allow you play them with multiple playback settings (policies):
3636

3737
- Suffle playback.
3838
- Play in order.
39-
- Play in reverse order (todo).
39+
- Play in reverse order.
4040
- Single cycle (todo).
4141
- Specific color name (todo).
4242

@@ -102,7 +102,7 @@ require('pckr').add({
102102

103103
```lua
104104
require('colorbox').setup({
105-
--- @type "shuffle"|"inorder"|"single"
105+
--- @type "shuffle"|"in_order"|"reverse_order"|"single"
106106
policy = "shuffle",
107107

108108
--- @type "startup"|"interval"|"filetype"

lua/colorbox.lua

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local json = require("colorbox.json")
66
--- @alias colorbox.Options table<any, any>
77
--- @type colorbox.Options
88
local Defaults = {
9-
--- @type "shuffle"|"inorder"|"single"
9+
--- @type "shuffle"|"in_order"|"reverse_order"|"single"
1010
policy = "shuffle",
1111

1212
--- @type "startup"|"interval"|"filetype"
@@ -160,7 +160,7 @@ local function _policy_shuffle()
160160
end
161161
end
162162

163-
local function _policy_inorder()
163+
local function _policy_in_order()
164164
if #FilteredColorNamesList > 0 then
165165
local previous_track = _load_previous_track() --[[@as PreviousTrack]]
166166
local i = previous_track == nil and 0
@@ -174,14 +174,31 @@ local function _policy_inorder()
174174
end
175175
end
176176

177+
local function _policy_reverse_order()
178+
if #FilteredColorNamesList > 0 then
179+
local previous_track = _load_previous_track() --[[@as PreviousTrack]]
180+
local i = previous_track == nil and 0
181+
or (
182+
previous_track.color_number - 1 < 0
183+
and (#FilteredColorNamesList - 1)
184+
or previous_track.color_number - 1
185+
)
186+
local color = FilteredColorNamesList[i + 1]
187+
vim.cmd(string.format([[color %s]], color))
188+
_save_previous_track(color, i)
189+
end
190+
end
191+
177192
local function _policy()
178193
if Configs.background == "dark" or Configs.background == "light" then
179194
vim.cmd(string.format([[set background=%s]], Configs.background))
180195
end
181196
if Configs.policy == "shuffle" then
182197
_policy_shuffle()
183-
elseif Configs.policy == "inorder" then
184-
_policy_inorder()
198+
elseif Configs.policy == "in_order" then
199+
_policy_in_order()
200+
elseif Configs.policy == "reverse_order" then
201+
_policy_reverse_order()
185202
end
186203
end
187204

lua/colorbox/utils.lua

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
-- Returns the XOR of two binary numbers
2+
--- @param a integer
3+
--- @param b integer
4+
--- @return integer
5+
local function bitwise_xor(a, b)
6+
local r = 0
7+
local f = math.floor
8+
for i = 0, 31 do
9+
local x = a / 2 + b / 2
10+
if x ~= f(x) then
11+
r = r + 2 ^ i
12+
end
13+
a = f(a / 2)
14+
b = f(b / 2)
15+
end
16+
return r
17+
end
18+
119
--- @param l any[]
220
--- @param f fun(v:any):number
321
--- @return number
@@ -25,9 +43,9 @@ local function randint(maximal)
2543
local s2, s3 = vim.loop.gettimeofday()
2644
local s4 = math.random()
2745
local r = math_mod(s1, maximal)
28-
r = math_mod(r * 3 + s2, maximal)
29-
r = math_mod(r * 7 + s3, maximal)
30-
r = math_mod(r * 11 + s4, maximal)
46+
r = math_mod(bitwise_xor(r, s2 or 3), maximal)
47+
r = math_mod(bitwise_xor(r, s3 or 7), maximal)
48+
r = math_mod(bitwise_xor(r, s4 or 11), maximal)
3149
return r
3250
end
3351

0 commit comments

Comments
 (0)