void MovementMap::SortVector(std::vector<Rect> collidingTiles) { Rect aux; for (int j = collidingTiles.size(); j > 0; j--) for (int i = 0; i < j-1; i++) if (!SortCompare(collidingTiles[i], collidingTiles[i+1])) { aux = collidingTiles[i]; collidingTiles[i] = collidingTiles[i+1]; collidingTiles[i+1] = aux; } }
Private bool compare_values( short int mode, bool dismiss) { /* Stack: |=============================|=============================| | BEFORE | AFTER | |=============================|=============================| top | Arg 2 | Result: 1 = TRUE, 0 = FALSE | |-----------------------------|-----------------------------| | Arg 1 | | |=============================|=============================| */ DESCRIPTOR * arg1; DESCRIPTOR * arg2; STRING_CHUNK * str1; long int len1; short int bytes1; char * p1; STRING_CHUNK * str2; long int len2; short int bytes2; char * p2; short int diff; bool eq = FALSE; /* TRUE if arg1 = arg2 */ bool gt = FALSE; /* TRUE if arg1 > arg2 */ short int n; register long int v1; register long int v2; bool nocase; arg2 = e_stack - 1; k_get_value(arg2); arg1 = e_stack - 2; k_get_value(arg1); recompare: switch(arg1->type) { case INTEGER: switch(arg2->type) { case INTEGER: v1 = arg1->data.value; v2 = arg2->data.value; if (!(eq = (v1 == v2))) { gt = (v1 > v2); } break; case FLOATNUM: if (!(eq = (fabs(((double)arg1->data.value) - arg2->data.float_value) <= pcfg.fltdiff))) { gt = (((double)arg1->data.value) > arg2->data.float_value); } break; case SUBR: k_get_string(arg2); /* **** FALL THROUGH **** */ case STRING: if (arg2->data.str.saddr == NULL) /* Special case, int vs null */ { gt = TRUE; goto exit_compare_values; } if (k_str_to_num(arg2)) goto recompare; /* Convert arg1 e-stack item to a string and compare as strings */ k_num_to_str(arg1); goto string_comparison; default: k_value_error(arg2); } break; case FLOATNUM: switch(arg2->type) { case INTEGER: if (!(eq = (fabs(arg1->data.float_value - ((double)arg2->data.value)) <= pcfg.fltdiff))) { gt = (arg1->data.float_value > ((double)arg2->data.value)); } break; case FLOATNUM: if (!(eq = (fabs(arg1->data.float_value - arg2->data.float_value) <= pcfg.fltdiff))) { gt = (arg1->data.float_value > arg2->data.float_value); } break; case SUBR: k_get_string(arg2); /* **** FALL THROUGH **** */ case STRING: if (arg2->data.str.saddr == NULL) /* Special case, float vs null */ { gt = TRUE; goto exit_compare_values; } if (k_str_to_num(arg2)) goto recompare; /* Convert arg1 to a string and compare as strings */ k_num_to_str(arg1); goto string_comparison; default: k_value_error(arg2); } break; case SUBR: k_get_string(arg1); /* **** FALL THROUGH **** */ case STRING: switch(arg2->type) { case INTEGER: case FLOATNUM: if (arg1->data.str.saddr == NULL) /* Special case, null vs num */ { goto exit_compare_values; } if (k_str_to_num(arg1)) goto recompare; /* Convert arg2 to a string and compare as strings */ k_num_to_str(arg2); goto string_comparison; case SUBR: k_get_string(arg2); /* **** FALL THROUGH **** */ case STRING: /* If one but not both strings is null, they do not match. If both are null, they match without any further work. */ if (arg1->data.str.saddr == NULL) { if (arg2->data.str.saddr == NULL) { eq = TRUE; } goto exit_compare_values; } else if (arg2->data.str.saddr == NULL) { gt = TRUE; goto exit_compare_values; } /* Both items are strings. See if they can BOTH be converted to numeric before we do anything else */ if (k_is_num(arg1) && k_is_num(arg2)) { k_str_to_num(arg1); k_str_to_num(arg2); goto recompare; } string_comparison: /* **** YUCK! Jumping into a switch statement */ str1 = arg1->data.str.saddr; str2 = arg2->data.str.saddr; if (str1 == str2) { /* Both point to same string */ eq = TRUE; } else if (str1 == NULL) { /* arg1 is a null string. arg2 is not */ eq = FALSE; /* gt = FALSE; already done */ } else if (str2 == NULL) { /* arg2 is a null string. arg1 is not */ eq = FALSE; gt = TRUE; } else /* Neither string is null */ { len1 = str1->string_len; bytes1 = str1->bytes; p1 = str1->data; len2 = str2->string_len; bytes2 = str2->bytes; p2 = str2->data; nocase = (process.program.flags & HDR_NOCASE) != 0; do { n = min(bytes1, bytes2); diff = SortCompare(p1, p2, n, nocase); if (diff != 0) /* Found a difference */ { eq = FALSE; gt = (diff > 0); goto exit_compare_values; } /* The strings match to the end of the shorter */ p1 += n; bytes1 -= n; len1 -= n; p2 += n; bytes2 -= n; len2 -= n; if (bytes2 == 0) { if (len2 == 0) /* We have hit the end of string 2 */ { if (len1 == 0) /* Perfect match */ { eq = TRUE; } else /* End of string 2 before end of string 1 */ { eq = FALSE; gt = TRUE; } goto exit_compare_values; } /* Find next chunk of string 2 */ str2 = str2->next; p2 = str2->data; bytes2 = str2->bytes; } if (bytes1 == 0) { if (len1 == 0) /* We have hit the end of string 1 */ { /* End of string 1 before end of string 2 */ eq = FALSE; goto exit_compare_values; } str1 = str1->next; p1 = str1->data; bytes1 = str1->bytes; } } while(TRUE); } break; default: k_value_error(arg2); } break; default: k_value_error(arg2); } exit_compare_values: if (dismiss) { k_dismiss(); k_dismiss(); } switch(mode) { case TEST_EQ: return eq; case TEST_GT: return gt; } return gt || eq; /* TEST_GE */ }