{"id":15104024,"url":"https://github.com/guilleatm/utils","last_synced_at":"2026-02-21T11:30:50.036Z","repository":{"id":156098914,"uuid":"307684505","full_name":"guilleatm/utils","owner":"guilleatm","description":"Useful functions for your lua project.  Gamedev.  Binary search and conversion between table and string.","archived":false,"fork":false,"pushed_at":"2020-10-27T15:58:17.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T12:30:20.943Z","etag":null,"topics":["binary-search","game-development","gamedev","love2d","love2d-framework","lua"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/guilleatm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-27T11:56:41.000Z","updated_at":"2020-10-27T23:46:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"2ea3927e-891b-4d5e-b498-9feee79d8753","html_url":"https://github.com/guilleatm/utils","commit_stats":{"total_commits":4,"total_committers":1,"mean_commits":4.0,"dds":0.0,"last_synced_commit":"279432b594e91665d8e646b8790bf266e70544c1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/guilleatm/utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guilleatm%2Futils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guilleatm%2Futils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guilleatm%2Futils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guilleatm%2Futils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guilleatm","download_url":"https://codeload.github.com/guilleatm/utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guilleatm%2Futils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263951694,"owners_count":23534786,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-search","game-development","gamedev","love2d","love2d-framework","lua"],"created_at":"2024-09-25T20:00:27.808Z","updated_at":"2025-10-05T22:55:24.643Z","avatar_url":"https://github.com/guilleatm.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Utils\n\nPushing module 2\n\n## Description\n\nUseful functions for your lua project.\n\n\u003e For using the functions you have to have the utils.lua file in your project folder and remember to use the require 'utils' (with the correct path if the file is not with the main)\n\n---\n\n## Table to string\n\nConverts a table to a string.  \n\nSyntax:\n\n```tableToString(table, separator, brackets)```\n\n* **table**: The table you want to convert to a string.\n* **separator**: The character (or string) that will appear between the elements of the table, default is a space ' ', but if you want to store the table into a lua file for loading it after, you might use a comma ',' and the dofile function. **optional**\n* **brackets**: If true, the returned string will be like \"{1, 2, 3}\", if not \"1, 2, 3\". **optional**\n\n### Example:\n\n```lua\nlocal myTable = {1, 2, 6, 9}\nlocal myTable2 = {56, \"Hello\", 'world', aKey = 78, t = {4, 7, \"Guitm\"}}\n\nlocal converted = tableToString(myTable, ',') -- converted = \"1,2,6,9\"\nlocal converted2 = tableToString(myTable2, ' / ', true) -- converted2 = \"{56 / \"Hello\" / \"world\" / aKey=78 / t={4 / 7 / \"Guitm\"}}\"\n\nprint(converted)\nprint(converted2)\n```\n\n---\n\n## String to table\n\nConverts a string to a table. **This function does not support keys yet**\n\nSyntax:\n\n```stringToTable(string, separator)```\n\n* **string**: The string that is going to be converted.\n* **separator**: If the element between two values of the table is not a space ' ', you will have to specify wich one it is. **optional**\n\n### Example:\n\n```lua\nlocal myString = \"1, 2, 6, 9\"\nlocal myString2 = \"56 'Hello' 'world' 78 {4 7 'Guitm'}\"\n\nlocal converted = stringToTable(myString, ',') -- converted = {1, 2, 6, 9}\nlocal converted2 = stringToTable(myString2) -- converted2 = {56, \"Hello\", \"world\", 78, {4, 7, \"Guitm\"}}\n\nfor k, e in pairs(converted) do\n\tprint(e)\nend\n\nprint()\n\nfor k, e in pairs(converted2) do\n\tprint(e)\nend\n```\n\n---\n\n## Binary search\n\nThis function returns the position of an element in an ordered list (table) in O(logN).  \n\nSyntax:\n\n```binarySearch(element, table)```\n\n* **element**: The element we want to know it's position.\n* **table**: The table where we are searching.\n\nIf you know between what indexes the element is, you can use the iStart and iEnd parameters but be carefull with this. For example if you know that the element is\nin the first five elements (including the 5 element):\n\n```binarySearch(myElement, myTable, nil, 5)```\n\nComplete syntax:  \n```binarySearch(element, table, iStart, iEnd)```\n\nIf there are duplicated elements in the table, the function will return 'randomly' one of the indexes (of the duplicated). If you want always the first in the table, you can use binarySearchAmplified()\n\n### Example:\n\n```lua\nlocal t = {1,2,3,4,5,5,5,7,7,8,8,8,8,8,9,234}\n\nbinarySearch(4, t) -- 4\nbinarySearch(9, t) -- 15\nbinarySearch(46, t) -- nil\nbinarySearch(7, t) -- It can be 8 or 9, depending on the table length\n\n```\n\n---\n\n## Amplified binary search\n\nBinary search with some useful parameters. \n\nSyntax:\n\n```binarySearch(element, table, toInsert, whereToLook)```\n\n* **element**: The element we want to know it's position.\n* **table**: The table where we are searching.\n* **toInsert**: If true, the function will return the index where the element have to be inserted for keeping the table sorted. You can use table.insert(table, indexReturned, element). **optional**\n* **whereToLook**: Imagine you have a sorted list (table) of objects like: objectPerson = {weight = {pounds, kilograms}}. And you want to have it sorted by weight in kilograms (obviusly if you sort by pounds, the result will be the same, it's just an exmple). So *whereToLook* will be a table with the path (keys) where the function have to look. For this example, it will be {'weight', 'kilograms'} **optional**\n\nYou can use iStart and iEnd like in binarySearch()\n\n### Example:\n\n```lua\nlocal t = {1,2,3,4,5,5,5,7,7,8,8,8,8,8,9,234}\nlocal t2 = {{a=3}, {a = 3}, {a = 4}}\n\nbinarySearchAmplified(4, t) -- 4\nbinarySearchAmplified(9, t) -- 15\nbinarySearchAmplified(46, t) -- nil\nbinarySearchAmplified(46, t, true) -- 16\n\nprint(binarySearchAmplified(3, t2, true, {'a'})) -- 1\nprint(binarySearchAmplified(8, t2, true, {'a'})) -- 4\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguilleatm%2Futils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguilleatm%2Futils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguilleatm%2Futils/lists"}