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
7c6c9e78
Commit
7c6c9e78
authored
5 years ago
by
Bruce Cowan
Browse files
Options
Downloads
Patches
Plain Diff
Add array_foreach
parent
b6e8fe8d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2639
passed
5 years ago
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/types/array.c
+17
-1
17 additions, 1 deletion
lib/types/array.c
lib/types/array.h
+5
-2
5 additions, 2 deletions
lib/types/array.h
src/array-length.c
+14
-1
14 additions, 1 deletion
src/array-length.c
with
36 additions
and
4 deletions
lib/types/array.c
+
17
−
1
View file @
7c6c9e78
/*
* SPDX-FileCopyrightText: 2018 Bruce Cowan <bruce@bcowan.me.uk>
* SPDX-FileCopyrightText: 2018
, 2019
Bruce Cowan <bruce@bcowan.me.uk>
*
* SPDX-License-Identifier: Apache-2.0
*/
...
...
@@ -296,3 +296,19 @@ array_sort (Array *array,
{
qsort
(
*
array
->
data
,
array
->
length
,
sizeof
(
void
*
),
func
);
}
/**
* array_foreach:
* @array: An array
* @func: A function to run on each element of the array
*
* Run a function on each element of the array
*/
void
array_foreach
(
Array
*
array
,
ForEachFunc
func
,
void
*
user_data
)
{
for
(
size_t
i
=
0
;
i
<
array
->
length
;
i
++
)
(
*
func
)
(
array
->
data
[
i
],
user_data
);
}
This diff is collapsed.
Click to expand it.
lib/types/array.h
+
5
−
2
View file @
7c6c9e78
...
...
@@ -35,5 +35,8 @@ size_t array_get_length (const Array *array);
size_t
array_get_capacity
(
const
Array
*
array
);
// Misc
void
array_sort
(
Array
*
array
,
CompareFunc
func
);
void
array_sort
(
Array
*
array
,
CompareFunc
func
);
void
array_foreach
(
Array
*
array
,
ForEachFunc
func
,
void
*
user_data
);
This diff is collapsed.
Click to expand it.
src/array-length.c
+
14
−
1
View file @
7c6c9e78
...
...
@@ -4,16 +4,29 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include
<inttypes.h>
#include
<stdint.h>
#include
<stdio.h>
#include
<array.h>
static
void
print_element
(
void
*
data
,
void
*
user_data
)
{
printf
(
"%"
PRIdPTR
" "
,
(
intptr_t
)
data
);
}
static
void
print_details
(
Array
*
arr
)
{
size_t
len
=
array_get_length
(
arr
);
size_t
capacity
=
array_get_capacity
(
arr
);
printf
(
"Array length %zu, Array capacity %zu
\n
"
,
len
,
capacity
);
printf
(
"Data is: "
);
array_foreach
(
arr
,
print_element
,
NULL
);
printf
(
"
\n
"
);
}
int
...
...
@@ -24,7 +37,7 @@ main (void)
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
array_add
(
arr
,
NULL
);
array_add
(
arr
,
(
const
void
*
)
(
intptr_t
)
i
);
print_details
(
arr
);
}
...
...
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