Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SWIFTsim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Show more breadcrumbs
SWIFT
SWIFTsim
Commits
6d6bbcb1
Commit
6d6bbcb1
authored
7 years ago
by
Jacob Kegerreis
Browse files
Options
Downloads
Patches
Plain Diff
Return the found index instead of using the pointer
parent
9a6f8a45
No related branches found
No related tags found
1 merge request
!547
Add generic utility function to find a value in an array
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/utilities.h
+6
-7
6 additions, 7 deletions
src/utilities.h
tests/testUtilities.c
+5
-5
5 additions, 5 deletions
tests/testUtilities.c
with
11 additions
and
12 deletions
src/utilities.h
+
6
−
7
View file @
6d6bbcb1
...
@@ -27,12 +27,11 @@
...
@@ -27,12 +27,11 @@
* @param x The value to find
* @param x The value to find
* @param array The array to search
* @param array The array to search
* @param n The length of the array
* @param n The length of the array
* @param index The found index
*
*
*
S
et -1 and n for x below and above the array edge values respectively.
*
R
et
urn
-1 and n for x below and above the array edge values respectively.
*/
*/
INLINE
static
void
find_value_in_monotonic_array
(
INLINE
static
int
find_value_in_monotonic_array
(
const
float
x
,
const
float
*
array
,
const
int
n
,
int
*
index
)
{
const
float
x
,
const
float
*
array
,
const
int
n
)
{
int
index_mid
,
index_low
=
0
,
index_high
=
n
;
int
index_mid
,
index_low
=
0
,
index_high
=
n
;
...
@@ -49,11 +48,11 @@ INLINE static void find_value_in_monotonic_array(
...
@@ -49,11 +48,11 @@ INLINE static void find_value_in_monotonic_array(
// Set index with the found index_low or an error value if outside the array
// Set index with the found index_low or an error value if outside the array
if
(
x
<
array
[
0
])
if
(
x
<
array
[
0
])
*
index
=
-
1
;
return
-
1
;
else
if
(
array
[
n
-
1
]
<=
x
)
else
if
(
array
[
n
-
1
]
<=
x
)
*
index
=
n
;
return
n
;
else
else
*
index
=
index_low
;
return
index_low
;
}
}
#endif
/* SWIFT_UTILITIES_H */
#endif
/* SWIFT_UTILITIES_H */
This diff is collapsed.
Click to expand it.
tests/testUtilities.c
+
5
−
5
View file @
6d6bbcb1
...
@@ -37,28 +37,28 @@ int main() {
...
@@ -37,28 +37,28 @@ int main() {
// Typical value
// Typical value
x
=
42
.
42
f
;
x
=
42
.
42
f
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
index
=
find_value_in_monotonic_array
(
x
,
array
,
n
);
if
(
index
!=
42
)
{
if
(
index
!=
42
)
{
error
(
"Failed with a typical value "
);
error
(
"Failed with a typical value "
);
}
}
// Value on array element
// Value on array element
x
=
33
.
f
;
x
=
33
.
f
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
index
=
find_value_in_monotonic_array
(
x
,
array
,
n
);
if
(
index
!=
33
)
{
if
(
index
!=
33
)
{
error
(
"Failed with an array element "
);
error
(
"Failed with an array element "
);
}
}
// Value below array
// Value below array
x
=
-
123
.
f
;
x
=
-
123
.
f
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
index
=
find_value_in_monotonic_array
(
x
,
array
,
n
);
if
(
index
!=
-
1
)
{
if
(
index
!=
-
1
)
{
error
(
"Failed with a value below the array "
);
error
(
"Failed with a value below the array "
);
}
}
// Value above array
// Value above array
x
=
123
.
f
;
x
=
123
.
f
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
index
=
find_value_in_monotonic_array
(
x
,
array
,
n
);
if
(
index
!=
n
)
{
if
(
index
!=
n
)
{
error
(
"Failed with a value above the array "
);
error
(
"Failed with a value above the array "
);
}
}
...
@@ -66,7 +66,7 @@ int main() {
...
@@ -66,7 +66,7 @@ int main() {
// Array slice with typical value
// Array slice with typical value
x
=
9
.
81
f
;
x
=
9
.
81
f
;
n
=
10
;
n
=
10
;
find_value_in_monotonic_array
(
x
,
array
+
5
,
n
,
&
index
);
index
=
find_value_in_monotonic_array
(
x
,
array
+
5
,
n
);
if
(
index
!=
4
)
{
if
(
index
!=
4
)
{
error
(
"Failed with an array slice "
);
error
(
"Failed with an array slice "
);
}
}
...
...
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