1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Dataflow region definition in a jeff program.
use crate::capnp::jeff_capnp;
use crate::reader::value::{ValueTable, WireValue};
use crate::Direction;
use super::metadata::sealed::HasMetadataSealed;
use super::op::Operation;
use super::string_table::StringTable;
use super::value::ValueId;
use super::ReadError;
/// Dataflow region defined in a jeff module.
#[derive(Clone, Copy, Debug)]
pub struct Region<'a> {
/// Internal capnproto region definition.
region: jeff_capnp::region::Reader<'a>,
/// Module-level register of reused strings.
strings: StringTable<'a>,
/// Function-level register of typed hyperedges.
values: ValueTable<'a>,
}
impl<'a> Region<'a> {
/// Create a new dataflow region reader from a capnp reader.
pub(crate) fn read_capnp(
region: jeff_capnp::region::Reader<'a>,
strings: StringTable<'a>,
values: ValueTable<'a>,
) -> Self {
Self {
region,
strings,
values,
}
}
/// Returns an iterator over the sources or target values of this region.
///
/// # Errors
///
/// - [`ReadError::ValueOutOfBounds`] if an encoded value references an invalid index in the value table.
pub fn boundary(
&self,
direction: Direction,
) -> impl Iterator<Item = Result<WireValue<'a>, ReadError>> {
let value_table = self.values;
let values = match direction {
Direction::Incoming => self.region.get_sources(),
Direction::Outgoing => self.region.get_targets(),
}
.expect("Boundary should be present");
values.iter().map(move |idx| value_table.get(idx))
}
/// Return an iterator over the source values of this region.
///
/// # Errors
///
/// - [`ReadError::ValueOutOfBounds`] if an encoded value references an invalid index in the value table.
pub fn sources(&self) -> impl Iterator<Item = Result<WireValue<'a>, ReadError>> {
self.boundary(Direction::Incoming)
}
/// Return an iterator over the target values of this region.
///
/// # Errors
///
/// - [`ReadError::ValueOutOfBounds`] if an encoded value references an invalid index in the value table.
pub fn targets(&self) -> impl Iterator<Item = Result<WireValue<'a>, ReadError>> {
self.boundary(Direction::Outgoing)
}
/// Returns the number of sources or target values in this region.
pub fn boundary_count(&self, direction: Direction) -> usize {
match direction {
Direction::Incoming => self.region.get_sources(),
Direction::Outgoing => self.region.get_targets(),
}
.expect("Boundary should be present")
.len() as usize
}
/// Returns the number of source values in this region.
pub fn source_count(&self) -> usize {
self.boundary_count(Direction::Incoming)
}
/// Returns the number of target values in this region.
pub fn target_count(&self) -> usize {
self.boundary_count(Direction::Outgoing)
}
/// Returns the boundary value at the given index, or `None` if the index is
/// out of bounds.
///
/// # Errors
///
/// - [`ReadError::ValueOutOfBounds`] if the encoded value references an invalid index in the value table.
pub fn boundary_value(
&self,
direction: Direction,
idx: usize,
) -> Option<Result<WireValue<'a>, ReadError>> {
let values = match direction {
Direction::Incoming => self.region.get_sources(),
Direction::Outgoing => self.region.get_targets(),
}
.expect("Boundary should be present");
if idx >= values.len() as usize {
return None;
}
Some(self.values.get(values.get(idx as ValueId)))
}
/// Returns the source value at the given index, or `None` if the index is
/// out of bounds.
///
/// # Errors
///
/// - [`ReadError::ValueOutOfBounds`] if the encoded value references an invalid index in the value table.
pub fn source(&self, idx: usize) -> Option<Result<WireValue<'a>, ReadError>> {
self.boundary_value(Direction::Incoming, idx)
}
/// Returns the target value at the given index, or `None` if the index is
/// out of bounds.
///
/// # Errors
///
/// - [`ReadError::ValueOutOfBounds`] if the encoded value references an invalid index in the value table.
pub fn target(&self, idx: usize) -> Option<Result<WireValue<'a>, ReadError>> {
self.boundary_value(Direction::Outgoing, idx)
}
/// Returns an iterator over the operations in this region.
pub fn operations(&self) -> impl Iterator<Item = Operation<'a>> {
let strings_table = self.strings;
let value_table = self.values;
self.region
.get_operations()
.expect("Ops should be present")
.iter()
.map(move |op| Operation::read_capnp(op, strings_table, value_table))
}
/// Returns the number of operations in this region.
pub fn operation_count(&self) -> usize {
self.region
.get_operations()
.expect("Ops should be present")
.len() as usize
}
/// Returns the `n`-th operation in this region.
///
/// # Panics
///
/// Panics if `n` is equal or greater than [`Region::operation_count`].
pub fn operation(&self, n: usize) -> Operation<'a> {
Operation::read_capnp(
self.region
.get_operations()
.expect("Ops should be present")
.get(n as u32),
self.strings,
self.values,
)
}
}
impl<'a> HasMetadataSealed for Region<'a> {
fn strings(&self) -> StringTable<'a> {
self.strings
}
fn metadata_reader(&self) -> capnp::struct_list::Reader<'a, jeff_capnp::meta::Owned> {
self.region
.get_metadata()
.expect("Metadata should be present")
}
}