-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceMatrix.cpp
More file actions
75 lines (61 loc) · 1.46 KB
/
DistanceMatrix.cpp
File metadata and controls
75 lines (61 loc) · 1.46 KB
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
/*
* File: DistanceMatrix.cpp
* Author: btacklind
*
* Created on May 22, 2015, 1:00 AM
*
* Handles the distance matrix used in defining a symmetric graph
*/
#include "DistanceMatrix.h"
#include <iostream>
#include <cstdlib>
using namespace std;
DistanceMatrix::DistanceMatrix(int p) {
points = p;
//# elements in triangle array
size = (points*points-points)/2;
matrix = (int*)malloc(sizeof(int)*size);
if (matrix == NULL){
cout << "failed to malloc matrix" <<endl;
exit(-1);
}
}
DistanceMatrix::DistanceMatrix(const DistanceMatrix& orig) {
points = orig.points;
size = orig.size;
matrix = (int*)malloc(sizeof(int)*size);
for (int i =0; i < size; i++){
matrix[i] = orig.matrix[i];
}
}
DistanceMatrix::~DistanceMatrix() {
if (matrix != NULL)
free(matrix);
}
/*
* Gets the placement of the distance of a element in the triangle matrix
* compacts the matrix into a linear output. (smaller number always first)
*/
int DistanceMatrix::getPosDMat(int i, int j) {
if (j < i){
int k = j;
j = i;
i = k;
}
return points*(i-1) - (i*i+i)/2 + j - 1;
}
void DistanceMatrix::set(int i, int j, int num) {
matrix[getPosDMat(i,j)]=num;
}
int DistanceMatrix::get(int i, int j) {
if (i==j) return 0;
return matrix[getPosDMat(i, j)];
}
void DistanceMatrix::printMatrix() {
for (int i = 1; i< points; i++){
for (int j = i + 1; j <= points ; j++){
cout<< get(i,j)<<",";
}
cout << endl;
}
}