@@ -18,15 +18,23 @@ private DFSMethods() {
1818 * If used start position hasn't any steps and is invalid for a pizza ->
1919 * remove this slice from startPositions and add all it's cells to the pizza.
2020 *
21- * @param pizza given pizza
22- * @param startPositions given slices in the pizza
23- * @param output list of valid slices
21+ * @param pizza given pizza(mutable)
22+ * @param startPositions given slices in the pizza(mutable)
23+ * @param output list of valid slices(mutable)
2424 * @return available steps
2525 */
2626 public static Map <Slice , List <Step >> getAvailableSteps (Pizza pizza , List <Slice > startPositions , List <Slice > output ) {
27- Profiler profiler = new Profiler ();
2827 Map <Slice , List <Step >> groupedSteps = new HashMap <>();
29- Iterator iter = startPositions .iterator ();
28+ Iterator iter ;
29+ //optimization for big arrays
30+ if (startPositions .size () > 1_000 ) {
31+ List <Slice > startPositionsSubset = startPositions .subList (0 , 20 );
32+ iter = startPositionsSubset .iterator ();
33+ }
34+ //iterate over all the start positions
35+ else {
36+ iter = startPositions .iterator ();
37+ }
3038 while (iter .hasNext ()) {
3139 Slice startPosition = (Slice ) iter .next ();
3240
@@ -58,7 +66,7 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
5866 groupedSteps .put (startPosition , steps );
5967 }
6068 }
61- LOGGER .info ("available steps for" +
69+ LOGGER .debug ("available steps for" +
6270 "\n pizza: " + pizza
6371 + "\n steps: " + groupedSteps );
6472 return groupedSteps ;
@@ -68,53 +76,58 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
6876 * Pick-ups a step with a minimal cells delta number,
6977 * execute it(cut it from the pizza, and add to a slice)
7078 *
71- * @param pizza given pizza
79+ * @param pizza given pizza(mutable)
7280 * @param step step to perform
73- * @param startPositions
74- * @param output @return formed slice that includes an original slice and delta from a step
81+ * @param startPositions given start positions(mutable_
82+ * @param output given list of output slices
7583 */
7684 public static void performStep (Pizza pizza , Step step , List <Slice > startPositions , List <Slice > output ) {
7785 //1. Pick ups a steps list with minimal total cells number
78- LOGGER .info ("STEP TO PERFORM " + step );
86+ LOGGER .debug ("STEP TO PERFORM " + step );
7987 //2. Cut all the step delta cells from pizza
80- LOGGER .info ("pizza before step: " + pizza
88+ LOGGER .debug ("pizza before step: " + pizza
8189 + "\n delta to remove from the pizza: " + step .delta );
8290 pizza .getCells ().removeAll (step .delta .cells );
83-
84- //3. remove previous version start position from startPositions
91+ //3. remove step start position from total start positions
8592 startPositions .remove (step .startPosition );
86-
8793 List <Cell > returnedList = step .startPosition .cells ;
8894 returnedList .addAll (step .delta .cells );
8995 Slice finalSlice = new Slice (returnedList );
90-
91- LOGGER .info ("PIZZA AFTER STEP:" + pizza );
92- //3. Add the step cells to an output slice
93-
94- if (finalSlice .cells .size () == pizza .getSliceInstruction ().getMaxNumberOfCellsPerSlice ()) {
96+ LOGGER .debug ("PIZZA AFTER STEP:" + pizza );
97+ //3. Add the step cells to an output slice if it's valid
98+ if (finalSlice .isValid (pizza )) {
9599 output .add (finalSlice );
96- } else {
100+ }
101+ //4. add start position + delta to start positions
102+ else {
97103 startPositions .add (finalSlice );
98104 }
99105 }
100106
101107 /**
102108 * Selects a step which start position has minimal delta in all the steps
103109 *
104- * @param steps
105- * @return
110+ * @param steps given steps
111+ * @return optimal step
106112 */
107113 public static Step selectStep (Map <Slice , List <Step >> steps ) {
108114 List <Step > min = steps .values ().stream ()
109- .min (Comparator .comparingLong (value -> value .stream ().map (step -> step .delta .cells .size ()).count ())).get ();
115+ .min (Comparator .comparingLong (value ->
116+ value .stream ()
117+ .map (step -> step .delta .cells .size ())
118+ .count ()))
119+ .get ();
110120 if (!min .isEmpty ()) {
111- LOGGER .info ("steps list with minimal number of delta cells: " + min );
121+ LOGGER .debug ("steps list with minimal number of delta cells: " + min );
112122 return min .get (0 );
113123 } else {
114- Optional <List <Step >> optionalStep = steps .values ().stream ().filter (steps1 -> !steps1 .isEmpty ()).findFirst ();
124+ Optional <List <Step >> optionalStep = steps .values ()
125+ .stream ()
126+ .filter (steps1 -> !steps1 .isEmpty ())
127+ .findFirst ();
115128 if (optionalStep .isPresent ()) {
116129 final Step step = optionalStep .get ().get (0 );
117- LOGGER .info ("Selected step to perform:" + step );
130+ LOGGER .debug ("Selected step to perform:" + step );
118131 return step ;
119132 } else return null ;
120133 }
0 commit comments