/*
 * This example is from the Tools.h++ manual, version 7
 *
 * (c) Copyright 1989 - 1996 Rogue Wave Software, Inc.
 * ALL RIGHTS RESERVED
 *
 * The software and information contained herein are proprietary to, and
 * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
 * intends to preserve as trade secrets such software and information.
 * This software is furnished pursuant to a written license agreement and
 * may be used, copied, transmitted, and stored only in accordance with
 * the terms of such license and with the inclusion of the above copyright
 * notice.  This software and information or any other copies thereof may
 * not be provided or otherwise made available to any other person.
 *
 * Notwithstanding any other lease or license that may pertain to, or
 * accompany the delivery of, this computer software and information, the
 * rights of the Government regarding its use, reproduction and disclosure
 * are as set forth in Section 52.227-19 of the FARS Computer
 * Software-Restricted Rights clause.
 * 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7013.
 * Contractor/Manufacturer is Rogue Wave Software, Inc.,
 * P.O. Box 2328, Corvallis, Oregon 97339.
 *
 * This computer software and information is distributed with "restricted
 * rights."  Use, duplication or disclosure is subject to restrictions as
 * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
 * Computer Software-Restricted Rights (April 1985)."  If the Clause at
 * 18-52.227-74 "Rights in Data General" is specified in the contract,
 * then the "Alternate III" clause applies.
 */
/* Note: This example requires the C++ Standard Library */

#ifdef RW_NO_STL
#error This example is for users with access to the C++ Standard Library
#else

#include <iostream.h>
#include <algorithm>
#include <rw/tvordvec.h>

struct Card {
  char  rank;
  char  suit;

  bool operator== (const Card& c) const
    { return rank == c.rank && suit == c.suit; }

  bool operator< (const Card& c) const
    { return rank < c.rank; }

  Card() { }
  Card(char r, char s) : rank(r), suit(s) { }

  // print a card: '3-H' = three of hearts, 'A-S' = ace of spades
friend ostream& operator<<(ostream& ostr, const Card& c)
  { return (ostr << c.rank << "-" << c.suit << "  "); }
};

/*
 * A generator class - return Cards in sequence
 */
class DeckGen {
  int rankIdx;  // indexes into static arrays below
  int suitIdx;
  static const char Ranks[13];
  static const char Suits[4];
public:
  DeckGen() : rankIdx(-1), suitIdx(-1) { }

  // generate the next Card
  Card operator()()
    {
      rankIdx = (rankIdx + 1) % 13;
      if (rankIdx == 0)
	// cycled through ranks, move on to next suit:
	suitIdx = (suitIdx + 1) % 4;
      return Card(Ranks[rankIdx], Suits[suitIdx]);
    }
};

const char DeckGen::Suits[4]  = {'S', 'H', 'D', 'C' };
const char DeckGen::Ranks[13] = {'A', '2', '3', '4',
				 '5', '6', '7', '8',
				 '9', 'T', 'J', 'Q', 'K' };

int main(){
  // Tools.h++ collection:
  RWTValOrderedVector<Card> deck;
  RWTValOrderedVector<Card>::size_type pos;

  Card aceOfSpades('A','S');
  Card firstCard;

  // Use standard library algorithm to generate deck:
  generate_n(back_inserter(deck.std()), 52, DeckGen());
  cout << endl << "The deck has been created" << endl;

  // Use Tools.h++ member function to find card:
  pos = deck.index(aceOfSpades);
  cout << "The Ace of Spades is at position " << pos+1 << endl;

  // Use standard library algorithm to shuffle deck:
  random_shuffle(deck.begin(), deck.end());
  cout << endl << "The deck has been shuffled" << endl;

  // Use Tools.h++ member functions:
  pos = deck.index(aceOfSpades);
  firstCard = deck.first();

  cout << "Now the Ace of Spades is at position " << pos+1
    << endl << "and the first card is " << firstCard << endl;

  return 0;
}


/* Output:

The deck has been created
The Ace of Spades is at position 1

The deck has been shuffled
Now the Ace of Spades is at position 37
and the first card is Q-D  

*/

#endif  //#ifdef RW_NO_STL
