Skip to content

Commit

Permalink
FEAT: new ATz action returning the seriest at 0-based position (index)
Browse files Browse the repository at this point in the history
CHANGE: `at` action on image with pair position was 0-based, now is 1-based

related to: Oldes/Rebol-issues#613
  • Loading branch information
Oldes committed Oct 6, 2021
1 parent 101284b commit fb0daad
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/boot/actions.reb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ at: action [
index [number! logic! pair!]
]

atz: action [
{Returns the series at the specified 0-based index.}
series [series! gob! port!]
index [number! logic! pair!]
]

index?: action [
{Returns the current position (index) of the series.}
series [series! gob! port! none!]
Expand Down
14 changes: 3 additions & 11 deletions src/core/f-series.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,21 @@

case A_SKIP:
case A_AT:
case A_ATZ:
len = Get_Num_Arg(arg);
{
REBI64 i = (REBI64)index + (REBI64)len;
if (action == A_SKIP) {
if (IS_LOGIC(arg)) i--;
} else { // A_AT
} else if (action == A_AT) {
if (len > 0) i--;
}
if (i > (REBI64)tail) i = (REBI64)tail;
else if (i < 0) i = 0;
VAL_INDEX(value) = (REBCNT)i;
}
break;
/*
case A_ATZ:
len = Get_Num_Arg(arg);
{
REBI64 idx = Add_Max(0, index, len, MAX_I32);
if (idx < 0) idx = 0;
VAL_INDEX(value) = (REBCNT)idx;
}
break;
*/

case A_INDEXQ:
SET_INTEGER(DS_RETURN, ((REBI64)index) + 1);
return R_RET;
Expand Down
1 change: 1 addition & 0 deletions src/core/t-gob.c
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ const REBCNT Gob_Flag_Words[] = {
case A_AT:
index--;
case A_SKIP:
case A_ATZ:
index += VAL_INT32(arg);
goto set_index;

Expand Down
13 changes: 5 additions & 8 deletions src/core/t-image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1070,21 +1070,18 @@ INLINE REBCNT ARGB_To_BGR(REBCNT i)
Pick_Path(value, arg, D_ARG(3));
return R_ARG3;

case A_SKIP:
case A_AT:
// This logic is somewhat complicated by the fact that INTEGER args use
// base-1 indexing, but PAIR args use base-0.
case A_AT: // base-1
case A_ATZ: // base-0
case A_SKIP: // base-0
if (IS_PAIR(arg)) {
if (action == A_AT) action = A_SKIP;
diff = (VAL_PAIR_Y_INT(arg) * VAL_IMAGE_WIDE(value) + VAL_PAIR_X_INT(arg)) +
((action == A_SKIP) ? 0 : 1);
diff = ((VAL_PAIR_Y_INT(arg) - ((action == A_AT) ? 1 : 0)) * VAL_IMAGE_WIDE(value) + VAL_PAIR_X_INT(arg));
} else
diff = Get_Num_Arg(arg);

index += diff;
if (action == A_SKIP) {
if (IS_LOGIC(arg)) index--;
} else {
} else if (action == A_AT) {
if (diff > 0) index--; // For at, pick, poke.
}

Expand Down
2 changes: 2 additions & 0 deletions src/tests/units/gob-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ Rebol [
--assert 1 = index? back g
--assert 2 = index? next g
--assert 3 = index? tail g
--assert 2 = index? at g 2
--test-- "indexz? gob!"
--assert 0 = indexz? g
--assert 0 = indexz? back g
--assert 1 = indexz? next g
--assert 2 = indexz? tail g
--assert 1 = indexz? atz g 1
===end-group===

===start-group=== "gob issues"
Expand Down
55 changes: 44 additions & 11 deletions src/tests/units/image-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,55 @@ Rebol [

===end-group===

===start-group=== "INDEX? / INDEXZ?"
===start-group=== "INDEX? / INDEXZ? / AT / ATZ"
img: make image! 2x2
--test-- "index? image!"
--assert 1 = index? img
--assert 2 = index? next img
--assert 5 = index? tail img
--assert 1 = index? img
--assert 2 = index? next img
--assert 5 = index? tail img
--assert 4 = index? skip tail img -1
--test-- "index? at image!"
--assert 1 = index? at img -1
--assert 1 = index? at img 0
--assert 1 = index? at img 1
--assert 2 = index? at img 2
--assert 5 = index? at img 6
--assert 1 = index? skip at img 2 -1
--test-- "index?/xy image!"
--assert 1x1 = index?/xy img
--assert 2x1 = index?/xy next img
--assert 1x3 = index?/xy tail img
--assert 2x2 = index?/xy skip tail img -1
--test-- "index?/xy at image!"
--assert 1x1 = index?/xy at img 1x1
--assert 1x2 = index?/xy at img 1x2
--assert 2x2 = index?/xy at img 2x2
--assert 1x3 = index?/xy at img 20x2
--assert 1x2 = index?/xy skip at img 2x2 -1x0
--test-- "indexz? image!"
--assert 0 = indexz? img
--assert 1 = indexz? next img
--assert 4 = indexz? tail img
--assert 0 = indexz? img
--assert 1 = indexz? next img
--assert 4 = indexz? tail img
--assert 3 = indexz? skip tail img -1
--test-- "indexz? atz image!"
--assert 0 = indexz? atz img -1
--assert 0 = indexz? atz img 0
--assert 2 = indexz? atz img 2
--assert 4 = indexz? atz img 6
--assert 3 = indexz? skip atz img 6 -1
--assert 1 = indexz? skip atz img 2 -1
--test-- "indexz?/xy image!"
--assert 0x0 = indexz?/xy img
--assert 1x0 = indexz?/xy next img
--assert 0x2 = indexz?/xy tail img
--assert 1x1 = indexz?/xy skip tail img -1x0
--test-- "indexz?/xy atz image!"
--assert 0x0 = indexz?/xy atz img 0x0
--assert 0x1 = indexz?/xy atz img 0x1
--assert 1x1 = indexz?/xy atz img 1x1
--assert 0x2 = indexz?/xy atz img 2x2
--assert 0x2 = indexz?/xy atz img 20x2
--assert 0x1 = indexz?/xy skip atz img 1x1 -1x0
===end-group===

===start-group=== "FOREACH"
Expand Down Expand Up @@ -250,26 +283,26 @@ Rebol [
000000000000FFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFF}
change at img 1x1 make image! [2x2 220.22.22]
change at img 2x2 make image! [2x2 220.22.22]
--assert img/rgb = #{
000000000000FFFFFFFFFFFF
000000DC1616DC1616FFFFFF
FFFFFFDC1616DC1616FFFFFF
FFFFFFFFFFFFFFFFFFFFFFFF}
change at img 2x2 make image! [3x3 33.33.33]
change at img 3x3 make image! [3x3 33.33.33]
--assert img/rgb = #{
000000000000FFFFFFFFFFFF
000000DC1616DC1616FFFFFF
FFFFFFDC1616212121212121
FFFFFFFFFFFF212121212121}
change at img 0x3 make image! [4x4 66.166.66]
change at img 1x4 make image! [4x4 66.166.66]
--assert img/rgb = #{
000000000000FFFFFFFFFFFF
000000DC1616DC1616FFFFFF
FFFFFFDC1616212121212121
42A64242A64242A64242A642}

change at img 3x0 make image! [2x1 #{AAAAAABBBBBB}]
change at img 4x1 make image! [2x1 #{AAAAAABBBBBB}]
--assert img/rgb = #{
000000000000FFFFFFAAAAAA
000000DC1616DC1616FFFFFF
Expand Down
16 changes: 16 additions & 0 deletions src/tests/units/series-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -627,27 +627,43 @@ Rebol [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
0 = indexz? atz s -1
0 = indexz? atz s 0
2 = indexz? atz s 2
3 = indexz? atz s 6
]
--test-- "indexz? on binary"
s: to binary! "abc"
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
0 = indexz? atz s -1
0 = indexz? atz s 0
2 = indexz? atz s 2
3 = indexz? atz s 6
]
--test-- "indexz? on block"
s: [1 2 3]
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
0 = indexz? atz s -1
0 = indexz? atz s 0
2 = indexz? atz s 2
3 = indexz? atz s 6
]
--test-- "indexz? on vector"
s: #[u16! 3]
--assert all [
0 = indexz? s
1 = indexz? next s
3 = indexz? tail s
0 = indexz? atz s -1
0 = indexz? atz s 0
2 = indexz? atz s 2
3 = indexz? atz s 6
]
===end-group===

Expand Down

0 comments on commit fb0daad

Please sign in to comment.