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

#include "cardwords_cardcontainer1dim.hh"
#include <assert.h>
#include <algo.h>

// remove a special card from the container:
void
CardWords_CardContainer1Dim::remove_card (CardWords_Card * parameter_card)
{
  assert (parameter_card != 0);
  assert (parameter_card->get_container() == this);
  assert (size() > 0);
  
  iterator iter = find(parameter_card);

  assert (iter != end());

  parameter_card->set_container(0);
  erase(iter);
}
      

// add a card to the container:
void
CardWords_CardContainer1Dim::add_card (CardWords_Card *to_be_added)
{
  assert (to_be_added != 0);
  if (to_be_added->get_container() != 0) {
    to_be_added->get_container()->remove_card(to_be_added);
  };
  assert (to_be_added->get_container() == 0 );
  assert (free_places() > 0);

  insert(to_be_added);
  to_be_added->set_container(this);
}

// remove all cards and put them into pile:
void
CardWords_CardContainer1Dim::clear  (CardWords_CardContainer1Dim *pile) 
{
  CardWords_Card * to_be_removed;
  while (!empty()) {
    to_be_removed = *begin();
    pile->add_card (to_be_removed);
  }
}

// information about how the space is used:
size_t
CardWords_CardContainer1Dim::free_places (void) const
{
  assert (size() >= 0 && size() <= sizeOfContainer);
  return (sizeOfContainer - size());
}

size_t
CardWords_CardContainer1Dim::occupied_places (void) const
{
  assert (size() >= 0 && size() <= sizeOfContainer);
  return size();
}

size_t
CardWords_CardContainer1Dim::total_places (void) const
{
  return sizeOfContainer;
}


// constructor for one_dimensional cobtainers
CardWords_CardContainer1Dim::CardWords_CardContainer1Dim (size_t size) :
  sizeOfContainer (size)
{
  assert (sizeOfContainer > 0);
}

// the destructor automatically deletes all contained cards:
CardWords_CardContainer1Dim::~CardWords_CardContainer1Dim ()
{
  iterator iter;
  for (iter = begin(); iter != end(); ++iter) {
    assert (*iter != 0);
    delete *iter;
  }
}

ostream & operator << (ostream & ostream,
		       const CardWords_CardContainer1Dim & container)
{ 
  ostream << container.occupied_places() << ' ';
  copy(container.begin(),container.end(),
       ostream_iterator<CardWords_Card*>(ostream," "));
  return ostream;
}

CardWords_Card *
CardWords_CardContainer1Dim::matching_card (const CardWords_CardBase &t) const
{
  // search the whole container for a matching card

  for (const_iterator iter = begin();
       iter != end();
       iter++) {
    if (t == **iter)
      return *iter;
  }
  return 0;
}

CardWords_Card *
CardWords_CardContainer1Dim::matching_card (CardWords_Char c) const
{
  // search the whole container for a matching card

  CardWords_Card *wildcard = 0;

  for (const_iterator iter = begin();
       iter != end();
       iter++) {

    const CardWords_Char & faceValue = (**iter).get_faceValue();

    if (c == faceValue)
      return *iter;
    if (faceValue == CardWords_Char::blanko) {
      // if we don't find a matching card, we can return a wildcard
      wildcard = *iter;
    }
  }
  return wildcard; // is either 0 or a real wildcard
}

// How many cards with this face value are there ?
size_t
CardWords_CardContainer1Dim::get_no_of_matching_cards(CardWords_Char c) const
{
  size_t no_of_matches = 0;
  for (const_iterator iter = begin();
       iter != end();
       iter++) {

    const CardWords_Char & faceValue = (**iter).get_faceValue();

    if (c == faceValue) {
      ++no_of_matches;
    }
  }
  return no_of_matches;
}
  
// Gets the n'th matching card with the required face value. However,
// this numbering is arbitrary! But it is at least guaranteed not to
// change as long as the container does not change. This means
// get_matching_card(c, 1) returns always the same ('first') card
// with face value c as long as no cards are added to or removed from
// the container. 1 <= n_th_match <= get_no_of_matching_cards(). if
// n_th_match is not inside this intervall, 0 is returned.
CardWords_Card *
CardWords_CardContainer1Dim::matching_card (CardWords_Char c,
                                          size_t n_th_match) const
{
  size_t current_match_no = 0;

  if (n_th_match == 0 || n_th_match > size()) {
    return 0;
  }
  for (const_iterator iter = begin();
       iter != end();
       iter++) {

    const CardWords_Char & faceValue = (**iter).get_faceValue();

    if (c == faceValue) {
      ++current_match_no;
      if (current_match_no == n_th_match) {
        return *iter;
      }
    }
  }
  return 0;
}
