[LON-CAPA-cvs] cvs: loncom /interface lonnavmaps.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Sun, 03 Nov 2002 21:05:04 -0000


bowersj2		Sun Nov  3 16:05:04 2002 EDT

  Modified files:              
    /loncom/interface	lonnavmaps.pm 
  Log:
  Preprocessing code for final iterator. Warning: This version contains 
  debugging output.
  
  
  
Index: loncom/interface/lonnavmaps.pm
diff -u loncom/interface/lonnavmaps.pm:1.96 loncom/interface/lonnavmaps.pm:1.97
--- loncom/interface/lonnavmaps.pm:1.96	Fri Nov  1 14:50:00 2002
+++ loncom/interface/lonnavmaps.pm	Sun Nov  3 16:05:03 2002
@@ -2,7 +2,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: lonnavmaps.pm,v 1.96 2002/11/01 19:50:00 bowersj2 Exp $
+# $Id: lonnavmaps.pm,v 1.97 2002/11/03 21:05:03 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1290,6 +1290,9 @@
                 }
 
                 $r->print("  $curMarkerBegin<a href=\"$link\">$title$partLabel</a> $curMarkerEnd $nonLinkedText");
+                $r->print(" TDV:" . $curRes->{DATA}->{TOP_DOWN_VAL}); # temp
+                $r->print(" BUV:" . $curRes->{DATA}->{BOT_UP_VAL}); # temp
+                $r->print(" DD:" . $curRes->{DATA}->{DISPLAY_DEPTH}); # temp
 
                 if ($curRes->{RESOURCE_ERROR}) {
                     $r->print(&Apache::loncommon::help_open_topic ("Navmap_Host_Down",
@@ -2066,40 +2069,71 @@
     # Now, we need to pre-process the map, by walking forward and backward
     # over the parts of the map we're going to look at.
 
-    my $forwardIterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP}, 
-                                                               $self->{FIRST_RESOURCE},
-                                                               $self->{FINISH_RESOURCE},
-                                                               $self->{FILTER},
-                                                               undef, $self->{CONDITION},
-                                                               FORWARD());
+    # The processing steps are exactly the same, except for a few small 
+    # changes, so I bundle those up in the following list of two elements:
+    # (direction_to_iterate, VAL_name, next_resource_method_to_call,
+    # first_resource).
+    # This prevents writing nearly-identical code twice.
+    my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext', 
+                        'FIRST_RESOURCE'],
+                       [BACKWARD(), 'BOT_UP_VAL', 'getPrevious', 
+                        'FINISH_RESOURCE'] );
+
+    foreach my $pass (@iterations) {
+        my $direction = $pass->[0];
+        my $valName = $pass->[1];
+        my $nextResourceMethod = $pass->[2];
+        my $firstResourceName = $pass->[3];
+
+        my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP}, 
+                                                            $self->{FIRST_RESOURCE},
+                                                            $self->{FINISH_RESOURCE},
+                                                            {}, undef, 0, $direction);
     
-    # prime the recursion
-    $self->{FIRST_RESOURCE}->{DATA}->{TOP_DOWN_VAL} = 0;
-    my $depth = 1;
-    $forwardIterator->next();
-    my $curRes = $forwardIterator->next();
-    while ($depth > 0) {
-        if ($curRes == $forwardIterator->BEGIN_MAP()) { $depth++; }
-        if ($curRes == $forwardIterator->END_MAP()) { $depth--; }
+        # prime the recursion
+        $self->{$firstResourceName}->{DATA}->{$valName} = 0;
+        my $depth = 1;
+        $iterator->next();
+        my $curRes = $iterator->next();
+        while ($depth > 0) {
+            if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
+            if ($curRes == $iterator->END_MAP()) { $depth--; }
         
-        if (ref($curRes)) {
-            my $topDownVal = $curRes->{DATA}->{TOP_DOWN_VAL};
-            my $nextResources = $curRes->getNext();
-            my $resourceCount = scalar(@{$nextResources});
+            if (ref($curRes)) {
+                my $resultingVal = $curRes->{DATA}->{$valName};
+                my $nextResources = $curRes->$nextResourceMethod();
+                my $resourceCount = scalar(@{$nextResources});
             
-            if ($resourceCount == 1) {
-                my $current = $nextResources->[0]->{DATA}->{TOP_DOWN_VAL} || 999999999;
-                $nextResources->[0]->{DATA}->{TOP_DOWN_VAL} = min($topDownVal, $current);
+                if ($resourceCount == 1) {
+                    my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
+                    $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
+                }
+                
+                if ($resourceCount > 1) {
+                    foreach my $res (@{$nextResources}) {
+                        my $current = $res->{DATA}->{$valName} || 999999999;
+                        $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
+                    }
+                }
+            }
+            if (ref($curRes) && $curRes->is_map() && $direction == FORWARD()) {
+                my $firstResource = $curRes->map_start();
+                my $finishResource = $curRes->map_finish();
+                my $newIterator = Apache::lonnavmaps::iterator->new($self->{NAV_MAP},
+                                                                    $firstResource,
+                                                                    $finishResource,
+                                                                    $self->{FILTER},
+                                                                    $self->{ALREADY_SEEN},
+                                                                    $self->{CONDITION});
             }
             
-            if ($resourceCount > 1) {
-                foreach my $res (@{$nextResources}) {
-                    my $current = $res->{DATA}->{TOP_DOWN_VAL} || 999999999;
-                    $res->{DATA}->{TOP_DOWN_VAL} = min($current, $topDownVal + 1);
-                }
+            # Assign the final val
+            if (ref($curRes) && $direction == BACKWARD()) {
+                $curRes->{DATA}->{DISPLAY_DEPTH} = min($curRes->{DATA}->{TOP_DOWN_VAL},
+                                                       $curRes->{DATA}->{BOT_UP_VAL});
             }
+            $curRes = $iterator->next();
         }
-        $curRes = $forwardIterator->next();
     }
 
     # Now we're ready to start iterating.
@@ -2115,6 +2149,10 @@
 # used by the main iterator for pre-processing. It also is able to isolate
 # much of the complexity of the main iterator, so the main iterator is much
 # simpler.
+# There is no real benefit in merging the main iterator and this one into one class...
+# all the logic in DFSiterator would need to be replicated, you gain no performance,
+# at best, you just make one massively complicated iterator in place of two 
+# somewhat complicated ones. ;-) - Jeremy
 
 # Here are the tokens for the iterator, replicated from iterator for convenience: