/*
 * dequeval.cpp: Example using RWDequeVal<T>
 *
 * $Id: dequeval.cpp,v 7.7 1996/03/05 08:21:45 jims Exp $
 *
 * (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.
 *
 ***************************************************************************
 *
 * $Log: dequeval.cpp,v $
 * Revision 7.7  1996/03/05 08:21:45  jims
 * Remove comment within a comment
 *
 * Revision 7.6  1996/02/18 21:36:56  kyle
 * Millenium changes #include from <> to ""; adds prototype for isodd.
 *
 * Revision 7.5  1996/01/06 18:28:16  kevinj
 * Fixed names after template merge.
 *
 * Revision 7.4  1995/09/07 22:23:59  pearson
 * Fix typo
 *
 * Revision 7.3  1995/09/07  22:21:31  pearson
 * Fixed assignment of iter in copy
 *
 * Revision 7.2  1995/09/06  22:57:14  jims
 * Add header section
 * Obtain iterators from RW interface
 * Use STL copy and erase to swap odds and evens
 *
 */


/*
 * This program partitions integers into even and odd numbers.
 */

#include "rw/tvdeque.h"
#include <iostream.h>

#  include <algorithm>  //STL algorithms header
#  include <utility>    //STL utilities; for ptr_fun

// This is a function to test for oddness:
bool isodd(int x)
{
	return (x % 2 != 0);
}

int main()
{
  typedef RWTValDeque<int>::iterator iterator;

  RWTValDeque<int> numbers;

  iterator iter;
  int n;

  cout << "Input a random assortment of integers (EOF to end):" << endl;

  // Put even numbers in front and odd numbers in back of deque:
  while (cin >> n) {
    if (n % 2 == 0)
      numbers.pushFront(n);
    else
      numbers.pushBack(n);
  }

  cout << endl << "Partitioned odds and evens." << endl;

  // Use iterators to list deque contents:
  cout << endl << "The deque now contains: ";

  for(iter = numbers.begin(); iter != numbers.end(); ++iter)
    cout << *iter << "  ";

  // Use the standard library to swap the odds and evens:
  cout << endl << endl
       << "Lets swap the odds and evens..." << endl << endl;

  // copy evens to the back:
  iter = find_if( numbers.begin(),
                  numbers.end(),
                  ptr_fun(isodd));

  copy(numbers.begin(),                 // from start of the deque
       iter,                            // until first odd
       back_inserter(numbers.std())     // insert at end of deque
      );

  // now erase them from the front:
  numbers.std().erase(numbers.begin(), iter);

  // Display and empty the deque with RW interface:
  cout << "The deque now contains: ";
  while (numbers.entries()) {
	 cout << numbers.popFront() << "  ";
  }

  cout << endl << endl;

  return 0;
}
