-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse_Cube.cpp
More file actions
126 lines (115 loc) · 3.4 KB
/
Copy pathSparse_Cube.cpp
File metadata and controls
126 lines (115 loc) · 3.4 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
#ifndef SPARSE_CUBE_CPP
#define SPARSE_CUBE_CPP
#include "../header/Sparse_Cube.h"
template <class type>
Sparse_Cube<type>::Sparse_Cube(int row, int col, int height) : rows(row), cols(col), heights(height)
{
tail = head = new Height_Node<type>(0, 0, -1);
++length;
}
template <class type>
Sparse_Cube<type>::~Sparse_Cube()
{
while (head)
{
Height_Node<type> *cur(head->next);
delete head;
head = cur;
}
head = tail = nullptr;
}
template <class type>
Height_Node<type> *Sparse_Cube<type>::get_height(int height, bool flag)
{
Height_Node<type> *prev_row(head);
while (prev_row->next && prev_row->next->height < height)
prev_row = prev_row->next;
bool found(prev_row->next && (prev_row->next->height == height));
if (found)
return prev_row->next;
if (!flag)
return nullptr;
return add_node_between_node_and_next(prev_row, height);
}
template <class type>
void Sparse_Cube<type>::link(Height_Node<type> *first, Height_Node<type> *second)
{
if (first)
first->next = second;
if (second)
second->prev = first;
}
template <class type>
Height_Node<type> *Sparse_Cube<type>::add_node_between_node_and_next(Height_Node<type> *node_before, int height)
{
Height_Node<type> *middle(new Height_Node<type>(height, rows, cols));
++length;
Height_Node<type> *node_after(node_before->next);
link(node_before, middle);
if (!node_after)
tail = middle;
else
link(middle, node_after);
return middle;
}
template <class type>
void Sparse_Cube<type>::set_value(type data, int row, int col, int height)
{
assert(-1 < row && row < rows);
assert(-1 < col && col < cols);
assert(-1 < height && height < heights);
Height_Node<type> *item(get_height(height, true));
item->matrix.set_value(data, row, col);
}
template <class type>
type Sparse_Cube<type>::get_value(int row, int col, int height)
{
assert(-1 < row && row < rows);
assert(-1 < col && col < cols);
assert(-1 < height && height < heights);
Height_Node<type> *item(get_height(height, false));
if (!item)
return 0;
return item->matrix.get_value(row, col);
}
template <class type>
void Sparse_Cube<type>::add(Sparse_Cube<type> &other)
{
assert(heights == other.heights && rows == other.rows && cols == other.cols);
for (Height_Node<type> *other_cur(other.head->next); other_cur; other_cur = other_cur->next)
{
Height_Node<type> *this_height(get_height(other_cur->height, true));
this_height->matrix.add(other_cur->matrix);
}
}
template <class type>
void Sparse_Cube<type>::print_cube()
{
std::cout << edl << "Print Cube: " << rows << " x " << cols << " x " << heights << edl;
Height_Node<type> *cur(head->next);
for (int i(0); i < heights; ++i)
{
if (cur && cur->height == i)
{
cur->matrix.print_matrix();
cur = cur->next;
}
else
{
for (int j(0); j < rows; ++j)
{
for (int k(0); k < cols; ++k)
std::cout << "0 ";
std::cout << edl;
}
}
}
}
template <class type>
void Sparse_Cube<type>::print_cube_nonzero()
{
std::cout << edl << "Print Cube: " << rows << " x " << cols << " x " << heights << edl;
for (Height_Node<type> *cur(head->next); cur; cur = cur->next)
cur->matrix.print_matrix_nonzero();
}
#endif