#include <iostream>
#include <iterator>
#include <set>

#include "flow.hpp"

// ============================================================================
// The sort system reads from the provided data and sorts it,
// then once the input runs out, starts outputting the
// sorted data on output. 

void sort( flows::reader<int> in, flows::writer<int> out)
{
  std::set<int> data(in.begin(), in.end());
  std::copy(data.begin(), data.end(), out.begin());
}

std::pair< flows::writer<int>, flows::reader<int> > create_sort()
{
  flows::flow<int> pair;
  return std::make_pair( pair.input(), 
                         flows::process<int>( pair.output(), sort ));
}

// ============================================================================
// The data system reads from standard input, sends to the
// sorter, then closes the sort flow and reads from the result flow.

int main() 
{
  std::pair< flows::writer<int>, flows::reader<int> > pair =
    create_sort();

  std::cout << "Please enter the numbers to be sorted:\n";

  std::copy(std::istream_iterator<int>(std::cin),
            std::istream_iterator<int>(),
            pair.first.begin());

  pair.first.close(); 

  std::copy(pair.second.begin(),
            pair.second.end(),
            std::ostream_iterator<int>(std::cout, "\n"));
}
