Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prog_kmeans
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
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
boris.stefanov
prog_kmeans
Commits
b440d343
Commit
b440d343
authored
2 years ago
by
Boris Stefanovic
Browse files
Options
Downloads
Patches
Plain Diff
ADD: write clusters to file
parent
35d53fa5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/io.c
+30
-21
30 additions, 21 deletions
src/io.c
src/io.h
+3
-2
3 additions, 2 deletions
src/io.h
with
33 additions
and
23 deletions
src/io.c
+
30
−
21
View file @
b440d343
...
@@ -21,10 +21,10 @@ fpt_t read_fpt(FILE* file) {
...
@@ -21,10 +21,10 @@ fpt_t read_fpt(FILE* file) {
vector_int_t
*
line_to_vector_int
(
char
*
line
,
const
size_t
dim
)
{
vector_int_t
*
line_to_vector_int
(
char
*
line
,
const
size_t
dim
)
{
vector_int_t
*
vector
=
vector_
int_
create
(
dim
);
vector_int_t
*
vector
=
vector_create
_int
(
dim
);
char
*
tgt
=
line
;
char
*
tgt
=
line
;
char
*
token
=
NULL
;
char
*
token
=
NULL
;
for
(
size_t
i
=
0
;
i
<
vector
->
dim
;
++
i
,
tgt
=
NULL
)
{
for
(
size_t
i
=
0
;
i
<
dim
;
++
i
,
tgt
=
NULL
)
{
token
=
strtok
(
tgt
,
","
);
token
=
strtok
(
tgt
,
","
);
// strtol returns 0 if number not read, which is the desired behaviour:
// strtol returns 0 if number not read, which is the desired behaviour:
vector
->
data
[
i
]
=
token
!=
NULL
?
strtol
(
token
,
NULL
,
10
)
:
0
;
vector
->
data
[
i
]
=
token
!=
NULL
?
strtol
(
token
,
NULL
,
10
)
:
0
;
...
@@ -33,10 +33,10 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) {
...
@@ -33,10 +33,10 @@ vector_int_t* line_to_vector_int(char* line, const size_t dim) {
}
}
vector_fpt_t
*
line_to_vector_fpt
(
char
*
line
,
const
size_t
dim
)
{
vector_fpt_t
*
line_to_vector_fpt
(
char
*
line
,
const
size_t
dim
)
{
vector_fpt_t
*
vector
=
vector_
fpt_
create
(
dim
);
vector_fpt_t
*
vector
=
vector_create
_fpt
(
dim
);
char
*
tgt
=
line
;
char
*
tgt
=
line
;
char
*
token
=
NULL
;
char
*
token
=
NULL
;
for
(
size_t
i
=
0
;
i
<
vector
->
dim
;
++
i
,
tgt
=
NULL
)
{
for
(
size_t
i
=
0
;
i
<
dim
;
++
i
,
tgt
=
NULL
)
{
token
=
strtok
(
tgt
,
","
);
token
=
strtok
(
tgt
,
","
);
// strtol returns 0 if number not read, which is the desired behaviour:
// strtol returns 0 if number not read, which is the desired behaviour:
vector
->
data
[
i
]
=
token
!=
NULL
?
strtod
(
token
,
NULL
)
:
0
;
vector
->
data
[
i
]
=
token
!=
NULL
?
strtod
(
token
,
NULL
)
:
0
;
...
@@ -74,25 +74,34 @@ list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim) {
...
@@ -74,25 +74,34 @@ list_points_fpt_t* get_vector_list_fpt(FILE* ifile, const size_t dim) {
}
}
static
int
_point_compare_clusters_int_
(
const
void
*
p1
,
const
void
*
p2
)
{
void
io_write_clusters_to_file_int
(
FILE
*
file
,
cluster_int_t
**
clusters
,
const
size_t
cluster_count
)
{
const
point_int_t
*
point1
=
(
const
point_int_t
*
)
p1
;
for
(
size_t
i
=
0
;
i
<
cluster_count
;
++
i
)
{
const
point_int_t
*
point2
=
(
const
point_int_t
*
)
p2
;
fprintf
(
file
,
"
\n
*
\n
"
);
return
point1
->
cluster
<
point2
->
cluster
?
-
1
:
point1
->
cluster
>
point2
->
cluster
?
1
:
0
;
list_points_node_int_t
*
node
=
clusters
[
i
]
->
points
->
head
;
while
(
node
!=
NULL
)
{
const
vector_int_t
point
=
*
(
node
->
point
);
fprintf
(
file
,
"%ld"
,
point
.
data
[
0
]);
for
(
size_t
p
=
1
;
p
<
point
.
dim
;
++
p
)
{
fprintf
(
file
,
" , %ld"
,
point
.
data
[
p
]);
}
fprintf
(
file
,
"
\n
"
);
node
=
node
->
next
;
}
}
}
}
void
io_write_clusters_to_file_fpt
(
FILE
*
file
,
cluster_fpt_t
**
clusters
,
const
size_t
cluster_count
)
{
void
io_write_clusters_to_file_int
(
FILE
*
file
,
point_int_t
**
points
,
const
size_t
point_count
)
{
for
(
size_t
i
=
0
;
i
<
cluster_count
;
++
i
)
{
qsort
(
points
,
point_count
,
sizeof
(
point_int_t
*
),
_point_compare_clusters_int_
);
// group points by cluster
fprintf
(
file
,
"
\n
*
\n
"
);
vector_int_t
*
current_cluster
=
NULL
;
list_points_node_fpt_t
*
node
=
clusters
[
i
]
->
points
->
head
;
point_int_t
*
current_point
=
NULL
;
while
(
node
!=
NULL
)
{
for
(
size_t
i
=
0
;
i
<
point_count
;
++
i
)
{
const
vector_fpt_t
point
=
*
(
node
->
point
);
current_point
=
points
[
i
];
fprintf
(
file
,
"%lf"
,
point
.
data
[
0
]);
if
(
current_point
->
cluster
!=
current_cluster
)
{
for
(
size_t
p
=
1
;
p
<
point
.
dim
;
++
p
)
{
current_cluster
=
current_point
->
cluster
;
fprintf
(
file
,
" , %lf"
,
point
.
data
[
p
]);
fprintf
(
file
,
"
\n
*
\n
"
);
}
fprintf
(
file
,
"
\n
"
);
node
=
node
->
next
;
}
}
vector_print_to_file_int
(
file
,
current_point
->
vector
);
}
}
}
}
void
io_write_clusters_to_file_fpt
(
FILE
*
file
,
point_fpt_t
**
points
,
const
size_t
point_count
)
{}
This diff is collapsed.
Click to expand it.
src/io.h
+
3
−
2
View file @
b440d343
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
#define PROG_KMEANS_IO_H
#define PROG_KMEANS_IO_H
#include
<stdio.h>
#include
<stdio.h>
#include
"cluster.h"
#include
"common.h"
#include
"common.h"
#include
"linkedlist.h"
#include
"linkedlist.h"
#include
"vector.h"
#include
"vector.h"
...
@@ -22,9 +23,9 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim);
...
@@ -22,9 +23,9 @@ list_points_int_t* get_vector_list_int(FILE* ifile, const size_t dim);
list_points_fpt_t
*
get_vector_list_fpt
(
FILE
*
ifile
,
const
size_t
dim
);
list_points_fpt_t
*
get_vector_list_fpt
(
FILE
*
ifile
,
const
size_t
dim
);
void
io_write_clusters_to_file_int
(
FILE
*
file
,
point
_int_t
**
point
s
,
const
size_t
point
_count
);
void
io_write_clusters_to_file_int
(
FILE
*
file
,
cluster
_int_t
**
cluster
s
,
const
size_t
cluster
_count
);
void
io_write_clusters_to_file_fpt
(
FILE
*
file
,
point
_fpt_t
**
point
s
,
const
size_t
point
_count
);
void
io_write_clusters_to_file_fpt
(
FILE
*
file
,
cluster
_fpt_t
**
cluster
s
,
const
size_t
cluster
_count
);
#endif //PROG_KMEANS_IO_H
#endif //PROG_KMEANS_IO_H
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