/*
 * Copyright (c) 2015, University of Kaiserslautern
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors:
 *    Janik Schlemminger
 *    Robert Gernhardt
 *    Matthias Jung
 */


//// * PARBS.cpp
//// *
//// *  Created on: Apr 9, 2014
//// *      Author: robert
//// */

//#include "PARBS.h"
//#include "../core/configuration/Configuration.h"
//#include "../../common/dramExtension.h"
//#include "map"
//#include "ThreadLoad.h"
//#include <algorithm>

//namespace scheduler {

//using namespace std;

//PAR_BS::PAR_BS(ControllerCore& controllerCore, bool useExternalBankstates, unsigned int capsize) :
//        controllerCore(controllerCore), useExternalBankstates(useExternalBankstates), capsize(capsize)
//{
//    if (useExternalBankstates)
//    {
//        batch = new FR_FCFS(controllerCore, true, false);
//        buffer = new FR_FCFS(controllerCore, true, false);
//    }
//    else
//    {
//        batch = new FR_FCFS(controllerCore, true, false);
//        buffer = new FR_FCFS(controllerCore, true, false);
//    }
//}

//PAR_BS::~PAR_BS()
//{

//}

//bool PAR_BS::hasPayloads()
//{
//    return batch->hasPayloads() || buffer->hasPayloads();
//}

//void PAR_BS::schedule(gp* payload)
//{
//    printDebugMessage("hello!");
//    buffer->schedule(payload);
//}

//gp* PAR_BS::getNextPayload()
//{
//    if (!batch->hasPayloads())
//    {
//        stringstream s;
//        s << "In batch: " << batch->getNumberOfQueuedPayloads() << "\t" << "in buffer: " << buffer->getNumberOfQueuedPayloads() << endl;
//        formBatch();
//        s<< "Formed new batch" << endl;
//        s << "In batch: " << batch->getNumberOfQueuedPayloads() << "\t" << "in buffer: " << buffer->getNumberOfQueuedPayloads() << endl;
//        printDebugMessage(s.str());
//        sc_assert(batch->hasPayloads());
//    }

//    gp* result = batch->getNextPayload();
//    if(result == NULL)
//        result = buffer->getNextPayload();
//    return result;
//}

//void PAR_BS::removePayload(gp* payload)
//{
//    buffer->removePayload(payload);
//    batch->removePayload(payload);

//    if (!useExternalBankstates)
//    {
//        DramExtension& extension = DramExtension::getExtension(payload);
//        internalBankstates.openRowInRowBuffer(extension.getBank(), extension.getRow());
//    }

//}

//void PAR_BS::formBatch()
//{
//    map<Thread, ThreadLoad> loads;

//    for (Bank bank : controllerCore.getBanks())
//    {
//        for (unsigned int i = 0; i < capsize; i++)
//        {
//            gp* payload = buffer->popOldest(bank);
//            if(payload == NULL)
//                break;
//            loads[DramExtension::getExtension(payload).getThread()].addTransaction(payload);
//        }
//    }

//    vector<ThreadLoad*> sortedLoads;
//    for (auto& threadLoadPair : loads)
//    {
//        sortedLoads.push_back(&threadLoadPair.second);
//    }

//    sort(sortedLoads.begin(), sortedLoads.end(), LoadPointerComparer());

//    for (auto& load : sortedLoads)
//    {
//        batch->schedule(load->getTransactions());
//    }

//}

//}
