// src/backend/resource.cc - 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 2 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, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "config.h"
#include "gettext.h"

#include <sgpemv2/ready_queue.hh>

using sgpem::ReadyQueue;

void
ReadyQueue::swap(position a, position b)
  throw (std::out_of_range)
{
  if (a == b) return;

  // Usage of "at()" isn't casual:
  // at() checks indexes, "[]" doesn't.
  // Once we've done the check once, we
  // can assume it's safe to use "[]";
  // this for performance reasons.
  Thread* temp = _scheds.at(a);
  _scheds[a] = _scheds.at(b);
  _scheds[b] = temp;
}


ReadyQueue::size_t
ReadyQueue::size() const
{
  return _scheds.size();
}


sgpem::Thread&
ReadyQueue::get_item_at(position index)
  throw (std::out_of_range)
{
  // Checks index access
  return *_scheds.at(index);
}

const sgpem::Thread&
ReadyQueue::get_item_at(position index) const
  throw (std::out_of_range)
{
  // Checks index access
  return *_scheds.at(index);
}

void
ReadyQueue::append(Thread& thread)
{
  _scheds.push_back(&thread);
}


void 
ReadyQueue::bubble_to_front(position x)
  throw(std::out_of_range)
{
  while(x > 0)
  {
    swap(x - 1, x);
    --x;
  }
}

void
ReadyQueue::erase_first()
{
  _scheds.erase(_scheds.begin());
}

