/* 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_cardcontainer2dim.hh

#ifndef CARDWORDS_CARDCONTAINER2DIM_HH
#define CARDWORDS_CARDCONTAINER2DIM_HH

#include "cardwords_cardcontainer.hh"
#include <map>
#include "cardwords_matrix.hh"

class CardWords_CardContainer2Dim
  : public CardWords_CardContainer,
    public map<CardWords_Card*, CardWords_CardTableLocation>
// Since every Card has its own place in memory,
// it is possible to compare the pointers directly. This is sufficient
// for a set or map.
{
public:
  // remove a special card from the container:
  virtual void remove_card (CardWords_Card *);
    
  // add a card to the container:
  virtual void add_card (CardWords_Card*, CardWords_CardTableLocation);
  virtual void add_card (CardWords_Card* t, size_t coordinate1,
                         size_t coordinate2)
    {add_card (t, CardWords_CardTableLocation(coordinate1, coordinate2));};

  // remove all cards and put them into pile:
  virtual void   clear  (class CardWords_CardContainer1Dim *pile);

  CardWords_CardContainer2Dim (size_t width, size_t height);

  // the destructor automatically deletes all contained cards:
  virtual ~CardWords_CardContainer2Dim ();

  CardWords_Card* operator[] (const CardWords_CardTableLocation &) const;
  CardWords_Card* operator() (size_t column, size_t row) const;

  CardWords_Card * & operator [] (const CardWords_CardTableLocation &);
  CardWords_Card * & operator () (size_t column, size_t row);

  const CardWords_CardTableLocation & operator[] (CardWords_Card *) const;

  size_t get_width(void) const;
  size_t get_height(void) const;
private:
  size_t width, height;
  CardWords_Matrix<CardWords_Card*> cardPointers;
};

inline
CardWords_Card*
CardWords_CardContainer2Dim::
operator[] (const CardWords_CardTableLocation & loc) const
{
  return cardPointers[loc];
}

inline
CardWords_Card*
CardWords_CardContainer2Dim::operator() (size_t column, size_t row) const
{
  return cardPointers(column, row);
}

inline
CardWords_Card * &
CardWords_CardContainer2Dim::
operator [] (const CardWords_CardTableLocation & loc)
{
  return cardPointers[loc];
}

inline
CardWords_Card * &
CardWords_CardContainer2Dim::operator () (size_t column, size_t row)
{
  return cardPointers(column, row);
}

inline
const CardWords_CardTableLocation &
CardWords_CardContainer2Dim::operator[] (CardWords_Card * card) const
{
  const_iterator iter = find(card);
  assert(iter != end());
  return iter->second;
}

inline
size_t
CardWords_CardContainer2Dim::get_width(void) const
{
  return width;
}

inline
size_t
CardWords_CardContainer2Dim::get_height(void) const
{
  return height;
}

#endif
