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
3572f993
Commit
3572f993
authored
6 years ago
by
Jacob Kegerreis
Browse files
Options
Downloads
Patches
Plain Diff
Do tweaks for merging
parent
9da3668a
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
+20
-54
20 additions, 54 deletions
src/utilities.h
tests/testUtilities.c
+18
-23
18 additions, 23 deletions
tests/testUtilities.c
with
38 additions
and
77 deletions
src/utilities.h
+
20
−
54
View file @
3572f993
...
...
@@ -21,73 +21,39 @@
#define SWIFT_UTILITIES_H
/**
* @brief Search for a value in a monotonic array to find the
index such that
* array[index] < value < array[index + 1]
* @brief Search for a value in a monotonic
ally increasing
array to find the
*
index such that
array[index] < value < array[index + 1]
*
* @param x The value to find
* @param arr The array to search
* @param arr
ay
The array to search
* @param n The length of the array
* @param i The found index
* @param i
ndex
The found index
*
* Set -1 and n for x below and above the array edge values respectively.
*/
INLINE
static
void
find_value_in_monotonic_array
(
float
x
,
float
*
arr
,
int
n
,
int
*
i
)
{
INLINE
static
void
find_value_in_monotonic_array
(
const
float
x
,
const
float
*
array
,
const
int
n
,
int
*
i
ndex
)
{
int
is_incr
=
(
arr
[
n
-
1
]
>
arr
[
0
]);
// Increasing or decreasing?
int
i_mid
,
i_low
=
0
,
i_high
=
n
;
int
index_mid
,
index_low
=
0
,
index_high
=
n
;
// Until arr
[i
_low] < x < arr
[i
_high=i_low+1]
while
(
i_high
-
i_low
>
1
)
{
i_mid
=
(
i_high
+
i_low
)
>>
1
;
// Middle index
// Until arr
ay[index
_low] < x < arr
ay[index
_high=i
ndex
_low+1]
while
(
i
ndex
_high
-
i
ndex
_low
>
1
)
{
i
ndex
_mid
=
(
i
ndex
_high
+
i
ndex
_low
)
/
2
.
f
;
// Middle index
//
If mid < x and increasing or x < mid and decreasing
if
(
(
arr
[
i
_mid
]
<=
x
)
==
is_incr
)
i_low
=
i_mid
;
//
Replace the low or high index with the middle
if
(
arr
ay
[
index
_mid
]
<=
x
)
i
ndex
_low
=
i
ndex
_mid
;
else
i_high
=
i_mid
;
i
ndex
_high
=
i
ndex
_mid
;
}
// Set i with the found i_low
,
or an error value if outside the array
if
(
x
<
arr
[
0
])
*
i
=
-
1
;
else
if
(
arr
[
n
-
1
]
<=
x
)
*
i
=
n
;
// Set i
ndex
with the found i
ndex
_low or an error value if outside the array
if
(
x
<
arr
ay
[
0
])
*
i
ndex
=
-
1
;
else
if
(
arr
ay
[
n
-
1
]
<=
x
)
*
i
ndex
=
n
;
else
*
i
=
i
_low
;
*
i
ndex
=
index
_low
;
}
#endif
/* SWIFT_UTILITIES_H */
This diff is collapsed.
Click to expand it.
tests/testUtilities.c
+
18
−
23
View file @
3572f993
...
...
@@ -26,54 +26,49 @@
int
main
()
{
/// Test find_value_in_monotonic_array()
int
n
=
100
;
float
arr
[
n
];
int
i
;
float
arr
ay
[
n
];
int
i
ndex
;
float
x
;
// Initialise test array
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
arr
[
j
]
=
j
;
arr
ay
[
j
]
=
j
;
}
// Typical value
x
=
42
.
42
f
;
find_value_in_monotonic_array
(
x
,
arr
,
n
,
&
i
);
if
(
i
!=
42
)
{
printf
(
"Failed with a typical value
\n
"
);
return
1
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
if
(
index
!=
42
)
{
error
(
"Failed with a typical value "
);
}
// Value on array element
x
=
33
.
f
;
find_value_in_monotonic_array
(
x
,
arr
,
n
,
&
i
);
if
(
i
!=
33
)
{
printf
(
"Failed with an array element
\n
"
);
return
1
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
if
(
index
!=
33
)
{
error
(
"Failed with an array element "
);
}
// Value below array
x
=
-
123
.
f
;
find_value_in_monotonic_array
(
x
,
arr
,
n
,
&
i
);
if
(
i
!=
-
1
)
{
printf
(
"Failed with a value below the array
\n
"
);
return
1
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
if
(
index
!=
-
1
)
{
error
(
"Failed with a value below the array "
);
}
// Value above array
x
=
123
.
f
;
find_value_in_monotonic_array
(
x
,
arr
,
n
,
&
i
);
if
(
i
!=
n
)
{
printf
(
"Failed with a value above the array
\n
"
);
return
1
;
find_value_in_monotonic_array
(
x
,
array
,
n
,
&
index
);
if
(
index
!=
n
)
{
error
(
"Failed with a value above the array "
);
}
// Array slice with typical value
x
=
9
.
81
f
;
n
=
10
;
find_value_in_monotonic_array
(
x
,
arr
+
5
,
n
,
&
i
);
if
(
i
!=
4
)
{
printf
(
"Failed with an array slice
\n
"
);
return
1
;
find_value_in_monotonic_array
(
x
,
array
+
5
,
n
,
&
index
);
if
(
index
!=
4
)
{
error
(
"Failed with an array slice "
);
}
return
0
;
...
...
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