#!/usr/bin/ruby

# Simulate the shuffle step of the Map-Reduce paradigm on a single machine.  Do
# not use on large data sets

# Assumes that map outputs one key-value pair on each line, where each value
# itself can be a list. Each record has the key followed by each element in
# the value-list, separated by the TAB character.

# For each key x, this program concatenates the lists from each key-value pair
# corresponding to key x, and then outputs a single line containing the key
# followed by the element of the concatenated list, separated by the TAB
# character.

# This output can now be used by reduce.

grouped_values = Hash.new()
while (map_key_val_string = gets) # Read one key-value pair as a string
  
  # Split the string into a list with the key followed by the elements in the
  # value-list
  map_key_val_list = map_key_val_string.split

 
  # Check to make sure the key exists
  next if map_key_val_list.length == 0

  # Extract the key from the list
  key = map_key_val_list.shift

  # Add the values to a big Hash table
  grouped_values[key] = [] if !grouped_values[key]
  map_key_val_list.each{|element| grouped_values[key].push element}
end

grouped_values.keys.each do |key|
  print key
  grouped_values[key].each {|element| print "\t", element}
  print "\n"
end
    
STDOUT.flush
