1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#ifndef JOBCREATE
#define JOBCREATE
#include <iostream>
#include <vector>
using namespace std;
class JobCreate {
public:
JobCreate();
JobCreate( const int &JobID, const int & excecuted_t, const int & total_t );
bool compare ( JobCreate jobElse);
int _JobID;
int _excecuted_t;
int _total_t;
void print();
/*private:
JobCreate& operator= ( const JobCreate& );*/
};
inline JobCreate::JobCreate()
:_JobID(0), _excecuted_t(0), _total_t(0){}
inline
JobCreate::
JobCreate (const int &JobID, const int & excecuted_t, const int & total_t )
: _JobID(JobID), _excecuted_t(excecuted_t), _total_t(total_t)
{}
inline
bool JobCreate::
compare ( JobCreate jobElse){
return this->_JobID - jobElse._JobID;
}
inline void
JobCreate::print()
{
cout << "(" <<this->_JobID << ","
<< this->_excecuted_t << ","
<< this->_total_t << ")"
<< endl;
}
#endif
#include "JobCreate.h"
#include <iostream>
#include <vector>
using namespace std;
class MinHeap{
public:
MinHeap( int max )
:_max_size(max), _size( 0 ){
Heap.reserve(_max_size);
//Heap[_size] = NULL;
}
//~MinHeap() {delete Heap;};
void insert( const JobCreate &newJob )
{
_size++;
Heap[_size] = newJob;
int curr = _size;
while (parent_exits(curr)&&
Heap[curr]._excecuted_t < Heap[parent(curr)]._excecuted_t)
{
swap ( curr, parent(curr));
curr = parent(curr);
}
}
JobCreate removemin(){
swap(1, _size);
_size--;
if (_size != 0) pushdown(1);
return Heap[_size + 1];
}
int capacity() {return Heap.capacity();}
void hp(const int iter) { return Heap[iter].print();}
int const size() { return _size; }
private:
int _max_size;
int _size;
std::vector<JobCreate> Heap;
int lchild ( int pos ) {return 2 * pos;}
int rchild ( int pos ) {return 2 * pos + 1;}
int parent ( int pos ) {return pos / 2;}
bool parent_exits ( int pos ) {return pos/2 != 0;}
bool isleaf ( int pos ) {return ((pos > _size/2) && (pos <= _size));}
void swap ( int pos1, int pos2 ){
JobCreate temp;
temp = Heap[ pos1 ];
Heap[ pos1 ] = Heap [ pos2 ];
Heap[ pos2 ] = temp;
//delete temp;
}
void pushdown ( int pos ){
int smallest;
while ( !isleaf(pos) ){
smallest = lchild(pos);
if ((smallest < _size)&&
(Heap[smallest]._excecuted_t>Heap[smallest+1]._excecuted_t))
smallest = smallest + 1;
if (Heap[pos]._excecuted_t <= Heap[smallest]._excecuted_t)
return;
swap ( pos, smallest );
pos = smallest;
}
}
};
int main(){
MinHeap MinHP(1000);
JobCreate job1(1, 2, 33);
JobCreate job2(3, 3, 33);
JobCreate job3(12, 4, 33);
JobCreate job4(15, 6, 33);
JobCreate job5(17, 8, 33);
JobCreate job6(19, 10, 33);
JobCreate job7(14, 31, 33);
JobCreate job8(44, 22, 33);
JobCreate job9(55, 21, 33);
JobCreate job10(34, 27, 33);
MinHP.insert(job10);
MinHP.insert(job9);
MinHP.insert(job8);
MinHP.insert(job7);
MinHP.insert(job6);
MinHP.insert(job5);
MinHP.insert(job4);
MinHP.insert(job3);
MinHP.insert(job2);
MinHP.insert(job1);
cout << MinHP.size() << endl;
int iter = 0;
while (iter < MinHP.size())
{MinHP.hp(iter);
iter ++;}
}
|