Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
c-stuff
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
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bruce Cowan
c-stuff
Commits
36350a57
Commit
36350a57
authored
5 years ago
by
Bruce Cowan
Browse files
Options
Downloads
Patches
Plain Diff
Change hashtable to use arrays rather than linked lists
parent
35ef2eb4
No related branches found
No related tags found
No related merge requests found
Pipeline
#2633
passed
5 years ago
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/types/hashtable.c
+43
-26
43 additions, 26 deletions
lib/types/hashtable.c
lib/types/hashtable.h
+0
-2
0 additions, 2 deletions
lib/types/hashtable.h
with
43 additions
and
28 deletions
lib/types/hashtable.c
+
43
−
26
View file @
36350a57
#include
"array.h"
#include
"hashtable.h"
#include
"mem.h"
...
...
@@ -19,10 +20,20 @@ typedef struct
*/
struct
_HashTable
{
SList
**
array
;
size_t
size
;
Array
**
buckets
;
size_t
size
;
};
static
void
key_value_free
(
void
*
element
)
{
KeyValue
*
kv
=
(
KeyValue
*
)
element
;
free
(
kv
->
key
);
free
(
kv
->
value
);
free
(
kv
);
}
/**
* hash_table_new:
* @size: Number of buckets to use.
...
...
@@ -32,23 +43,23 @@ struct _HashTable
HashTable
*
hash_table_new
(
size_t
size
)
{
HashTable
*
new
=
malloc
(
sizeof
(
HashTable
));
new
->
array
=
calloc
(
size
,
sizeof
(
SList
*
));
new
->
size
=
size
;
HashTable
*
new
=
malloc
(
sizeof
(
HashTable
));
new
->
buckets
=
calloc
(
size
,
sizeof
(
Array
*
));
new
->
size
=
size
;
return
new
;
return
new
;
}
static
unsigned
strhash
(
const
char
*
string
)
{
unsigned
h
=
5381
;
char
c
;
unsigned
h
=
5381
;
char
c
;
while
((
c
=
*
string
++
))
h
=
(
h
<<
5
)
+
h
+
c
;
while
((
c
=
*
string
++
))
h
=
(
h
<<
5
)
+
h
+
c
;
return
h
;
return
h
;
}
/**
...
...
@@ -66,14 +77,17 @@ hash_table_insert (HashTable *table,
const
char
*
key
,
const
char
*
value
)
{
unsigned
index
=
strhash
(
key
)
%
table
->
size
;
unsigned
index
=
strhash
(
key
)
%
table
->
size
;
KeyValue
*
kv
=
check_malloc
(
sizeof
(
KeyValue
));
kv
->
key
=
strdup
(
key
);
kv
->
value
=
strdup
(
value
);
SList
*
list
=
slist_prepend
(
table
->
array
[
index
],
kv
);
table
->
array
[
index
]
=
list
;
// Only create array if needed
if
(
!
table
->
buckets
[
index
])
table
->
buckets
[
index
]
=
array_new
(
key_value_free
);
array_add
(
table
->
buckets
[
index
],
kv
);
}
/**
...
...
@@ -86,16 +100,19 @@ hash_table_insert (HashTable *table,
void
hash_table_print_all
(
HashTable
*
table
)
{
for
(
size_t
i
=
0
;
i
<
table
->
size
;
i
++
)
{
SList
*
bucket
=
table
->
array
[
i
];
if
(
bucket
==
NULL
)
continue
;
for
(
SList
*
list
=
bucket
;
list
;
list
=
list
->
next
)
{
KeyValue
*
kv
=
list
->
data
;
printf
(
"%s:%s
\n
"
,
kv
->
key
,
kv
->
value
);
}
}
for
(
size_t
i
=
0
;
i
<
table
->
size
;
i
++
)
{
Array
*
bucket
=
table
->
buckets
[
i
];
if
(
!
bucket
)
continue
;
size_t
length
=
array_get_length
(
bucket
);
for
(
size_t
j
=
0
;
j
<
length
;
j
++
)
{
KeyValue
*
kv
=
(
KeyValue
*
)
array_get
(
bucket
,
j
);
printf
(
"%s:%s
\n
"
,
kv
->
key
,
kv
->
value
);
}
}
}
This diff is collapsed.
Click to expand it.
lib/types/hashtable.h
+
0
−
2
View file @
36350a57
...
...
@@ -2,8 +2,6 @@
#include
<stddef.h>
#include
"slist.h"
typedef
struct
_HashTable
HashTable
;
HashTable
*
hash_table_new
(
size_t
size
);
...
...
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