/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   
   This program 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.

   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

*/

// cardwords_string.cc

#include "cardwords_string.hh"

int
CardWords_String::compare (const CardWords_String &other) const
{
  vector<CardWords_Char>::const_iterator it_this =
    representation.begin();
  
  vector<CardWords_Char>::const_iterator it_other =
    other.representation.begin();

  while (true) {
    if (it_this == representation.end()) {
      if (it_other == other.representation.end()) {
	return 0;
      }
      else {
	return +1; // see footnote at the end of this method
      }
    }
    else {
      if (it_other == other.representation.end()) {
	return -1; // see footnote at the end of this method
      }
      else {
	int cardwordsCharCompare = (*it_this++).compare (*it_other++);
	if (cardwordsCharCompare != 0) {
	  return cardwordsCharCompare;
	}
	else {
	  continue;
	}
      }
    }
  }
  // This is the proimised footnote.
  // This function compares two strings. It differs from the common
  // implementation of string-comparison in the following way:
  // A word wich starts with characters that themselves form a different
  // word has a lesser value than that different word, e.g.  
  // "words" is assumed to have a lesser value than "word".

  // The reason is the following: each word will have a key. That key
  // is a string with the same characters as in the word, but ordered
  // according to their value. Thus "words" and "sword" both have the
  // same key "dorsw", "doors" has the key "doors".
  // Assume, you want to lay a new word and can use the characters 'd',
  // 'o', 'r', 's' and a wildcard.
  // You can than search the {keys for the [words in the (dictionary for
  // five-letter-words)]} for a keys that contain the letters 'd', 'o',
  // 'r', 's' until you reach a key that is greater than "dors". The
  // following words are immpossible with your letters.  
}

