#!/usr/bin/perl -w

#
#  census_to_intermediate_red_init
#  Copyright (C) 2010 by USC/ISI
#  $Id$
#  
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License,
#  version 2, as published by the Free Software Foundation.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#  
#  
#  This code was originally written by Xun Fan <xunfan@isi.edu>.
#

# The main function of this reduce phase is to eliminate redundent ip records.

use Fsdb::IO::Reader;
use Fsdb::IO::Writer;

#my $input_stream = new Fsdb::IO::Reader(-fh => \*STDIN, );
my $output_stream = new Fsdb::IO::Writer(-fh => \*STDOUT, -fscode => 't', -cols => [qw(ip history)]);
my @input_arow;
my $current_ip =  undef;

my $def_history = '1'; # the history of the record, represents in 2**n.

#sub read_rows{
#	$input_stream->read_row_to_aref(\@input_arow) or return undef;
#	return ($input_arow[0]);
#}

while(<>){
	# read input
	#
	my ($ip) = split;
	#last if (!defined($ip)); #eof
	
	# Start per ip  process
	# when the ip changes, output the records.
	if (!defined($current_ip)){
		# The first row
		$current_ip = $ip;
		$output_stream->write_row($current_ip, $def_history);
	}else{
		next if($current_ip eq $ip); # Eliminate redundent records.
			
		if ($current_ip ne $ip){
			# The ip is changing
			# output the new ip records.
			$current_ip = $ip;
			$output_stream->write_row($current_ip, $def_history);
		}
	}
}

