Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Prog_C
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
Model registry
Operate
Environments
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
thibault.chatillo
Prog_C
Commits
254dbefd
Commit
254dbefd
authored
4 years ago
by
thib
Browse files
Options
Downloads
Patches
Plain Diff
everything is working
parent
f04b24c6
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
hashmap/hash.c
+23
-24
23 additions, 24 deletions
hashmap/hash.c
hashmap/hash.h
+2
-1
2 additions, 1 deletion
hashmap/hash.h
hashmap/lst_vec.c
+2
-2
2 additions, 2 deletions
hashmap/lst_vec.c
hashmap/main.c
+13
-6
13 additions, 6 deletions
hashmap/main.c
with
40 additions
and
33 deletions
hashmap/hash.c
+
23
−
24
View file @
254dbefd
#include
"hash.h"
// typedef struct _element {
// void *data;
// struct _element *next;
// } element;
typedef
struct
_entry_t
{
char
*
key
;
char
*
value
;
...
...
@@ -15,7 +10,6 @@ typedef struct _hm_t {
int
length
;
}
hm_t
;
//
int
hash
(
char
*
str
,
int
length
)
{
int
val
=
0
;
int
len
=
strlen
(
str
);
...
...
@@ -59,10 +53,13 @@ void hm_destroy(hm_t **hm) {
hm
=
NULL
;
}
entry_t
*
createEntry
(
char
*
key
,
char
*
value
)
{
entry_t
*
entry
=
malloc
(
sizeof
(
entry_t
));
entry
->
key
=
malloc
(
sizeof
(
char
)
*
(
strlen
(
key
)
+
1
));
entry
->
value
=
malloc
(
sizeof
(
char
)
*
(
strlen
(
key
)
+
1
));
assert
(
entry
->
key
!=
NULL
);
entry
->
value
=
malloc
((
strlen
(
value
)
+
1
));
assert
(
entry
->
value
!=
NULL
);
strcpy
(
entry
->
key
,
key
);
strcpy
(
entry
->
value
,
value
);
return
entry
;
...
...
@@ -77,14 +74,13 @@ bool isSameKey(char *key, void *x) {
hm_t
*
hm_set
(
hm_t
*
hm
,
char
*
key
,
char
*
value
)
{
int
h
=
hash
(
key
,
hm
->
length
);
// printf("h(%s) = %d\n", key, h);
element
*
el
=
find_element
(
&
hm
->
elements
[
h
],
isSameKey
,
key
);
if
(
el
!=
NULL
)
{
// same key -> update val
//
printf("update val
ue
\n");
printf
(
"update val
\n
"
);
strcpy
(((
entry_t
*
)(
el
->
data
))
->
value
,
value
);
}
else
{
// key is unique -> push it
// printf("push\n");
lst_vector_push
(
&
hm
->
elements
[
h
],
(
void
*
)
createEntry
(
key
,
value
));
}
...
...
@@ -99,35 +95,38 @@ char *hm_get(hm_t *hm, char *key) {
if
(
el
==
NULL
)
{
return
"NULL"
;
}
return
((
entry_t
*
)(
el
->
data
))
->
value
;
}
// retire une clé de la hm et la retourne
void
hm_rm
(
hm_t
*
hm
,
char
*
key
)
{
char
*
hm_rm
(
hm_t
*
hm
,
char
*
key
){
element
*
el
=
remove_element
(
&
hm
->
elements
[
hash
(
key
,
hm
->
length
)],
isSameKey
,
key
);
if
(
el
!=
NULL
)
{
printf
(
"%s - %s removed
\n
"
,((
entry_t
*
)(
el
->
data
))
->
key
,((
entry_t
*
)(
el
->
data
))
->
value
);
destroyEntry
((
entry_t
*
)(
el
->
data
));
free
(
el
);
return
;
}
if
(
el
==
NULL
){
printf
(
"trying to remove non existing entry !
\n
"
);
return
"NULL"
;
}
entry_t
*
entry
=
(
entry_t
*
)(
el
->
data
);
char
*
value
=
malloc
(
sizeof
(
char
)
*
(
strlen
(
entry
->
value
)
+
1
));
strcpy
(
value
,
entry
->
value
);
destroyEntry
(
entry
);
free
(
el
);
return
value
;
}
// convertit la hm en chaîne de caractères
// char *hm_to_str(hm_t *hm);
// affiche le contenu de la hm
void
hm_print
(
hm_t
*
hm
)
{
for
(
int
i
=
0
;
i
<
hm
->
length
;
++
i
)
{
int
len
=
lst_vector_length
(
&
hm
->
elements
[
i
]);
printf
(
"
h
[%d]
: "
,
i
);
printf
(
"[%d]: "
,
i
);
for
(
int
j
=
0
;
j
<
len
;
j
++
)
{
element
*
el
=
lst_vector_get
(
&
hm
->
elements
[
i
],
j
);
printf
(
"
%s - %s "
,
((
entry_t
*
)(
el
->
data
))
->
key
,
printf
(
"
(
%s - %s
)
"
,
((
entry_t
*
)(
el
->
data
))
->
key
,
((
entry_t
*
)(
el
->
data
))
->
value
);
}
printf
(
"
\n
"
);
...
...
This diff is collapsed.
Click to expand it.
hashmap/hash.h
+
2
−
1
View file @
254dbefd
...
...
@@ -22,8 +22,9 @@ void hm_destroy(hm_t **hm);
hm_t
*
hm_set
(
hm_t
*
hm
,
char
*
key
,
char
*
value
);
// retourne la valeur associé à la clé, key
char
*
hm_get
(
hm_t
*
hm
,
char
*
key
);
// retire une clé de la hm et la retourne
void
hm_rm
(
hm_t
*
hm
,
char
*
key
);
char
*
hm_rm
(
hm_t
*
hm
,
char
*
key
);
// affiche le contenu de la hm
void
hm_print
(
hm_t
*
hm
);
...
...
This diff is collapsed.
Click to expand it.
hashmap/lst_vec.c
+
2
−
2
View file @
254dbefd
...
...
@@ -189,14 +189,14 @@ element *remove_element(lst_vector *v, bool (*f)(char *, void *), char *key) {
if
(
f
(
key
,
current
->
data
))
{
if
(
loops
==
0
){
return
lst_vector_pop
(
v
);
return
lst_vector_pop
(
v
);
//first
}
printf
(
"found it
\n
"
);
prev
->
next
=
current
->
next
;
return
current
;
}
prev
=
current
;
current
=
current
->
next
;
loops
++
;
}
return
NULL
;
}
...
...
This diff is collapsed.
Click to expand it.
hashmap/main.c
+
13
−
6
View file @
254dbefd
...
...
@@ -5,17 +5,24 @@
int
main
()
{
hm_t
*
hm
=
hm_create
(
20
);
hm_t
*
hm
=
hm_create
(
7
);
for
(
int
i
=
0
;
i
<
1
00
;
i
++
){
char
str1
[
1
0
];
char
str2
[
1
0
];
for
(
int
i
=
0
;
i
<
1
5
;
i
++
){
char
str1
[
8
0
];
char
str2
[
8
0
];
sprintf
(
str1
,
"%d"
,
i
);
sprintf
(
str2
,
"%d"
,
i
);
sprintf
(
str2
,
"%d"
,
i
*
10
);
hm_set
(
hm
,
str1
,
str2
);
// hm_rm(hm,str1);
}
hm_print
(
hm
);
printf
(
"
\n
get (1 - %s)
\n
"
,
hm_get
(
hm
,
"1"
));
printf
(
"get (8 - %s)
\n\n
"
,
hm_get
(
hm
,
"8"
));
char
*
value
=
hm_rm
(
hm
,
"9"
);
printf
(
"9 - %s removed
\n
"
,
value
);
free
(
value
);
hm_print
(
hm
);
hm_destroy
(
&
hm
);
}
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