/* 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_dicbot_cardselectioncontents.cc

#include <algo.h>

#include "cardwords_dicbot_cardselectioncontents.hh"

size_t CardWords_DicBot_CardSelectionContents::max_hash_wilds = 1;

CardWords_DicBot_CardSelectionContents::
CardWords_DicBot_CardSelectionContents(const CardWords_CardSelection &sel)
  : noOfChars   (CardWords_Char::noOfChars()),
    card_desc   (sel),
    face_value  (sel.size()),
    points      (sel.size()),
    card_count_i(noOfChars, 0),
    index_i     (noOfChars),
    maps_i      (noOfChars),
    charcomb    (0),
    sorted_str  ()
{
  vector<CardWords_CardDescription>::const_iterator iter;
  size_t index;
  
  sort ( card_desc.begin(), card_desc.end() );
  for ((index = 0),(iter = card_desc.begin());
       iter != card_desc.end();
       ++index,++iter) {
    points[index] = (*iter).get_points();
    face_value[index] = (*iter).get_faceValue();
    
    if ( face_value[index] != CardWords_Char::blanko ) {
      sorted_str += face_value[index];
    }
    
    if (card_count_i [ face_value[index].internal_no() ]++ == 0) {
      index_i [ face_value[index].internal_no() ] = index;
    }
    if (maps_i[face_value[index].internal_no()].find(points[index])
        == maps_i[face_value[index].internal_no()].end()) {
      maps_i[face_value[index].internal_no()].
        insert(pair<int,pair<size_t,size_t> >(points[index],
                                              pair<size_t,size_t>(index, 0)));
    }
    ++(maps_i[face_value[index].internal_no()][points[index]].second);
  }

  if (card_count_i [ CardWords_Char::blanko.internal_no() ]
      <= max_hash_wilds) {
    if (card_count_i [ CardWords_Char::blanko.internal_no() ] == 0) {
      charcomb =
        new CardWords_CharCombination<CardWords_FastHashKey> (sorted_str);
    }
    else {
      charcomb = new CardWords_CharCombination<CardWords_FastHashKey>
        (sorted_str,
         card_count_i [CardWords_Char::blanko.internal_no()]);
    }
    if (charcomb == 0) {
      throw CardWords_XLowResources (gettext("Out of memory in "\
                                             "constructor of class "\
                                             "CardWords_DicBot_"\
                                             "CardSelectionContents"\
                                             ": not enough memory "\
                                             "for charcomb"));
    }
  }
}
