// src/backend/sgpemv2/ready_queue.hh - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This 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 3 of the License, or
// (at your option) any later version.
//
// SGPEMv2 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 SGPEMv2. If not, see http://www.gnu.org/licenses/.


#ifndef READY_QUEUE_HH
#define READY_QUEUE_HH 1

#include <sgpemv2/sgpemv2-visibility.hh>

#include <stdexcept>
#include <vector>

namespace sgpem
{
  class ReadyQueue;
  class Thread;
  class SG_DLLEXPORT ReadyQueue
  {
  public:
    typedef unsigned int position;
    typedef unsigned int size_t;

  /**
   * \brief Swaps the object at position \a a with the object
   * at position \a b Used for implementing in-place sorting algorithms.
   *
   * \param a first object position
   * \param b second object position
   */
    void       swap(position a, position b);

    /**
     * \brief Return the size of the queue.
     *
     * \return _scheds.size()
     */
    size_t     size() const;

    /**
     * \brief Returns an item contained in the vector.
     * Throws an std::out_of_range standard exception
     * if position is < 1 or > size()-1.
     *
     * \return The item at positon \a index.
     * \throw std::out_of_range if position is < 1 or > size()-1.
     */
    Thread&    get_item_at(position index);


    /**
     * \brief Returns a costant reference to an item contained in the vector.
     * Throws an std::out_of_range standard exception
     * if position is < 1 or > size()-1.
     *
     * \return A constant reference to the item at positon \a index.
     * \throw std::out_of_range if position is < 1 or > size()-1
     */
    const Thread& get_item_at(position index) const;

    /**
     * \brief Add a new ::Thread at the end of the queue.
     *
     * \param schedulable A reference to the appending thread.
     */
    void       append(Thread& schedulable);

    /**
     * \brief Bubble element x to the front of the queue
     */
    void       bubble_to_front(position x);

    /**
     * \brief Remove the first item from the threads vector.
     */
    void       erase_first();

  private:
    /**
     * \brief Threads container definition.
     */
    typedef std::vector<Thread*> Threads;

    /**
     * \brief Threads container member.
     */
    Threads _scheds;
  };

}

#endif

