Skip to content

Commit

Permalink
Implement binary search for feasibility_set::contains
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Hader committed Oct 7, 2024
1 parent 13bc940 commit 43aa5f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/interval.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ int lp_interval_cmp_lower_bounds(const lp_interval_t* I1, const lp_interval_t* I
/** Compares the upper bounds of the intervals */
int lp_interval_cmp_upper_bounds(const lp_interval_t* I1, const lp_interval_t* I2);

/** Compares an interval I with a value v. Returns 0 if v in I, 1 if v is below I, -1 if v is above I. */
int lp_interval_cmp_value(const lp_interval_t* I, const lp_value_t* v);

/**
* Comparison of intervals based on upper bounds, with additional intersect info.
*/
Expand Down
18 changes: 11 additions & 7 deletions src/interval/interval.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,17 +673,21 @@ int lp_rational_interval_contains_rational(const lp_rational_interval_t* I, cons
return 1;
}

int lp_interval_contains(const lp_interval_t* I, const lp_value_t* v) {
int lp_interval_cmp_value(const lp_interval_t* I, const lp_value_t* v) {
int cmp_a_v = lp_value_cmp(&I->a, v);
if (I->is_point) {
return cmp_a_v == 0;
return cmp_a_v;
}
if (I->a_open && cmp_a_v >= 0) return 0;
if (!I->a_open && cmp_a_v > 0) return 0;
if (I->a_open && cmp_a_v >= 0) return 1;
if (!I->a_open && cmp_a_v > 0) return 1;
int cmp_v_b = lp_value_cmp(v, &I->b);
if (I->b_open && cmp_v_b >= 0) return 0;
if (!I->b_open && cmp_v_b > 0) return 0;
return 1;
if (I->b_open && cmp_v_b >= 0) return -1;
if (!I->b_open && cmp_v_b > 0) return -1;
return 0;
}

int lp_interval_contains(const lp_interval_t* I, const lp_value_t* v) {
return lp_interval_cmp_value(I, v) == 0;
}

int lp_interval_contains_int(const lp_interval_t* I) {
Expand Down
15 changes: 12 additions & 3 deletions src/polynomial/feasibility_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,18 @@ char* lp_feasibility_set_to_string(const lp_feasibility_set_t* set) {
}

int lp_feasibility_set_contains(const lp_feasibility_set_t* set, const lp_value_t* value) {
// TODO: binary search
for (size_t i = 0; i < set->size; ++ i) {
if (lp_interval_contains(set->intervals + i, value)) {
size_t l = 0, r = set->size;
while(r > l) {
size_t m = l + (r - l) / 2;
int cmp = lp_interval_cmp_value(set->intervals + m, value);
if (cmp > 0) {
// v below I
r = m;
} else if (cmp < 0) {
// v above I
l = m + 1;
} else {
// v in I
return 1;
}
}
Expand Down

0 comments on commit 43aa5f5

Please sign in to comment.