Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
controle-c-003
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
florian.burgener
controle-c-003
Commits
e8ff9adc
Commit
e8ff9adc
authored
2 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Exercice 2
parent
81fccf8d
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
ex2/ex2.c
+132
-24
132 additions, 24 deletions
ex2/ex2.c
with
132 additions
and
24 deletions
ex2/ex2.c
+
132
−
24
View file @
e8ff9adc
...
...
@@ -16,30 +16,138 @@
#include
<stdlib.h>
#include
<string.h>
typedef
struct
Entry
{
char
*
key
;
double
value
;
}
Entry
;
typedef
struct
Vector
{
int
capacity
;
int
length
;
Entry
**
items
;
}
Vector
;
typedef
struct
HashMap
{
int
length
;
Vector
**
entries
;
}
HashMap
;
int
hash
(
char
key
[
4
],
int
length
)
{
int
index
=
0
;
for
(
int
i
=
0
;
i
<
4
;
++
i
)
{
index
=
(
43
*
index
+
key
[
i
])
%
length
;
}
return
index
;
}
Entry
*
entry_init
(
char
*
key
,
double
value
)
{
Entry
*
e
=
(
Entry
*
)
malloc
(
sizeof
(
Entry
));
e
->
key
=
key
;
e
->
value
=
value
;
return
e
;
}
void
entry_destroy
(
Entry
*
e
)
{
free
(
e
);
}
Vector
*
vector_init
(
int
capacity
)
{
Vector
*
v
=
(
Vector
*
)
malloc
(
sizeof
(
Vector
));
v
->
capacity
=
capacity
;
v
->
length
=
0
;
v
->
items
=
(
Entry
**
)
malloc
(
sizeof
(
Entry
*
)
*
v
->
capacity
);
return
v
;
}
void
vector_destroy
(
Vector
*
v
)
{
for
(
int
i
=
0
;
i
<
v
->
length
;
i
+=
1
)
{
entry_destroy
(
v
->
items
[
i
]);
}
free
(
v
->
items
);
free
(
v
);
}
void
vector_push
(
Vector
*
v
,
Entry
*
e
)
{
v
->
items
[
v
->
length
]
=
e
;
v
->
length
+=
1
;
}
void
vector_print
(
Vector
*
v
)
{
for
(
int
i
=
0
;
i
<
v
->
length
;
i
+=
1
)
{
printf
(
"(%s, %.1lf)"
,
v
->
items
[
i
]
->
key
,
v
->
items
[
i
]
->
value
);
if
(
i
<
v
->
length
-
1
)
{
printf
(
", "
);
}
}
}
HashMap
*
hash_map_init
(
int
length
)
{
HashMap
*
dict
=
(
HashMap
*
)
malloc
(
sizeof
(
HashMap
));
dict
->
length
=
length
;
dict
->
entries
=
(
Vector
**
)
malloc
(
sizeof
(
Vector
*
)
*
dict
->
length
);
for
(
int
i
=
0
;
i
<
length
;
i
+=
1
)
{
dict
->
entries
[
i
]
=
vector_init
(
100
);
}
return
dict
;
}
void
hash_map_destroy
(
HashMap
*
dict
)
{
for
(
int
i
=
0
;
i
<
dict
->
length
;
i
+=
1
)
{
vector_destroy
(
dict
->
entries
[
i
]);
}
free
(
dict
->
entries
);
free
(
dict
);
}
void
hash_map_insert
(
HashMap
*
dict
,
char
*
key
,
double
value
)
{
int
index
=
hash
(
key
,
dict
->
length
);
Vector
*
v
=
dict
->
entries
[
index
];
for
(
int
i
=
0
;
i
<
v
->
length
;
i
+=
1
)
{
if
(
strcmp
(
v
->
items
[
i
]
->
key
,
key
)
==
0
)
{
v
->
items
[
i
]
->
value
=
value
;
return
;
}
}
// string copy
Entry
*
e
=
entry_init
(
key
,
value
);
vector_push
(
v
,
e
);
}
void
hash_map_print
(
HashMap
*
dict
)
{
for
(
int
i
=
0
;
i
<
dict
->
length
;
i
+=
1
)
{
printf
(
"%d: "
,
i
);
vector_print
(
dict
->
entries
[
i
]);
printf
(
"
\n
"
);
}
}
int
main
()
{
// int32_t values_length = 5;
// double values[values_length];
// for (int32_t i = 0; i < values_length; i += 1) {
// double value;
// scanf("%lf", &value);
// values[i] = value;
// }
// int32_t values_length = 5;
// int32_t values[values_length];
// for (int32_t i = 0; i < values_length; i += 1) {
// int32_t value;
// scanf("%d", &value);
// values[i] = value;
// }
// char a[100];
// int32_t b;
// scanf("%s %d", a, &b);
// printf("%s %d\n", a, b);
printf
(
"ex1
\n
"
);
HashMap
*
dict
=
hash_map_init
(
10
);
hash_map_insert
(
dict
,
"ajku"
,
1
.
1
);
hash_map_insert
(
dict
,
"ajkv"
,
2
.
2
);
hash_map_insert
(
dict
,
"ajkw"
,
3
.
1
);
hash_map_insert
(
dict
,
"ajkx"
,
4
.
2
);
hash_map_insert
(
dict
,
"ajky"
,
5
.
7
);
hash_map_insert
(
dict
,
"ajkz"
,
6
.
1
);
hash_map_insert
(
dict
,
"ajka"
,
1
.
8
);
hash_map_insert
(
dict
,
"ajkb"
,
2
.
7
);
hash_map_insert
(
dict
,
"ajkc"
,
3
.
6
);
hash_map_insert
(
dict
,
"bjkd"
,
1
.
9
);
hash_map_print
(
dict
);
hash_map_insert
(
dict
,
"ajkt"
,
0
.
1
);
hash_map_insert
(
dict
,
"ajku"
,
9
.
1
);
hash_map_insert
(
dict
,
"ajkx"
,
2
.
9
);
printf
(
"
\n
"
);
hash_map_print
(
dict
);
hash_map_destroy
(
dict
);
return
EXIT_SUCCESS
;
}
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