{"id":16125580,"url":"https://github.com/semibran/lattice","last_synced_at":"2025-04-06T13:12:46.548Z","repository":{"id":130492046,"uuid":"79703101","full_name":"semibran/lattice","owner":"semibran","description":"Efficiently manage flattened multidimensional arrays","archived":false,"fork":false,"pushed_at":"2017-06-27T17:48:10.000Z","size":17,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-12T18:59:22.372Z","etag":null,"topics":["cell","graph","grid","lattice","point"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/semibran.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","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":"2017-01-22T07:56:23.000Z","updated_at":"2017-06-27T07:55:11.000Z","dependencies_parsed_at":"2024-03-15T04:16:52.322Z","dependency_job_id":null,"html_url":"https://github.com/semibran/lattice","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Flattice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Flattice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Flattice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Flattice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/semibran","download_url":"https://codeload.github.com/semibran/lattice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247485291,"owners_count":20946398,"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":["cell","graph","grid","lattice","point"],"created_at":"2024-10-09T21:30:02.039Z","updated_at":"2025-04-06T13:12:46.530Z","avatar_url":"https://github.com/semibran.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lattice\nMultidimensional arrays are most efficiently expressed as flat, one-dimensional arrays containing the desired data in sequence. However, retrieving and modifying values from such an array often requires some complex math.\n\nThis module abstracts away such formulas in the form of functions to facilitate the use of flattened multidimensional arrays in your code.\n\n## usage\n```js\nconst { contains, flatten, project } = require('lattice')\n```\n\n### `contains(space, point) -\u003e boolean`\nDetermines if the given `point` is inside `space`.\n```js\n\u003e var space = [25, 25]\n[25, 25]\n\n\u003e contains(space, [4, 16])\ntrue\n\n\u003e contains(space, [12, 25])\nfalse\n```\n\n### `flatten(space, point) -\u003e index`\nUnwraps a point into its corresponding one-dimensional index.\n```js\nvar index = flatten(world.size, [12, 12])\nworld.tiles[index] = 1\n```\n\nIn the above example, `world` would be an object of the following form.\n```js\nvar world = {\n  size: [25, 25],\n  tiles: new Uint8Array(25 * 25)\n}\n```\n\nThis function is a dimension-agnostic implementation of [a common address calculation algorithm](https://en.wikipedia.org/wiki/Row-_and_column-major_order#Address_calculation_in_general), which usually appears in the form `y * width + x` when reading from flattened two-dimensional arrays.\n\n### `project(space, index) -\u003e point`\nWraps an index into its corresponding multidimensional point.\n```js\n\u003e project([25, 25], 312)\n[12, 12]\n```\n```js\ngame.state.map((alive, index) =\u003e {\n  var point = project(game.space, index)\n  var score = moore(1, game.space.length)\n    .map(neighbor =\u003e add(cell, neighbor))\n    .map(neighbor =\u003e wrap(game.space, neighbor))\n    .reduce((score, neighbor) =\u003e score + game.state[flatten(game.space, neighbor)], 0)\n  return score === 3 || score === 2 \u0026\u0026 alive\n})\n```\n\n## see also\n- [`semibran/vector`](https://github.com/semibran/vector) - basic operations for multidimensional vectors\n\n## license\n[MIT](https://opensource.org/licenses/MIT) © [Brandon Semilla](https://git.io/semibran)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemibran%2Flattice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsemibran%2Flattice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemibran%2Flattice/lists"}