/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder 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 MoleCuilder.  If not, see .
 */
/*
 * KeySet.cpp
 *
 *  Created on: Oct 20, 2011
 *      Author: heber
 */
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include 
#include "CodePatterns/Log.hpp"
#include "KeySet.hpp"
/** Constructor of class KeySet.
 *
 * This class contains a set of indices for the atoms contained in a fragment.
 *
 */
KeySet::KeySet()
{}
/** Destructor of class KeySet.
 *
 */
KeySet::~KeySet()
{}
/** Comparator to allow for placing in a map.
 *
 * @param a instance to compare to
 * @return true - this instance has less items or the first different item itself is less,
 *         false - else
 */
bool KeySet::operator<(const KeySet &a) const
{
  //LOG(0, "my check is used.");
  if (size() < a.size()) {
    return true;
  } else {
    if (size() > a.size()) {
      return false;
    } else {
      KeySet::iterator IteratorA = begin();
      KeySet::iterator IteratorB = a.begin();
      while ((IteratorA != end()) && (IteratorB != a.end())) {
        if ((*IteratorA) <  (*IteratorB))
          return true;
        else if ((*IteratorA) > (*IteratorB)) {
            return false;
          } // else, go on to next index
        IteratorA++;
        IteratorB++;
      } // end of while loop
    }// end of check in case of equal sizes
  }
  return false; // if we reach this point, they are equal
}
/** Scans a single line for number and puts them into \a KeySet.
 * \param *buffer buffer to scan
 * \return true - at least one valid atom id parsed, false - CurrentSet is empty
 */
bool KeySet::ScanBufferIntoKeySet(char *buffer)
{
  std::stringstream line;
  int AtomNr;
  int status = 0;
  line.str(buffer);
  while (!line.eof()) {
    line >> AtomNr;
    if (AtomNr >= 0) {
      insert(AtomNr);  // insert at end, hence in same order as in file!
      status++;
    } // else it's "-1" or else and thus must not be added
  }
  std::stringstream output;
  for(KeySet::iterator runner = begin(); runner != end(); runner++)
    output << (runner == begin() ? "" : " " ) << (*runner);
  LOG(2, "INFO: The scanned KeySet is " << output.str());
  return (status != 0);
}