** Try to find a boundary in table 't'. (A 'boundary' is an integer index ** such that t[i] is present and t[i+1] is absent, or 0 if t[1] is absent ** and 'maxinteger' if t[maxinteger] is present.) ** (In the next explanation, we use Lua indices, that is, with base 1. ** The code itself uses base 0 when indexing the array part of the table.) ** The code starts with 'limit = t->alimit', a position
| 863 | ** therefore cannot be used as a new limit.) |
| 864 | */ |
| 865 | lua_Unsigned luaH_getn (Table *t) { |
| 866 | unsigned int limit = t->alimit; |
| 867 | if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ |
| 868 | /* there must be a boundary before 'limit' */ |
| 869 | if (limit >= 2 && !isempty(&t->array[limit - 2])) { |
| 870 | /* 'limit - 1' is a boundary; can it be a new limit? */ |
| 871 | if (ispow2realasize(t) && !ispow2(limit - 1)) { |
| 872 | t->alimit = limit - 1; |
| 873 | setnorealasize(t); /* now 'alimit' is not the real size */ |
| 874 | } |
| 875 | return limit - 1; |
| 876 | } |
| 877 | else { /* must search for a boundary in [0, limit] */ |
| 878 | unsigned int boundary = binsearch(t->array, 0, limit); |
| 879 | /* can this boundary represent the real size of the array? */ |
| 880 | if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { |
| 881 | t->alimit = boundary; /* use it as the new limit */ |
| 882 | setnorealasize(t); |
| 883 | } |
| 884 | return boundary; |
| 885 | } |
| 886 | } |
| 887 | /* 'limit' is zero or present in table */ |
| 888 | if (!limitequalsasize(t)) { /* (2)? */ |
| 889 | /* 'limit' > 0 and array has more elements after 'limit' */ |
| 890 | if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ |
| 891 | return limit; /* this is the boundary */ |
| 892 | /* else, try last element in the array */ |
| 893 | limit = luaH_realasize(t); |
| 894 | if (isempty(&t->array[limit - 1])) { /* empty? */ |
| 895 | /* there must be a boundary in the array after old limit, |
| 896 | and it must be a valid new limit */ |
| 897 | unsigned int boundary = binsearch(t->array, t->alimit, limit); |
| 898 | t->alimit = boundary; |
| 899 | return boundary; |
| 900 | } |
| 901 | /* else, new limit is present in the table; check the hash part */ |
| 902 | } |
| 903 | /* (3) 'limit' is the last element and either is zero or present in table */ |
| 904 | lua_assert(limit == luaH_realasize(t) && |
| 905 | (limit == 0 || !isempty(&t->array[limit - 1]))); |
| 906 | if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) |
| 907 | return limit; /* 'limit + 1' is absent */ |
| 908 | else /* 'limit + 1' is also present */ |
| 909 | return hash_search(t, limit); |
| 910 | } |
| 911 | |
| 912 | |
| 913 |
no test coverage detected