MCPcopy Create free account
hub / github.com/ciphermodelabs/ciphercore / instantiate

Method instantiate

ciphercore-base/src/ops/multiplexer.rs:49–81  ·  view source on GitHub ↗
(&self, context: Context, arguments_types: Vec<Type>)

Source from the content-addressed store, hash-verified

47#[typetag::serde]
48impl CustomOperationBody for Mux {
49 fn instantiate(&self, context: Context, arguments_types: Vec<Type>) -> Result<Graph> {
50 if arguments_types.len() != 3 {
51 return Err(runtime_error!("Invalid number of arguments for Mux"));
52 }
53 let t = arguments_types[0].clone();
54 if !t.is_scalar() && !t.is_array() {
55 return Err(runtime_error!("Flag for Mux must be a scalar or an array"));
56 }
57 if t.get_scalar_type() != BIT {
58 return Err(runtime_error!("Flag for Mux must consist of bits"));
59 }
60 if arguments_types[1].get_scalar_type() != arguments_types[2].get_scalar_type() {
61 return Err(runtime_error!(
62 "Choices for Mux must have the same scalar type"
63 ));
64 }
65
66 let g = context.create_graph()?;
67 let i_flag = g.input(arguments_types[0].clone())?;
68 let i_choice1 = g.input(arguments_types[1].clone())?;
69 let i_choice0 = g.input(arguments_types[2].clone())?;
70 if arguments_types[1].get_scalar_type() == BIT {
71 i_choice0
72 .add(i_flag.multiply(i_choice0.add(i_choice1)?)?)?
73 .set_as_output()?;
74 } else {
75 let i_choice0 = i_choice0.mixed_multiply(i_flag.clone())?;
76 let i_choice1 = i_choice1.mixed_multiply(i_flag.add(g.ones(scalar_type(BIT))?)?)?;
77 i_choice0.add(i_choice1)?.set_as_output()?;
78 }
79 g.finalize()?;
80 Ok(g)
81 }
82
83 fn get_name(&self) -> String {
84 "Mux".to_owned()

Callers

nothing calls this directly

Calls 13

cloneMethod · 0.80
is_scalarMethod · 0.80
is_arrayMethod · 0.80
onesMethod · 0.80
scalar_typeFunction · 0.50
get_scalar_typeMethod · 0.45
create_graphMethod · 0.45
inputMethod · 0.45
set_as_outputMethod · 0.45
addMethod · 0.45
multiplyMethod · 0.45
mixed_multiplyMethod · 0.45

Tested by

no test coverage detected