forked from uestla/Sparse-Matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-type.h
More file actions
144 lines (96 loc) · 2.84 KB
/
custom-type.h
File metadata and controls
144 lines (96 loc) · 2.84 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
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
/**
* This file is part of the SparseMatrix library
*
* @license MIT
* @author Petr Kessler (https://kesspess.cz)
* @link https://github.com/uestla/Sparse-Matrix
*/
#include <string>
#include <iostream>
#include "../inc/testslib.h"
#include "../inc/SparseMatrixMock.h"
using namespace std;
string joinStrings(const string & a, const string & b)
{
string c = a;
if (a.length() && b.length()) {
c += " ";
}
c += b;
return c;
}
struct person
{
person(void) : firstname(""), lastname("")
{}
person(const person & p) : firstname(p.firstname), lastname(p.lastname)
{}
person(const string & f, const string & l) : firstname(f), lastname(l)
{}
bool operator == (const person & p) const
{
return this->firstname == p.firstname && this->lastname == p.lastname;
}
person operator + (const person & p) const
{
return person(joinStrings(this->firstname, p.firstname), joinStrings(this->lastname, p.lastname));
}
person operator - (const person & p) const
{
return person(p.lastname, this->lastname);
}
person operator * (const person & p) const
{
if (*this == person() || p == person()) {
return person();
}
return person(joinStrings(this->firstname, p.lastname), joinStrings(this->lastname, p.firstname));
}
string firstname, lastname;
};
ostream & operator << (ostream & os, const person & p)
{
os << p.firstname << ", " << p.lastname;
return os;
}
void testElementTypes(void)
{
cout << "custom element types..." << flush;
// addition
SparseMatrix<person> a(4, 5);
a.set(person("John", "Doe"), 3, 2);
SparseMatrix<person> b(4, 5);
b.set(person("Foo", "Bar"), 3, 2);
SparseMatrix<person> sum = a.add(b);
assertEquals<person>(person("John Foo", "Doe Bar"), sum.get(3, 2));
// subtraction
SparseMatrix<person> diff = a.subtract(b);
assertEquals<person>(person("Bar", "Doe"), diff.get(3, 2));
// matrix-matrix multiplication
SparseMatrix<person> c(5, 3);
c.set(person("Foo", "Bar"), 2, 3);
SparseMatrix<person> product = a.multiply(c);
for (int i = 1, rows = product.getRowCount(); i <= rows; i++) {
for (int j = 1, cols = product.getColumnCount(); j <= cols; j++) {
person value = product.get(i, j);
if (i == 3 && j == 3) {
assertEquals<person>(person("John Bar", "Doe Foo"), value);
} else {
assertEquals<person>(person(), value);
}
}
}
// vector-matrix multiplication
vector<person> people;
people.push_back(person("John", "Doe"));
people.push_back(person("Foo", "Bar"));
people.push_back(person("Willy", "Wonka"));
people.push_back(person("Jon", "Snow"));
people.push_back(person("Bridget", "Johnes"));
vector<person> result = a.multiply(people);
assertEquals<person>(person(), result[0]);
assertEquals<person>(person(), result[1]);
assertEquals<person>(person("John Bar", "Doe Foo"), result[2]);
assertEquals<person>(person(), result[3]);
cout << " OK" << endl;
}