mirror of
https://github.com/ascii-boxes/boxes.git
synced 2024-12-04 22:11:07 +01:00
Refactor is_blankward() in shape.c
This commit is contained in:
parent
0eed7422f6
commit
30950e7f72
@ -384,8 +384,8 @@ static size_t find_horizontal_shape(design_t *current_design, comparison_t comp_
|
||||
}
|
||||
|
||||
uint32_t *shape_relevant = prepare_comp_shape(current_design, hshape, j, comp_type,
|
||||
is_blank_leftward(current_design, hshape, j),
|
||||
is_blank_rightward(current_design, hshape, j));
|
||||
is_blankward(current_design, hshape, j, 1),
|
||||
is_blankward(current_design, hshape, j, 0));
|
||||
size_t length_relevant = u32_strlen(shape_relevant);
|
||||
|
||||
for (size_t k = 0; k < current_design->shape[hshape].height; ++k) {
|
||||
|
80
src/shape.c
80
src/shape.c
@ -176,6 +176,8 @@ void freeshape(sentry_t *shape)
|
||||
}
|
||||
BFREE (shape->chars);
|
||||
BFREE (shape->mbcs);
|
||||
BFREE (shape->blank_leftward);
|
||||
BFREE (shape->blank_rightward);
|
||||
|
||||
*shape = SENTRY_INITIALIZER;
|
||||
}
|
||||
@ -398,28 +400,16 @@ static int *new_blankward_cache(const size_t shape_height)
|
||||
|
||||
|
||||
|
||||
int is_blank_leftward(design_t *current_design, const shape_t shape, const size_t shape_line_idx)
|
||||
static int is_blankward_calc(
|
||||
design_t *current_design, const shape_t shape, const size_t shape_line_idx, const int is_leftward)
|
||||
{
|
||||
if (current_design == NULL) {
|
||||
return 0; /* this would be a bug */
|
||||
}
|
||||
sentry_t *shape_data = current_design->shape + shape;
|
||||
if (shape_line_idx >= shape_data->height) {
|
||||
return 0; /* this would be a bug */
|
||||
}
|
||||
if (shape_data->blank_leftward != NULL && shape_data->blank_leftward[shape_line_idx] >= 0) {
|
||||
return shape_data->blank_leftward[shape_line_idx]; /* cached value available */
|
||||
}
|
||||
if (shape_data->blank_leftward == NULL) {
|
||||
shape_data->blank_leftward = new_blankward_cache(shape_data->height);
|
||||
}
|
||||
|
||||
int result = -1;
|
||||
if (is_west(shape_data, 1)) {
|
||||
result = 1;
|
||||
sentry_t *shape_data = current_design->shape + shape;
|
||||
if (is_west(shape_data, is_leftward ? 1 : 0)) {
|
||||
result = is_leftward ? 1 : 0;
|
||||
}
|
||||
else if (is_east(shape_data, 0)) {
|
||||
result = 0;
|
||||
else if (is_east(shape_data, is_leftward ? 0 : 1)) {
|
||||
result = is_leftward ? 0 : 1;
|
||||
}
|
||||
else {
|
||||
shape_t *side = north_side;
|
||||
@ -429,7 +419,13 @@ int is_blank_leftward(design_t *current_design, const shape_t shape, const size_
|
||||
pos = find_in_south(shape);
|
||||
}
|
||||
result = 1;
|
||||
for (size_t i = 0; i < (size_t) pos; i++) {
|
||||
size_t loop_init = (size_t) pos + 1;
|
||||
size_t loop_end = SHAPES_PER_SIDE;
|
||||
if (is_leftward) {
|
||||
loop_init = 0;
|
||||
loop_end = (size_t) pos;
|
||||
}
|
||||
for (size_t i = loop_init; i < loop_end; i++) {
|
||||
sentry_t *tshape = current_design->shape + side[i];
|
||||
if (tshape->mbcs != NULL && !bxs_is_blank(tshape->mbcs[shape_line_idx])) {
|
||||
result = 0;
|
||||
@ -437,14 +433,12 @@ int is_blank_leftward(design_t *current_design, const shape_t shape, const size_
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shape_data->blank_leftward[shape_line_idx] = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// TODO HERE is_blank_rightward() and is_blank_leftward() are nearly identical -> consolidate
|
||||
int is_blank_rightward(design_t *current_design, const shape_t shape, const size_t shape_line_idx)
|
||||
|
||||
int is_blankward(design_t *current_design, const shape_t shape, const size_t shape_line_idx, const int is_leftward)
|
||||
{
|
||||
if (current_design == NULL) {
|
||||
return 0; /* this would be a bug */
|
||||
@ -453,38 +447,22 @@ int is_blank_rightward(design_t *current_design, const shape_t shape, const size
|
||||
if (shape_line_idx >= shape_data->height) {
|
||||
return 0; /* this would be a bug */
|
||||
}
|
||||
if (shape_data->blank_rightward != NULL && shape_data->blank_rightward[shape_line_idx] >= 0) {
|
||||
return shape_data->blank_rightward[shape_line_idx]; /* cached value available */
|
||||
int *blankward_cache = is_leftward ? shape_data->blank_leftward : shape_data->blank_rightward;
|
||||
if (blankward_cache != NULL && blankward_cache[shape_line_idx] >= 0) {
|
||||
return blankward_cache[shape_line_idx]; /* cached value available */
|
||||
}
|
||||
if (shape_data->blank_rightward == NULL) {
|
||||
shape_data->blank_rightward = new_blankward_cache(shape_data->height);
|
||||
}
|
||||
|
||||
int result = -1;
|
||||
if (is_west(shape_data, 0)) {
|
||||
result = 0;
|
||||
}
|
||||
else if (is_east(shape_data, 1)) {
|
||||
result = 1;
|
||||
}
|
||||
else {
|
||||
shape_t *side = north_side;
|
||||
int pos = find_in_north(shape);
|
||||
if (pos < 0) {
|
||||
side = south_side_rev;
|
||||
pos = find_in_south(shape);
|
||||
if (blankward_cache == NULL) {
|
||||
blankward_cache = new_blankward_cache(shape_data->height);
|
||||
if (is_leftward) {
|
||||
shape_data->blank_leftward = blankward_cache;
|
||||
}
|
||||
result = 1;
|
||||
for (size_t i = (size_t) pos + 1; i < SHAPES_PER_SIDE; i++) {
|
||||
sentry_t *tshape = current_design->shape + side[i];
|
||||
if (tshape->mbcs != NULL && !bxs_is_blank(tshape->mbcs[shape_line_idx])) {
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
shape_data->blank_rightward = blankward_cache;
|
||||
}
|
||||
}
|
||||
|
||||
shape_data->blank_rightward[shape_line_idx] = result;
|
||||
int result = is_blankward_calc(current_design, shape, shape_line_idx, is_leftward);
|
||||
blankward_cache[shape_line_idx] = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
22
src/shape.h
22
src/shape.h
@ -54,24 +54,16 @@ int empty_side (sentry_t *sarr, const int aside);
|
||||
|
||||
|
||||
/**
|
||||
* Determine if there are only blanks to the left of this shape on the given line. The result is cached in the shape.
|
||||
* Determine if there are only blanks to the left (or right) of this shape on the given line. The result is cached in
|
||||
* the shape.
|
||||
* @param current_design the design whose shapes to use
|
||||
* @param shape the shape for which to calculate "blank_leftward"
|
||||
* @param shape the shape for which to calculate the result
|
||||
* @param shape_line_idx the index of the shape line to assess
|
||||
* @return 1 if blank leftward, 0 otherwise. Will always return 1 if shape is part of the left (west) box side, and
|
||||
* always 0 if shape is an east side shape (not a corner)
|
||||
* @param is_leftward 1 to check on the left, 0 to check on the right
|
||||
* @return 1 if blank, 0 otherwise. Box contents is always counted as "not blank" (because it could be), and the outside
|
||||
* of a box counts as blank.
|
||||
*/
|
||||
int is_blank_leftward(design_t *current_design, const shape_t shape, const size_t shape_line_idx);
|
||||
|
||||
/**
|
||||
* Determine if there are only blanks to the right of this shape on the given line. The result is cached in the shape.
|
||||
* @param current_design the design whose shapes to use
|
||||
* @param shape the shape for which to calculate "blank_rightward"
|
||||
* @param shape_line_idx the index of the shape line to assess
|
||||
* @return 1 if blank rightward, 0 otherwise. Will always return 1 if shape is part of the right (east) box side, and
|
||||
* always 0 if shape is a west side shape (not a corner)
|
||||
*/
|
||||
int is_blank_rightward(design_t *current_design, const shape_t shape, const size_t shape_line_idx);
|
||||
int is_blankward(design_t *current_design, const shape_t shape, const size_t shape_line_idx, const int is_leftward);
|
||||
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user