| 567 | |
| 568 | |
| 569 | static int unbound_search (Table *t, unsigned int j) { |
| 570 | unsigned int i = j; /* i is zero or a present index */ |
| 571 | j++; |
| 572 | /* find `i' and `j' such that i is present and j is not */ |
| 573 | while (!ttisnil(luaH_getnum(t, j))) { |
| 574 | i = j; |
| 575 | j *= 2; |
| 576 | if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ |
| 577 | /* table was built with bad purposes: resort to linear search */ |
| 578 | i = 1; |
| 579 | while (!ttisnil(luaH_getnum(t, i))) i++; |
| 580 | return i - 1; |
| 581 | } |
| 582 | } |
| 583 | /* now do a binary search between them */ |
| 584 | while (j - i > 1) { |
| 585 | unsigned int m = (i+j)/2; |
| 586 | if (ttisnil(luaH_getnum(t, m))) j = m; |
| 587 | else i = m; |
| 588 | } |
| 589 | return i; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /* |
no test coverage detected