MCPcopy Create free account
hub / github.com/SakuraEngine/SakuraEngine / luaH_getn

Function luaH_getn

xrepo/packages/l/lua/port/lua/src/ltable.c:921–966  ·  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

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

Callers 2

luaV_objlenFunction · 0.70
lua_UnsignedFunction · 0.70

Calls 6

ispow2realasizeFunction · 0.85
binsearchFunction · 0.85
luaH_realasizeFunction · 0.85
luaH_getintFunction · 0.85
hash_searchFunction · 0.85
castFunction · 0.50

Tested by

no test coverage detected