| 1 |
|
| # Undo and redo. |
| 2 |
|
| |
| 3 |
|
| MODULE = State::Store PACKAGE = State::Store |
| 4 |
|
| |
| 5 |
|
| # undo / redo, with an optional number of steps: how many were taken. |
| 6 |
|
| void |
| 7 |
|
| undo(self, ...) |
| 8 |
|
| SV *self |
| 9 |
|
| ALIAS: |
| 10 |
|
| redo = 1 |
| 11 |
|
| PREINIT: |
| 12 |
|
| store *s; |
| 13 |
|
| size_t n; |
| 14 |
|
| const char *what; |
| 15 |
|
| SV *hold; |
| 16 |
|
| CODE: |
| 17 |
62033 |
▶ | what = ix ? "State::Store::redo" : "State::Store::undo"; |
Branch: 1 branch
| Branch | True | False | Total |
| what = ix ? "State::Store::redo" : "State::Store::undo"; |
30724 |
31309 |
62033 |
|
| 18 |
62033 |
| s = store_handle(aTHX_ self, what); |
| 19 |
62033 |
▶ | if (items > 2) croak("%s: too many arguments", what); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) croak("%s: too many arguments", what); |
1 |
62032 |
62033 |
|
| 20 |
62032 |
▶ | n = store_steps(aTHX_ items > 1 ? ST(1) : NULL, what); |
Branch: 1 branch
| Branch | True | False | Total |
| n = store_steps(aTHX_ items > 1 ? ST(1) : NULL, what); |
50765 |
11267 |
62032 |
|
| 21 |
62026 |
| hold = sv_2mortal(newSVsv(self)); |
| 22 |
62026 |
| n = store_move(aTHX_ s, !ix, n, what); |
| 23 |
62022 |
| store_deliver(aTHX_ s, hold); |
| 24 |
62022 |
| ST(0) = sv_2mortal(newSVuv((UV)n)); |
| 25 |
62022 |
| XSRETURN(1); |
| 26 |
|
| |
| 27 |
|
| void |
| 28 |
|
| can_undo(self) |
| 29 |
|
| SV *self |
| 30 |
|
| ALIAS: |
| 31 |
|
| can_redo = 1 |
| 32 |
|
| PREINIT: |
| 33 |
|
| store *s; |
| 34 |
|
| CODE: |
| 35 |
6 |
▶ | s = store_handle(aTHX_ self, ix ? "State::Store::can_redo" : "State::Store::can_undo"); |
Branch: 1 branch
| Branch | True | False | Total |
| s = store_handle(aTHX_ self, ix ? "State::Store::can_redo" : "State::Store::can_undo"); |
3 |
3 |
6 |
|
| 36 |
6 |
▶ | ST(0) = boolSV((ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s)) > 0); |
Branch: 2 branches
| Branch | True | False | Total |
| ST(0) = boolSV((ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s)) > 0); |
3 |
3 |
6 |
| ST(0) = boolSV((ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s)) > 0); |
2 |
4 |
6 |
|
| 37 |
6 |
| XSRETURN(1); |
| 38 |
|
| |
| 39 |
|
| void |
| 40 |
|
| undo_count(self) |
| 41 |
|
| SV *self |
| 42 |
|
| ALIAS: |
| 43 |
|
| redo_count = 1 |
| 44 |
|
| PREINIT: |
| 45 |
|
| store *s; |
| 46 |
|
| CODE: |
| 47 |
1087 |
▶ | s = store_handle(aTHX_ self, ix ? "State::Store::redo_count" : "State::Store::undo_count"); |
Branch: 1 branch
| Branch | True | False | Total |
| s = store_handle(aTHX_ self, ix ? "State::Store::redo_count" : "State::Store::undo_count"); |
524 |
563 |
1087 |
|
| 48 |
1087 |
▶ | ST(0) = sv_2mortal(newSVuv((UV)(ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s)))); |
Branch: 1 branch
| Branch | True | False | Total |
| ST(0) = sv_2mortal(newSVuv((UV)(ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s)))); |
524 |
563 |
1087 |
|
| 49 |
1087 |
| XSRETURN(1); |
| 50 |
|
| |
| 51 |
|
| # past / future: the values undo, or redo, would reach, in the order they |
| 52 |
|
| # would reach them - the past oldest first, the future the next one first. |
| 53 |
|
| # The stored scalars themselves, as current hands out. How many, in scalar |
| 54 |
|
| # context. |
| 55 |
|
| void |
| 56 |
|
| past(self) |
| 57 |
|
| SV *self |
| 58 |
|
| ALIAS: |
| 59 |
|
| future = 1 |
| 60 |
|
| PREINIT: |
| 61 |
|
| store *s; |
| 62 |
|
| size_t n, i; |
| 63 |
|
| PPCODE: |
| 64 |
44253 |
▶ | s = store_handle(aTHX_ self, ix ? "State::Store::future" : "State::Store::past"); |
Branch: 1 branch
| Branch | True | False | Total |
| s = store_handle(aTHX_ self, ix ? "State::Store::future" : "State::Store::past"); |
22119 |
22134 |
44253 |
|
| 65 |
44251 |
▶ | n = ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s); |
Branch: 1 branch
| Branch | True | False | Total |
| n = ix ? (STORE_HAMT->vec_count)(aTHX_ s->future) : store_past_len(aTHX_ s); |
22118 |
22133 |
44251 |
|
| 66 |
44251 |
▶ | if (GIMME_V != G_LIST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V != G_LIST) { |
8 |
44243 |
44251 |
|
| 67 |
8 |
▶ | XPUSHs(sv_2mortal(newSVuv((UV)n))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVuv((UV)n))); |
0 |
8 |
8 |
|
| 68 |
8 |
| XSRETURN(1); |
| 69 |
|
| } |
| 70 |
44243 |
▶ | EXTEND(SP, (SSize_t)n); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, (SSize_t)n); |
44243 |
0 |
44243 |
| EXTEND(SP, (SSize_t)n); |
0 |
44243 |
44243 |
|
| 71 |
370801 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
326558 |
44243 |
370801 |
|
| 72 |
326558 |
▶ | SV *v = ix ? store_future_at(aTHX_ s, i) : store_past_at(aTHX_ s, i); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *v = ix ? store_future_at(aTHX_ s, i) : store_past_at(aTHX_ s, i); |
21124 |
305434 |
326558 |
|
| 73 |
326558 |
| PUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(v))); |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
|
| void |
| 77 |
|
| clear_history(self) |
| 78 |
|
| SV *self |
| 79 |
|
| PREINIT: |
| 80 |
|
| store *s; |
| 81 |
|
| CODE: |
| 82 |
2730 |
| s = store_handle(aTHX_ self, "State::Store::clear_history"); |
| 83 |
2730 |
| sv_2mortal(newSVsv(self)); |
| 84 |
2730 |
| store_clear_history(aTHX_ s, "State::Store::clear_history"); |
| 85 |
2728 |
| ST(0) = self; |
| 86 |
2728 |
| XSRETURN(1); |
| 87 |
|
| |
| 88 |
|
| # 0 when both stacks are well formed vectors, or the number of the first |
| 89 |
|
| # check that failed: 1 for the past, 2 for the future. For the tests. |
| 90 |
|
| IV |
| 91 |
|
| _check(self) |
| 92 |
|
| SV *self |
| 93 |
|
| PREINIT: |
| 94 |
|
| store *s; |
| 95 |
|
| CODE: |
| 96 |
711 |
| s = store_handle(aTHX_ self, "State::Store::_check"); |
| 97 |
1422 |
▶ | RETVAL = (STORE_HAMT->vec_check)(aTHX_ s->past) ? 1 |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (STORE_HAMT->vec_check)(aTHX_ s->past) ? 1 |
706 |
5 |
711 |
|
| 98 |
711 |
▶ | : (STORE_HAMT->vec_check)(aTHX_ s->future) ? 2 : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| : (STORE_HAMT->vec_check)(aTHX_ s->future) ? 2 : 0; |
711 |
0 |
711 |
| : (STORE_HAMT->vec_check)(aTHX_ s->future) ? 2 : 0; |
0 |
711 |
711 |
|
| 99 |
|
| OUTPUT: |
| 100 |
|
| RETVAL |
| 101 |
|
| |
| 102 |
|
| # Both stacks as lists, past then future, for a test to compare with a model: |
| 103 |
|
| # the past as undo sees it, without steps beyond the limit not yet cut. |
| 104 |
|
| void |
| 105 |
|
| _stacks(self) |
| 106 |
|
| SV *self |
| 107 |
|
| PREINIT: |
| 108 |
|
| store *s; |
| 109 |
|
| AV *past, *future; |
| 110 |
|
| PPCODE: |
| 111 |
259 |
| s = store_handle(aTHX_ self, "State::Store::_stacks"); |
| 112 |
259 |
| past = (STORE_HAMT->vec_to_av)(aTHX_ s->past); |
| 113 |
259 |
| future = (STORE_HAMT->vec_to_av)(aTHX_ s->future); |
| 114 |
259 |
▶ | if (!past || !future) croak("State::Store::_stacks: out of memory"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!past || !future) croak("State::Store::_stacks: out of memory"); |
259 |
0 |
259 |
| if (!past || !future) croak("State::Store::_stacks: out of memory"); |
0 |
259 |
259 |
|
| 115 |
883 |
▶ | while ((size_t)(av_len(past) + 1) > store_past_len(aTHX_ s)) SvREFCNT_dec(av_shift(past)); |
Branch: 1 branch
| Branch | True | False | Total |
| while ((size_t)(av_len(past) + 1) > store_past_len(aTHX_ s)) SvREFCNT_dec(av_shift(past)); |
624 |
259 |
883 |
|
| 116 |
259 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
259 |
259 |
|
| 117 |
259 |
| PUSHs(sv_2mortal(newRV_noinc((SV *)past))); |
| 118 |
259 |
| PUSHs(sv_2mortal(newRV_noinc((SV *)future))); |
| 119 |
|
| |
| 120 |
|
| # history_limit: the limit, or undef for none. With an argument, sets it, |
| 121 |
|
| # cuts the past to it at once, and returns the store. |
| 122 |
|
| void |
| 123 |
|
| history_limit(self, ...) |
| 124 |
|
| SV *self |
| 125 |
|
| PREINIT: |
| 126 |
|
| store *s; |
| 127 |
|
| size_t limit; |
| 128 |
|
| int limited; |
| 129 |
|
| CODE: |
| 130 |
1925 |
| s = store_handle(aTHX_ self, "State::Store::history_limit"); |
| 131 |
1925 |
▶ | if (items > 2) croak("State::Store::history_limit: too many arguments"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) croak("State::Store::history_limit: too many arguments"); |
1 |
1924 |
1925 |
|
| 132 |
1924 |
▶ | if (items == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 1) { |
1908 |
16 |
1924 |
|
| 133 |
1908 |
▶ | ST(0) = s->limited ? sv_2mortal(newSVuv((UV)s->limit)) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| ST(0) = s->limited ? sv_2mortal(newSVuv((UV)s->limit)) : &PL_sv_undef; |
213 |
1695 |
1908 |
|
| 134 |
1908 |
| XSRETURN(1); |
| 135 |
|
| } |
| 136 |
16 |
| store_limit_arg(aTHX_ ST(1), &limit, &limited, "State::Store::history_limit"); |
| 137 |
11 |
| sv_2mortal(newSVsv(self)); |
| 138 |
11 |
| store_set_limit(aTHX_ s, limit, limited, "State::Store::history_limit"); |
| 139 |
9 |
| ST(0) = self; |
| 140 |
9 |
| XSRETURN(1); |