aboutsummaryrefslogtreecommitdiff
path: root/ltable.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-10-01 17:24:37 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2019-10-01 17:24:37 -0300
commitb2a580bdb1982e45bb37f95b78c2dafec6efa7a6 (patch)
tree76de58214adff838a26346514a8a6fba459bde9b /ltable.c
parent89f6a85f034b2535e43e421991098fa05a92cd60 (diff)
downloadlua-b2a580bdb1982e45bb37f95b78c2dafec6efa7a6.tar.gz
lua-b2a580bdb1982e45bb37f95b78c2dafec6efa7a6.tar.bz2
lua-b2a580bdb1982e45bb37f95b78c2dafec6efa7a6.zip
Janitorial work
- Several details in 'lcode.c' - A few more tests for code generation - Bug in assert in 'lcode.c' ("=" x "==") - Comments in 'lopcodes.h' and 'ltable.c'
Diffstat (limited to 'ltable.c')
-rw-r--r--ltable.c44
1 files changed, 24 insertions, 20 deletions
diff --git a/ltable.c b/ltable.c
index d8ff3d80..5561d45e 100644
--- a/ltable.c
+++ b/ltable.c
@@ -833,39 +833,41 @@ static unsigned int binsearch (const TValue *array, unsigned int i,
833** and 'maxinteger' if t[maxinteger] is present.) 833** and 'maxinteger' if t[maxinteger] is present.)
834** (In the next explanation, we use Lua indices, that is, with base 1. 834** (In the next explanation, we use Lua indices, that is, with base 1.
835** The code itself uses base 0 when indexing the array part of the table.) 835** The code itself uses base 0 when indexing the array part of the table.)
836** The code starts with 'limit', a position in the array part that may 836** The code starts with 'limit = t->alimit', a position in the array
837** be a boundary. 837** part that may be a boundary.
838**
838** (1) If 't[limit]' is empty, there must be a boundary before it. 839** (1) If 't[limit]' is empty, there must be a boundary before it.
839** As a common case (e.g., after 't[#t]=nil'), check whether 'hint-1' 840** As a common case (e.g., after 't[#t]=nil'), check whether 'limit-1'
840** is present. If so, it is a boundary. Otherwise, do a binary search 841** is present. If so, it is a boundary. Otherwise, do a binary search
841** between 0 and limit to find a boundary. In both cases, try to 842** between 0 and limit to find a boundary. In both cases, try to
842** use this boundary as the new 'limit', as a hint for the next call. 843** use this boundary as the new 'alimit', as a hint for the next call.
844**
843** (2) If 't[limit]' is not empty and the array has more elements 845** (2) If 't[limit]' is not empty and the array has more elements
844** after 'limit', try to find a boundary there. Again, try first 846** after 'limit', try to find a boundary there. Again, try first
845** the special case (which should be quite frequent) where 'limit+1' 847** the special case (which should be quite frequent) where 'limit+1'
846** is empty, so that 'limit' is a boundary. Otherwise, check the 848** is empty, so that 'limit' is a boundary. Otherwise, check the
847** last element of the array part (set it as a new limit). If it is empty, 849** last element of the array part. If it is empty, there must be a
848** there must be a boundary between the old limit (present) and the new 850** boundary between the old limit (present) and the last element
849** limit (absent), which is found with a binary search. (This boundary 851** (absent), which is found with a binary search. (This boundary always
850** always can be a new limit.) 852** can be a new limit.)
853**
851** (3) The last case is when there are no elements in the array part 854** (3) The last case is when there are no elements in the array part
852** (limit == 0) or its last element (the new limit) is present. 855** (limit == 0) or its last element (the new limit) is present.
853** In this case, must check the hash part. If there is no hash part, 856** In this case, must check the hash part. If there is no hash part
854** the boundary is 0. Otherwise, if 'limit+1' is absent, 'limit' is 857** or 'limit+1' is absent, 'limit' is a boundary. Otherwise, call
855** a boundary. Finally, if 'limit+1' is present, call 'hash_search' 858** 'hash_search' to find a boundary in the hash part of the table.
856** to find a boundary in the hash part of the table. (In those 859** (In those cases, the boundary is not inside the array part, and
857** cases, the boundary is not inside the array part, and therefore 860** therefore cannot be used as a new limit.)
858** cannot be used as a new limit.)
859*/ 861*/
860lua_Unsigned luaH_getn (Table *t) { 862lua_Unsigned luaH_getn (Table *t) {
861 unsigned int limit = t->alimit; 863 unsigned int limit = t->alimit;
862 if (limit > 0 && isempty(&t->array[limit - 1])) { 864 if (limit > 0 && isempty(&t->array[limit - 1])) { /* (1)? */
863 /* (1) there must be a boundary before 'limit' */ 865 /* there must be a boundary before 'limit' */
864 if (limit >= 2 && !isempty(&t->array[limit - 2])) { 866 if (limit >= 2 && !isempty(&t->array[limit - 2])) {
865 /* 'limit - 1' is a boundary; can it be a new limit? */ 867 /* 'limit - 1' is a boundary; can it be a new limit? */
866 if (ispow2realasize(t) && !ispow2(limit - 1)) { 868 if (ispow2realasize(t) && !ispow2(limit - 1)) {
867 t->alimit = limit - 1; 869 t->alimit = limit - 1;
868 setnorealasize(t); 870 setnorealasize(t); /* now 'alimit' is not the real size */
869 } 871 }
870 return limit - 1; 872 return limit - 1;
871 } 873 }
@@ -880,8 +882,8 @@ lua_Unsigned luaH_getn (Table *t) {
880 } 882 }
881 } 883 }
882 /* 'limit' is zero or present in table */ 884 /* 'limit' is zero or present in table */
883 if (!limitequalsasize(t)) { 885 if (!limitequalsasize(t)) { /* (2)? */
884 /* (2) 'limit' > 0 and array has more elements after 'limit' */ 886 /* 'limit' > 0 and array has more elements after 'limit' */
885 if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */ 887 if (isempty(&t->array[limit])) /* 'limit + 1' is empty? */
886 return limit; /* this is the boundary */ 888 return limit; /* this is the boundary */
887 /* else, try last element in the array */ 889 /* else, try last element in the array */
@@ -899,7 +901,7 @@ lua_Unsigned luaH_getn (Table *t) {
899 lua_assert(limit == luaH_realasize(t) && 901 lua_assert(limit == luaH_realasize(t) &&
900 (limit == 0 || !isempty(&t->array[limit - 1]))); 902 (limit == 0 || !isempty(&t->array[limit - 1])));
901 if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1)))) 903 if (isdummy(t) || isempty(luaH_getint(t, cast(lua_Integer, limit + 1))))
902 return limit; /* 'limit + 1' is absent... */ 904 return limit; /* 'limit + 1' is absent */
903 else /* 'limit + 1' is also present */ 905 else /* 'limit + 1' is also present */
904 return hash_search(t, limit); 906 return hash_search(t, limit);
905} 907}
@@ -908,6 +910,8 @@ lua_Unsigned luaH_getn (Table *t) {
908 910
909#if defined(LUA_DEBUG) 911#if defined(LUA_DEBUG)
910 912
913/* export these functions for the test library */
914
911Node *luaH_mainposition (const Table *t, const TValue *key) { 915Node *luaH_mainposition (const Table *t, const TValue *key) {
912 return mainpositionTV(t, key); 916 return mainpositionTV(t, key);
913} 917}