Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
progseq-hashtable
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
dario.genga
progseq-hashtable
Commits
ae801549
Commit
ae801549
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Patches
Plain Diff
Add hashtable methods
parent
8aec952c
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
hashtable.c
+124
-9
124 additions, 9 deletions
hashtable.c
main.c
+31
-0
31 additions, 0 deletions
main.c
with
155 additions
and
9 deletions
hashtable.c
+
124
−
9
View file @
ae801549
...
...
@@ -8,41 +8,156 @@
#include
<stdlib.h>
#include
<string.h>
int
hash
(
char
*
key
,
int64_t
length
)
{
// If the size of the int is too low the hash methods will generate overflow errors
int64_t
val
=
0
;
for
(
size_t
i
=
0
;
i
<
strlen
(
key
);
i
++
)
{
val
=
43
*
val
+
key
[
i
];
}
return
(
val
%
length
);
}
entry
*
entry_create_empty
()
{
entry
*
e
=
malloc
(
sizeof
(
entry
));
e
->
key
=
NULL
;
e
->
value
=
NULL
;
e
->
next
=
NULL
;
return
e
;
}
hm
*
hm_create
(
unsigned
int
length
)
{
hm
*
hashmap
=
malloc
(
sizeof
(
hm
));
hashmap
->
length
=
length
;
hashmap
->
entries
=
malloc
(
sizeof
(
entry
)
*
length
);
for
(
in
t
i
=
0
;
i
<
length
;
i
++
)
{
hashmap
->
entries
[
i
]
=
malloc
(
sizeof
(
entry
)
);
for
(
size_
t
i
=
0
;
i
<
length
;
i
++
)
{
hashmap
->
entries
[
i
]
=
entry_create_empty
(
);
}
return
hashmap
;
}
entry
*
get_entry_by_key
(
entry
*
e
,
char
*
key
)
{
if
(
e
->
key
==
key
)
{
return
e
;
}
else
if
(
e
->
next
!=
NULL
)
{
return
get_entry_by_key
(
e
->
next
,
key
);
}
else
{
return
e
;
}
}
void
hm_destroy
(
hm
**
hm
)
{
for
(
int
i
=
0
;
i
<
(
*
hm
)
->
length
;
i
++
)
{
free
((
*
hm
)
->
entries
[
i
]);
entry
*
e
=
(
*
hm
)
->
entries
[
i
];
while
(
e
!=
NULL
)
{
entry
*
to_free
=
e
;
e
=
e
->
next
;
free
(
to_free
);
}
}
free
((
*
hm
)
->
entries
);
free
((
*
hm
));
(
*
hm
)
=
NULL
;
}
hm
*
hm_set
(
hm
*
hm
,
const
char
*
const
key
,
const
char
*
const
value
)
{
return
NULL
;
// Hash the key and get the corresponding entry
int
key_index
=
hash
((
char
*
)
key
,
hm
->
length
);
entry
*
e
=
get_entry_by_key
(
hm
->
entries
[
key_index
],
(
char
*
)
key
);
// Set the key and create the next entry if the key is not existing
if
(
e
->
key
!=
key
)
{
e
->
key
=
(
char
*
)
key
;
if
(
e
->
next
==
NULL
)
{
e
->
next
=
entry_create_empty
();
}
}
// Set the entry value and return the hashmap
e
->
value
=
(
char
*
)
value
;
return
hm
;
}
char
*
hm_get
(
const
hm
*
const
hm
,
const
char
*
const
key
)
{
return
NULL
;
int
key_index
=
hash
((
char
*
)
key
,
hm
->
length
);
char
*
c
=
get_entry_by_key
(
hm
->
entries
[
key_index
],
(
char
*
)
key
)
->
value
;
return
c
;
}
char
*
hm_rm
(
hm
*
hm
,
const
char
*
const
key
)
{
return
NULL
;
int
key_index
=
hash
((
char
*
)
key
,
hm
->
length
);
entry
*
e
=
get_entry_by_key
(
hm
->
entries
[
key_index
],
(
char
*
)
key
);
entry
*
previous_entry
=
NULL
;
entry
*
current_entry
=
hm
->
entries
[
key_index
];
int
entry_index
=
0
;
while
(
current_entry
!=
e
)
{
previous_entry
=
current_entry
;
current_entry
=
current_entry
->
next
;
entry_index
++
;
}
if
(
entry_index
==
0
)
{
hm
->
entries
[
key_index
]
=
hm
->
entries
[
key_index
]
->
next
;
}
else
if
(
previous_entry
!=
NULL
)
{
previous_entry
->
next
=
e
->
next
;
}
char
*
entry_key
=
e
->
key
;
free
(
e
);
e
=
NULL
;
return
entry_key
;
}
int
get_entry_list_size
(
entry
*
e
)
{
int
size
=
0
;
size
+=
sizeof
(
e
->
key
)
+
sizeof
(
e
->
value
)
+
16
;
if
(
e
->
next
!=
NULL
)
{
size
+=
get_entry_list_size
(
e
->
next
);
}
return
size
;
}
char
*
hmo_str
(
const
hm
*
const
hm
)
{
return
NULL
;
char
*
hm_to_str
(
const
hm
*
const
hm
)
{
int
str_size
=
sizeof
(
hm
)
*
hm
->
length
;
for
(
int
i
=
0
;
i
<
hm
->
length
;
i
++
)
{
str_size
+=
get_entry_list_size
(
hm
->
entries
[
i
]);
}
char
*
hm_str
=
malloc
(
str_size
);
// Ensure that the string is empty before doing anything
hm_str
[
0
]
=
'\0'
;
strcat
(
hm_str
,
"[
\n
"
);
for
(
int
i
=
0
;
i
<
hm
->
length
;
i
++
)
{
strcat
(
hm_str
,
" [
\n
"
);
entry
*
e
=
hm
->
entries
[
i
];
while
(
e
!=
NULL
&&
e
->
key
!=
NULL
)
{
strcat
(
hm_str
,
" [
\"
"
);
strcat
(
hm_str
,
e
->
key
);
strcat
(
hm_str
,
"
\"
:
\"
"
);
strcat
(
hm_str
,
e
->
value
);
strcat
(
hm_str
,
"
\"
]"
);
if
(
e
->
next
!=
NULL
&&
e
->
next
->
key
!=
NULL
)
{
strcat
(
hm_str
,
","
);
}
strcat
(
hm_str
,
"
\n
"
);
e
=
e
->
next
;
}
strcat
(
hm_str
,
" ]"
);
if
(
i
<
hm
->
length
-
1
)
{
strcat
(
hm_str
,
","
);
}
strcat
(
hm_str
,
"
\n
"
);
}
strcat
(
hm_str
,
"]
\n
"
);
return
hm_str
;
}
void
hm_print
(
const
hm
*
const
hm
)
{
return
NULL
;
char
*
str
=
hm_to_str
(
hm
);
printf
(
"%s
\n
"
,
str
);
free
(
str
);
}
This diff is collapsed.
Click to expand it.
main.c
+
31
−
0
View file @
ae801549
...
...
@@ -9,6 +9,37 @@
int
main
()
{
hm
*
hashmap
=
hm_create
(
10
);
hm_set
(
hashmap
,
"Key1"
,
"Value1"
);
hm_set
(
hashmap
,
"Key2"
,
"Value1"
);
hm_set
(
hashmap
,
"Key1"
,
"Value2"
);
hm_set
(
hashmap
,
"Key3"
,
"Value1"
);
hm_set
(
hashmap
,
"Key4"
,
"FOUR"
);
hm_set
(
hashmap
,
"Key1"
,
"Value3"
);
hm_set
(
hashmap
,
"Key5"
,
"I think it's good now"
);
hm_set
(
hashmap
,
"Key6"
,
"6"
);
hm_set
(
hashmap
,
"Key7"
,
"7"
);
hm_set
(
hashmap
,
"Key8"
,
"8"
);
hm_set
(
hashmap
,
"Key9"
,
"9"
);
hm_set
(
hashmap
,
"Key27"
,
"MORE ?"
);
hm_set
(
hashmap
,
"Key10"
,
"Is this the end ?"
);
printf
(
"Value of %s : %s
\n
"
,
"Key1"
,
hm_get
(
hashmap
,
"Key1"
));
printf
(
"Value of %s : %s
\n
"
,
"Key2"
,
hm_get
(
hashmap
,
"Key2"
));
printf
(
"Value of %s : %s
\n
"
,
"Key3"
,
hm_get
(
hashmap
,
"Key3"
));
printf
(
"Value of %s : %s
\n
"
,
"Key4"
,
hm_get
(
hashmap
,
"Key4"
));
printf
(
"Value of %s : %s
\n
"
,
"Key5"
,
hm_get
(
hashmap
,
"Key5"
));
printf
(
"Value of %s : %s
\n
"
,
"Key6"
,
hm_get
(
hashmap
,
"Key6"
));
printf
(
"Value of %s : %s
\n
"
,
"Key7"
,
hm_get
(
hashmap
,
"Key7"
));
printf
(
"Value of %s : %s
\n
"
,
"Key8"
,
hm_get
(
hashmap
,
"Key8"
));
printf
(
"Value of %s : %s
\n
"
,
"Key9"
,
hm_get
(
hashmap
,
"Key9"
));
printf
(
"Value of %s : %s
\n
"
,
"Key10"
,
hm_get
(
hashmap
,
"Key10"
));
hm_print
(
hashmap
);
hm_rm
(
hashmap
,
"Key1"
);
hm_print
(
hashmap
);
hm_rm
(
hashmap
,
"Key10"
);
hm_print
(
hashmap
);
hm_destroy
(
&
hashmap
);
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