MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / luaH_getn

Function luaH_getn

3rd/lua-5.4.3/src/ltable.c:912–957  ·  view source on GitHub ↗

** 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

Source from the content-addressed store, hash-verified

910** therefore cannot be used as a new limit.)
911*/
912lua_Unsigned luaH_getn (Table *t) {
913 unsigned int limit = t->alimit;
914 if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
915 /* there must be a boundary before 'limit' */
916 if (limit >= 2 && !isempty(&t->array[limit - 2])) {
917 /* 'limit - 1' is a boundary; can it be a new limit? */
918 if (ispow2realasize(t) && !ispow2(limit - 1)) {
919 t->alimit = limit - 1;
920 setnorealasize(t); /* now 'alimit' is not the real size */
921 }
922 return limit - 1;
923 }
924 else { /* must search for a boundary in [0, limit] */
925 unsigned int boundary = binsearch(t->array, 0, limit);
926 /* can this boundary represent the real size of the array? */
927 if (ispow2realasize(t) && boundary > luaH_realasize(t) / 2) {
928 t->alimit = boundary; /* use it as the new limit */
929 setnorealasize(t);
930 }
931 return boundary;
932 }
933 }
934 /* 'limit' is zero or present in table */
935 if (!limitequalsasize(t)) { /* (2)? */
936 /* 'limit' > 0 and array has more elements after 'limit' */
937 if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
938 return limit; /* this is the boundary */
939 /* else, try last element in the array */
940 limit = luaH_realasize(t);
941 if (isempty(&t->array[limit - 1])) { /* empty? */
942 /* there must be a boundary in the array after old limit,
943 and it must be a valid new limit */
944 unsigned int boundary = binsearch(t->array, t->alimit, limit);
945 t->alimit = boundary;
946 return boundary;
947 }
948 /* else, new limit is present in the table; check the hash part */
949 }
950 /* (3) 'limit' is the last element and either is zero or present in table */
951 lua_assert(limit == luaH_realasize(t) &&
952 (limit == 0 || !isempty(&t->array[limit - 1])));
953 if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
954 return limit; /* 'limit + 1' is absent */
955 else /* 'limit + 1' is also present */
956 return hash_search(t, limit);
957}
958
959
960

Callers 2

luaV_objlenFunction · 0.85
lua_UnsignedFunction · 0.85

Calls 5

ispow2realasizeFunction · 0.85
binsearchFunction · 0.85
luaH_realasizeFunction · 0.85
luaH_getintFunction · 0.85
hash_searchFunction · 0.85

Tested by

no test coverage detected