Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SWIFT
SWIFTsim
Commits
3572f993
Commit
3572f993
authored
May 21, 2018
by
Jacob Kegerreis
Browse files
Do tweaks for merging
parent
9da3668a
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/utilities.h
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 */
tests/testUtilities.c
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
;
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment