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
2926de72
Commit
2926de72
authored
2 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Validation exercice 2
parent
1b6ed1ef
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
+99
-8
99 additions, 8 deletions
ex2/ex2.c
with
99 additions
and
8 deletions
ex2/ex2.c
+
99
−
8
View file @
2926de72
...
...
@@ -9,13 +9,14 @@
*
*/
#include
<math.h>
#include
<stdbool.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
#define KEY_MAXIMUM_LENGTH 4
#define HASH_MAP_LENGTH 10
#define VECTOR_DEFAULT_CAPACITY 100
typedef
struct
Entry
{
char
*
key
;
double
value
;
...
...
@@ -32,6 +33,92 @@ typedef struct HashMap {
Vector
**
entries
;
}
HashMap
;
/**
* @brief Hashes a string.
*
* @param key
* @param length
* @return int
*/
int
hash
(
char
key
[
4
],
int
length
);
/**
* @brief Initializes an entry.
*
* @param key
* @param value
* @return Entry*
*/
Entry
*
entry_init
(
char
*
key
,
double
value
);
/**
* @brief Frees the memory of the entry.
*
* @param e
*/
void
entry_destroy
(
Entry
*
e
);
/**
* @brief Initializes an vector.
*
* @param capacity
* @return Vector*
*/
Vector
*
vector_init
(
int
capacity
);
/**
* @brief Frees the memory of the vector.
*
* @param e
*/
void
vector_destroy
(
Vector
*
v
);
/**
* @brief Adds an element at the end of the vector.
*
* @param v
* @param e
*/
void
vector_push
(
Vector
*
v
,
Entry
*
e
);
/**
* @brief Displays the vector.
*
* @param v
*/
void
vector_print
(
Vector
*
v
);
/**
* @brief Initializes an hash map.
*
* @param length
* @return HashMap*
*/
HashMap
*
hash_map_init
(
int
length
);
/**
* @brief Frees the memory of the hash map.
*
* @param dict
*/
void
hash_map_destroy
(
HashMap
*
dict
);
/**
* @brief Inserts the value in the hash map.
*
* @param dict
* @param key
* @param value
*/
void
hash_map_insert
(
HashMap
*
dict
,
char
*
key
,
double
value
);
/**
* @brief Displays the hash map.
*
* @param dict
*/
void
hash_map_print
(
HashMap
*
dict
);
int
hash
(
char
key
[
4
],
int
length
)
{
int
index
=
0
;
for
(
int
i
=
0
;
i
<
4
;
++
i
)
{
...
...
@@ -42,12 +129,15 @@ int hash(char key[4], int length) {
Entry
*
entry_init
(
char
*
key
,
double
value
)
{
Entry
*
e
=
(
Entry
*
)
malloc
(
sizeof
(
Entry
));
e
->
key
=
key
;
e
->
key
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
KEY_MAXIMUM_LENGTH
+
1
));
e
->
key
[
0
]
=
'\0'
;
strcpy
(
e
->
key
,
key
);
e
->
value
=
value
;
return
e
;
}
void
entry_destroy
(
Entry
*
e
)
{
free
(
e
->
key
);
free
(
e
);
}
...
...
@@ -89,7 +179,7 @@ HashMap *hash_map_init(int length) {
dict
->
entries
=
(
Vector
**
)
malloc
(
sizeof
(
Vector
*
)
*
dict
->
length
);
for
(
int
i
=
0
;
i
<
length
;
i
+=
1
)
{
dict
->
entries
[
i
]
=
vector_init
(
100
);
dict
->
entries
[
i
]
=
vector_init
(
VECTOR_DEFAULT_CAPACITY
);
}
return
dict
;
...
...
@@ -115,7 +205,6 @@ void hash_map_insert(HashMap *dict, char *key, double value) {
}
}
// string copy
Entry
*
e
=
entry_init
(
key
,
value
);
vector_push
(
v
,
e
);
}
...
...
@@ -129,7 +218,8 @@ void hash_map_print(HashMap *dict) {
}
int
main
()
{
HashMap
*
dict
=
hash_map_init
(
10
);
HashMap
*
dict
=
hash_map_init
(
HASH_MAP_LENGTH
);
hash_map_insert
(
dict
,
"ajku"
,
1
.
1
);
hash_map_insert
(
dict
,
"ajkv"
,
2
.
2
);
hash_map_insert
(
dict
,
"ajkw"
,
3
.
1
);
...
...
@@ -142,10 +232,11 @@ int main() {
hash_map_insert
(
dict
,
"bjkd"
,
1
.
9
);
hash_map_print
(
dict
);
printf
(
"
\n
"
);
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
);
...
...
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