bitwise swap
This commit is contained in:
parent
2165511624
commit
cd81150e6c
@ -68,7 +68,7 @@ proc `[]=`(s: var ColorStack, i: uint8 | BackwardsIndex, c: Color) =
|
|||||||
|
|
||||||
iterator items(s: ColorStack): Color =
|
iterator items(s: ColorStack): Color =
|
||||||
# s.len is unsigned so it will wrap around if it's 0
|
# s.len is unsigned so it will wrap around if it's 0
|
||||||
# but that's no problem, we can just no-op if len is 0
|
# so we will no-op in that case
|
||||||
if s.len != 0:
|
if s.len != 0:
|
||||||
for i in countdown(s.len - 1, 0'u8):
|
for i in countdown(s.len - 1, 0'u8):
|
||||||
yield cast[Color]((s.data shr (i * 3)) and masks[1])
|
yield cast[Color]((s.data shr (i * 3)) and masks[1])
|
||||||
@ -107,6 +107,26 @@ proc moveSubstackPre(src, dst: var ColorStack, nToMove: uint8) =
|
|||||||
src.len -= nToMove
|
src.len -= nToMove
|
||||||
|
|
||||||
|
|
||||||
|
proc swap(s: var ColorStack, i1, i2: uint8) =
|
||||||
|
# Swap the values at two indices in the stack
|
||||||
|
# further explanation to follow
|
||||||
|
if i1 == i2: return
|
||||||
|
|
||||||
|
let diff = if i1 > i2:
|
||||||
|
(i1 - i2) * 3
|
||||||
|
else:
|
||||||
|
(i2 - i1) * 3
|
||||||
|
|
||||||
|
let mask = (masks[1] shl s.offset(i1)) or (masks[1] shl s.offset(i2))
|
||||||
|
mask.show
|
||||||
|
let masked = s.data and mask
|
||||||
|
masked.show
|
||||||
|
let swapped = ((masked shl diff) and mask) or ((masked shr diff) and mask)
|
||||||
|
swapped.show
|
||||||
|
s.data = (s.data and bitnot(mask)) or swapped
|
||||||
|
s.data.show
|
||||||
|
|
||||||
|
|
||||||
proc `$`(s: ColorStack): string =
|
proc `$`(s: ColorStack): string =
|
||||||
result = "St@["
|
result = "St@["
|
||||||
for c in s:
|
for c in s:
|
||||||
@ -120,8 +140,8 @@ var one: ColorStack
|
|||||||
one.add(cGreen)
|
one.add(cGreen)
|
||||||
one.add(cBlue)
|
one.add(cBlue)
|
||||||
one.add(cYellow)
|
one.add(cYellow)
|
||||||
# one.add(cPurple)
|
one.add(cPurple)
|
||||||
# one.add(cRed)
|
one.add(cRed)
|
||||||
|
|
||||||
var two: ColorStack
|
var two: ColorStack
|
||||||
# two.add(cPurple)
|
# two.add(cPurple)
|
||||||
@ -131,8 +151,10 @@ two.add(cRed)
|
|||||||
echo "one: ", one
|
echo "one: ", one
|
||||||
echo "two: ", two
|
echo "two: ", two
|
||||||
|
|
||||||
one[^2] = cPurple
|
one.swap(0, 4)
|
||||||
echo "one: ", one
|
echo "one: ", one
|
||||||
|
# one[^2] = cPurple
|
||||||
|
# echo "one: ", one
|
||||||
|
|
||||||
# echo "one 0: ", one[0]
|
# echo "one 0: ", one[0]
|
||||||
# echo "one 2: ", one[2]
|
# echo "one 2: ", one[2]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user