/* mysortu.c
   Jim Plank
   7 February, 1994 */

/* This program is just like mysort.c, except that it deletes all 
   duplicate lines.  That is, if there are more than one of a particular
   line in a file, then mysortu prints out just one of them.  It is
   equivalent to unix's 'sort -u'.

   This is effected by checking to see if a line is in the tree before
   inserting it.  rb_find_key_n() is used to see if the line is in the
   tree.
*/
   
   

#include <string.h>
#include <stdio.h>
#include "fields.h"
#include "rb.h"


main()
{
  IS is;
  char *copy;
  Rb_node sorted_lines, tmp;
  int found;

  sorted_lines = make_rb();
  is = new_inputstruct(NULL);

  while(get_line(is) >= 0) {

    /* Insert the line into the tree only if it is not there already */
    (void) rb_find_key_n(sorted_lines, is->text1, &found);
    if (!found) {
      copy = strdup(is->text1);
      rb_insert(sorted_lines, copy, NULL);
    }
  }

  rb_traverse(tmp, sorted_lines) {
    printf("%s", tmp->k.key);
  }
}
