[Bio] / FigKernelPackages / Tracer.pm Repository:
ViewVC logotype

Diff of /FigKernelPackages/Tracer.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.61, Fri Jul 28 02:03:04 2006 UTC revision 1.62, Thu Aug 24 07:58:29 2006 UTC
# Line 2324  Line 2324 
2324      }      }
2325  }  }
2326    
2327    =head3 CompareLists
2328    
2329    C<< my ($inserted, $deleted) = Tracer::CompareLists(\@newList, \@oldList, $keyIndex); >>
2330    
2331    Compare two lists of tuples, and return a hash analyzing the differences. The lists
2332    are presumed to be sorted alphabetically by the value in the $keyIndex column.
2333    The return value contains a list of items that are only in the new list
2334    (inserted) and only in the old list (deleted).
2335    
2336    =over 4
2337    
2338    =item newList
2339    
2340    Reference to a list of new tuples.
2341    
2342    =item oldList
2343    
2344    Reference to a list of old tuples.
2345    
2346    =item keyIndex (optional)
2347    
2348    Index into each tuple of its key field. The default is 0.
2349    
2350    =item RETURN
2351    
2352    Returns a 2-tuple consisting of a reference to the list of items that are only in the new
2353    list (inserted) followed by a reference to the list of items that are only in the old
2354    list (deleted).
2355    
2356    =back
2357    
2358    =cut
2359    
2360    sub CompareLists {
2361        # Get the parameters.
2362        my ($newList, $oldList, $keyIndex) = @_;
2363        if (! defined $keyIndex) {
2364            $keyIndex = 0;
2365        }
2366        # Declare the return variables.
2367        my ($inserted, $deleted) = ([], []);
2368        # Loop through the two lists simultaneously.
2369        my ($newI, $oldI) = (0, 0);
2370        my ($newN, $oldN) = (scalar @{$newList}, scalar @{$oldList});
2371        while ($newI < $newN || $oldI < $oldN) {
2372            # Get the current object in each list. Note that if one
2373            # of the lists is past the end, we'll get undef.
2374            my $newItem = $newList->[$newI];
2375            my $oldItem = $oldList->[$oldI];
2376            if (! defined($newItem) || $newItem->[$keyIndex] gt $oldItem->[$keyIndex]) {
2377                # The old item is not in the new list, so mark it deleted.
2378                push @{$deleted}, $oldItem;
2379                $oldI++;
2380            } elsif (! defined($oldItem) || $oldItem->[$keyIndex] gt $newItem->[$keyIndex]) {
2381                # The new item is not in the old list, so mark it inserted.
2382                push @{$inserted}, $newItem;
2383                $newI++;
2384            } else {
2385                # The item is in both lists, so push forward.
2386                $oldI++;
2387                $newI++;
2388            }
2389        }
2390        # Return the result.
2391        return ($inserted, $deleted);
2392    }
2393    
2394    
2395  1;  1;

Legend:
Removed from v.1.61  
changed lines
  Added in v.1.62

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3