22
33import java .util .ArrayList ;
44import java .util .HashMap ;
5+ import java .util .HashSet ;
56import java .util .LinkedList ;
67
78import net .imglib2 .Cursor ;
@@ -43,13 +44,13 @@ public Compute(
4344
4445 public < O extends RealType < O > > RandomAccessibleInterval < O > into ( final RandomAccessibleInterval < O > target )
4546 {
46- // Recursive copy: initializes interval iterators
47- final IFunction f = this .operation .copy ();
48- // Set temporary computation holders
49- final O scrap = target . randomAccess (). get (). createVariable ();
50- f . setScrap ( scrap );
47+ // Recursive copy: initializes interval iterators and sets temporary computation holder
48+ final IFunction f = this .operation .reInit (
49+ target . randomAccess (). get (). createVariable (),
50+ new HashMap < String , RealType < ? > >(),
51+ converter );
5152
52- final boolean compatible_iteration_order = this .setup ( f , converter );
53+ final boolean compatible_iteration_order = this .setup ( f );
5354
5455 // Check compatible iteration order and dimensions
5556 if ( compatible_iteration_order )
@@ -74,7 +75,7 @@ public < O extends RealType< O > > RandomAccessibleInterval< O > into( final Ran
7475 }
7576
7677 @ SuppressWarnings ({ "rawtypes" , "unchecked" })
77- private boolean setup ( final IFunction f , final Converter < ?, ? > converter )
78+ private boolean setup ( final IFunction f )
7879 {
7980 final LinkedList < IFunction > ops = new LinkedList <>();
8081 ops .add ( f );
@@ -88,6 +89,9 @@ private boolean setup( final IFunction f, final Converter< ?, ? > converter )
8889 // Collect Var instances to check that each corresponds to an upstream Let
8990 final ArrayList < Var > vars = new ArrayList <>();
9091
92+ // Collect Let instances to check that their declared variables are used
93+ final HashSet < Let > lets = new HashSet <>();
94+
9195 IFunction parent = null ;
9296
9397 // Iterate into the nested operations
@@ -100,8 +104,6 @@ private boolean setup( final IFunction f, final Converter< ?, ? > converter )
100104 if ( op instanceof IterableImgSource )
101105 {
102106 final IterableImgSource iis = ( IterableImgSource )op ;
103- // Side effect: set the converter from input to output types
104- iis .setConverter ( converter );
105107 images .addLast ( iis .rai );
106108 }
107109 else if ( op instanceof IUnaryFunction )
@@ -112,6 +114,11 @@ else if ( op instanceof IUnaryFunction )
112114 {
113115 ops .addLast ( ( ( IBinaryFunction )op ).getSecond () );
114116
117+ if ( op instanceof Let )
118+ {
119+ lets .add ( ( Let )op );
120+ }
121+
115122 if ( op instanceof ITrinaryFunction )
116123 {
117124 ops .addLast ( ( ( ITrinaryFunction )op ).getThird () );
@@ -126,6 +133,7 @@ else if ( op instanceof Var )
126133 }
127134
128135 // Check Vars: are they all using names declared in upstream Lets
136+ final HashSet < Let > used = new HashSet <>();
129137 all : for ( final Var var : vars )
130138 {
131139 parent = var ;
@@ -134,15 +142,27 @@ else if ( op instanceof Var )
134142 if ( parent instanceof Let )
135143 {
136144 Let let = ( Let )parent ;
137- if ( let .varName != var .name )
145+ if ( let .getVarName () != var .getName () )
138146 continue ;
139147 // Else, found: Var is in use
148+ used .add ( let );
140149 continue all ;
141150 }
142151 }
143152 // No upstream Let found
144- throw new RuntimeException ( "The Var(\" " + var .name + "\" ) does not read from any upstream Let. " );
145- }
153+ throw new RuntimeException ( "The Var(\" " + var .getName () + "\" ) does not read from any upstream Let. " );
154+ }
155+
156+ // Check Lets: are their declared variables used in downstream Vars?
157+ if ( lets .size () != used .size () )
158+ {
159+ lets .removeAll ( used );
160+ String msg = "The Let-declared variable" + ( 1 == lets .size () ? "" : "s" );
161+ for ( final Let let : lets )
162+ msg += " \" " + let .getVarName () + "\" " ;
163+ msg += " " + ( 1 == lets .size () ? "is" : "are" ) + " not used by any downstream Var." ;
164+ throw new RuntimeException ( msg );
165+ }
146166
147167 return Util .compatibleIterationOrder ( images );
148168 }
0 commit comments