** 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
| 936 | ** therefore cannot be used as a new limit.) |
| 937 | */ |
| 938 | lua_Unsigned luaH_getn (Table *t) { |
| 939 | unsigned int limit = t->alimit; |
| 940 | if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */ |
| 941 | /* there must be a boundary before 'limit' */ |
| 942 | if (limit >= 2 && !isempty(&t->array[limit - 2])) { |
| 943 | /* 'limit - 1' is a boundary; can it be a new limit? */ |
| 944 | if (ispow2realasize(t) && !ispow2(limit - 1)) { |
| 945 | t->alimit = limit - 1; |
| 946 | setnorealasize(t); /* now 'alimit' is not the real size */ |
| 947 | } |
| 948 | return limit - 1; |
| 949 | } |
| 950 | else { /* must search for a boundary in [0, limit] */ |
| 951 | unsigned int boundary = binsearch(t->array, 0, limit); |
| 952 | /* can this boundary represent the real size of the array? */ |
| 953 | if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) { |
| 954 | t->alimit = boundary; /* use it as the new limit */ |
| 955 | setnorealasize(t); |
| 956 | } |
| 957 | return boundary; |
| 958 | } |
| 959 | } |
| 960 | /* 'limit' is zero or present in table */ |
| 961 | if (!limitequalsasize(t)) { /* (2)? */ |
| 962 | /* 'limit' > 0 and array has more elements after 'limit' */ |
| 963 | if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ |
| 964 | return limit; /* this is the boundary */ |
| 965 | /* else, try last element in the array */ |
| 966 | limit = luaH_realasize(t); |
| 967 | if (isempty(&t->array[limit - 1])) { /* empty? */ |
| 968 | /* there must be a boundary in the array after old limit, |
| 969 | and it must be a valid new limit */ |
| 970 | unsigned int boundary = binsearch(t->array, t->alimit, limit); |
| 971 | t->alimit = boundary; |
| 972 | return boundary; |
| 973 | } |
| 974 | /* else, new limit is present in the table; check the hash part */ |
| 975 | } |
| 976 | /* (3) 'limit' is the last element and either is zero or present in table */ |
| 977 | lua_assert(limit == luaH_realasize(t) && |
| 978 | (limit == 0 || !isempty(&t->array[limit - 1]))); |
| 979 | if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) |
| 980 | return limit; /* 'limit + 1' is absent */ |
| 981 | else /* 'limit + 1' is also present */ |
| 982 | return hash_search(t, limit); |
| 983 | } |
| 984 | |
| 985 | |
| 986 |
no test coverage detected