This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
superaxander's profile picture

BasicUtils v1.5

Started by superaxander, 18 March 2013 - 07:28 AM
superaxander #1
Posted 18 March 2013 - 08:28 AM
BasicUtils v1.5

BasicUtils is a collection of functions that you can use in many situations. It is divided in several parts.
TableUtils
SpoilerIn tableutils there are some functions about tables.

tableutils.same(table1, table2) --checks if the two tables contain the same
tableutils.copy(source, destination) --copies the content of one table to another(destination must be a empty table)
SystemUtils
SpoilerIn systemutils are some general usefull functions.

systemutils.yield(yieldtime, maxwait) --use this instead of sleeping to make your program faster without too long without yielding messages.
systemutils.startThread(func1, func2,...) --will make the coroutines and resume them(returns table with coroutines)
GraphicUtils
SpoilerIn graphic utils are gui based functions.

graphicutils.drawImage(image, xPos, yPos, maxX, maxY, minX, minY) --basically paintutils.drawImage but you can draw a part of a image(optional)
graphicutils.drawImage(path, xPos, yPos, maxX, maxY, minX, minY) --the same but you don't have to load the image.
graphicutils.setBackgroundColor(color) --clears the screen and fills it with the specified color
StringUtils
SpoilerI need help with that one
Misc
SpoilerSome misc. stuff

assert(condition, errormessage, errorlevel) --thanks to TheOrginalBIT with a optional errorlevel parameter
isString(var)
isThread(var)
isCoroutine(var)
isNumber(var)
isTable(var)
isNil(var)
isFunction(var)
isDead(var)
isAlive(var)

Usage:
First run the file using shell.run and you're ready.

Download:
pastebin get DQ7JUHxT utils

Changelog
SpoilerV1.5:
-Added tableutils.deepcopy functions thanks to TheOriginalBIT
V1.4:
-Changed is functions thanks to TheOriginalBIT
-Changed tableutils.same thanks to TheOriginalBIT
-Changed tableutils.copy thanks to TheOriginalBIT
-Fixed bugs thanks to TheOriginalBIT
V1.3:
-Added isFunction
-Added graphicutils.setBackgroundColor(color)
-Added systemutils.startThread(func1,func2,…)
V1.2:
-Added type checking
V1.1:
-Added tableutils.same
-Added graphicutils.drawImage
-Added graphicutils.drawImagePath
V1.0:
-Intial release


Cheers
Edited on 22 March 2013 - 07:47 AM
superaxander #2
Posted 18 March 2013 - 11:50 PM
Version 1.3 released!
-Added isFunction
-Added graphicutils.setBackgroundColor(color)
-Added systemutils.startThread(func1,func2,…)
theoriginalbit #3
Posted 18 March 2013 - 11:59 PM
haha! my assert function! :D/>
superaxander #4
Posted 19 March 2013 - 12:00 AM
haha! my assert function! :D/>/>/>
I know I needed it in some Functions.
Edited on 18 March 2013 - 11:00 PM
theoriginalbit #5
Posted 19 March 2013 - 12:03 AM
I know I needed it in some Functions.
Yeh I noticed…

Some improvements to your is functions
Spoiler


--[[
misc functions
]]

assert = function(condition, errMsg, level)
  if not condition then
    error(errMsg, (tonumber(level) or 1) + 1)
  end
  return condition
end

isFunction = function(var)
  return type(var) == "function"
end

isTable = function(var)
  return type(var) == "table"
end

isString = function(var)
  return type(var) == "string"
end

isNumber = function(var)
  return type(var) == "number"
end

isNil = function(var)
  return type(var) == "nil"
end

isCoroutine = function(var)
  return type(var) == "thread"
end

isThread = function(var)
  return type(var) == "thread"
end

isDead = function(var)
  assert(isThread(var, "Argument #1: expected thread/coroutine got "..type(var), 2)
  return coroutine.status(var) == "dead"
end

isAlive = function(var)
  assert(isThread(var, "Argument #1: expected thread/coroutine got "..type(var), 2)
  return not isDead(var)
end
superaxander #6
Posted 19 March 2013 - 12:06 AM
Didn't think of that xD
theoriginalbit #7
Posted 19 March 2013 - 12:13 AM
Didn't think of that xD
:P/> here is another improvement… your tableutils.same function is REALLY inefficient and actually returns true when it shouldn't in some cases ( i tested :P/> )

so here is an improved version :)/>
Spoiler


tableutils.same = function(table1, table2)
  assert(isTable(table1), "Argument #1: expected table got "..type(table1), 2)
  assert(isTable(table2), "Argument #2: expected table got "..type(table2), 2)
  for k,v in pairs(table1) do
    if not (table2[k] and table2[k] == v) then
      return false, k
    end
    systemutils.yield(0.1)
  end
  return true
end
superaxander #8
Posted 19 March 2013 - 12:23 AM
Version 1.4 Released
-Changed is functions thanks to TheOriginalBIT
-Changed tableutils.same thanks to TheOriginalBIT
-Changed tableutils.copy thanks to TheOriginalBIT
-Fixed bugs thanks to TheOriginalBIT
Edited on 19 March 2013 - 12:25 AM
theoriginalbit #9
Posted 19 March 2013 - 12:39 AM
More bugs and some fixes

A slightly fixed copy function
Spoiler


tableutils.copy = function(source, destination)
  assert(isTable(source), "Argument #1: expected table got "..type(table), 2)
  destination = destination or {}

  for k,v in pairs(source) do
	destination[k] = v
	systemutils.yield(0.1)
  end

  local same, h = same(source, destination)
  if not same then
	error("error copying: "..h, 2)
  end

  return true
end


Line 48 and 53 missing a ')'
SpoilerOld

assert(isThread(var, "Argument #1: expected thread/coroutine got "..type(var), 2)
New


assert(isThread(var), "Argument #1: expected thread/coroutine got "..type(var), 2)

Line 147—151 extra ')'
SpoilerOld


assert(isNumber(yPos), "Argument #3: number expected got ")..type(yPos), errorlevel)
assert(isNumber(maxX), "Argument #4: number expected got ")..type(maxX), errorlevel)
assert(isNumber(maxY), "Argument #5: number expected got ")..type(maxY), errorlevel)
assert(isNumber(minX), "Argument #6: number expected got ")..type(minX), errorlevel)
assert(isNumber(minY), "Argument #7: number expected got ")..type(minY), errorlevel

New


assert(isNumber(yPos), "Argument #3: number expected got ")..type(yPos), errorlevel)
assert(isNumber(maxX), "Argument #4: number expected got ")..type(maxX), errorlevel)
assert(isNumber(maxY), "Argument #5: number expected got ")..type(maxY), errorlevel)
assert(isNumber(minX), "Argument #6: number expected got ")..type(minX), errorlevel)
assert(isNumber(minY), "Argument #7: number expected got ")..type(minY), errorlevel

Line 151 missing ')' at end of line

lots of problems in graphicutils.drawImage
Spoilery and x are not defined but used in line 140, 141, 155, 156

the for loop is wrong in on lines 152, 154
superaxander #10
Posted 19 March 2013 - 01:26 AM
More bugs and some fixes

A slightly fixed copy function
Spoiler


tableutils.copy = function(source, destination)
  assert(isTable(source), "Argument #1: expected table got "..type(table), 2)
  destination = destination or {}

  for k,v in pairs(source) do
	destination[k] = v
	systemutils.yield(0.1)
  end

  local same, h = same(source, destination)
  if not same then
	error("error copying: "..h, 2)
  end

  return true
end


Line 48 and 53 missing a ')'
SpoilerOld

assert(isThread(var, "Argument #1: expected thread/coroutine got "..type(var), 2)
New


assert(isThread(var), "Argument #1: expected thread/coroutine got "..type(var), 2)

Line 147—151 extra ')'
SpoilerOld


assert(isNumber(yPos), "Argument #3: number expected got ")..type(yPos), errorlevel)
assert(isNumber(maxX), "Argument #4: number expected got ")..type(maxX), errorlevel)
assert(isNumber(maxY), "Argument #5: number expected got ")..type(maxY), errorlevel)
assert(isNumber(minX), "Argument #6: number expected got ")..type(minX), errorlevel)
assert(isNumber(minY), "Argument #7: number expected got ")..type(minY), errorlevel

New


assert(isNumber(yPos), "Argument #3: number expected got ")..type(yPos), errorlevel)
assert(isNumber(maxX), "Argument #4: number expected got ")..type(maxX), errorlevel)
assert(isNumber(maxY), "Argument #5: number expected got ")..type(maxY), errorlevel)
assert(isNumber(minX), "Argument #6: number expected got ")..type(minX), errorlevel)
assert(isNumber(minY), "Argument #7: number expected got ")..type(minY), errorlevel

Line 151 missing ')' at end of line

lots of problems in graphicutils.drawImage
Spoilery and x are not defined but used in line 140, 141, 155, 156

the for loop is wrong in on lines 152, 154
Fixed! Thanks
theoriginalbit #11
Posted 19 March 2013 - 01:31 AM
Fixed! Thanks
No problems… fyi most of those bugs could have been found just by running the script and letting the compiler yell at you :P/>
superaxander #12
Posted 19 March 2013 - 01:50 AM
Ik but I wasn't on a computer today
theoriginalbit #13
Posted 22 March 2013 - 06:40 AM
oh, I suggest adding a deepCopy to tableutils…. that way if there is a table within the table it will make a copy of that too, not just reference it
I've done the code.
Spoiler


tableutils.deepCopy = function(source, destination)
  assert(isTable(source), "Argument #1: expected table got "..type(table), 2)
  destination = destination or {}

  for k,v in pairs(source) do
    if type( v ) == 'table' do
      destination[k] = {}
      tableutils.deepCopy( v, destination[k] )
    else
      destination[k] = v
    end
    systemutils.yield(0.1)
  end

  return true
end
superaxander #14
Posted 22 March 2013 - 08:44 AM
oh, I suggest adding a deepCopy to tableutils…. that way if there is a table within the table it will make a copy of that too, not just reference it
I've done the code.
Spoiler


tableutils.deepCopy = function(source, destination)
  assert(isTable(source), "Argument #1: expected table got "..type(table), 2)
  destination = destination or {}

  for k,v in pairs(source) do
    if type( v ) == 'table' do
      destination[k] = {}
      tableutils.deepCopy( v, destination[k] )
    else
      destination[k] = v
    end
    systemutils.yield(0.1)
  end

  return true
end
Thanks!
superaxander #15
Posted 22 March 2013 - 08:48 AM
V1.5 Is out!
Changelog:
-Added tableutils.deepcopy functions thanks to TheOriginalBIT
Eric #16
Posted 22 March 2013 - 12:35 PM
But can you deepcopy while preserving references?


local t = {}
t.value = 1
t.self = t

local q = deepcopy(t)

assert(q.self == q)
MysticT #17
Posted 22 March 2013 - 02:00 PM
Here's a deep-copy function that preserves references as Eric said.

function table.deep_copy(t)
  local lookup = {}
  local function copy(obj)
	if type(obj) ~= "table" then
	  return obj
	elseif lookup[obj] ~= nil then
	  return lookup[obj]
	end
	local tbl = {}
	lookup[obj] = tbl
	for k, v in pairs(obj) do
	  tbl[copy(k)] = copy(v)
	end
	return setmetatable(tbl, getmetatable(obj))
  end
  return copy(t)
end
It also copies the metatable (doesn't really copy it, just sets it to the same).

Edit: damn forum not keeping indentation :P/>
Edited on 22 March 2013 - 01:03 PM
albrat #18
Posted 01 July 2013 - 05:13 AM
I was taking a look at this thread and saw that you needed a string handling function still… :)/>

this code takes a string with "breaker" points in it and removes the breakers / splits the output into a table.



-- Our string splitting Function

function string:split( inSplitPattern, outResults )

   if not outResults then
	  outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
	  table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
	  theStart = theSplitEnd + 1
	  theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end

The function is called by inserting this type of code


mess = "test code write message"
tablemes = string.split(mess, " ")  --  This is the string splitter and its processing.
message = tablemes[1]  --  --> output of "test"
pas = tablemes[2] --  -->  Output of "code"

The default for the script is to remove a SPACE char. but by changing what you send to the script it can remove any char.
superaxander #19
Posted 01 July 2013 - 07:05 AM
I am sorry this is discontinued
theoriginalbit #20
Posted 01 July 2013 - 07:11 AM
I was taking a look at this thread and saw that you needed a string handling function still… :)/>

this code takes a string with "breaker" points in it and removes the breakers / splits the output into a table.


-- Our string splitting Function

function string:split( inSplitPattern, outResults )

   if not outResults then
	  outResults = { }
   end
   local theStart = 1
   local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   while theSplitStart do
	  table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
	  theStart = theSplitEnd + 1
	  theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
   end
   table.insert( outResults, string.sub( self, theStart ) )
   return outResults
end
There is a far more efficient method of splitting a string… I suggest looking into Lua Patterns


function string.split( str, patt )
  local t = {}
  for s in str:gmatch('[^'..patt..']+') do
    t[#t+1] = s
  end
  return t
end

Also as for this statement

if not outResults then
  outResults = { }
end
we can use some logic to make this easier for us…

outResults = outResults or {}
this works because of 2 things…
1. in Lua nil resolves to false…
2. boolean logic
true or true = true
true or false = true
false or true = true
false or false = false
So basically when outResults is nil, it becomes false, and the value after the `or` is used, meaning a new table will be assigned.
Cranium #21
Posted 01 July 2013 - 09:35 AM
Locked per request.