Use the BSEARCH() macro in overlap placement

Currently the code rolls its own binary search, but now that we have
a well-tested binary search implementation in obt/ we can make use
of that.
This commit is contained in:
Dana Jansens 2013-09-01 15:17:11 -04:00
parent 047a201498
commit e33c070d15

View file

@ -19,7 +19,9 @@
#include "config.h" #include "config.h"
#include "geom.h" #include "geom.h"
#include "place_overlap.h" #include "place_overlap.h"
#include "obt/bsearch.h"
#include <glib.h>
#include <stdlib.h> #include <stdlib.h>
static void make_grid(const Rect* client_rects, static void make_grid(const Rect* client_rects,
@ -170,29 +172,23 @@ static int total_overlap(const Rect* client_rects,
return overlap; return overlap;
} }
/* Unfortunately, the libc bsearch() function cannot be used to find the static int find_first_grid_position_greater_or_equal(int search_value,
position of a value that is not in the array, and glib doesn't const int* edges,
provide a binary search function at all. So, tricky as it is, if we int max_edges)
want to avoid linear scan of the edge array, we have to roll our
own. */
static int grid_position(int value,
const int* edges,
int max_edges)
{ {
int low = 0; g_assert(max_edges >= 2);
int high = max_edges - 1; g_assert(search_value >= edges[0]);
int mid = low + (high - low) / 2; g_assert(search_value <= edges[max_edges - 1]);
while (low != mid) {
if (value < edges[mid]) BSEARCH_SETUP();
high = mid; BSEARCH(int, edges, 0, max_edges, search_value);
else if (value > edges[mid])
low = mid; if (BSEARCH_FOUND())
else /* value == edges[mid] */ return BSEARCH_AT();
return mid;
mid = low + (high - low) / 2; g_assert(BSEARCH_FOUND_NEAREST_SMALLER());
} /* Get the nearest larger instead. */
/* we get here when low == mid. can have low == high or low == high - 1 */ return BSEARCH_AT() + 1;
return (value <= edges[low] ? low : high);
} }
static void expand_width(Rect* r, int by) static void expand_width(Rect* r, int by)
@ -263,9 +259,11 @@ static void center_in_field(Point* top_left,
{ {
/* Find minimal rectangle. */ /* Find minimal rectangle. */
int orig_right_edge_index = int orig_right_edge_index =
grid_position(top_left->x + req_size->width, x_edges, max_edges); find_first_grid_position_greater_or_equal(
top_left->x + req_size->width, x_edges, max_edges);
int orig_bottom_edge_index = int orig_bottom_edge_index =
grid_position(top_left->y + req_size->height, y_edges, max_edges); find_first_grid_position_greater_or_equal(
top_left->y + req_size->height, y_edges, max_edges);
ExpandInfo i = { ExpandInfo i = {
.top_left = top_left, .top_left = top_left,
.orig_width = x_edges[orig_right_edge_index] - top_left->x, .orig_width = x_edges[orig_right_edge_index] - top_left->x,