一个vector简单实例:Round-Robin时间片轮转

#include <iostream>
#include<vector>
#include <stdexcept>
using namespace std;

template <typename T>
class RoundRobin
{
    public:
        //Client can give a hint as to the number of expected elements for increased efficiency
        RoundRobin(int numExpected = 0);
        virtual ~RoundRobin();
        //Appends element to the end of the list
        void add(const T& elem);
        //Removes the first (and only the first) element
        //in the list that is equal (with operator==) to elem.
        void remove(const T& elem);
        //Returns the next element in the list, starting with the first,
        //and cycling back to the first when end of the list is
        //reached, taking into account elements that are added or removed.
        T& getNext();
    protected:
        std::vector<T> mElems;
        typename std::vector<T>::iterator mCurElem;
    private:
        //Prevent assignment and pass-by-value.
        RoundRobin(const RoundRobin& src);
        RoundRobin& operator=(const RoundRobin& rhs);
};
template <typename T>
RoundRobin<T>::RoundRobin(int numExpected)
{
    //if the client gave a guideline, reserve that much space.
    mElems.reserve(numExpected);
    mCurElem = mElems.begin();
}

template <typename T>
RoundRobin<T>::~RoundRobin()
{
    //nothing
}

template <typename T>
void RoundRobin<T>::add(const T& elem)
{
    //the vector could reallocate and invalidate the iterator with the push_back call.
    //take advantage of the random access iterator features to save our spot
    int pos = mCurElem - mElems.begin();
    //add
    mElems.push_back(elem);
    //Reset out iterator to make sure it's valid
    mCurElem = mElems.begin() + pos;
}

template <typename T>
void RoundRobin<T>::remove(const T& elem)
{
    typename std::vector<T>::iterator it;
    for (it = mElems.begin(); it != mElems.end(); ++it) {
        if(*it == elem) {
            //Removing an element will invalidate our mCurElem iterator if
            //it refers to an element past the point of the removal
            int newPos;
            //if current iterator is before or at the one we're removing,
            //the new position is the same as before.
            if(mCurElem <= it) {
                newPos = mCurElem - mElems.begin();
            } else {
                //otherwise, it's one less than before
                newPos = mCurElem - mElems.begin() - 1;
            }
            //erase the element and ignore the return value
            mElems.erase(it);
            //reset iterator to make sure it's valid
            mCurElem = mElems.begin() + newPos;
            //if we were pointing to the last element and it was removed
            //we need to loop back to the first
            if (mCurElem == mElems.end()) {
                mCurElem = mElems.begin();
            }
            return;
        }
    }
}

template <typename T>
T& RoundRobin<T>::getNext()
{
    //First, make sure there are any elements.
    if (mElems.empty()) {
        throw std::out_of_range("No elements in the list");
    }
    //retrieve a reference to return
    T& retVal = *mCurElem;
    ++mCurElem;
    if (mCurElem == mElems.end()){
        mCurElem = mElems.begin();
    }
    //return the reference
    return retVal;
}


template <typename T>
RoundRobin<T>::RoundRobin(const RoundRobin& src)
{
    typename std::vector<T>::iterator it;
    for (it = src.mElems.begin(); it != src.mElems.end(); ++it) {
        mElems.push_back(*it);
    }
    int pos = src.mCurElem - src.mElems.begin();
    mCurElem = mElems.begin() + pos;
}

template <typename T>
RoundRobin<T>& RoundRobin<T>::operator=(const RoundRobin& rhs)
{
    typename std::vector<T>::iterator it;
    for (it = rhs.mElems.begin(); it != rhs.mElems.end(); ++it) {
        mElems.push_back(*it);
    }
    int pos = rhs.mCurElem - rhs.mElems.begin();
    mCurElem = mElems.begin() + pos;
}

//simple process class
class Process
{
    public:
        Process(const string& name) : mName(name) {}
        void doWorkDuringTimeSlice() {
            cout<< "Process "<< mName
            << "performing work during time slice."<<endl;
        }
        //Needed for the RoundRobin::remove method to work.
        bool operator==(const Process& rhs) {
            return mName ==rhs.mName;
        }
    protected:
        string mName;
};

//simple round-robin based process scheduler.
class Scheduler
{
    public:
        //Constructo takes a vector of processes.
        Scheduler(const vector<Process>& processes);
        void scheduleTimeSlice();
        void removeProcess(const Process& process);
    protected:
        RoundRobin<Process> rr;
};
Scheduler::Scheduler(const vector<Process>& processes) {
    /* must be const_iterator when vector is const */
    for (vector<Process>::const_iterator iter = processes.begin();
    iter != processes.end(); ++iter) {
        rr.add(*iter);
    }
}
void Scheduler::scheduleTimeSlice(){
    try{
        rr.getNext().doWorkDuringTimeSlice();
    } catch ( const std::out_of_range& ) {
        cerr << "No more processes to schedule."<<endl;
    }
}
void Scheduler::removeProcess(const Process& process){
    rr.remove(process);
}
int main()
{
    vector<Process> processes;//= {Process("1"),Process("2"),Process("3")};
    processes.push_back(Process("1"));
    processes.push_back(Process("2"));
    processes.push_back(Process("3"));
    Scheduler sched(processes);
    for(int i=0;i<4;++i)
        sched.scheduleTimeSlice();
    sched.removeProcess(processes[1]);
    for(int i=0;i<4;++i)
        sched.scheduleTimeSlice();
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值