/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   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_dictionary.hh

#ifndef CARDWORDS_DICTIONARY_HH
#define CARDWORDS_DICTIONARY_HH

#include "cardwords_dicbot_cardselectioncontents.hh"
#include "cardwords_fasthashkey.hh"
#include "cardwords_hashtable.hh"

#ifndef CARDTABLE_REQUIREMENTS_DEFINED
class CardWords_DicBot_CardTableRequirements;
#endif

#ifndef BEST_ORDER_GENERATOR_DEFINED
class CardWords_DicBot_BestOrderGenerator;
#endif


class CardWords_Dictionary {
public:

  bool
  contains_word (const CardWords_String &) const;

  const CardWords_String *
  insert_word (const CardWords_String &);

  void
  erase_word  (const CardWords_String &);

  // add a dictionary
  void
  insert_file (const string & filename);
  
  // remove words in that file
  void
  remove_file (const string & filename);

  // This function will save the result in a member variable. It will live
  // until the next call to find_possible_words.
  const vector<CardWords_CardSelection> *
  find_possible_words (size_t min_word_length,
                       size_t max_word_length,
                       CardWords_DicBot_CardSelectionContents & sc,
                       class CardWords_DicBot_CardTableRequirements * req,
                       class CardWords_DicBot_BestOrderGenerator * bog);

  CardWords_Dictionary (size_t min_word_length,
                        size_t max_word_length,
                        size_t hash_table_size);

  // Returns the number of contained words:
  size_t word_count(void) const;
  
private:
  vector< CardWords_HashTable< CardWords_String > > dict;

  vector<CardWords_CardSelection> founds;

  size_t minWordLength, maxWordLength;
};

inline
CardWords_Dictionary::CardWords_Dictionary
(size_t min_word_length,
 size_t max_word_length,
 size_t hash_table_size)
  :
  dict(max_word_length - min_word_length + 1,
       CardWords_HashTable<CardWords_String>(hash_table_size,
                                             &CardWords_FastHashKey::key_of)),
  minWordLength(min_word_length),
  maxWordLength(max_word_length)
{
  assert (max_word_length >= min_word_length);
}


inline bool
CardWords_Dictionary::contains_word (const CardWords_String & word) const
{
  size_t len = word.length();
  assert (len >= minWordLength);
  assert (len <= maxWordLength);
  return (bool) dict[len - minWordLength].contains(word);
}

inline const CardWords_String *
CardWords_Dictionary::insert_word (const CardWords_String & word)
{
  size_t len = word.length();
  assert (len >= minWordLength);
  assert (len <= maxWordLength);
  return dict[len - minWordLength].insert(word);
}

inline void
CardWords_Dictionary::erase_word (const CardWords_String & word)
{
  size_t len = word.length();
  assert (len >= minWordLength);
  assert (len <= maxWordLength);
  dict[len - minWordLength].remove(word);
}

inline   size_t
CardWords_Dictionary::word_count(void) const
{
  size_t index, count;
  for (index = count = 0; index < maxWordLength - minWordLength; ++index) {
    count += dict[index].size();
  }
  return count;
}

#endif

