/*
 * lineshuffler.cpp
 *
 * Implementation of the LineShuffler class.  See header file for function
 * descriptions.
 *
 */


#include "lineshuffler.hpp"
#include <algorithm>

LineShuffler::LineShuffler()
{
    // Nothing else to do.
}

string LineShuffler::removeLine()
{
    int count = lines_.size();
    int index = rnd_.next(count);	// Choose a random line.
    swap(lines_[index], lines_[count - 1]);
                                        // Swap the chosen line with the line
                                        // at the very end.
    string randomLine(lines_[index]);	// This is the line we want.
    swap(lines_[index], lines_[count - 1]);
                                        // Swap the chosen line with the line
                                        // at the very end.

    delete [] lines_.back();		// Delete the line at the very end
    lines_.pop_back();			// (i.e., the one we chose).
                                        
    return randomLine;
}


void LineShuffler::addLine(const string& line)
{
    char* mycopy = new char[line.size()+1];
    strcpy(mycopy, line.c_str());
    lines_.push_back(mycopy);
}


bool LineShuffler::isEmpty()
{  
    return lines_.size() == 0;
}

/* Junk to make emacs use the right C++ style when editing this code:
 * Local Variables:
 * c-file-style: "stroustrup"
 * End:
 */
