| [Lua] Data types and structures |
| « Citizen » 1743340740000
| 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Introduction Every programmer has used a data structure at least once. The goal of this topic is to provide easy access to the most common data types and structures written in Lua. What is a data structure? A data structure is a way to store and organize information so that it's easier to use and work with in a computer program. Why data structures are so important? Data structures are important because they help you keep information organized, so you can find and use it faster. Imagine you have a big box of legos, but they're all mixed up. If you need a red piece, it’s going to take a while to find it. But if you organize the Legos by color or size, you can find the piece you need much quicker. What is a data type? A data type is like a way to tell the computer what kind of information you're working with. It's like sorting your toys into different categories, so the computer knows what to expect and how to handle it. An enum (enumeration) is like giving a list of things special names to make them easier to use. Code: Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 local function enum(def) Usage 1: Lua code 1 2 3 4 5 6 7 8 9 10 11 roomType = enum({Usage 2: Lua code 1 2 3 4 5 6 7 8 9 roomType2 = enum({If key is does not exist in the enum it's nil. Lua code 1 print(roomType.UNKNOWN) -- nilA pair is a container that holds two values, which can be of different types. Lua code 1 2 3 4 5 6 7 8 9 10 11 local function pair(x, y) Usage: Lua code 1 2 3 4 p = pair(100, "luanoob") A tuple can store multiple values of different types. Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 local function tuple(...) Usage: Lua code 1 2 3 4 5 6 7 8 t = tuple(69, "touhouisbest", true, 3.14) A Linked List is a data structure used to store a collection of elements in a sequence, but it does not provide O(1) access to elements. Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 do
Usage: Lua code 1 2 3 4 5 6 7 8 9 10 11 list = LinkedList:new() A stack is a linear data structure that is LIFO. This means that the last element added to the stack is the first one to be removed. Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 do
Usage: Lua code 1 2 3 4 5 6 7 8 stack = Stack:new() A queue is a linear data structure that is FIFO. This means that the first element added to the queue is the first one to be removed. Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 do
Usage: Lua code 1 2 3 4 5 6 7 8 9 -- Example Usage A set is a collection of elements, where each element is unique (no duplications). Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 do
Usage: Lua code 1 2 3 4 5 6 7 8 9 10 s = Set.new() Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 do
Usage: Lua code 1 2 3 4 5 6 7 8 9 10 11 dq = Deque.new() A hash tableis a data structure that stores key-value pairs and allows for efficient data retrieval using keys. It provides constant-time average complexity for insert, delete and lookup. Lua code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 do Last edit on 1743447540000 |
| « Censor » 1743689400000
| 0 | ||
| Very useful topic, thank you for sharing! |