From 242ef3819a6120c9bcae4b5e5ed66f259663819e Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Tue, 12 Dec 2023 14:13:09 -0500 Subject: [PATCH 1/4] Update array functions Add array:indexOf --- .../expression2/tests/runtime/types/array.txt | 9 ++- .../gmod_wire_expression2/core/array.lua | 66 ++++++++++--------- lua/wire/client/e2descriptions.lua | 1 + 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/data/expression2/tests/runtime/types/array.txt b/data/expression2/tests/runtime/types/array.txt index f53186de55..7eb7e13602 100644 --- a/data/expression2/tests/runtime/types/array.txt +++ b/data/expression2/tests/runtime/types/array.txt @@ -21,7 +21,7 @@ assert(KV[50, number] == 200) assert(KV[100, number] == 2000) local Rec = 0 -foreach(I, V:number = KV) { +foreach(I:number, V:number = KV) { Rec++ assert(KV[I, number] == V) } @@ -29,4 +29,9 @@ foreach(I, V:number = KV) { assert(Rec == 1) # Only 1 because this breaks the internal ipairs impl (Shouldn't use an array like this anyway.) A[1, vector] = vec() -assert(A[1, vector] == vec()) \ No newline at end of file +assert(A[1, vector] == vec()) + +A[2, entity] = entity() + +assert(A:indexOf(entity()) == 2) +assert(A:indexOf(noentity()) == 0) diff --git a/lua/entities/gmod_wire_expression2/core/array.lua b/lua/entities/gmod_wire_expression2/core/array.lua index 999575dfd0..4a1a4e9689 100644 --- a/lua/entities/gmod_wire_expression2/core/array.lua +++ b/lua/entities/gmod_wire_expression2/core/array.lua @@ -65,16 +65,16 @@ end -- Looped functions and operators -------------------------------------------------------------------------------- registerCallback( "postinit", function() - local getf, setf + local NO_LEGACY = { legacy = false } for k,v in pairs( wire_expression_types ) do local name = k:lower() - if (name == "normal") then name = "number" end + if name == "normal" then name = "number" end local nameupperfirst = upperfirst( name ) local id = v[1] local default = v[2] local typecheck = v[6] - if (!blocked_types[id]) then -- blocked check start + if not blocked_types[id] then -- blocked check start -------------------------------------------------------------------------------- -- Get functions @@ -113,10 +113,9 @@ registerCallback( "postinit", function() __e2setcost(5) registerFunction( name, "r:n", id, function(self, args) - local op1, op2 = args[2], args[3] - local array, index = op1[1](self,op1), op2[1](self,op2) + local array, index = args[2], args[3] return getter( self, array, index ) - end, nil, nil, { deprecated = true }) + end, nil, nil, { legacy = false, deprecated = true }) -------------------------------------------------------------------------------- -- Set functions @@ -153,10 +152,9 @@ registerCallback( "postinit", function() end registerFunction("set" .. nameupperfirst, "r:n"..id, id, function(self,args) - local op1, op2, op3 = args[2], args[3], args[4] - local array, index, value = op1[1](self,op1), op2[1](self,op2), op3[1](self,op3) + local array, index, value = args[1], args[2], args[3] return setter( self, array, index, value ) - end, nil, nil, { deprecated = true }) + end, nil, nil, { legacy = false, deprecated = true }) -------------------------------------------------------------------------------- @@ -166,63 +164,71 @@ registerCallback( "postinit", function() __e2setcost(7) registerFunction( "push" .. nameupperfirst, "r:" .. id, id, function(self,args) - local op1, op2 = args[2], args[3] - local array, value = op1[1](self,op1), op2[1](self,op2) + local array, value = args[1], args[2] return setter( self, array, #array + 1, value ) - end) + end, nil, nil, NO_LEGACY) -------------------------------------------------------------------------------- -- Insert functions -- Inserts the value at the specified index. Subsequent values are moved up to compensate. -------------------------------------------------------------------------------- registerFunction( "insert" .. nameupperfirst, "r:n" .. id, id, function( self, args ) - local op1, op2, op3 = args[2], args[3], args[4] - local array, index, value = op1[1](self,op1), op2[1](self,op2), op3[1](self,op3) + local array, index, value = args[1], args[2], args[3] return setter( self, array, index, value, true ) - end) + end, nil, nil, NO_LEGACY) -------------------------------------------------------------------------------- -- Pop functions -- Removes and returns the last value in the array. -------------------------------------------------------------------------------- registerFunction( "pop" .. nameupperfirst, "r:", id, function(self,args) - local op1 = args[2] - local array = op1[1](self,op1) - if (!array) then return fixDefault( default ) end + local array = args[1] + if not array then return fixDefault(default) end return getter( self, array, #array, true ) - end) + end, nil, nil, NO_LEGACY) -------------------------------------------------------------------------------- -- Unshift functions -- Inserts the value at the beginning of the array. Subsequent values are moved up to compensate. -------------------------------------------------------------------------------- registerFunction( "unshift" .. nameupperfirst, "r:" .. id, id, function(self,args) - local op1, op2 = args[2], args[3] - local array, value = op1[1](self,op1), op2[1](self,op2) + local array, value = args[1], args[2] return setter( self, array, 1, value, true ) - end) + end, nil, nil, NO_LEGACY) -------------------------------------------------------------------------------- -- Shift functions -- Removes and returns the first value of the array. Subsequent values are moved down to compensate. -------------------------------------------------------------------------------- registerFunction( "shift" .. nameupperfirst, "r:", id, function(self,args) - local op1 = args[2] - local array = op1[1](self,op1) - if (!array) then return fixDefault( default ) end + local array = args[1] + if not array then return fixDefault(default) end return getter( self, array, 1, true ) - end) + end, nil, nil, NO_LEGACY) -------------------------------------------------------------------------------- -- Remove functions -- Removes and returns the specified value of the array. Subsequent values are moved down to compensate. -------------------------------------------------------------------------------- registerFunction( "remove" .. nameupperfirst, "r:n", id, function(self,args) - local op1, op2 = args[2], args[3] - local array, index = op1[1](self,op1), op2[1](self,op2) - if (!array or !index) then return fixDefault( default ) end + local array, index = args[1], args[2] + if not array or not index then return fixDefault(default) end return getter( self, array, index, true ) - end) + end, nil, nil, NO_LEGACY) + + -- indexOf + + registerFunction("indexOf", "r:" .. id, "n", function(self, args) + local arr, val = args[1], args[2] + for i, j in ipairs(arr) do + if j == val then + self.prf = self.prf + i + return i + end + end + self.prf = self.prf + #arr + return 0 + end, nil, { "value" }, NO_LEGACY) -------------------------------------------------------------------------------- -- Foreach operators diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index 46b59838b4..3499d30f16 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -1400,6 +1400,7 @@ E2Helper.Descriptions["concat(r:snn)"] = "Concatenates all values in the array, E2Helper.Descriptions["count(r:)"] = "Returns the number of entries in the array" E2Helper.Descriptions["exists(r:n)"] = "Returns 1 if the array contains any value at specified index" E2Helper.Descriptions["id(r:)"] = "Returns the unique ID of the array" +E2Helper.Descriptions["indexOf"] = "Returns the index of the element in the array or 0 if it's not found" E2Helper.Descriptions["invert(r)"] = "Inverts the array, creating a lookup table" E2Helper.Descriptions["min(r:)"] = "Returns the smallest number in array" E2Helper.Descriptions["max(r:)"] = "Returns the largest number in array" From fe3459eb1d21cfdd3922678ab7c00f35e5d83021 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Tue, 12 Dec 2023 14:42:51 -0500 Subject: [PATCH 2/4] Remove remainder of C nots --- lua/entities/gmod_wire_expression2/core/array.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/array.lua b/lua/entities/gmod_wire_expression2/core/array.lua index 4a1a4e9689..2416232724 100644 --- a/lua/entities/gmod_wire_expression2/core/array.lua +++ b/lua/entities/gmod_wire_expression2/core/array.lua @@ -36,7 +36,7 @@ registerType("array", "r", {}, nil, nil, function(v) - return !istable(v) + return not istable(v) end ) @@ -83,7 +83,7 @@ registerCallback( "postinit", function() __e2setcost(1) local function getter( self, array, index, doremove ) - if (!array or !index) then return fixDefault( default ) end -- Make sure array and index are value + if not array or not index then return fixDefault( default ) end -- Make sure array and index are value local ret if (doremove) then ret = table_remove( array, index ) @@ -123,7 +123,7 @@ registerCallback( "postinit", function() -------------------------------------------------------------------------------- local function setter( self, array, index, value, doinsert ) - if (!array or !index) then return fixDefault( default ) end -- Make sure array and index are valid + if not array or not index then return fixDefault( default ) end -- Make sure array and index are valid if (typecheck and typecheck( value )) then return fixDefault( default ) end -- If typecheck returns true, the type is wrong. if (doinsert) then if index > 2^31 or index < 0 then return fixDefault( default ) end -- too large, possibility of crashing gmod @@ -513,7 +513,7 @@ end -------------------------------------------------------------------------------- __e2setcost(1) e2function array array:add( array other ) - if (!next(this) and !next(other)) then return {} end -- Both of them are empty + if not next(this) and not next(other) then return {} end -- Both of them are empty local ret = {} for i=1,#this do ret[i] = this[i] @@ -531,7 +531,7 @@ end -------------------------------------------------------------------------------- __e2setcost(1) e2function array array:merge( array other ) - if (!next(this) and !next(other)) then return {} end -- Both of them are empty + if not next(this) and not next(other) then return {} end -- Both of them are empty local ret = {} for i=1,math.max(#this,#other) do ret[i] = other[i] or this[i] From 59d5e9565ec0953e5767b5d939366afbbf99d1fb Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Mon, 18 Dec 2023 12:56:31 -0500 Subject: [PATCH 3/4] Update description --- lua/wire/client/e2descriptions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index 3499d30f16..13b7587afc 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -1400,7 +1400,7 @@ E2Helper.Descriptions["concat(r:snn)"] = "Concatenates all values in the array, E2Helper.Descriptions["count(r:)"] = "Returns the number of entries in the array" E2Helper.Descriptions["exists(r:n)"] = "Returns 1 if the array contains any value at specified index" E2Helper.Descriptions["id(r:)"] = "Returns the unique ID of the array" -E2Helper.Descriptions["indexOf"] = "Returns the index of the element in the array or 0 if it's not found" +E2Helper.Descriptions["indexOf"] = "Returns the index of the element in the array or 0 if it's not found. This function is very inefficient for large arrays. You should use look-up tables if you intend on using this frequently." E2Helper.Descriptions["invert(r)"] = "Inverts the array, creating a lookup table" E2Helper.Descriptions["min(r:)"] = "Returns the smallest number in array" E2Helper.Descriptions["max(r:)"] = "Returns the largest number in array" From 5f09f91a7c0fd9522f5e05bd252274903502d6e0 Mon Sep 17 00:00:00 2001 From: Denneisk <20892685+Denneisk@users.noreply.github.com> Date: Mon, 18 Dec 2023 17:20:04 -0500 Subject: [PATCH 4/4] Change loop --- lua/entities/gmod_wire_expression2/core/array.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/array.lua b/lua/entities/gmod_wire_expression2/core/array.lua index 2416232724..ae7f42ca85 100644 --- a/lua/entities/gmod_wire_expression2/core/array.lua +++ b/lua/entities/gmod_wire_expression2/core/array.lua @@ -220,13 +220,14 @@ registerCallback( "postinit", function() registerFunction("indexOf", "r:" .. id, "n", function(self, args) local arr, val = args[1], args[2] - for i, j in ipairs(arr) do - if j == val then + local l = #arr + for i = 1, l do + if arr[i] == val then self.prf = self.prf + i return i end end - self.prf = self.prf + #arr + self.prf = self.prf + l return 0 end, nil, { "value" }, NO_LEGACY)