Bitcoin ABC
0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
src
script
conditionstack.h
Go to the documentation of this file.
1
// Copyright (c) 2024 The Bitcoin developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_SCRIPT_CONDITIONSTACK_H
6
#define BITCOIN_SCRIPT_CONDITIONSTACK_H
7
8
#include <cassert>
9
#include <cstdint>
10
#include <limits>
11
28
class
ConditionStack
{
29
private
:
31
static
constexpr
uint32_t
NO_FALSE
= std::numeric_limits<uint32_t>::max();
32
34
uint32_t
m_stack_size
= 0;
37
uint32_t
m_first_false_pos
=
NO_FALSE
;
38
39
public
:
40
bool
empty
()
const
{
return
m_stack_size
== 0; }
41
bool
all_true
()
const
{
return
m_first_false_pos
==
NO_FALSE
; }
42
void
push_back
(
bool
f
) {
43
if
(
m_first_false_pos
==
NO_FALSE
&& !
f
) {
44
// The stack consists of all true values, and a false is added.
45
// The first false value will appear at the current size.
46
m_first_false_pos
=
m_stack_size
;
47
}
48
++
m_stack_size
;
49
}
50
void
pop_back
() {
51
assert
(
m_stack_size
> 0);
52
--
m_stack_size
;
53
if
(
m_first_false_pos
==
m_stack_size
) {
54
// When popping off the first false value, everything becomes true.
55
m_first_false_pos
=
NO_FALSE
;
56
}
57
}
58
void
toggle_top
() {
59
assert
(
m_stack_size
> 0);
60
if
(
m_first_false_pos
==
NO_FALSE
) {
61
// The current stack is all true values; the first false will be the
62
// top.
63
m_first_false_pos
=
m_stack_size
- 1;
64
}
else
if
(
m_first_false_pos
==
m_stack_size
- 1) {
65
// The top is the first false value; toggling it will make
66
// everything true.
67
m_first_false_pos
=
NO_FALSE
;
68
}
else
{
69
// There is a false value, but not on top. No action is needed as
70
// toggling anything but the first false value is unobservable.
71
}
72
}
73
};
74
75
#endif
// BITCOIN_SCRIPT_CONDITIONSTACK_H
ConditionStack
A data type to abstract out the condition stack during script execution.
Definition
conditionstack.h:28
ConditionStack::push_back
void push_back(bool f)
Definition
conditionstack.h:42
ConditionStack::empty
bool empty() const
Definition
conditionstack.h:40
ConditionStack::m_first_false_pos
uint32_t m_first_false_pos
The position of the first false value on the implied stack, or NO_FALSE if all true.
Definition
conditionstack.h:37
ConditionStack::m_stack_size
uint32_t m_stack_size
The size of the implied stack.
Definition
conditionstack.h:34
ConditionStack::NO_FALSE
static constexpr uint32_t NO_FALSE
A constant for m_first_false_pos to indicate there are no falses.
Definition
conditionstack.h:31
ConditionStack::toggle_top
void toggle_top()
Definition
conditionstack.h:58
ConditionStack::pop_back
void pop_back()
Definition
conditionstack.h:50
ConditionStack::all_true
bool all_true() const
Definition
conditionstack.h:41
GetRand
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition
random.h:85
assert
assert(!tx.IsCoinBase())
Generated on Thu Dec 26 2024 02:38:04 for Bitcoin ABC by
1.9.8