Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
scprog-private
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kunze, Eric
scprog-private
Commits
122a8430
Commit
122a8430
authored
3 years ago
by
eric
Browse files
Options
Downloads
Patches
Plain Diff
added access operator
parent
775d88b0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exercise5/test.cc
+69
-15
69 additions, 15 deletions
exercise5/test.cc
with
69 additions
and
15 deletions
exercise5/test.cc
+
69
−
15
View file @
122a8430
#include
<iostream>
#include
<iostream>
#include
<numeric>
#include
<vector>
#include
<vector>
template
<
class
T
>
template
<
class
T
>
...
@@ -11,9 +12,9 @@ public:
...
@@ -11,9 +12,9 @@ public:
using
size_type
=
std
::
size_t
;
using
size_type
=
std
::
size_t
;
private:
private:
std
::
vector
<
value_type
>
values
;
std
::
vector
<
value_type
>
values
_
;
std
::
vector
<
size_type
>
indices
;
std
::
vector
<
size_type
>
indices
_
;
std
::
vector
<
size_type
>
offset
;
std
::
vector
<
size_type
>
offset
_
;
size_type
rows_
;
size_type
rows_
;
size_type
cols_
;
size_type
cols_
;
...
@@ -24,9 +25,17 @@ public:
...
@@ -24,9 +25,17 @@ public:
rows_
=
rows
;
rows_
=
rows
;
cols_
=
cols
;
cols_
=
cols
;
values
=
std
::
vector
<
value_type
>
(
nzpr
*
cols
,
0.0
);
values_
=
std
::
vector
<
value_type
>
(
nzpr
*
cols
,
0.0
);
indices
=
std
::
vector
<
size_type
>
(
nzpr
*
cols
,
0
);
indices_
=
std
::
vector
<
size_type
>
(
nzpr
*
cols
,
0
);
offset
=
std
::
vector
<
size_type
>
(
rows
+
1
);
offset_
=
std
::
vector
<
size_type
>
(
rows
+
1
);
}
CSR
(
std
::
vector
<
value_type
>
values
,
std
::
vector
<
size_type
>
indices
,
std
::
vector
<
size_type
>
offset
)
{
rows_
=
offset
.
size
()
-
1
;
cols_
=
1
;
values_
=
values
;
indices_
=
indices
;
offset_
=
offset
;
}
}
size_type
rows
()
{
size_type
rows
()
{
return
rows_
;
return
rows_
;
...
@@ -35,21 +44,37 @@ public:
...
@@ -35,21 +44,37 @@ public:
size_type
cols
()
{
size_type
cols
()
{
return
cols_
;
return
cols_
;
}
}
value_type
operator
()
(
size_type
const
i
,
size_type
const
j
){
size_type
col_entries_before
=
std
::
accumulate
(
offset_
.
begin
(),
offset_
.
begin
()
+
i
,
0
);
size_type
col_entries
=
offset_
[
i
];
auto
first
=
indices_
.
begin
()
+
sum
;
auto
last
=
first
+
col_entries
;
auto
low
=
std
::
lower_bound
(
first
,
last
,
j
);
if
(
low
!=
last
&&
*
low
==
j
)
return
*
(
values_
.
begin
()
+
std
::
distance
(
indices_
.
begin
(),
low
));
else
return
0.0
;
}
void
set
(
size_type
row
,
size_type
col
,
value_type
val
)
{
void
set
(
size_type
row
,
size_type
col
,
value_type
val
)
{
if
(
values
.
back
()
!=
0.0
)
if
(
values
_
.
back
()
!=
0.0
)
throw
"value cannot be saved, already too many non zero values existing in this row"
;
throw
"value cannot be saved, already too many non zero values existing in this row"
;
size_type
start_index
=
offset
[
row
];
size_type
start_index
=
offset
_
[
row
];
size_type
end_index
;
size_type
end_index
;
if
(
row
<
rows
()){
if
(
row
<
rows
())
{
end_index
=
offset
[
row
+
1
]
-
1
;
end_index
=
offset
_
[
row
+
1
]
-
1
;
}
else
{
}
else
{
end_index
=
indices
.
size
()
-
2
;
end_index
=
indices
_
.
size
()
-
2
;
}
}
size_type
*
start_row
=
&
(
values
[
start_index
]);
size_type
*
start_row
=
&
(
values
_
[
start_index
]);
size_type
*
end_row
=
&
(
values
[
end_index
]);
size_type
*
end_row
=
&
(
values
_
[
end_index
]);
auto
low
=
std
::
lower_bound
(
start_row
,
end_row
,
val
);
auto
low
=
std
::
lower_bound
(
start_row
,
end_row
,
val
);
...
@@ -61,7 +86,7 @@ public:
...
@@ -61,7 +86,7 @@ public:
private
:
private
:
// shifting all entries starting at position pos to the right; the last element is lost
// shifting all entries starting at position pos to the right; the last element is lost
void
shift_r
(
std
::
vector
<
value_type
>
&
v
,
size_type
pos
)
{
void
shift_r
(
std
::
vector
<
value_type
>
&
v
,
size_type
pos
)
{
for
(
int
i
=
v
.
size
()
-
1
;
i
>
pos
&&
i
<
v
.
size
();
--
i
)
{
for
(
size_type
i
=
v
.
size
()
-
1
;
i
>
pos
&&
i
<
v
.
size
();
--
i
)
{
v
[
i
]
=
v
[
i
-
1
];
v
[
i
]
=
v
[
i
-
1
];
}
}
v
[
pos
]
=
0.0
;
v
[
pos
]
=
0.0
;
...
@@ -109,9 +134,38 @@ int main () {
...
@@ -109,9 +134,38 @@ int main () {
double
*
ptr
=
&
(
v
[
3
]);
double
*
ptr
=
&
(
v
[
3
]);
std
::
cout
<<
"v[4] = ptr + 1 = "
<<
*
(
ptr
+
1
);
std
::
cout
<<
"v[4] = ptr + 1 = "
<<
*
(
ptr
+
1
)
<<
std
::
endl
;
// start a sparse matrix
std
::
cout
<<
"
\n
---> start a example..."
<<
std
::
endl
;
std
::
vector
<
double
>
values
{
1.0
,
2.0
,
5.0
,
3.0
,
4.0
};
std
::
vector
<
size_type
>
indices
{
2
,
0
,
1
,
1
,
3
};
std
::
vector
<
size_type
>
offset
{
1
,
2
,
2
};
CSR
<
double
>
bsp
(
values
,
indices
,
offset
);
std
::
vector
<
int
>
vec
{
3
};
std
::
cout
<<
"*vec.begin() = "
<<
*
(
vec
.
begin
())
<<
std
::
endl
;
std
::
cout
<<
"*vec.end() - 1 = "
<<
*
(
vec
.
end
()
-
1
)
<<
std
::
endl
;
std
::
cout
<<
"*vec.begin() + 1 = "
<<
*
(
vec
.
begin
()
+
1
)
<<
std
::
endl
;
std
::
cout
<<
"*offset.rbegin() = "
<<
*
(
offset
.
rbegin
()
+
2
)
<<
std
::
endl
;
std
::
cout
<<
std
::
accumulate
(
offset
.
begin
(),
offset
.
begin
()
+
2
,
0
)
<<
std
::
endl
;
std
::
cout
<<
"bsp(2,3) = "
<<
bsp
(
2
,
3
)
<<
std
::
endl
;
std
::
cout
<<
"
\n
... fertig."
<<
std
::
endl
;
std
::
cout
<<
"
\n
... fertig."
<<
std
::
endl
;
return
1
;
return
1
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment