Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hugr-core/src/std_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn std_reg() -> ExtensionRegistry {
arithmetic::float_types::EXTENSION.to_owned(),
collections::array::EXTENSION.to_owned(),
collections::list::EXTENSION.to_owned(),
collections::borrow_array::EXTENSION.to_owned(),
collections::static_array::EXTENSION.to_owned(),
collections::value_array::EXTENSION.to_owned(),
logic::EXTENSION.to_owned(),
Expand Down
1 change: 1 addition & 0 deletions hugr-core/src/std_extensions/collections.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! List type and operations.

pub mod array;
pub mod borrow_array;
pub mod list;
pub mod static_array;
pub mod value_array;
2 changes: 1 addition & 1 deletion hugr-core/src/std_extensions/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub trait ArrayOpBuilder: GenericArrayOpBuilder {
self.add_generic_array_unpack::<Array>(elem_ty, size, input)
}
/// Adds an array clone operation to the dataflow graph and return the wires
/// representing the originala and cloned array.
/// representing the original and cloned array.
///
/// # Arguments
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ mod tests {

use crate::extension::prelude::bool_t;
use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::{
extension::prelude::qb_t,
ops::{OpTrait, OpType},
Expand All @@ -203,6 +204,7 @@ mod tests {

#[rstest]
#[case(Array)]
#[case(BorrowArray)]
fn test_clone_def<AK: ArrayKind>(#[case] _kind: AK) {
let op = GenericArrayClone::<AK>::new(bool_t(), 2).unwrap();
let optype: OpType = op.clone().into();
Expand All @@ -217,6 +219,7 @@ mod tests {

#[rstest]
#[case(Array)]
#[case(BorrowArray)]
fn test_clone<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = bool_t();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ mod tests {
use crate::extension::prelude::bool_t;
use crate::ops::{OpTrait, OpType};
use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::std_extensions::collections::value_array::ValueArray;

use super::*;

#[rstest]
#[case(ValueArray, Array)]
#[case(BorrowArray, Array)]
fn test_convert_from_def<AK: ArrayKind, OtherAK: ArrayKind>(
#[case] _kind: AK,
#[case] _other_kind: OtherAK,
Expand All @@ -264,6 +266,7 @@ mod tests {

#[rstest]
#[case(ValueArray, Array)]
#[case(BorrowArray, Array)]
fn test_convert_into_def<AK: ArrayKind, OtherAK: ArrayKind>(
#[case] _kind: AK,
#[case] _other_kind: OtherAK,
Expand All @@ -276,6 +279,7 @@ mod tests {

#[rstest]
#[case(ValueArray, Array)]
#[case(BorrowArray, Array)]
fn test_convert_from<AK: ArrayKind, OtherAK: ArrayKind>(
#[case] _kind: AK,
#[case] _other_kind: OtherAK,
Expand All @@ -296,6 +300,7 @@ mod tests {

#[rstest]
#[case(ValueArray, Array)]
#[case(BorrowArray, Array)]
fn test_convert_into<AK: ArrayKind, OtherAK: ArrayKind>(
#[case] _kind: AK,
#[case] _other_kind: OtherAK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ mod tests {

use crate::extension::prelude::bool_t;
use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::{
extension::prelude::qb_t,
ops::{OpTrait, OpType},
Expand All @@ -187,6 +188,7 @@ mod tests {

#[rstest]
#[case(Array)]
#[case(BorrowArray)]
fn test_discard_def<AK: ArrayKind>(#[case] _kind: AK) {
let op = GenericArrayDiscard::<AK>::new(bool_t(), 2).unwrap();
let optype: OpType = op.clone().into();
Expand All @@ -198,6 +200,7 @@ mod tests {

#[rstest]
#[case(Array)]
#[case(BorrowArray)]
fn test_discard<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = bool_t();
Expand Down
10 changes: 10 additions & 0 deletions hugr-core/src/std_extensions/collections/array/array_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ mod tests {
use crate::extension::prelude::usize_t;
use crate::std_extensions::arithmetic::float_types::float64_type;
use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::std_extensions::collections::value_array::ValueArray;
use crate::{
builder::{DFGBuilder, Dataflow, DataflowHugr, inout_sig},
Expand All @@ -351,6 +352,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_array_ops<AK: ArrayKind>(#[case] _kind: AK) {
for def in GenericArrayOpDef::<AK>::iter() {
let ty = if def == GenericArrayOpDef::get {
Expand All @@ -373,6 +375,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
/// Test building a HUGR involving a new_array operation.
fn test_new_array<AK: ArrayKind>(#[case] _kind: AK) {
let mut b = DFGBuilder::new(inout_sig(vec![qb_t(), qb_t()], AK::ty(2, qb_t()))).unwrap();
Expand All @@ -389,6 +392,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
/// Test building a HUGR involving an unpack operation.
fn test_unpack<AK: ArrayKind>(#[case] _kind: AK) {
let mut b = DFGBuilder::new(inout_sig(AK::ty(2, qb_t()), vec![qb_t(), qb_t()])).unwrap();
Expand All @@ -405,6 +409,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_get<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = bool_t();
Expand All @@ -430,6 +435,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_set<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = bool_t();
Expand All @@ -452,6 +458,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_swap<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = bool_t();
Expand All @@ -473,6 +480,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_pops<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = bool_t();
Expand Down Expand Up @@ -505,6 +513,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_discard_empty<AK: ArrayKind>(#[case] _kind: AK) {
let size = 0;
let element_ty = bool_t();
Expand All @@ -523,6 +532,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
/// Initialize an array operation where the element type is not from the prelude.
fn test_non_prelude_op<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ mod tests {
use rstest::rstest;

use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::std_extensions::collections::value_array::ValueArray;
use crate::{
extension::prelude::qb_t,
Expand All @@ -195,6 +196,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_repeat_def<AK: ArrayKind>(#[case] _kind: AK) {
let op = GenericArrayRepeat::<AK>::new(qb_t(), 2);
let optype: OpType = op.clone().into();
Expand All @@ -205,6 +207,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_repeat<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let element_ty = qb_t();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ mod tests {

use crate::extension::prelude::usize_t;
use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::std_extensions::collections::value_array::ValueArray;
use crate::{
extension::prelude::{bool_t, qb_t},
Expand All @@ -255,6 +256,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_scan_def<AK: ArrayKind>(#[case] _kind: AK) {
let op = GenericArrayScan::<AK>::new(bool_t(), qb_t(), vec![usize_t()], 2);
let optype: OpType = op.clone().into();
Expand All @@ -265,6 +267,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_scan_map<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let src_ty = qb_t();
Expand All @@ -290,6 +293,7 @@ mod tests {
#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_scan_accs<AK: ArrayKind>(#[case] _kind: AK) {
let size = 2;
let src_ty = qb_t();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ mod test {
use crate::std_extensions::arithmetic::float_types::ConstF64;

use crate::std_extensions::collections::array::Array;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::std_extensions::collections::value_array::ValueArray;

use super::*;

#[rstest]
#[case(Array)]
#[case(ValueArray)]
#[case(BorrowArray)]
fn test_array_value<AK: ArrayKind>(#[case] _kind: AK) {
let array_value = GenericArrayValue::<AK>::new(usize_t(), vec![ConstUsize::new(3).into()]);
array_value.validate().unwrap();
Expand Down
13 changes: 13 additions & 0 deletions hugr-core/src/std_extensions/collections/array/op_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Builder trait for array operations in the dataflow graph.

use crate::std_extensions::collections::array::GenericArrayOpDef;
use crate::std_extensions::collections::borrow_array::BorrowArray;
use crate::std_extensions::collections::value_array::ValueArray;
use crate::{
Wire,
Expand Down Expand Up @@ -390,6 +391,11 @@ pub fn build_all_value_array_ops<B: Dataflow>(builder: B) -> B {
build_all_array_ops_generic::<B, ValueArray>(builder)
}

/// Helper function to build a Hugr that contains all basic array operations.
pub fn build_all_borrow_array_ops<B: Dataflow>(builder: B) -> B {
build_all_array_ops_generic::<B, BorrowArray>(builder)
}

/// Testing utilities to generate Hugrs that contain array operations.
#[cfg(test)]
mod test {
Expand All @@ -411,4 +417,11 @@ mod test {
let builder = DFGBuilder::new(sig).unwrap();
build_all_value_array_ops(builder).finish_hugr().unwrap();
}

#[test]
fn all_borrow_array_ops() {
let sig = Signature::new_endo(Type::EMPTY_TYPEROW);
let builder = DFGBuilder::new(sig).unwrap();
build_all_borrow_array_ops(builder).finish_hugr().unwrap();
}
}
Loading
Loading