-
Notifications
You must be signed in to change notification settings - Fork 0
/
DegreeArray.cpp
77 lines (63 loc) · 1.6 KB
/
DegreeArray.cpp
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
#include "DegreeArray.h"
using namespace std;
#define ROUND(x)((size_t)(x+0.5))
DegreeArray::DegreeArray(const float *degreeWeight,size_t V,float maxWeight)
:m_nVertex(V){
size_t i,d;
top = ROUND(maxWeight);
indexmap = new list<size_t>::iterator[V];// map from vertex index to round off degree
m_pDegree = new list<size_t>[top+1];
for (i=0;i<V;i++)
{
d = ROUND(degreeWeight[i]);
list<size_t>& l = m_pDegree[d];
l.push_front(i);
indexmap[i] = l.begin();
}
}
DegreeArray::~DegreeArray(void)
{
delete[] m_pDegree; // what's wrong?
delete[] indexmap;
}
size_t DegreeArray::getMax(){
return m_pDegree[top].front();
}
size_t DegreeArray::extractMax()
{
if (empty())
{
cerr << "cannot extract elements from an empty degree array."<<endl;
exit(1);
}
size_t head = m_pDegree[top].front();
m_pDegree[top].pop_front();
m_nVertex--;
while (m_pDegree[top].size() == 0 && top > 0 ){
top --;
}
return head;
}
void DegreeArray::decrease(size_t inx,float oValue,float nValue)
{
size_t oldvalue = ROUND(oValue),
newvalue = ROUND(nValue);
if (oldvalue == newvalue)return;
list<size_t>::iterator ptr = indexmap[inx];
m_pDegree[oldvalue].erase(ptr);
m_pDegree[newvalue].push_front(inx);
indexmap[inx] = m_pDegree[newvalue].begin();
while (m_pDegree[top].size() == 0 && top > 0 ){
top --;
}
}
void DegreeArray::remove(size_t inx,float value)
{
size_t v = ROUND(value);
list<size_t>::iterator ptr = indexmap[inx];
m_pDegree[v].erase(ptr);
m_nVertex --;
while (m_pDegree[top].size() == 0 && top > 0 ){
top --;
}
}