-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.cpp
124 lines (117 loc) · 3.03 KB
/
Encoder.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
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
//
// Encoder.cpp
// CSA_Simulator
//
// Created by Erik Sandgren on 28/12/15.
// Copyright © 2015 Erik Sandgren. All rights reserved.
//
#include "Encoder.hpp"
#include <ctime>
#include <cmath>
#include <cstdlib>
using namespace std;
Encoder::Encoder(int n,int n_rx,int d[], double Lambda[], int size)
{
n_=n;
n_rx_=n_rx;
d_=d;
Lambda_=Lambda;
size_ = size;
double cumsum = 0;
cdf_.resize(size_);
for (int i = 0; i < size_; i++)
{
cumsum += Lambda_[i];
cdf_.at(i) = cumsum;
}
srand ( (unsigned int)time(NULL) );
}
// Simply implements a way of randomly picking a repetition rate according to a defined
// degree distribution.
int Encoder::getRepRate()
{
double tmp;
int repRate = 0;
tmp = ((double) rand() / (RAND_MAX));
while(repRate == 0){
for(int i = size_ - 1; i >= 0; i--)
{
if (tmp < cdf_.at(i))
{
repRate = (int) d_[i];
}
}
}
return repRate;
}
// Chooses a degree and CNs for transmission for a VN replicas are placed uniformly in the 'n'
// following CNs
void Encoder::distributeRepsUniform(Node* VN, vector<Node*>*CNs ){
int repRate=getRepRate();
int tmp;
vector<int> returnVec(repRate);
bool invalidPlacement;
Node* CN;
for (int i = 0; i < repRate; i++) {
do {
invalidPlacement = false;
tmp=rand() % n_;
for (int ii = 0; ii < i; ii++) {
if(tmp == returnVec[ii]) {invalidPlacement=true;};
}
} while (invalidPlacement);
returnVec[i] = tmp;
}
// Connects the correct CNs to the VN and the VN to the CNs...
for (int i = 0; i < repRate; i++)
{
CN=CNs->at(returnVec.at(i) + n_rx_);
CN->addNeighbor(VN);
VN->addNeighbor(CN);
}
}
// Chooses a degree and CNs for transmission for a VN (a first replica is placed in the next CN)
void Encoder::distributeRepsFirstSlot(Node* VN, vector<Node*>*CNs)
{
int repRate = getRepRate();
int tmp;
vector<int> returnVec(repRate);
bool invalidPlacement;
Node* CN;
returnVec[0] = 0;
for (int i = 1; i < repRate; i++) {
do {
invalidPlacement = false;
tmp = rand() % (n_ - 1) + 1;
for (int ii = 0; ii <i; ii++) {
if(tmp == returnVec[ii]) {invalidPlacement = true;};
}
} while (invalidPlacement);
returnVec[i] = tmp;
}
// Connects the correct CNs to the VN and the VN to the CNs...
for (int i = 0; i < repRate; i++)
{
CN = CNs->at(returnVec.at(i) + n_rx_);
CN->addNeighbor(VN);
VN->addNeighbor(CN);
}
}
void Encoder::distributeRepsSC(Node* VN,vector<Node*>*CNs )
{
int tmp;
vector<int> returnVec;
Node* CN;
for (int i = 0; i < d_[0]; i++)
{
tmp=(rand() % n_ ) + i * n_;
returnVec.push_back(tmp);
}
// Connects the correct CNs to the VN and the VN to the CNs...
for (int i = 0; i < d_[0]; i++)
{
CN = CNs->at(returnVec.at(i) + n_rx_);
CN->addNeighbor(VN);
VN->addNeighbor(CN);
}
}