LevelUp! Studio » set https://blog.levelup.in.th Experience the new world. Fri, 26 May 2017 10:06:07 +0000 th hourly 1 http://wordpress.org/?v=3.8.1 [Lua] การทำ เซ็ทแบบไม่เรียง จาก Table https://blog.levelup.in.th/2015/07/31/lua-using-unordered-sets-from-table/ https://blog.levelup.in.th/2015/07/31/lua-using-unordered-sets-from-table/#comments Fri, 31 Jul 2015 16:17:41 +0000 http://blog.levelup.in.th/?p=4582 การทำ เซ็ทแบบไม่เรียง จาก Table นั้นทำมาเพื่อความประหยัดการประมวลผล ซึ่งโดยปกติแล้วถ้าเราใช้ Table เวลาเราจะดึงข้อมูลเราจะวน For จนกว่าจะเจอสิ่งที่เราต้องการแล้วค่อยดึง ทำให้ใช้การประมวลผลจำนวนมากกว่า การทำ Set ซึ่งจะเข้าถึง Index โดยตรง ซึ่งวิธีทำก็ไม่ยาก ใช้ 3 ฟังก์ชั่นด้านล่าง input ตัวแรกจะเป็น table ที่เราสร้างขึ้นมา ส่วนตัวที่สองก็เป็น key ที่เราจะใช้ในการเพิ่มลดหรือค้นหานั่นเอง

function addToSet(set, key)
set[key] = true
end
function removeFromSet(set, key)
set[key] = nil
end
function setContains(set, key)
return set[key] ~= nil
end

credits: http://stackoverflow.com/questions/2282444/how-to-check-if-a-table-contains-an-element-in-lua

]]>
https://blog.levelup.in.th/2015/07/31/lua-using-unordered-sets-from-table/feed/ 0